views:

2018

answers:

4

I'm trying to send a file and other POST variables to a PHP script on my server. There are no good resources on Google and the code samples I've found don't work. Preferably without using cURL.

+1  A: 

I'd say roll your own. Its not too complicated.

Capture an HTTP post sent from a browser in Wireshark and reverse engineer as necessary using the spec as your guide. (See Andreas Magnusson's answer below for perhaps more relevant specs.)

I would recommend this approach personally for learning the protocol rather than just going pure spec. Its pretty difficult to learn things just from the spec. I would rather explore the different behaviors by known http clients and try to figure out how things are working by using the spec as my guide.

Format and send the data accordingly over a socket once you're comfortable with HTTP.

Also, If you are not familiar with socket programming, check out Beej's guide to socket programming.

Doug T.
Wireshark?? Seriously, reading the spec and coding from that should always be your initial solution, if rolling your own is the goal. Then when you're finished and the darn thing still doesn't work, and you have no idea why, is it time to compare your output to that from a browser by Wireshark. Tweaking a Wireshark capture is the HTTP equivalence of Cargo-Cult programming.
Andreas Magnusson
Ok change "tweak" to "reverse engineer to learn about the protocol". Its pretty difficult to learn from just specs, you need something to play around with.
Doug T.
First of all you need the correct spec (see my answer) and then your initial attempts should always be according to the spec, if that doesn't work, diff:ing with a Wireshark capture from an application that does work is always helpful.
Andreas Magnusson
+2  A: 

Just a couple of resources make it pretty easy to roll your own

Here is an example of a GET request via ASIO (the C++ networking library in Boost)

Here is the HTTP protocol made really easy

The GET request is how you can view any page on your site. With that code you can download any page and get it as raw text. As you can see it sends a GET header to the server. As explained in that HTTP protocol page, the POST request looks like this

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=Cosby&favorite+flavor=flies
  1. To send a file:
  2. You put your URL after post
  3. change the content type to the type of file you are trying to upload.
  4. Set Content-Length to the number of bytes in that file
  5. Append the file after a carrage return (replace "home=Cosby&favorite+flavor=flies")
Tom Leys
+1  A: 

Another (more quick-n-dirty) solution is to use a utility, via a system() or similar call. For example the wget utility has a --post-file option.

ynimous
+3  A: 

If you're going to roll your own you'd need the relevant RFC for HTTP file uploading (googling on "rfc http file upload" will yield the same result). This RFC also shows how to handle a mix of files and other FORM-data (or POST variables). The problem is of course that you'll probably want to read the MIME RFC as well...

Andreas Magnusson
+1 for finding the most relevant spec.
Doug T.