cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Use read/write with same curl handle.

From: Nilesh <nilesh_at_kenati.com>
Date: Wed, 01 Mar 2006 21:44:59 +0530

Thansk for sample code.
I used the same for for testing response from the server ( with only
difference is , I have reset the default http header Expect: using
CURLOPT_HTTPHEADER)

Following is the what I got when I ran the binary,*

> POST /soap HTTP/1.1
Host: 192.168.22.91
Accept: */*
Content-Length: 11
Content-Type: application/x-www-form-urlencoded

BLA BLA BLA< HTTP/1.0 200 OK
< Date: Wed Mar 1 21:32:29 2006
< Server: GoAhead-Webs
< Last-modified: Sat Feb 25 13:59:46 2006
< Content-length: 359
< Content-type: text/xml*

Some more info for your perusal:
 - I am runing this on bug-endian machine ( PPC arch). Wonring is this
is causing problem.
 - Server is on little endian machine
 - I tried tracing transfer.c. The flow tells me, it never writes data
to write callback using
   Curl_client_write(data, CLIENTWRITE_BODY, k->str, nread);
   when I tried putting this call in ( REAL HACK ) transfer.c at line
no. 1211 ( Approx) output of binray show me data as well as below,
*
< HTTP/1.0 200 OK
< Date: Wed Mar 1 21:24:21 2006
< Server: GoAhead-Webs
< Last-modified: Sat Feb 25 13:59:46 2006
< Content-length: 359
< Content-type: text/xml
Received 361 bytes*

Thanks,
Nilesh

Daniel Stenberg wrote:

> On Wed, 1 Mar 2006, Nilesh wrote:
>
>> Some how from there data is not getting passed to my write_callback.
>> Only goes to header_callback.
>
>
> See the attached example code. It makes a POST and fetches the
> response using the multi interface and gets all data sent to its write
> callback.
>
> This works for you, doesn't it?
>
>------------------------------------------------------------------------
>
>/*****************************************************************************
> * _ _ ____ _
> * Project ___| | | | _ \| |
> * / __| | | | |_) | |
> * | (__| |_| | _ <| |___
> * \___|\___/|_| \_\_____|
> *
> * $Id: multi-single.c,v 1.5 2004/05/24 15:16:29 bagder Exp $
> *
> * This is a very simple example using the multi interface, receiving the
> * downloaded data in a callback.
> */
>
>#include <stdio.h>
>#include <string.h>
>
>/* somewhat unix-specific */
>#include <sys/time.h>
>#include <unistd.h>
>
>/* curl stuff */
>#include <curl/curl.h>
>
>static size_t
>write_callback(void *ptr, size_t size, size_t nmemb, void *data)
>{
> size_t realsize = size * nmemb;
> (void)data; /* we don't use this */
>
> fprintf(stderr, "Received %ld bytes\n", (long)realsize);
>
> return realsize;
>}
>
>
>/*
> * Simply download a HTTP file.
> */
>int main(int argc, char **argv)
>{
> CURL *http_handle;
> CURLM *multi_handle;
>
> int still_running; /* keep number of running handles */
>
> http_handle = curl_easy_init();
>
> /* set the options (I left out a few, you'll get the point anyway) */
> curl_easy_setopt(http_handle, CURLOPT_URL, "http://daniel.haxx.se/");
>
> /* set what to POST */
> curl_easy_setopt(http_handle, CURLOPT_POSTFIELDS, "BLA BLA BLA");
>
> /* enable verbose output */
> curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1);
>
> /* send all data to this function */
> curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, write_callback);
>
> /* init a multi stack */
> multi_handle = curl_multi_init();
>
> /* add the individual transfers */
> curl_multi_add_handle(multi_handle, http_handle);
>
> /* we start some action by calling perform right away */
> while(CURLM_CALL_MULTI_PERFORM ==
> curl_multi_perform(multi_handle, &still_running));
>
> while(still_running) {
> struct timeval timeout;
> int rc; /* select() return code */
>
> fd_set fdread;
> fd_set fdwrite;
> fd_set fdexcep;
> int maxfd;
>
> FD_ZERO(&fdread);
> FD_ZERO(&fdwrite);
> FD_ZERO(&fdexcep);
>
> /* set a suitable timeout to play around with */
> timeout.tv_sec = 1;
> timeout.tv_usec = 0;
>
> /* get file descriptors from the transfers */
> curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
>
> rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
>
> switch(rc) {
> case -1:
> /* select error */
> still_running = 0;
> printf("select() returns error, this is badness\n");
> break;
> case 0:
> default:
> /* timeout or readable/writable sockets */
> while(CURLM_CALL_MULTI_PERFORM ==
> curl_multi_perform(multi_handle, &still_running));
> break;
> }
> }
>
> curl_multi_cleanup(multi_handle);
>
> curl_easy_cleanup(http_handle);
>
> return 0;
>}
>
>
Received on 2006-03-01