KTVHTTPCache bug记录

KTVHTTPCache这个框架有闪退的bug:

1.HTTPConnection 数组越界的bug

原始代码

if(writeQueueSize >= READ_CHUNKSIZE) return;
DDRange range = [[ranges objectAtIndex:rangeIndex] ddrangeValue];

修改后的:

    if(writeQueueSize >= READ_CHUNKSIZE) return;
    if (ranges.count == 0 || rangeIndex > ranges.count - 1) {
        return;
    }
    DDRange range = [[ranges objectAtIndex:rangeIndex] ddrangeValue]; 

原始代码



修改后的:

  if (ranges.count == 0) {
        return;
   }
  DDRange range = [[ranges objectAtIndex:0] ddrangeValue];
    

2.KTVHCDownload.m 字典赋值崩溃问题


修改后

- (NSURLSessionTask *)downloadWithRequest:(KTVHCDataRequest *)request delegate:(id)delegate {
    
    if (!request) {
        return nil;
    }
    
    [self lock];
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:request.URL];
    mRequest.timeoutInterval = self.timeoutInterval;
    mRequest.cachePolicy = NSURLRequestReloadIgnoringCacheData;
    [request.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        if ([self.availableHeaderKeys containsObject:key] ||
            [self.whitelistHeaderKeys containsObject:key]) {
            [mRequest setValue:obj forHTTPHeaderField:key];
        }
    }];
    [self.additionalHeaders enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        [mRequest setValue:obj forHTTPHeaderField:key];
    }];
    NSURLSessionDataTask *task = [self.session dataTaskWithRequest:mRequest];

    if (delegate) {
        [self.delegateDictionary setObject:delegate forKey:task];
    }
    
    if (request) {
        [self.requestDictionary setObject:request forKey:task];
    }
    
    task.priority = 1.0;
    [task resume];
    KTVHCLogDownload(@"%p, Add Request\nrequest : %@\nURL : %@\nheaders : %@\nHTTPRequest headers : %@\nCount : %d", self, request, request.URL, request.headers, mRequest.allHTTPHeaderFields, (int)self.delegateDictionary.count);
    [self unlock];
    return task;
}

3.KTVHCDataDownload.m 有个代理方法crash

 Application threw exception NSInvalidArgumentException: -[NSURLResponse allHeaderFields]: unrecognized selector sent to instance 

原因是 把NSURLSessionDataDelegate的代理方法的参数对象写错了,把NSURLResponse 写成子类 NSHTTPURLResponse 类型,运行时实际回调的是父类实例
修改如下:


.png
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)task
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    [self lock];
    
    KTVHCDataRequest *dataRequest = [self.requestDictionary objectForKey:task];
    NSError *error = nil;
    KTVHCDataResponse *dataResponse = nil;
    if ([response isMemberOfClass:NSHTTPURLResponse.class]) { // NSHTTPURLResponse
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        dataResponse  = [[KTVHCDataResponse alloc] initWithURL:dataRequest.URL headers:httpResponse.allHeaderFields];
        KTVHCLogDownload(@"%p, Receive response\nrequest : %@\nresponse : %@\nHTTPResponse : %@", self, dataRequest, dataResponse, httpResponse.allHeaderFields);
        if (!error && httpResponse.statusCode > 400) {
            error = [KTVHCError errorForResponseUnavailable:task.currentRequest.URL
                                                    request:task.currentRequest
                                                   response:task.response];
        }
    } else { // NSURLResponse
        dataResponse  = [[KTVHCDataResponse alloc] initWithURL:dataRequest.URL headers:nil];
    }
 ...................   
  ......
}

4.lock方法使用懒加载问题

项目里面全是这种写法

应当把锁属性创建过程写在初始化方法中

5.KTVHCDownload死锁问题




应该改成: 把NSLock使用递归锁替代;并且锁的创建写在对象的初始化方法中


6.HTTPConnection.m 有数组越界bug

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

HTTPConnection.m:1477
DDRange range = [[ranges objectAtIndex:rangeIndex] ddrangeValue];

修改代码如下:


你可能感兴趣的:(KTVHTTPCache bug记录)