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.