Skip to content

Commit ecf6899

Browse files
committed
Fix request stream exhaustion error by copying the original NSInputStream
1 parent 6cf4565 commit ecf6899

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

AFNetworking/AFHTTPClient.m

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,9 @@ - (id)copyWithZone:(NSZone *)zone {
740740
@interface AFHTTPBodyPart : NSObject
741741
@property (nonatomic, assign) NSStringEncoding stringEncoding;
742742
@property (nonatomic, strong) NSDictionary *headers;
743-
@property (nonatomic, strong) NSInputStream *inputStream;
743+
@property (nonatomic, strong) NSData *inputData;
744+
@property (nonatomic, strong) NSURL *inputURL;
745+
@property (nonatomic, readonly) NSInputStream *inputStream;
744746
@property (nonatomic, assign) unsigned long long bodyContentLength;
745747

746748
@property (nonatomic, assign) BOOL hasInitialBoundary;
@@ -821,8 +823,8 @@ - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
821823
AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
822824
bodyPart.stringEncoding = self.stringEncoding;
823825
bodyPart.headers = mutableHeaders;
824-
bodyPart.inputStream = [NSInputStream inputStreamWithURL:fileURL];
825-
826+
bodyPart.inputURL = fileURL;
827+
826828
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
827829
bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];
828830

@@ -867,7 +869,7 @@ - (void)appendPartWithHeaders:(NSDictionary *)headers
867869
bodyPart.stringEncoding = self.stringEncoding;
868870
bodyPart.headers = headers;
869871
bodyPart.bodyContentLength = [body length];
870-
bodyPart.inputStream = [NSInputStream inputStreamWithData:body];
872+
bodyPart.inputData = body;
871873

872874
[self.bodyStream appendHTTPBodyPart:bodyPart];
873875
}
@@ -898,7 +900,7 @@ - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
898900

899901
#pragma mark -
900902

901-
@interface AFMultipartBodyStream ()
903+
@interface AFMultipartBodyStream () <NSCopying>
902904
@property (nonatomic, assign) NSStreamStatus streamStatus;
903905
@property (nonatomic, strong) NSError *streamError;
904906

@@ -931,6 +933,20 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding {
931933
return self;
932934
}
933935

936+
-(id)copyWithZone:(NSZone *)zone {
937+
AFMultipartBodyStream *bodyStreamCopy = [[[self class] allocWithZone:zone] initWithStringEncoding:self.stringEncoding];
938+
939+
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts)
940+
{
941+
AFHTTPBodyPart *bodyPartCopy = [bodyPart copy];
942+
[bodyStreamCopy appendHTTPBodyPart:bodyPartCopy];
943+
}
944+
945+
[bodyStreamCopy setInitialAndFinalBoundaries];
946+
947+
return bodyStreamCopy;
948+
}
949+
934950
- (void)setInitialAndFinalBoundaries {
935951
if ([self.HTTPBodyParts count] > 0) {
936952
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
@@ -1051,8 +1067,9 @@ - (BOOL)_setCFClientFlags:(__unused CFOptionFlags)inFlags
10511067
AFFinalBoundaryPhase = 4,
10521068
} AFHTTPBodyPartReadPhase;
10531069

1054-
@interface AFHTTPBodyPart () {
1070+
@interface AFHTTPBodyPart () <NSCopying> {
10551071
AFHTTPBodyPartReadPhase _phase;
1072+
NSInputStream *_inputStream;
10561073
unsigned long long _phaseReadOffset;
10571074
}
10581075

@@ -1063,7 +1080,8 @@ @implementation AFHTTPBodyPart
10631080
@synthesize stringEncoding = _stringEncoding;
10641081
@synthesize headers = _headers;
10651082
@synthesize bodyContentLength = _bodyContentLength;
1066-
@synthesize inputStream = _inputStream;
1083+
@synthesize inputData = _inputData;
1084+
@synthesize inputURL = _inputURL;
10671085
@synthesize hasInitialBoundary = _hasInitialBoundary;
10681086
@synthesize hasFinalBoundary = _hasFinalBoundary;
10691087

@@ -1078,13 +1096,39 @@ - (id)init {
10781096
return self;
10791097
}
10801098

1099+
- (id)copyWithZone:(NSZone *)zone {
1100+
AFHTTPBodyPart *bodyPartCopy = [[[self class] allocWithZone:zone] init];
1101+
1102+
bodyPartCopy.stringEncoding = self.stringEncoding;
1103+
bodyPartCopy.headers = self.headers;
1104+
bodyPartCopy.bodyContentLength = self.bodyContentLength;
1105+
bodyPartCopy.inputData = self.inputData;
1106+
bodyPartCopy.inputURL = self.inputURL;
1107+
1108+
return bodyPartCopy;
1109+
}
1110+
10811111
- (void)dealloc {
10821112
if (_inputStream) {
10831113
[_inputStream close];
10841114
_inputStream = nil;
10851115
}
10861116
}
10871117

1118+
- (NSInputStream *)inputStream {
1119+
if (_inputStream) {
1120+
return _inputStream;
1121+
}
1122+
1123+
if (self.inputData) {
1124+
_inputStream = [NSInputStream inputStreamWithData:self.inputData];
1125+
} else if (self.inputURL) {
1126+
_inputStream = [NSInputStream inputStreamWithURL:self.inputURL];
1127+
}
1128+
1129+
return _inputStream;
1130+
}
1131+
10881132
- (NSString *)stringForHeaders {
10891133
NSMutableString *headerString = [NSMutableString string];
10901134
for (NSString *field in [self.headers allKeys]) {

AFNetworking/AFURLConnectionOperation.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,19 @@ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
657657
}
658658
}
659659

660+
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
661+
{
662+
NSInputStream *newBodyStream = nil;
663+
664+
NSInputStream *bodyStream = request.HTTPBodyStream;
665+
if ([bodyStream conformsToProtocol:@protocol(NSCopying)])
666+
{
667+
newBodyStream = [bodyStream copy];
668+
}
669+
670+
return newBodyStream;
671+
}
672+
660673
#pragma mark - NSCoding
661674

662675
- (id)initWithCoder:(NSCoder *)aDecoder {

0 commit comments

Comments
 (0)