开发者

Is this a mal-formed http request from an iPad, which kills Node.js multipart parser

开发者 https://www.devze.com 2023-01-20 12:43 出处:网络
The below code is used in an iPad app to send an HTTP request to a Node.js web server, which produces the following error, but works fine w/ a regular HTML+browser form.

The below code is used in an iPad app to send an HTTP request to a Node.js web server, which produces the following error, but works fine w/ a regular HTML+browser form.

The server is Node.js + formidable which has a multipart parser that only dies on this line of code with this error:

message: parser error, 0 of 29162 bytes parsed

stack: Error: parser error, 0 of 29162 bytes parsed 开发者_JAVA百科 at IncomingForm.write (/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:120:17) at IncomingMessage. (/usr/local/lib/node/.npm/formidable/0.9.8/package/lib/formidable/incoming_form.js:73:12)

at IncomingMessage.emit (events:27:15)
at HTTPParser.onBody (http:100:23)
at Stream.ondata (http:763:22)
at IOWatcher.callback (net:494:29)
at node.js:768:9

This is the iPad code:

NSMutableURLRequest * theRequest   = [[NSMutableURLRequest alloc] initWithURL:url];

[theRequest setTimeoutInterval:60];

[theRequest setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[theRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];


//media 

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"iosaudio.cai\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:theAudio]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[theRequest setHTTPBody:body];

Is the request being sent malformed? If so, why and how should it be done?


We actually just had this same problem in some iOS code posting to Node.js. Our issue turned out to be the CR-LF preceding the first boundary. Node.js uses a component for parsing MIME that is very picky about the format and the preceding CR-LF characters are viewed as malformed. I'm not sure, but the CR-LF following your last boundary may cause the same issue. Your first boundary should look like this:

[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

Your final boundary should look like this:

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

Note that intervening boundaries should include the CR-LF before and after the boundary:

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

My read of the specification indicates you can have data before the first boundary and after the final boundary, but the parser should ignore these areas. The component Node.js is using to parse this is being very picky. There is a patch to the parsing component, but it hasn't made its way into the project yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号