Re: sending HTTP requests... newbie (continued)
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Mon, 22 Oct 2007 19:53:09 -0500
TreatmentPlant wrote:
After many more hours of fiddling to get the right files into the right places, find the missing .dll files, rediscovering that a lot of file are missing from the different versions of the download libraries, rebuilding .lib files etc.... I finally have a "working product" (of sorts)
My next problem...
I can send my data to the server and I get the response back, but the response comes back as a CURLcode datatype. I want to do some more processing on the server repsonse, so am wondering how can I get the response cast(?) as a char array or string? or copied into another datatype variable?
Reading through the curl help files they mumble something about writing the results to a FILE* but I just need to so some text processing, parsing, tokenising etc on the results, I don't want to have to write to a file, then read from the file as I think this double handling unnecessary.
It looks like from the documentation that receiving the data can be
handled by a callback function. That is, you want to define your own
function, then after curl_easy_init() but before curl_easy_perform(),
you want to call curl_easy_setopt() with the CURLOPT_WRITEFUNCTION
constant and the pointer to your callback function. Then when you call
curl_easy_perform(), that will cause your callback function to get run
however many times is necessary to get all the data transferred to you.
You probably also want to call curl_easy_setopt() with the CURLOPT_WRITEDATA
to define the void *stream value that will be passed to your callback.
Basically, libcurl is very flexible and lets you do whatever you want to
with the data, and it does this by providing an interface where it can
call a function and say "here's the next piece of data".
Here's some partial code for an implementation that might write it to
an array and resize that as necessary:
typedef struct CurlBuffer
{
size_t allocated;
size_t used;
char* contents;
} CurlBuffer;
size_t curlReallocWriterCallback(
void *ptr, size_t size, size_t nmemb, void *stream)
{
CurlBuffer *buffer = (CurlBuffer) stream;
size_t bytesIncoming = size * nmemb;
size_t bufferSizeNeeded = buffer->used + bytesIncoming;
if (bufferSizeNeeded > buffer->allocated)
{
void* newContents = realloc(buffer->contents, bufferSizeNeeded);
if (newContents == NULL) { return 0; }
buffer->contents = newContents;
buffer->allocated = bufferSizeNeeded;
}
memmove(buffer->contents + buffer->used, ptr, bytesIncoming);
buffer->used += bytesIncoming;
}
void foo()
{
CURL* handle = curl_easy_init();
CurlBuffer buffer;
buffer.allocated = 0;
buffer.used = 0;
buffer.contents = NULL;
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, curlReallocWriterCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, & buffer);
curl_easy_setopt(...... whatever else ......);
CURLcode curlCode = curl_easy_perform_handle();
curl_easy_cleanup(handle);
free(buffer.contents);
}
This code won't be efficient, and the error-handling is omitted, but
hopefully it gets across the general idea. You could also, instead of
buffering everything up front, parse it as it comes across in the stream.
That is, you could make the callback function call your parsing code.
- Logan
.
- Follow-Ups:
- Re: sending HTTP requests... newbie (continued)
- From: TreatmentPlant
- Re: sending HTTP requests... newbie (continued)
- References:
- Re: sending HTTP requests... newbie (continued)
- From: TreatmentPlant
- Re: sending HTTP requests... newbie (continued)
- From: TreatmentPlant
- Re: sending HTTP requests... newbie (continued)
- Prev by Date: Re: Converting interleaved data to planar?
- Next by Date: Re: sending HTTP requests... newbie (continued)
- Previous by thread: Re: sending HTTP requests... newbie (continued)
- Next by thread: Re: sending HTTP requests... newbie (continued)
- Index(es):
Relevant Pages
|