Ben Copsey

Added ASIHTTPREQUEST_DEBUG, when set will print a string version of an ASIFormDa…

…taRequest body to the console when the body is built
@@ -26,6 +26,12 @@ typedef enum _ASIPostFormat { @@ -26,6 +26,12 @@ typedef enum _ASIPostFormat {
26 ASIPostFormat postFormat; 26 ASIPostFormat postFormat;
27 27
28 NSStringEncoding stringEncoding; 28 NSStringEncoding stringEncoding;
  29 +
  30 +#if ASIHTTPREQUEST_DEBUG
  31 + // Will store a string version of the request body that will be printed to the console when ASIHTTPREQUEST_DEBUG is set in GCC_PREPROCESSOR_DEFINITIONS
  32 + NSString *debugBodyString;
  33 +#endif
  34 +
29 } 35 }
30 36
31 #pragma mark utilities 37 #pragma mark utilities
@@ -13,8 +13,16 @@ @@ -13,8 +13,16 @@
13 @interface ASIFormDataRequest () 13 @interface ASIFormDataRequest ()
14 - (void)buildMultipartFormDataPostBody; 14 - (void)buildMultipartFormDataPostBody;
15 - (void)buildURLEncodedPostBody; 15 - (void)buildURLEncodedPostBody;
  16 +- (void)appendPostString:(NSString *)string;
  17 +
16 @property (retain) NSMutableDictionary *postData; 18 @property (retain) NSMutableDictionary *postData;
17 @property (retain) NSMutableDictionary *fileData; 19 @property (retain) NSMutableDictionary *fileData;
  20 +
  21 +#if ASIHTTPREQUEST_DEBUG
  22 +- (void)addToDebugBody:(NSString *)string;
  23 +@property (retain, nonatomic) NSString *debugBodyString;
  24 +#endif
  25 +
18 @end 26 @end
19 27
20 @implementation ASIFormDataRequest 28 @implementation ASIFormDataRequest
@@ -46,6 +54,10 @@ @@ -46,6 +54,10 @@
46 54
47 - (void)dealloc 55 - (void)dealloc
48 { 56 {
  57 +#if ASIHTTPREQUEST_DEBUG
  58 + [debugBodyString release];
  59 +#endif
  60 +
49 [postData release]; 61 [postData release];
50 [fileData release]; 62 [fileData release];
51 [super dealloc]; 63 [super dealloc];
@@ -122,6 +134,11 @@ @@ -122,6 +134,11 @@
122 if ([self haveBuiltPostBody]) { 134 if ([self haveBuiltPostBody]) {
123 return; 135 return;
124 } 136 }
  137 +
  138 +#if ASIHTTPREQUEST_DEBUG
  139 + [self setDebugBodyString:@""];
  140 +#endif
  141 +
125 if (![self postData] && ![self fileData]) { 142 if (![self postData] && ![self fileData]) {
126 [super buildPostBody]; 143 [super buildPostBody];
127 return; 144 return;
@@ -135,13 +152,22 @@ @@ -135,13 +152,22 @@
135 } else { 152 } else {
136 [self buildMultipartFormDataPostBody]; 153 [self buildMultipartFormDataPostBody];
137 } 154 }
138 - 155 +
139 [super buildPostBody]; 156 [super buildPostBody];
  157 +
  158 +#if ASIHTTPREQUEST_DEBUG
  159 + NSLog(@"%@",[self debugBodyString]);
  160 + [self setDebugBodyString:nil];
  161 +#endif
140 } 162 }
141 163
142 164
143 - (void)buildMultipartFormDataPostBody 165 - (void)buildMultipartFormDataPostBody
144 { 166 {
  167 +#if ASIHTTPREQUEST_DEBUG
  168 + [self addToDebugBody:@"\r\n==== Building a multipart/form-data body ====\r\n"];
  169 +#endif
  170 +
145 NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); 171 NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding]));
146 172
147 // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. 173 // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does.
@@ -149,19 +175,19 @@ @@ -149,19 +175,19 @@
149 175
150 [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; 176 [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]];
151 177
152 - [self appendPostData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:[self stringEncoding]]]; 178 + [self appendPostString:[NSString stringWithFormat:@"--%@\r\n",stringBoundary]];
153 179
154 // Adds post data 180 // Adds post data
155 - NSData *endItemBoundary = [[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:[self stringEncoding]]; 181 + NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary];
156 NSEnumerator *e = [[self postData] keyEnumerator]; 182 NSEnumerator *e = [[self postData] keyEnumerator];
157 NSString *key; 183 NSString *key;
158 int i=0; 184 int i=0;
159 while (key = [e nextObject]) { 185 while (key = [e nextObject]) {
160 - [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:[self stringEncoding]]]; 186 + [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key]];
161 - [self appendPostData:[[[self postData] objectForKey:key] dataUsingEncoding:[self stringEncoding]]]; 187 + [self appendPostString:[[self postData] objectForKey:key]];
162 i++; 188 i++;
163 if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body 189 if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body
164 - [self appendPostData:endItemBoundary]; 190 + [self appendPostString:endItemBoundary];
165 } 191 }
166 } 192 }
167 193
@@ -174,8 +200,8 @@ @@ -174,8 +200,8 @@
174 NSString *contentType = [fileInfo objectForKey:@"contentType"]; 200 NSString *contentType = [fileInfo objectForKey:@"contentType"];
175 NSString *fileName = [fileInfo objectForKey:@"fileName"]; 201 NSString *fileName = [fileInfo objectForKey:@"fileName"];
176 202
177 - [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, fileName] dataUsingEncoding:[self stringEncoding]]]; 203 + [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, fileName]];
178 - [self appendPostData:[[NSString stringWithFormat:@"Content-Type: %@; charset=%@\r\n\r\n", contentType, charset] dataUsingEncoding:[self stringEncoding]]]; 204 + [self appendPostString:[NSString stringWithFormat:@"Content-Type: %@; charset=%@\r\n\r\n", contentType, charset]];
179 205
180 if ([file isKindOfClass:[NSString class]]) { 206 if ([file isKindOfClass:[NSString class]]) {
181 [self appendPostDataFromFile:file]; 207 [self appendPostDataFromFile:file];
@@ -185,22 +211,32 @@ @@ -185,22 +211,32 @@
185 i++; 211 i++;
186 // Only add the boundary if this is not the last item in the post body 212 // Only add the boundary if this is not the last item in the post body
187 if (i != [[self fileData] count]) { 213 if (i != [[self fileData] count]) {
188 - [self appendPostData:endItemBoundary]; 214 + [self appendPostString:endItemBoundary];
189 } 215 }
190 } 216 }
191 217
192 - [self appendPostData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:[self stringEncoding]]]; 218 + [self appendPostString:[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary]];
193 219
  220 +#if ASIHTTPREQUEST_DEBUG
  221 + [self addToDebugBody:@"==== End of multipart/form-data body ====\r\n"];
  222 +#endif
