Jump to content

  • Log In with Google      Sign In   
  • Create Account

Trouble uploading file using POST request


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 21 February 2012 - 08:02 AM

Hello,

I have homework to upload file using HTTP protocol, therefore I cannot use any kind of library, it must be raw packets with send() and recv().
I've tried to inspect what packets Firefox sends when I try to upload file, but it's quite messy. Failed to find any good link on Google too, so I hope someone could help me out with this.

User enters link to upload form (currently I'm trying http://yourfilelink.com/upload.php) and path to file which to upload (using Main.cpp). When I send POST request and file data server replies with "400 Bad Request", tried adding various lines to header but no luck so far. Here's my header:
string HTTPRequest = "";
HTTPRequest += "POST " + Path + "HTTP/1.1" + "\r\n"; // Path is "/upload.php"
HTTPRequest += "Host: " + Domain + "\r\n"; // Domain is "yourfilelink.com"
HTTPRequest += "Connection: keep-alive\r\n";
HTTPRequest += "Content-Length: " + tostring(FileSize) + "\r\n"; // function returns string "5322"
HTTPRequest += "Content-Type: application/octet-stream\r\n";
HTTPRequest += "Content-Disposition: attachment; filename=\"" + Filename2Upload + "\"\r\n"; // Filename2Upload is name of file, not whole path
HTTPRequest += "\r\n";
send(Socket, HTTPRequest.c_str(), HTTPRequest.length(), 0);

Header is followed by file transfer:
char FileBuffer[0xFF] = {0};
size_t SentSoFar = 0;
while(SentSoFar < FileSize) {
  int ToSend = min(sizeof(FileBuffer), FileSize - SentSoFar);
  File2Upload.read(FileBuffer, ToSend);
  send(Socket, FileBuffer, ToSend, 0);
  SentSoFar += ToSend;
}

And finally I receive reply (which is small page of Bad Request error):
char HTTPReply[0xFF] = {0};
int ReplySize = recv(Socket, HTTPReply, sizeof(HTTPReply), 0);

Would be nice if anyone could help me out :)
Thank you in advance.

Sponsor:

#2 Diath   Members   -  Reputation: 204

Like
0Likes
Like

Posted 21 February 2012 - 09:30 AM

I am not familiar with this but add kind of logging system to your upload.php file and check what does script receive, maybe you're sending something wrong.

#3 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 21 February 2012 - 09:44 AM

It's not mine. I just found random file sharing website which I'm trying to upload file to.

#4 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 21 February 2012 - 09:54 AM

Unusual homework...

Failed to find any good link on Google too


To create a POST request manually, it must comply with HTTP spec. Which is a lot of very dry reading. But it does have an example on what a file upload might look like.

Simple version is that POST request depends on the form that defines it, so there's more in request than just file to upload.

In addition, when using a third party service, there might be other checks in place.

#5 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 23 February 2012 - 02:18 PM

Thank you. It seems I'm still having problems Posted Image
I made simple .php page which allows to upload file. Used Firefox with HttpFox addon to track packets and tried to simulate them, however I'm having some troubles.

According to HttpFox this gets sent when I upload file:
(Request-Line) POST /****/upload_file.php HTTP/1.1
Host ****
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.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
Connection keep-alive
Referer http://****/****/upload.php
Content-Type multipart/form-data; boundary=---------------------------265001916915724
Content-Length 483636
-----------------------------265001916915724
Content-Disposition: form-data; name="file"; filename="putty.exe"
Content-Type: application/octet-stream
<binary data="" here="">
-----------------------------265001916915724
Content-Disposition: form-data; name="submit"
Submit
-----------------------------265001916915724--

And this is what I send:
HTTPRequest POSTRequest1("POST " + Path + " HTTP/1.1");
POSTRequest1["Host"] = Domain;
POSTRequest1["Connection"] = "keep-alive";
// Boundary is randomly generated, looks similar to Firefox's one
POSTRequest1["Content-Type"] = "multipart/form-data; boundary=" + Boundary;
// I don't set length here yet because I do not know it

HTTPRequest POSTRequest2("--" + Boundary);
POSTRequest2["Content-Disposition"] = "form-data; name=\"file\"; filename=\"" + Filename2Upload + "\"";
POSTRequest2["Content-Type"] = "application/octet-stream";

HTTPRequest POSTRequest3("--" + Boundary);
POSTRequest3["Content-Disposition"] = "form-data; name=\"submit\"";
POSTRequest3.SetContent("Submit", 6);

HTTPRequest POSTRequest4("--" + Boundary + "--");

// now I know length, calculating and setting it
size_t TotalLength = POSTRequest2.GetLength() + POSTRequest3.GetLength() + POSTRequest4.GetLength() + GetFileSize(Filename2Upload);
POSTRequest1["Content-Length"] = tostring(TotalLength);

POSTRequest1.send(Socket);
POSTRequest2.send(Socket);
SendFile(Filename2Upload, Socket);
POSTRequest3.send(Socket);
POSTRequest4.send(Socket);

I use this example to upload file: http://www.w3schools.com/php/php_file_upload.asp
And I get Return Error 3, which means "The uploaded file was only partially uploaded." according to http://php.net/manual/en/features.file-upload.errors.php
Can this happen because I send my request or file in parts, not everything at once?

#6 TheUnbeliever   Members   -  Reputation: 961

Like
0Likes
Like

Posted 23 February 2012 - 02:41 PM

I've never done this, and although the spec is the place to go, you might find use in installing Ethereal and directly comparing the packets sent by e.g. Firefox (you can filter using IP and protocol to get rid of almost all the cruft) and by your implementation.
[TheUnbeliever]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS