Permalink
Browse files

Issue: Timeout when buffer ends during ending boundry

  • Loading branch information...
1 parent 247863c commit c92f306c6d17e4071f42761024d2001753f1a6d3 Tomohisa Takaoka committed Nov 9, 2012
Showing with 28 additions and 25 deletions.
  1. +28 −25 AFNetworking/AFHTTPClient.m
@@ -28,15 +28,15 @@
#import <Availability.h>
#ifdef _SYSTEMCONFIGURATION_H
- #import <netinet/in.h>
- #import <netinet6/in6.h>
- #import <arpa/inet.h>
- #import <ifaddrs.h>
- #import <netdb.h>
+#import <netinet/in.h>
+#import <netinet6/in6.h>
+#import <arpa/inet.h>
+#import <ifaddrs.h>
+#import <netdb.h>
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED
- #import <UIKit/UIKit.h>
+#import <UIKit/UIKit.h>
#endif
#ifdef _SYSTEMCONFIGURATION_H
@@ -107,7 +107,7 @@ - (id)initWithField:(id)field value:(id)value {
if (!self) {
return nil;
}
-
+
self.field = field;
self.value = value;
@@ -146,7 +146,7 @@ - (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
if([value isKindOfClass:[NSDictionary class]]) {
- // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
+ // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[[[value allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]] enumerateObjectsUsingBlock:^(id nestedKey, NSUInteger idx, BOOL *stop) {
id nestedValue = [value objectForKey:nestedKey];
@@ -210,7 +210,7 @@ + (AFHTTPClient *)clientWithBaseURL:(NSURL *)url {
- (id)initWithBaseURL:(NSURL *)url {
NSParameterAssert(url);
-
+
self = [super init];
if (!self) {
return nil;
@@ -317,7 +317,7 @@ - (void)startMonitoringNetworkReachability {
}
self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
-
+
__weak __typeof(&*self)weakSelf = self;
AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
__strong __typeof(&*weakSelf)strongSelf = weakSelf;
@@ -460,7 +460,7 @@ - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]);
NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
-
+
__block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding];
if (parameters) {
@@ -518,7 +518,7 @@ - (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method
path:(NSString *)path
-{
+{
NSString *URLStringToMatched = [[[self requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] absoluteString];
for (NSOperation *operation in [self.operationQueue operations]) {
@@ -657,8 +657,8 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
return nil;
}
- self.stringEncoding = (NSStringEncoding)[aDecoder decodeIntegerForKey:@"stringEncoding"];
- self.parameterEncoding = (AFHTTPClientParameterEncoding)[aDecoder decodeIntegerForKey:@"parameterEncoding"];
+ self.stringEncoding = [aDecoder decodeIntegerForKey:@"stringEncoding"];
+ self.parameterEncoding = [aDecoder decodeIntegerForKey:@"parameterEncoding"];
self.registeredHTTPOperationClassNames = [aDecoder decodeObjectForKey:@"registeredHTTPOperationClassNames"];
self.defaultHeaders = [aDecoder decodeObjectForKey:@"defaultHeaders"];
@@ -824,7 +824,7 @@ - (void)appendPartWithFileData:(NSData *)data
NSParameterAssert(name);
NSParameterAssert(fileName);
NSParameterAssert(mimeType);
-
+
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
[mutableHeaders setValue:mimeType forKey:@"Content-Type"];
@@ -836,7 +836,7 @@ - (void)appendPartWithFormData:(NSData *)data
name:(NSString *)name
{
NSParameterAssert(name);
-
+
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"", name] forKey:@"Content-Disposition"];
@@ -907,7 +907,7 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding {
return nil;
}
- self.stringEncoding = encoding;
+ self.stringEncoding = encoding;
self.HTTPBodyParts = [NSMutableArray array];
self.numberOfBytesInPacket = NSIntegerMax;
@@ -920,7 +920,7 @@ - (void)setInitialAndFinalBoundaries {
bodyPart.hasInitialBoundary = NO;
bodyPart.hasFinalBoundary = NO;
}
-
+
[[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES];
[[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
}
@@ -940,7 +940,6 @@ - (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
if ([self streamStatus] == NSStreamStatusClosed) {
return 0;
}
-
NSInteger bytesRead = 0;
while ((NSUInteger)bytesRead < MIN(length, self.numberOfBytesInPacket)) {
@@ -955,7 +954,6 @@ - (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
}
}
}
-
return bytesRead;
}
@@ -975,7 +973,7 @@ - (void)open {
}
self.streamStatus = NSStreamStatusOpen;
-
+
[self setInitialAndFinalBoundaries];
self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator];
}
@@ -1034,6 +1032,7 @@ - (BOOL)_setCFClientFlags:(CFOptionFlags)inFlags
AFHeaderPhase = 2,
AFBodyPhase = 3,
AFFinalBoundaryPhase = 4,
+ AFEndPhase = 5,
} AFHTTPBodyPartReadPhase;
@interface AFHTTPBodyPart () {
@@ -1067,7 +1066,7 @@ - (void)dealloc {
if (_inputStream) {
[_inputStream close];
_inputStream = nil;
- }
+ }
}
- (NSString *)stringForHeaders {
@@ -1098,6 +1097,9 @@ - (unsigned long long)contentLength {
}
- (BOOL)hasBytesAvailable {
+ if (_phase == AFFinalBoundaryPhase) {
+ return YES;
+ }
switch (self.inputStream.streamStatus) {
case NSStreamStatusNotOpen:
case NSStreamStatusOpening:
@@ -1114,7 +1116,6 @@ - (BOOL)hasBytesAvailable {
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
NSInteger bytesRead = 0;
-
if (_phase == AFEncapsulationBoundaryPhase) {
NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
bytesRead += [self readData:encapsulationBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - bytesRead)];
@@ -1137,9 +1138,9 @@ - (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
if (_phase == AFFinalBoundaryPhase) {
NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]);
+
bytesRead += [self readData:closingBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - bytesRead)];
}
-
return bytesRead;
}
@@ -1178,11 +1179,13 @@ - (BOOL)transitionToNextPhase {
[self.inputStream close];
_phase = AFFinalBoundaryPhase;
break;
+ case AFFinalBoundaryPhase:
+ _phase = AFEndPhase;
+ break;
default:
_phase = AFEncapsulationBoundaryPhase;
break;
}
-
_phaseReadOffset = 0;
return YES;

0 comments on commit c92f306

Please sign in to comment.