curl-library
Re: Libcurl newbie question
Date: Thu, 06 Apr 2006 10:49:00 -0700
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
>
>
Received on 2006-04-06