Hi
I'm looking to upload files using http post using winsock and have a php script handle it. I've got it working fine for text files but with files that contain null characters such as exes it doesn't work. It is only copying the file buffer up to the null char into the http request. I'm not sure the correct way to do this. Help very much appreciated
Regards
Ricky
My code:
int HTTP_POST(char* UploadURL, char * PHP_Script)
{
WSADATA wsa;
struct hostent *remoteHost;
SOCKET sock;
struct sockaddr_in addr;
unsigned long win=0;
int Content_Length;
char Post_Request[998576];//998576
char Response[8576]; //2018
int iResult;
DWORD dwError;
//file handle
HANDLE hFile, hMapFile;
//something to contain the number of bytes read
DWORD dwNumRead = 0;
//a boolean test variable, to test for success of reads
BOOL bTest;
//a buffer… can actually be of any type
//Opening an existing file for reading:
char* file = "C:\\test.exe";
hFile = CreateFile(file, GENERIC_READ,FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
/* if there is a problem opening the file, a call to
GetLastError will tell you what it is */
//GetFileSize to determine size of buffer for ReadFile
DWORD BufferSize = GetFileSize(hFile,NULL);
cout << BufferSize <<endl;
char* ReadBuffer = new char [BufferSize];// create a new char with the filesize to hold the buffer of file
if(!ReadFile(hFile, ReadBuffer,BufferSize,&dwNumRead,NULL))
{
printf ("ReadFile Fail");
}
/* bTest will be TRUE if the read is successful.
If false, take a look at GetLastError */
//To close the file:
CloseHandle(hFile);
// cout << hex <<ReadBuffer;
// Sleep(5000);
string to_send = ReadBuffer;
char*RemoteFileName = "test.exe";
string name = RemoteFileName;
//Initialise winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsa);
if(iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
remoteHost = gethostbyname(UploadURL);
if(remoteHost == NULL)
{
dwError = WSAGetLastError();
if (dwError != 0)
{
if(dwError == WSAHOST_NOT_FOUND)
{
printf ("Host %s not found.\n", UploadURL);
WSACleanup();
Sleep(10000);
return 2;
}
else if (dwError == WSANO_DATA)
{
printf ("No data record found.\n");
Sleep(10000);
return 2;
}
else
{
printf("Function failed with error: %ld\n", dwError);
Sleep(10000);
return 2;
}
}
}
else
{
printf ("Successfully connected to host: %s.\n", UploadURL);
Sleep(5000);
}
win = *(unsigned long*) remoteHost->h_addr;
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
addr.sin_family=AF_INET;
addr.sin_port=htons(80);
addr.sin_addr.s_addr = win;
// Create a SOCKET for connecting to server
sock = socket(AF_INET, SOCK_STREAM, 0);
if( sock == INVALID_SOCKET)
{
printf("Server: Error at socket(), error code: %ld\n", WSAGetLastError());
// Clean up
WSACleanup();
// and exit with error
return 2;
}
Content_Length = (to_send.size()+name.size()+287);
//HTTP POST Post_Request, constructed using a packet sniffer
sprintf(Post_Request, "POST %s HTTP/1.1\r\n", PHP_Script);
sprintf(Post_Request, "%sHost: %s\r\n", Post_Request, UploadURL);
sprintf(Post_Request, "%sContent-Type: multipart/form-data; boundary=---------------------------297771262821979\r\n", Post_Request);
sprintf(Post_Request, "%sContent-Length: %d\r\n", Post_Request, Content_Length);
sprintf(Post_Request, "%s\r\n", Post_Request);//blank line under content length
sprintf(Post_Request, "%s-----------------------------297771262821979\r\n", Post_Request);
sprintf(Post_Request, "%sContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n", Post_Request, name.c_str());
sprintf(Post_Request, "%sContent-Type: application/octet-stream\r\n", Post_Request);
sprintf(Post_Request, "%s\r\n", Post_Request);
sprintf(Post_Request, "%s%s\r\n", Post_Request, ReadBuffer);
sprintf(Post_Request, "%s-----------------------------297771262821979\r\n", Post_Request);
sprintf(Post_Request, "%sContent-Disposition: form-data; name=\"submit\"\r\n", Post_Request);
sprintf(Post_Request, "%s\r\n", Post_Request);
sprintf(Post_Request, "%sSubmit\r\n\r\n", Post_Request);
sprintf(Post_Request, "%s-----------------------------297771262821979--\r\n\r\n\0", Post_Request);
//cout << Post_Request ;
// Connect to server.
iResult = connect(sock, (SOCKADDR*)&addr, sizeof(addr));
if(iResult == SOCKET_ERROR)
{
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 2;
}
// Send buffer
iResult = send(sock, Post_Request, strlen(Post_Request), 0);
if(iResult == SOCKET_ERROR)
{
printf("send() failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 2;
}
else
{
printf("Bytes sent: %d\n", iResult);
}
while (iResult > 0 )
{
iResult = recv(sock, Response, strlen(Response), 0);
if ( iResult > 0 )
{
printf("Bytes received: %d\n", iResult);
printf("%s", Response);
Sleep(5000);
}
else if (iResult == 0)
{
printf("Connection closed\n");
Sleep(5000);
}
else
{
printf("recv failed with error: %d\n", WSAGetLastError());
Sleep(5000);
}
//
}
// cleanup
delete ReadBuffer;
closesocket(sock);
WSACleanup();
//cout << win << endl;
//cin.get();
return EXIT_SUCCESS;
}