我尝试使用curl库来构建HTTP代理。我现在遇到的问题是分块HTTP响应不正确。通过检查文件保存的HTTP响应,我发现缺少HTTP响应开始和结束时的块大小。 HTTP响应来自curl回调函数,如下所示。
size_t httpClient::write_data(char *ptr, size_t size, size_t nmemb, void* userdata)
{
// get total size
size_t realsize = size*nmemb;
// get object
struct MemoryStruct* mem = (struct MemoryStruct*)userdata;
// size of memory block pointerd by the mem->memory is changed to the new bytes
mem->memory = (char*)realloc(mem->memory, (mem->size + realsize));
if(mem->memory == NULL)
{
printf("not enough memory (realloc returned NULL)\n");
exit(1);
}
// copy to memory structure
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
//mem->memory[mem->size] = 0;
cout<<"Write ["<<realsize<<"] bytes Data to chunk"<<endl;
return realsize;
}
希望有人可以指出我为此回调函数所犯的错误。谢谢:D
预期的分块HTTP响应如下所示:
HTTP/1.1 200 OK^M
Date: Wed, 17 Oct 2012 07:44:54 GMT^M
Expires: -1^M
Cache-Control: private, max-age=0^M
Content-Type: text/html; charset=UTF-8^M
Set-Cookie: PREF=ID=58da9f2271ea2d2b:FF=0:TM=1350459894:LM=1350459894:S=EnJS1hQo2d6_AnPM; expires=Fri, 17-Oct-2014 07:44:54 GMT; path=/; domain=.google.com^M
Set-Cookie: NID=65=RW0txpQSNA4NwlRhp0y1I6iF3L0xfugw8Bv4GMsB1yE1qu7iGoBO_2ZxqS0-DSeS4tJKnV26JlfVZmsnjxnjdUaHTDj3-AFREsvyMiE8wSKyabwYG8x-e18Pj8smdxUs; expires=Thu, 18-Apr-2013 07:44:54 GMT; path=/; domain=.google.com; HttpOnly^M
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."^M
Content-Encoding: gzip^M
Transfer-Encoding: chunked^M
Server: gws^M
X-XSS-Protection: 1; mode=block^M
X-Frame-Options: SAMEORIGIN^M
^M
907a^M
十六进制格式的数字907a是我的HTTP响应文件中遗漏的块大小。
答案 0 :(得分:0)
我可以向你提出下一步:
struct MemoryStruct
。struct MemoryStruct *userdata
,而不是void *userdata
。您的代码将显示为:
MemoryStruct data;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_data);
---------------------------------------------------------------------
// if you'll used std::string instead char*
---------------------------------------------------------------------
size_t write_data(char *data, size_t size, size_t count, MemoryStruct *userdata)
{
int result = 0;
if(userdata->memory != NULL) {
userdata->memory->append(data, size*count);
result = size*count;
userdata->size += result;
}
return result;
}
卷曲库curl_easy_perform(CURL *handler)
的调用函数后,您可以得到正确的响应。
PS。如果我错了你的问题,请在调用回调函数的地方显示你的代码