194 } 223 }
195 224
196 - (void)buildURLEncodedPostBody 225 - (void)buildURLEncodedPostBody
197 { 226 {
  227 +
198 // We can't post binary data using application/x-www-form-urlencoded 228 // We can't post binary data using application/x-www-form-urlencoded
199 if ([[self fileData] count] > 0) { 229 if ([[self fileData] count] > 0) {
200 [self setPostFormat:ASIMultipartFormDataPostFormat]; 230 [self setPostFormat:ASIMultipartFormDataPostFormat];
201 [self buildMultipartFormDataPostBody]; 231 [self buildMultipartFormDataPostBody];
202 return; 232 return;
203 } 233 }
  234 +
  235 +#if ASIHTTPREQUEST_DEBUG
  236 + [self addToDebugBody:@"\r\n==== Building an application/x-www-form-urlencoded body ====\r\n"];
  237 +#endif
  238 +
  239 +
204 NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); 240 NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding]));
205 241
206 [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset]]; 242 [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset]];
@@ -212,13 +248,53 @@ @@ -212,13 +248,53 @@
212 int count = [[self postData] count]-1; 248 int count = [[self postData] count]-1;
213 while (key = [e nextObject]) { 249 while (key = [e nextObject]) {
214 NSString *data = [NSString stringWithFormat:@"%@=%@%@", [self encodeURL:key], [self encodeURL:[[self postData] objectForKey:key]],(i<count ? @"&" : @"")]; 250 NSString *data = [NSString stringWithFormat:@"%@=%@%@", [self encodeURL:key], [self encodeURL:[[self postData] objectForKey:key]],(i<count ? @"&" : @"")];
215 - [self appendPostData:[data dataUsingEncoding:[self stringEncoding]]]; 251 + [self appendPostString:data];
216 i++; 252 i++;
217 } 253 }
  254 +#if ASIHTTPREQUEST_DEBUG
  255 + [self addToDebugBody:@"\r\n==== End of application/x-www-form-urlencoded body ====\r\n"];
  256 +#endif
  257 +}
  258 +
  259 +- (void)appendPostString:(NSString *)string
  260 +{
  261 +#if ASIHTTPREQUEST_DEBUG
  262 + [self addToDebugBody:string];
  263 +#endif
  264 + [super appendPostData:[string dataUsingEncoding:[self stringEncoding]]];
  265 +}
  266 +
  267 +#if ASIHTTPREQUEST_DEBUG
  268 +- (void)appendPostData:(NSData *)data
  269 +{
  270 + [self addToDebugBody:[NSString stringWithFormat:@"[%lu bytes of data]",(unsigned long)[data length]]];
  271 + [super appendPostData:data];
  272 +}
  273 +
  274 +- (void)appendPostDataFromFile:(NSString *)file
  275 +{
  276 + NSError *err = nil;
  277 + unsigned long long fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath:file error:&err] objectForKey:NSFileSize] unsignedLongLongValue];
  278 + if (err) {
  279 + [self addToDebugBody:[NSString stringWithFormat:@"[Error: Failed to obtain the file of the file at '%@']",file]];
  280 + } else {
  281 + [self addToDebugBody:[NSString stringWithFormat:@"[%llu bytes of data from file '%@']",fileSize,file]];
  282 + }
  283 +
  284 + [super appendPostDataFromFile:file];
  285 +}
  286 +
  287 +- (void)addToDebugBody:(NSString *)string
  288 +{
  289 + [self setDebugBodyString:[[self debugBodyString] stringByAppendingString:string]];
218 } 290 }
  291 +#endif
219 292
220 @synthesize postData; 293 @synthesize postData;
221 @synthesize fileData; 294 @synthesize fileData;
222 @synthesize postFormat; 295 @synthesize postFormat;
223 @synthesize stringEncoding; 296 @synthesize stringEncoding;
  297 +#if ASIHTTPREQUEST_DEBUG
  298 +@synthesize debugBodyString;
  299 +#endif
224 @end 300 @end
This diff was suppressed by a .gitattributes entry.