'Parsing http body with C++
Currently I am constructing a small C++ web server with https://github.com/pistacheio/pistache.
However, when submitting a binary file, I only get the full body as std::string from the api.
My research tells me pistache.io lacks a "body parser" which is needed here?
Is a body always structured like this? ( I left out most of the binary data for overview)
-----------------------------41240796293250357103616514428
Content-Disposition: form-data; name="myfile"; filename="configuration.zip"
Content-Type: application/zip
PK
-----------------------------41240796293250357103616514428
And how would I parse it with C++? If it contains binary data I also cannot read it line by line.
My unexperienced human guess is
- First line boundary
- Second and third line meta data
- Fourth line blank
- From here on read byte wise until boundary
Solution 1:[1]
I solved it.
First of all you could use a library that has a body parser integrated but I did not want to start from scratch again.
Then molbdnilo wrote in the comments that there is some kind of documentation of those multipart forms: RFC 1867
There is actually not too much information and its quite funny to read that text which was supposed to be really innovate back in 1995. My guess was pretty close at the end.
It is sufficient to learn from those examples down in the RFC 1867 documentation. Parse the first lines using the newline character as a separator until you get to the start of the binary data. The separator for reading the binary data is the boundary in the bodys first line.
What also needs to be said is that a body can have a different "enctypes", so my parser only works for "multipart/form-data". Also it is possible to transmit more than one file with this form data. I rather not post my code here as it is not really complete but sufficient for my task.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andre |
