Apache2ウェブサーバ上で実行されているPHPアプリケーションを使用してファイルをアップロードしようとしているC++プログラムがあります。私は何をすべきかわからない非常に奇妙な問題を抱えています。したがって、httpSendRequest()関数を使用してHTTPリクエストを作成し、wireshark で表示すると適切にフォーマットされているように見えます。しかし、ファイルはアップロードされていません.APIのaccess.logファイルを見ると、明らかに両方のパケットキャプチャにもかかわらず、ユーザーエージェントやコンテンツの長さは表示されません。
Apache2のaccess.logが同じパケットの場合
参考として、200ステータスを返すリクエストは、httpSendRequestを使用する私のプログラムの代わりに、burp suiteを使って送信されたものと同じです。また、Webブラウザを使用してファイルをアップロードすることもできます。
httpSendRequestを使用してリクエストを作成するコードは、ほとんどがthis codeguru postから直接取得されています。
#include <windows.h>
#include <wininet.h>
#include <iostream>
#define ERROR_OPEN_FILE 10
#define ERROR_MEMORY 11
#define ERROR_SIZE 12
#define ERROR_INTERNET_OPEN 13
#define ERROR_INTERNET_CONN 14
#define ERROR_INTERNET_REQ 15
#define ERROR_INTERNET_SEND 16
using namespace std;
int main()
{
// Local variables
static char *filename = "test.txt"; //Filename to be loaded
static char *type = "image/jpg";
static char boundary[] = "PaulRules"; //Header boundary
static char nameForm[] = "fileToUpload"; //Input form name
static char iaddr[] = "192.168.0.105"; //IP address
static char url[] = "upload.php"; //URL
char hdrs[255]; //Headers
char * buffer; //Buffer containing file + headers
char * content; //Buffer containing file
FILE * pFile; //File pointer
long lSize; //File size
size_t result;
// Open file
pFile = fopen (filename , "rb");
if (pFile==NULL) return ERROR_OPEN_FILE;
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
content = (char*) malloc (sizeof(char)*lSize);
if (content == NULL) return ERROR_MEMORY;
// copy the file into the buffer:
result = fread (content,1,lSize,pFile);
if (result != lSize) return ERROR_SIZE;
// terminate
fclose (pFile);
//allocate memory to contain the whole file + HEADER
buffer = (char*) malloc (sizeof(char)*lSize + 2048);
//print header
sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"fileToUpload\"; filename=\"test.bmp\"\r\n",boundary,nameForm,filename);
sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type);
int cb = strlen(buffer);
char * bp = buffer + cb;
memcpy(bp, content, lSize);
bp += lSize;
int cw = sprintf(bp,"\r\n--%s--\r\n",boundary);
//Open internet connection
HINTERNET hSession = InternetOpen("Paul was here :)",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession==NULL) return ERROR_INTERNET_OPEN;
HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(hConnect==NULL) return ERROR_INTERNET_CONN;
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*", 0, 1);
if(hRequest==NULL) return ERROR_INTERNET_REQ;
BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, cb + lSize + cw);
if(!sent) return ERROR_INTERNET_SEND;
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
return 0;
}
そして、ここでは、PHPスクリプトがサーバー側にある
<?php
$target_dir = "recvFile/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "File already exists";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "File to large";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "ERROR File not uploaded";
}
//attempt to upload the file
else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
echo "Command:1";
}
else {
echo $_FILES["fileToUpload"]["tmp_name"];
echo "ERROR uploading file";
}
}
?>
はありコロンはUser-Agentの製品トークンの合法的な文字ですアン? – IInspectable
@IInspectable私はそれが合法だと確信していますが、とにかくそれが不要なので、私はそれを取り除きました。同じ問題が残っているにもかかわらず。 – pchihak