0

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

4

1 回答 1

0

我可以向您建议下一步:

  1. 进入您的代码部分,即设置回调函数,struct MemoryStruct在堆栈上创建。
  2. 将参数写入回调函数原型中,struct MemoryStruct *userdata以代替void *userdata.
  3. 您的代码将如下所示:

    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 库的函数后,curl_easy_perform(CURL *handler)您可以获得正确的响应。

PS。如果我误解了您的问题,请在调用回调函数的位置显示您的代码

于 2012-10-17T10:11:11.297 回答