Libcurl initiates a POST request time delay issue. Except can be empty

Source: Internet
Author: User

Recently in making a group purchase hotel app to share the Qzone function, using Libcurl to access the Qzone shared CGI interface, hotel sharing information by post transmission, in the test found that the sharing interface has an average of 2s delay, this delay is too big bar, and then asked the space of the interface person, Answer: How can, the average call latency of this interface is 100-200ms, is certainly your code has the problem. Well, start by checking the code, using Strace-p to trace the system call, and finding that Curl sent two requests, where the response of the first request was particularly slow, and that was the culprit that caused the delay, and tcpdump later found that the first request that Curl sent contained a expect: 100-continue head, this is why, so began Baidu, Google, get the explanation as follows:

When using the Libcurl post method, if the post data size is greater than 1024 bytes, Libcurl does not send the POST request directly, but is divided into two steps to execute the request:
1, send a request, the request header contains a expect:100-continue field, to ask the server is willing to accept the data
2. After receiving an answer from the 100-continue returned from the server, it will actually initiate the POST request and send the data to the server.

For the field "100-continue", the RFC document (http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html# sec8.2.3) explains this: it allows the client to determine whether the server is willing to receive the data before sending the request data, and if the server is willing to receive it, the client will actually send the data, because if the client sends the request data directly, but the server rejects the request, this behavior can lead to significant resource overhead. So in order to avoid this, Libcurl takes this approach when sending a POST request that is larger than 1024 bytes, but in contrast, it causes a delay in the request, and not all servers will correctly handle and answer "100-continue". such as LIGHTTPD, will return 417 "expectation Failed", resulting in a request logic error.

If you determine that the server does not reject more than 1024 bytes of post requests, you may not use this method and you can avoid the two side effects mentioned above, and the workaround is as follows:

    1. Disable Expection Header
    2. Curl_easy_setopt (curl, Curlopt_httpheader, Array (' Expect: '));

Disable this mechanism, test the Qzone sharing interface average call latency reduced to 200ms, this is the normal delay well, hey.
Resources:
Http://www.laruence.com/2011/01/20/1840.html

from:http://blog.csdn.net/zxgfa/article/details/7604624

Libcurl initiates a POST request time delay issue. Except can be empty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.