• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ripiz

Trouble uploading file using POST request

5 posts in this topic

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 [url="http://yourfilelink.com/upload.php"]http://yourfilelink.com/upload.php[/url]) 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:
[CODE]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);[/CODE]

Header is followed by file transfer:
[CODE]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;
}[/CODE]

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

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
It's not mine. I just found random file sharing website which I'm trying to upload file to.
0

Share this post


Link to post
Share on other sites
Unusual homework...

[quote]Failed to find any good link on Google too[/quote]

To create a POST request manually, it must comply with [url="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1"]HTTP spec[/url]. 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.
0

Share this post


Link to post
Share on other sites
Thank you. It seems I'm still having problems [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
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:
[CODE]
(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--
[/CODE]

And this is what I send:
[CODE]
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);
[/CODE]

Can issue arise because I send request in parts, not whole thing at once?
0

Share this post


Link to post
Thank you. It seems I'm still having problems [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
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:
[CODE]
(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--
[/CODE]

And this is what I send:
[CODE]
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);
[/CODE]

I use this example to upload file: http://www.w3schools.com/php/php_file_upload.asp
And I get Return Error 3, which means "[color=#000000][font=verdana, arial, helvetica, sans-serif]The uploaded file was only partially uploaded.[/font][/color]" according to [url="http://php.net/manual/en/features.file-upload.errors.php"]http://php.net/manual/en/features.file-upload.errors.php[/url]
Can this happen because I send my request or file in parts, not everything at once?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0