cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: request for some simple HTTP response parser applications

From: Frank Mcgeough <fmcgeough_at_mac.com>
Date: Mon, 02 Aug 2010 18:34:03 -0400

> On Mon, Aug 2, 2010 at 1:27 AM, Colin Hogben <curl_at_pythontech.co.uk>
> wrote:
> Daniel Stenberg wrote:
>
> http://curl.haxx.se/libcurl/c/example.html
>
> Examples are a good way for people to get a feel for how the library
> works. However, even if you know there are code examples available,
> IMO it's quite hard to find them on the website. Currently there
> are links from "C API" and "Features". I think it would be good if
> they were also referenced from the "Using libcurl" page, the
> Tutorial and, crucially, in the navigation panel at the top "libcurl
> index" level.
>
> --
> Colin Hogben
>
> On Aug 2, 2010, at 2:09 PM, Murat Sezgin wrote:
>
>> Thanks for this info. I want to use libcurl to parse a HTTP buffer.
>> I couldn't find an example for this purpose on this example page.
>> Is it possible to do this with libcurl? I will pass a HTTP buffer
>> or stream (most probably it will be a char * pointer) and it will
>> parse the header of this buffer/stream.
>>
>> Regards,
>> Murat
>>
>>
>
Libcurl makes this pretty simple (assuming I'm understanding what
you're asking). You can use CURLOPT_HEADERFUNCTION and
CURLOPT_WRITEHEADER options with curl_easy_setopt to get callbacks for
each header received back from your HTTP request.

Assuming you are writing in C++, for example, you could define a class
HttpHeaders

class HttpHeaders {
public:
      void addHeader(const string& s);
};

and then setup a static function for the callback :

size_t CurlCallback::rcvHeaders(void *buffer, size_t size, size_t
nmemb, void *userp) {
     char *d = (char*)buffer;
     CHttpHeaders *pHeaders = (HttpHeaders *)(userp);

     int result = 0;
     if (pHeaders != NULL) {
         std::string s = "";
         s.append(d, size * nmemb);
         pHeaders->addHeader(s);
         result = size * nmemb;
     }
     return result;
}

then just ensure you set the options properly before executing the
request :

curl_easy_setopt(curlHandle, CURLOPT_HEADERFUNCTION,
&CurlCallback::rcvHeaders); // our static function
curl_easy_setopt(curlHandle, CURLOPT_WRITEHEADER, &headers); //
"headers" is a member variable referencing HttpHeaders

parsing the individual string in a header is dependent upon what
language you are using and what headers you expect back. Does this
help?

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-08-03