没有为post http消息调用libcurl write callback

时间:2018-05-08 12:42:23

标签: http winapi libcurl comet chunked-encoding

简介

我正在向服务器发送POST请求,该请求使用分块消息进行响应。 所以我试图在每个收到的分块http消息上调用writecallback。

代码

#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

size_t write_callback(char *d, size_t n, size_t l, void *userp)
{
    cerr << ""--- Called once" << endl;
    return n*l;
}

string xml_msg()
{
    return "<<some request data>>";
}

curl_slist* get_header(size_t content_length)
{
    auto list = curl_slist_append(nullptr, "<<protocol version>>");
    list = curl_slist_append(list, "Content-Type: text/xml");
    list = curl_slist_append(list, "Content-Length: " + content_length);
    return list;
}

void main()
{
    auto xml = xml_msg();

    curl_global_init(CURL_GLOBAL_ALL);

    auto curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "<<server url>>");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0)");
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_easy_setopt(curl, CURLOPT_USERPWD, "<<user credentials>>");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, get_header(xml.size()));
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, xml.data());
    curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);

    curl_easy_perform(curl);

    curl_easy_cleanup(curl);
    curl_global_cleanup();
}

详细日志

* STATE: INIT => CONNECT handle 0x15c4de0; line 1422 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x15c4de0; line 1458 (connection #0)
*   Trying xxx.xxx.xxx.xxx...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x15c4de0; line 1539 (connection #0)
* Connected to <<host>> (xxx.xxx.xxx.xxx) port 80 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x15c4de0; line 1591 (connection #0)
* Marked for [keep alive]: HTTP default
* STATE: SENDPROTOCONNECT => PROTOCONNECT handle 0x15c4de0; line 1605 (connection #0)
* STATE: PROTOCONNECT => DO handle 0x15c4de0; line 1626 (connection #0)
* Server auth using Basic with user '<<credentials>>'
> POST <<URL>>
Host: <<host>>
Authorization: Basic <<base64 credentials>>
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0)
Accept: */*
Content-Type: text/xml
Content-Length: 204

* upload completely sent off: 204 out of 204 bytes
* STATE: DO => DO_DONE handle 0x15c4de0; line 1688 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x15c4de0; line 1813 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x15c4de0; line 1823 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
< Date: Tue, 08 May 2018 12:29:49 GMT
* Server is not blacklisted
< Server: <<server>>
< Expires: Thu, 01 Jan 1970 00:00:00 GMT
< Content-Language: en-US
< Cache-Control: no-cache, no-store
< Pragma: no-cache
< Content-Type: application/xml;charset=UTF-8
< Set-Cookie: <<cookie>>
< Transfer-Encoding: chunked
<
--- Called once
* STATE: PERFORM => DONE handle 0x15c4de0; line 1992 (connection #0)
* multi_done
* Connection #0 to <<server>> left intact

问题

当由于FIN tcp数据包超时而服务器关闭连接时调用了Writecallback,而不是在收到chunked http响应时。

此事件间隔约30秒。

问题

我做错了什么?

更新1

服务器返回带有PUSH标志的tcp段和带有包含XML的分块传输编码的http消息。消息以CLRF结尾。同时Win API套接字不允许读取它,select()返回0,这意味着在这个套接字上没有任何可读/写的内容。

由于心跳超时(即服务器的内部实现)关闭连接之前30秒延迟,服务器使用分块传输编码发送最终的http消息,其中包含0和CLRF。在该消息之后,select()显示新的套接字状态,libcurl调用write回调并返回分块的消息内容。

这是我在调试libcurl后看到的。我需要找到收到libcurl后收到的chunked http消息的方法,而不是在收到最终的http消息之后。

0 个答案:

没有答案