cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Problem found testing the sample code

From: lyx <sdulyx_at_163.com>
Date: Thu, 28 Apr 2016 19:24:23 +0800 (CST)

Hi Daniel,
I'm using curl-7.48.0. The SSL version is OpenSSL 1.0.2g. The OS is Ubuntu 14.04 LTS. The test code will display the request and response packet content in the screen. I ran the program several times and the response message could not be completely displayed every time.

The code is as following:

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

/* Auxiliary function that waits on the socket. */
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
{
    struct timeval tv;
    fd_set infd, outfd, errfd;
    int res;

    tv.tv_sec = timeout_ms / 1000;
    tv.tv_usec= (timeout_ms % 1000) * 1000;

    FD_ZERO(&infd);
    FD_ZERO(&outfd);
    FD_ZERO(&errfd);

    FD_SET(sockfd, &errfd); /* always check for error */

    if(for_recv) {
        FD_SET(sockfd, &infd);
    }
    else {
        FD_SET(sockfd, &outfd);
    }

    /* select() returns the number of signalled sockets or -1 */
    res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
    return res;
}

int main(void)
{
    CURL *curl;
    CURLcode res;
    /* Minimalistic http request */
    const char *request = "GET / HTTP/1.0\r\nHost: www.baidu.com\r\nAccept: */*\r\n\r\n";
    curl_socket_t sockfd; /* socket */
    long sockextr;
    size_t iolen;
    curl_off_t nread;

    /* A general note of caution here: if you're using curl_easy_recv() or
    curl_easy_send() to implement HTTP or _any_ other protocol libcurl
    supports "natively", you're doing it wrong and you should stop.

    This example uses HTTP only to show how to use this API, it does not
    suggest that writing an application doing this is sensible.
*/

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com");
        /* Do not do the transfer - only connect to host */
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        
        res = curl_easy_perform(curl);

        if(CURLE_OK != res) {
            printf("Error: %s\n", strerror(res));
            return 1;
        }

        /* Extract the socket from the curl handle - we'll need it for waiting.
    * Note that this API takes a pointer to a 'long' while we use
    * curl_socket_t for sockets otherwise.
    */
        res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr);

        if(CURLE_OK != res) {
            printf("Error: %s\n", curl_easy_strerror(res));
            return 1;
        }

        sockfd = (curl_socket_t)sockextr;

        /* wait for the socket to become ready for sending */
        if(!wait_on_socket(sockfd, 0, 60000L)) {
            printf("Error: timeout.\n");
            return 1;
        }

        puts("Sending request.");
        /* Send the request. Real applications should check the iolen
    * to see if all the request has been sent */
        res = curl_easy_send(curl, request, strlen(request), &iolen);

        if(CURLE_OK != res) {
            printf("Error: %s\n", curl_easy_strerror(res));
            return 1;
        }
        puts("Reading response.");

        /* read the response */
        for(;;)
        {
            char buf[1024+1];
            memset (buf, 0, sizeof (buf));

            wait_on_socket(sockfd, 1, 60000L);
            res = curl_easy_recv(curl, buf, 1024, &iolen);

            if(CURLE_OK != res)
                break;

            nread = (curl_off_t)iolen;

            printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread);
            printf ("%s", buf);
        }
        
        printf ("\n");

        /* always cleanup */
        curl_easy_cleanup(curl);
    }

    return 0;
}

>
>Message: 3
>Date: Thu, 28 Apr 2016 10:02:17 +0200 (CEST)
>From: Daniel Stenberg <daniel_at_haxx.se>
>To: libcurl development <curl-library_at_cool.haxx.se>
>Subject: Re: Problem found testing the sample code
>Message-ID: <alpine.DEB.2.20.1604280958570.5747_at_tvnag.unkk.fr>
>Content-Type: text/plain; charset=US-ASCII; format=flowed
>
>On Wed, 27 Apr 2016, lyx wrote:
>
>> I'm testing libcurl sample code in my ubuntu. This code is as listed in
>> https://curl.haxx.se/libcurl/c/sendrecv.html. I copied it from above webpage
>> and compiled in my ubuntu. I enabled VERBOS option and changed the URL to
>> https://www.baidu.com to test customized send and recv under https protocol.
>> I find the sample code worked sometimes I run it while failed to work in
>> other times.
>
>Then please give us more details (what the problems are, which libcurl version
>you're using on what platform and what TLS backend you use). I tried it now
>against my own HTTPS site and it seemed to behave fine many times in a row.
>
>> The sample code will not display any https reply or can only
>> display part of the reply.
>
>The sample code doesn't display any request nor response, correct. That's by
>design. It's only a small (and silly) example.
>
>> If I only use curl_easy_perform to test this website without using recv and
>> send function, it worked every time.
>
>You should also be aware that we *strongly* discourage the use of
>curl_easy_recv and curl_easy_send if you intend to use one of the protocols
>libcurl already supports.
>
>--
>
> / daniel.haxx.se
>
>
>------------------------------
>
>Subject: Digest Footer
>
>_______________________________________________
>curl-library mailing list
>curl-library_at_cool.haxx.se
>http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-library
>
>
>------------------------------
>
>End of curl-library Digest, Vol 128, Issue 34
>*********************************************

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2016-04-28