Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'stream_exhausted_fix' of https://github.com/aburgel/AFN…
Browse files Browse the repository at this point in the history
…etworking into aburgel-stream_exhausted_fix
  • Loading branch information
mattt committed Jan 6, 2013
2 parents b7ec928 + ecf6899 commit 77471bb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
58 changes: 51 additions & 7 deletions AFNetworking/AFHTTPClient.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -741,7 +741,9 @@ - (id)copyWithZone:(NSZone *)zone {
@interface AFHTTPBodyPart : NSObject @interface AFHTTPBodyPart : NSObject
@property (nonatomic, assign) NSStringEncoding stringEncoding; @property (nonatomic, assign) NSStringEncoding stringEncoding;
@property (nonatomic, strong) NSDictionary *headers; @property (nonatomic, strong) NSDictionary *headers;
@property (nonatomic, strong) NSInputStream *inputStream; @property (nonatomic, strong) NSData *inputData;
@property (nonatomic, strong) NSURL *inputURL;
@property (nonatomic, readonly) NSInputStream *inputStream;
@property (nonatomic, assign) unsigned long long bodyContentLength; @property (nonatomic, assign) unsigned long long bodyContentLength;


@property (nonatomic, assign) BOOL hasInitialBoundary; @property (nonatomic, assign) BOOL hasInitialBoundary;
Expand Down Expand Up @@ -823,8 +825,8 @@ - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init]; AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
bodyPart.stringEncoding = self.stringEncoding; bodyPart.stringEncoding = self.stringEncoding;
bodyPart.headers = mutableHeaders; bodyPart.headers = mutableHeaders;
bodyPart.inputStream = [NSInputStream inputStreamWithURL:fileURL]; bodyPart.inputURL = fileURL;

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil]; NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue]; bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];


Expand Down Expand Up @@ -869,7 +871,7 @@ - (void)appendPartWithHeaders:(NSDictionary *)headers
bodyPart.stringEncoding = self.stringEncoding; bodyPart.stringEncoding = self.stringEncoding;
bodyPart.headers = headers; bodyPart.headers = headers;
bodyPart.bodyContentLength = [body length]; bodyPart.bodyContentLength = [body length];
bodyPart.inputStream = [NSInputStream inputStreamWithData:body]; bodyPart.inputData = body;


[self.bodyStream appendHTTPBodyPart:bodyPart]; [self.bodyStream appendHTTPBodyPart:bodyPart];
} }
Expand Down Expand Up @@ -900,7 +902,7 @@ - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {


#pragma mark - #pragma mark -


@interface AFMultipartBodyStream () @interface AFMultipartBodyStream () <NSCopying>
@property (nonatomic, assign) NSStreamStatus streamStatus; @property (nonatomic, assign) NSStreamStatus streamStatus;
@property (nonatomic, strong) NSError *streamError; @property (nonatomic, strong) NSError *streamError;


Expand Down Expand Up @@ -933,6 +935,20 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding {
return self; return self;
} }


-(id)copyWithZone:(NSZone *)zone {
AFMultipartBodyStream *bodyStreamCopy = [[[self class] allocWithZone:zone] initWithStringEncoding:self.stringEncoding];

for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts)
{
AFHTTPBodyPart *bodyPartCopy = [bodyPart copy];
[bodyStreamCopy appendHTTPBodyPart:bodyPartCopy];
}

[bodyStreamCopy setInitialAndFinalBoundaries];

return bodyStreamCopy;
}

- (void)setInitialAndFinalBoundaries { - (void)setInitialAndFinalBoundaries {
if ([self.HTTPBodyParts count] > 0) { if ([self.HTTPBodyParts count] > 0) {
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) { for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
Expand Down Expand Up @@ -1059,8 +1075,9 @@ - (BOOL)_setCFClientFlags:(__unused CFOptionFlags)inFlags
AFFinalBoundaryPhase = 4, AFFinalBoundaryPhase = 4,
} AFHTTPBodyPartReadPhase; } AFHTTPBodyPartReadPhase;


@interface AFHTTPBodyPart () { @interface AFHTTPBodyPart () <NSCopying> {
AFHTTPBodyPartReadPhase _phase; AFHTTPBodyPartReadPhase _phase;
NSInputStream *_inputStream;
unsigned long long _phaseReadOffset; unsigned long long _phaseReadOffset;
} }


Expand All @@ -1074,7 +1091,8 @@ @implementation AFHTTPBodyPart
@synthesize stringEncoding = _stringEncoding; @synthesize stringEncoding = _stringEncoding;
@synthesize headers = _headers; @synthesize headers = _headers;
@synthesize bodyContentLength = _bodyContentLength; @synthesize bodyContentLength = _bodyContentLength;
@synthesize inputStream = _inputStream; @synthesize inputData = _inputData;
@synthesize inputURL = _inputURL;
@synthesize hasInitialBoundary = _hasInitialBoundary; @synthesize hasInitialBoundary = _hasInitialBoundary;
@synthesize hasFinalBoundary = _hasFinalBoundary; @synthesize hasFinalBoundary = _hasFinalBoundary;


Expand All @@ -1089,13 +1107,39 @@ - (id)init {
return self; return self;
} }


- (id)copyWithZone:(NSZone *)zone {
AFHTTPBodyPart *bodyPartCopy = [[[self class] allocWithZone:zone] init];

bodyPartCopy.stringEncoding = self.stringEncoding;
bodyPartCopy.headers = self.headers;
bodyPartCopy.bodyContentLength = self.bodyContentLength;
bodyPartCopy.inputData = self.inputData;
bodyPartCopy.inputURL = self.inputURL;

return bodyPartCopy;
}

- (void)dealloc { - (void)dealloc {
if (_inputStream) { if (_inputStream) {
[_inputStream close]; [_inputStream close];
_inputStream = nil; _inputStream = nil;
} }
} }


- (NSInputStream *)inputStream {
if (_inputStream) {
return _inputStream;
}

if (self.inputData) {
_inputStream = [NSInputStream inputStreamWithData:self.inputData];
} else if (self.inputURL) {
_inputStream = [NSInputStream inputStreamWithURL:self.inputURL];
}

return _inputStream;
}

- (NSString *)stringForHeaders { - (NSString *)stringForHeaders {
NSMutableString *headerString = [NSMutableString string]; NSMutableString *headerString = [NSMutableString string];
for (NSString *field in [self.headers allKeys]) { for (NSString *field in [self.headers allKeys]) {
Expand Down
13 changes: 13 additions & 0 deletions AFNetworking/AFURLConnectionOperation.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -657,6 +657,19 @@ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
} }
} }


- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
{
NSInputStream *newBodyStream = nil;

NSInputStream *bodyStream = request.HTTPBodyStream;
if ([bodyStream conformsToProtocol:@protocol(NSCopying)])
{
newBodyStream = [bodyStream copy];
}

return newBodyStream;
}

#pragma mark - NSCoding #pragma mark - NSCoding


- (id)initWithCoder:(NSCoder *)aDecoder { - (id)initWithCoder:(NSCoder *)aDecoder {
Expand Down

0 comments on commit 77471bb

Please sign in to comment.