Reputation: 3171
I've seen multiple posts all over Internet about different methods, but still I didn't find a proper answer.
The problem: my CGI application is running in an embedded web server, the language I'm using is C, and what I'm trying to do is to process a file sent with a POST request of a form.
The application has been doing everything so far without the use of any libraries, but now I need to add this function and I don't really know which library can I use to do so. And doing it from scratch seems way too complicate for such a simple objective. Maybe not so much for the technical implications, but specially for the possible deviations between browsers.
Any advice about it?
EDIT: I want to be able to POST files from a browser, and I want to avoid the multipart content type, because it's overhead...
Upvotes: 3
Views: 5735
Reputation:
To upload a file in a browser using <input type="file">
, you must use enctype="multipart/form-data"
. This is not negotiable -- file inputs simply don't work with the default form encoding (application/x-www-form-urlencoded
).
I'd strongly recommend using something like Perl's CGI
module to parse the upload if you can. If you're absolutely stuck with C, though, you might want to try something like https://github.com/iafonov/multipart-parser-c.
Upvotes: 2
Reputation: 2852
I've written a server that does just this. The packets you will receive from this post request will just be data. It will be formatted as POST, which means there will be some information like
HTTP: POST Some stuff like version Data: your data
You just scan till you see Data: or some header that denotes the post request, and then you copy this data. That will be your file/arguments/whatever is sent in post. It will not be in the url (which has its own header section)
Edit: Post Header example
POST /path/script.cgi HTTP/1.0
From: [email protected]
User-Agent: HTTPTool/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
home=Mosby&favorite+flavor=flies -> This is your data (this happens to be arguments home and favoriteflavor
Upvotes: 0