我尝试使用 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 响应文件中遗漏的块大小。