[C/C++] Parse multipart/form-data POST request in a small web server
-
Hi there. I am looking for a simple, basic, preferable a C way of parsing multipart/form-data POST request from a client browser. So far I have a really simple web server, which reads web browser requests in a loop and prints them to to command prompt window:
do { len = recv(..., input\_buf, ...); } while (!strstr(input\_buf, "\\r\\n\\r\\n") && len > 0); // Try to parse header and data char \* pch = nullptr, \*context = nullptr; pch = strtok\_s(input\_buf, "\\r\\n", &context); while (pch != nullptr) { ::puts(pch); pch = strtok\_s (nullptr, "\\r\\n", &context); }
When this web server starts, it just shows the following web form to the web browser:
<form type=multipart/form-data method=post action=submit><input type=file multiple required /><input type=submit /></form>
So far so good, ok, I connect to my server with firefox, select some test text file, which is a really small one - just a couple of bytes and hit an html form submit button. Server reads request and I have the following output in command prompt window:
POST /submit HTTP/1.1
Host: localhost:6666
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/
24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:6666/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0But the question is, how do I get file data? I was reading tons of online docs, when it says something about boundary strings - I have no idea what are boundary strings. I am just trying to figure out how to grab raw bytes file data. I cannot use any 3rd party libraries. And what concerns me, is that Content-Length is zero - seems like file was not transferred at all :( Can anyone help me with this? Thanks in advance :)
011011010110000101100011011010000110100101101110 0110010101110011
-
Hi there. I am looking for a simple, basic, preferable a C way of parsing multipart/form-data POST request from a client browser. So far I have a really simple web server, which reads web browser requests in a loop and prints them to to command prompt window:
do { len = recv(..., input\_buf, ...); } while (!strstr(input\_buf, "\\r\\n\\r\\n") && len > 0); // Try to parse header and data char \* pch = nullptr, \*context = nullptr; pch = strtok\_s(input\_buf, "\\r\\n", &context); while (pch != nullptr) { ::puts(pch); pch = strtok\_s (nullptr, "\\r\\n", &context); }
When this web server starts, it just shows the following web form to the web browser:
<form type=multipart/form-data method=post action=submit><input type=file multiple required /><input type=submit /></form>
So far so good, ok, I connect to my server with firefox, select some test text file, which is a really small one - just a couple of bytes and hit an html form submit button. Server reads request and I have the following output in command prompt window:
POST /submit HTTP/1.1
Host: localhost:6666
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/
24.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:6666/
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 0But the question is, how do I get file data? I was reading tons of online docs, when it says something about boundary strings - I have no idea what are boundary strings. I am just trying to figure out how to grab raw bytes file data. I cannot use any 3rd party libraries. And what concerns me, is that Content-Length is zero - seems like file was not transferred at all :( Can anyone help me with this? Thanks in advance :)
011011010110000101100011011010000110100101101110 0110010101110011