Libcurl and libcurl
0. Add a protocol header for the curl used
Original article:
If you specify URL without protocol: // prefix, curl will attempt to guess what protocol you might want. it will then default to HTTP but try other protocols based on often-used host name prefixes. for example, for host names starting with "ftp. "curl will assume you want to speak FTP.
1. Write the curl_easy_perform () callback data directly to the FILE (FILE *)
Original article:
Libcurl offers its own default internal callback that will take care of the data if you don't set the callback with CURLOPT_WRITEFUNCTION. it will then simply output the stored ed data to stdout. you can have the default callback write the data to a different file handle by passing a 'file * 'to a FILE opened for writing with the CURLOPT_WRITEDATA option.
Implementation in source code:
In this way, you can write less callback functions (hello, how laZy are you). The example is as follows:
FILE *fp;fp = fopen("/root/test.bmp", "wb");...curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);...fclose(fp);
2. curl_easy_perform return value processing
An error occurred while saving with CURLOPT_ERRORBUFFER. buf_size = CURL_ERROR_SIZE
Or use curl_easy_strerror (res) (this is simple)
Example:
/* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) { printf("%s curl_easy_perform() error! \n", __FUNCTION__); printf("error msg = %s\n", curl_easy_strerror(res)); return -1; }
3. Configure CURLOPT_NOSIGNAL in a multi-threaded Environment
Original article:
When using multiple threads you shoshould set the CURLOPT_NOSIGNAL option to 1 for all handles. everything will or might work fine yet t that timeouts are not honored during the DNS lookup-which you can work around by building libcurl with c-ares support. c-ares is a library that provides asynchronous name resolves. on some platforms, libcurl simply will not function properly multi-threaded unless this option is set.
For CURLOPT_TIMEOUT (0 by default), CURLOPT_CONNECTTIMEOUT (300 by default) Options:
In unix-like systems, this might cause signals to be used unless CURLOPT_NOSIGNAL is set.
4. Necessity of setting CURLOPT_VERBOSE and CURLOPT_HEADER
Original article:
There's one golden rule when these things occur: set the CURLOPT_VERBOSE option to 1. it'll cause the library to spew out the entire protocol details it sends, some internal info and some other ed protocol data as well (especially when using FTP ). if you're using HTTP, adding the headers in the specified ed output to study is also a clever way to get a better understanding why the server behaves the way it does. include headers in the normal body output with CURLOPT_HEADER set 1.
Tested:
After you set curl_easy_setopt (curl, CURLOPT_HEADER, 1L), the callback function returns information related to the http header (originally directly output to stdout). This information must be filtered out, so do not set this.
5. curl post considerations
Original article:
Using POST with HTTP 1.1 implies the use of a "exact CT: 100-continue" header. You can disable this header with CURLOPT_HTTPHEADER as usual.
Explanation:
When the libcurl POST method is used, if the size of the POST data is greater than 1024 bytes, libcurl will not directly send the POST request, but will execute the request in two steps:
<1> send a request containing an exact CT: 100-continue field in the request header to check whether the server is willing to accept data.
<2> after receiving the 100-continue response from the server, it will initiate a 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 as follows:
It allows the client to determine whether the server is willing to receive the data before sending the request data. If the server is willing to receive the data, the client will actually send the data,
The reason for this is that if the client directly sends the request data, but the server rejects the request, this kind of behavior will bring a lot of resource overhead.
To avoid this problem, libcurl adopts this method when sending a POST request larger than 1024 bytes. However, it causes a higher request latency,
In addition, not all servers will correctly handle and respond to "100-continue", such as lighttpd, 417 "Expectation Failed" will be returned, resulting in a request logic error.
Solution:
// POST data contains more than 1024 bytes, struct curl_slist * headerlist = NULL; static const char buf [] = "Expect CT:"; headerlist = curl_slist_append (headerlist, buf ); /* initalize custom header list */curl_easy_setopt (curl, CURLOPT_HTTPHEADER, headerlist);/* set header */curl_slist_free_all (headerlist);/* free slist */
6. Correct return of the callback function
return (size * nmemb);
Cause:
Your callback function shocould return the number of bytes it "took care ". if that is not the exact same amount of bytes that was passed to it, libcurl will abort the operation and return with an error code.
If the data received in the callback function is incorrect, you may return 0 or the number of data that you have processed,
Because the source code is processed as follows:
/* If the previous block of data ended with CR and this block of data is just a NL, then the length might be zero * // len is the data length to be sent to the callback function if (len) {wrote = data-> set. fwrite_func (ptr, 1, len, data-> set. out);} else {wrote = len;} if (wrote! = Len) {failf (data, "Failed writing body (% zu! = % Zu) ", wrote, len); return CURLE_WRITE_ERROR ;}
The use of libcurl library in c ++ Multithreading
The Library itself is thread-safe. Do not share the CURL * handle among multiple threads. It should be correct. The following is an official statement:
Multi-threading Issues in curl. haxx. se/..l.html:
The first basic rule is that you must never
Simultaneously share a libcurl handle (be it easy or multi or whatever)
Between multiple threads. Only use one handle in one thread at any
Time. You can pass the handles around among threads, but you must never
Use a single handle from more than one thread at any given time.
Libcurl is completely thread safe, cannot t for two
Issues: signals and SSL/TLS handlers. Signals are used for timing out
Name resolves (during DNS lookup)-when built without c-ares support
And not on Windows.
If you are accessing HTTPS or FTPS URLs in
Multi-threaded manner, you are then of course using the underlying SSL
Library multi-threaded and those libs might have their own requirements
On this issue. Basically, you need to provide one or two functions
Allow it to function properly.
In windows, how does one install curl? How can I configure libcurl? I'm a cainiao. Thank you,