Skip to content
This repository
Browse code

Fix request stream exhaustion error by copying the original NSInputSt…

…ream
  • Loading branch information...
commit ecf6899d5fd1844ecffcb125349559adeb57306c 1 parent 6cf4565
Alex Burgel authored January 05, 2013
58  AFNetworking/AFHTTPClient.m
@@ -740,7 +740,9 @@ - (id)copyWithZone:(NSZone *)zone {
740 740
 @interface AFHTTPBodyPart : NSObject
741 741
 @property (nonatomic, assign) NSStringEncoding stringEncoding;
742 742
 @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;
744 746
 @property (nonatomic, assign) unsigned long long bodyContentLength;
745 747
 
746 748
 @property (nonatomic, assign) BOOL hasInitialBoundary;
@@ -821,8 +823,8 @@ - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
821 823
     AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
822 824
     bodyPart.stringEncoding = self.stringEncoding;
823 825
     bodyPart.headers = mutableHeaders;
824  
-    bodyPart.inputStream = [NSInputStream inputStreamWithURL:fileURL];
825  
-    
  826
+    bodyPart.inputURL = fileURL;
  827
+
826 828
     NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
827 829
     bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];
828 830
     
@@ -867,7 +869,7 @@ - (void)appendPartWithHeaders:(NSDictionary *)headers
867 869
     bodyPart.stringEncoding = self.stringEncoding;
868 870
     bodyPart.headers = headers;
869 871
     bodyPart.bodyContentLength = [body length];
870  
-    bodyPart.inputStream = [NSInputStream inputStreamWithData:body];
  872
+    bodyPart.inputData = body;
871 873
     
872 874
     [self.bodyStream appendHTTPBodyPart:bodyPart];
873 875
 }
@@ -898,7 +900,7 @@ - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
898 900
 
899 901
 #pragma mark -
900 902
 
901  
-@interface AFMultipartBodyStream ()
  903
+@interface AFMultipartBodyStream () <NSCopying>
902 904
 @property (nonatomic, assign) NSStreamStatus streamStatus;
903 905
 @property (nonatomic, strong) NSError *streamError;
904 906
 
@@ -931,6 +933,20 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding {
931 933
     return self;
932 934
 }
933 935
 
  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
+
934 950
 - (void)setInitialAndFinalBoundaries {
935 951
     if ([self.HTTPBodyParts count] > 0) {
936 952
         for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
@@ -1051,8 +1067,9 @@ - (BOOL)_setCFClientFlags:(__unused CFOptionFlags)inFlags
1051 1067
     AFFinalBoundaryPhase         = 4,
1052 1068
 } AFHTTPBodyPartReadPhase;
1053 1069
 
1054  
-@interface AFHTTPBodyPart () {
  1070
+@interface AFHTTPBodyPart () <NSCopying> {
1055 1071
     AFHTTPBodyPartReadPhase _phase;
  1072
+    NSInputStream *_inputStream;
1056 1073
     unsigned long long _phaseReadOffset;
1057 1074
 }
1058 1075
 
@@ -1063,7 +1080,8 @@ @implementation AFHTTPBodyPart
1063 1080
 @synthesize stringEncoding = _stringEncoding;
1064 1081
 @synthesize headers = _headers;
1065 1082
 @synthesize bodyContentLength = _bodyContentLength;
1066  
-@synthesize inputStream = _inputStream;
  1083
+@synthesize inputData = _inputData;
  1084
+@synthesize inputURL = _inputURL;
1067 1085
 @synthesize hasInitialBoundary = _hasInitialBoundary;
1068 1086
 @synthesize hasFinalBoundary = _hasFinalBoundary;
1069 1087
 
@@ -1078,6 +1096,18 @@ - (id)init {
1078 1096
     return self;
1079 1097
 }
1080 1098
 
  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
+
1081 1111
 - (void)dealloc {
1082 1112
     if (_inputStream) {
1083 1113
         [_inputStream close];
@@ -1085,6 +1115,20 @@ - (void)dealloc {
1085 1115
     }
1086 1116
 }
1087 1117
 
  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
+
1088 1132
 - (NSString *)stringForHeaders {
1089 1133
     NSMutableString *headerString = [NSMutableString string];
1090 1134
     for (NSString *field in [self.headers allKeys]) {
13  AFNetworking/AFURLConnectionOperation.m
@@ -657,6 +657,19 @@ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
657 657
     }
658 658
 }
659 659
 
  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
+
660 673
 #pragma mark - NSCoding
661 674
 
662 675
 - (id)initWithCoder:(NSCoder *)aDecoder {

0 notes on commit ecf6899

Please sign in to comment.
Something went wrong with that request. Please try again.