Re: [gmime-devel] Problem parsing HTTP POST multipart/form-data



Hi Ron,

On 4/15/2016 4:48 AM, Ron wrote:
Hi,

I'm having huge problems parsing multipart/form-data. I'm using 2.4.21
(because CentOS 6).

This is my POST command:

curl -v -F key1=value1 -F "key2=value2" -F "file= test txt"
http://localhost:5657/v1/test

This is the resulting HTTP request:

[snip]


Now, how do I parse this? On the server side I'm using libevent. My
approach was:

   stream = g_mime_stream_mem_new();

   // not shown, but write all headers to the stream using
g_mime_stream_write() each followed by a CRLF
   // add empty CRLF, and add the body using g_mime_stream_write()

Don't forget to call g_mime_stream_reset (stream);

Otherwise the parser will attempt to parse the stream starting at the very end of the content (i.e. the parser will have nothing to parse).


   parser = g_mime_parser_new_with_stream (stream);

   // parse the block from the stream.  vdoes not exist?
   multipart = g_mime_multipart_new();
   while ((object = g_mime_parser_construct_part(parser)) != NULL) {
     g_mime_multipart_add(multipart, object);
   }
   // but this loops only once??

Yes, it will only loop once because it will return a GMimeMultipart since that's what the content is.

The HTTP headers contain the following Content-Type header:

Content-Type: multipart/form-data;
 boundary=----------------------------2912a7dc5b77


and so GMimeParser will parse that header and recognize that it is parsing a multipart and so create a new GMimeMultipart object and then parse the boundary string and use that to figure out where each of the child parts begin and end, appending them into the multipart/form-data object that it instantiated once it parsed the Content-Type header.

When it finishes parsing all of the children of the multipart/form-data part, it will return the multipart/form-data object.

Hope that helps,

Jeff


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]