curl-library
Re: Libcurl newbie question
Date: Thu, 6 Apr 2006 18:19:05 -0000
On Thu, Apr 6, 2006, Saikat Kanjilal <sxk1969_at_hotmail.com> said:
> getReponse will update a configuration file with a value obtained from the
> server, I guess I could just write to the file outside of the c++ code but I
> was hoping for a way to get a handle back to the class from the C code
>
you do not want to update the file from within the callback. the callback
could be called multiple times if libcurl decides to split the response up
into chunks. what you want is for the callback to store the data you need
into a buffer ((void)*data in my example). then after your call to
curl_perform finishes in your main loop, you play with the values in the
data buffer.
allan
>
>>From: <man_at_tfhs.net>
>>Reply-To: libcurl development <curl-library_at_cool.haxx.se>
>>To: "libcurl development" <curl-library_at_cool.haxx.se>
>>Subject: Re: Libcurl newbie question
>>Date: Thu, 6 Apr 2006 18:04:30 -0000
>>
>>i do not speak c++, cannot help you much, but i do not understand why you
>>want to call getResponse inside the callback. you did not tell us what
>>getResponse does.
>>
>>allan
>>
>>On Thu, Apr 6, 2006, Saikat Kanjilal <sxk1969_at_hotmail.com> said:
>>
>> > Alan:
>> > Thanks for responding, my method that retrieves the response is defined
>> > currently as an extern method, I need to take the contents of the
>>response
>> > and write this to a file. In order to do this I need access to the
>> > enclosing class, how do I get this access, listed below is the scenario:
>> >
>> > Header File:
>> > class A
>> > {
>> > public:
>> > bool getResponse();
>> > };
>> >
>> >
>> > Implementation
>> > bool A::getResponse(std::string response)
>> > {
>> > }
>> >
>> >
>> > extern "C" size_t httpWriteCallback(void *ptr, size_t size, size_t
>>nmemb,
>> > void *data)
>> > {
>> > int realsize = size * nmemb;
>> > struct resp_struct *resp = (struct resp_struct *)data;
>> >
>> > //how do I access the class method here and call
>> > //getResponse, do I have access to the this pointer here
>> > if(!(resp->used)){
>> > resp->used = (realsize < resp->len) ? realsize : resp->len;
>> > memcpy(&resp->bytes, ptr, resp->used);
>> > resp->bytes[(resp->used)-1]=0;
>> > }
>> >
>> >
>> > }
>> >
>> >
>> > Thanks Again
>> > SK
>> >
>> >
>> >
>> >
>> >
>> >>From: <man_at_tfhs.net>
>> >>Reply-To: libcurl development <curl-library_at_cool.haxx.se>
>> >>To: "libcurl development" <curl-library_at_cool.haxx.se>
>> >>Subject: Re: Libcurl newbie question
>> >>Date: Thu, 6 Apr 2006 15:53:19 -0000
>> >>
>> >>On Thu, Apr 6, 2006, Saikat Kanjilal <sxk1969_at_hotmail.com> said:
>> >>
>> >> > Hi All:
>> >> > I have a client side app that does a simple http multipart post and
>> >>sends a
>> >> > file upto a server. I was wondering what the options are in libcurl
>>to
>> >>read
>> >> > the response sent back by the server. The server is simply going to
>> >>send
>> >> > back a string, also some pros and cons of each option would be
>>immensely
>> >> > valuable to me.
>> >>
>> >>i have a program that does this exact thing, here is what i use:
>> >>
>> >>#define MAX_RESP_LEN 512
>> >>struct resp_struct {
>> >> int used;
>> >> int len;
>> >> char bytes[MAX_RESP_LEN];
>> >>};
>> >>
>> >>size_t
>> >>httpWriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
>> >>{
>> >> int realsize = size * nmemb;
>> >> struct resp_struct *resp = (struct resp_struct *)data;
>> >>
>> >> if(!(resp->used)){
>> >> resp->used = (realsize < resp->len) ? realsize : resp->len;
>> >> memcpy(&resp->bytes, ptr, resp->used);
>> >> resp->bytes[(resp->used)-1]=0;
>> >> }
>> >>
>> >> return realsize;
>> >>}
>> >>
>> >>in main()
>> >>
>> >> struct resp_struct resp = {0,MAX_RESP_LEN,{0}};
>> >>
>> >> if(curl_easy_setopt(mycurl, CURLOPT_WRITEFUNCTION,
>>httpWriteCallback)){
>> >> fprintf(stderr,"CHILD: Could not add write callback: ERROR\n");
>> >> fprintf(stderr,"CHILD: %s\n",(char*)&error_buffer);
>> >> return(-1);
>> >> }
>> >>
>> >> if(curl_easy_setopt(mycurl, CURLOPT_WRITEDATA, (void *)&resp)){
>> >> fprintf(stderr,"CHILD: Could not add write pointer: ERROR\n");
>> >> fprintf(stderr,"CHILD: %s\n",(char*)&error_buffer);
>> >> return(-1);
>> >> }
>> >>
>> >>libcurl will call the callback multiple times, if the http response
>> >>requires it. my code only buffers the first call to the callback, and
>>then
>> >>only the first 512 bytes of that call. after that it 'lies' to libcurl,
>> >>and says it did something with the data. that is enough for my use, as
>>my
>> >>handler on the server side sends back a two word text response, and i
>>have
>> >>never seen libcurl break that up over two calls to the callback. if that
>> >>ever happens, i am in trouble :)
>> >>
>> >>HTH
>> >>
>> >>allan
>> >>
>> >>--
>> >>m. allan noah
>> >>IT Director, TfHS.net
>> >>ph# (804) 355-5489
>> >>tf# (866) 724-9722
>> >>fx# (804) 355-0477
>> >>
>> >>
>> >
>> >
>> >
>>
>>--
>>m. allan noah
>>IT Director, TfHS.net
>>ph# (804) 355-5489
>>tf# (866) 724-9722
>>fx# (804) 355-0477
>>
>>
>
>
>
-- m. allan noah IT Director, TfHS.net ph# (804) 355-5489 tf# (866) 724-9722 fx# (804) 355-0477Received on 2006-04-06