curl-library
Re: Using curl_eay_getinfo when msg->data.result == CURLE_OK
Date: Thu, 08 Jul 2004 18:02:11 +0100
Hi Jeff
Jeff Pohlmeyer wrote:
>>what is actually happening, is that when the curl message 'cm' has a
>>message CURLMSG_DONE it tries to obtain what type of file has just been
>>downloaded. it's not actually returning anything other than NULL from
>>this curl_easy_getinfo() function. I'm wondering why not and the value
>>I get it always unknown/binary.
>>
>>
>
>
>Libcurl tries to obtain this info from the content-type header, but not all
>servers provide this information. If there is no content-type header, then
>libcurl just returns NULL for CURLINFO_CONTENT_TYPE.
>
>Here is a quick-and dirty example of using libmagic to get what you need.
>( If you have "file --version" >= 4.0 then you should have libmagic. )
>
>Be sure to pass -lmagic to the linker...
>
>[---------------]
>
>#include <stdio.h>
>#include <magic.h>
>#include <curl/curl.h>
>
>
>size_t write_cb (char *buffer, size_t size, size_t nitems, void *outstream)
>{
> const char* mimetype = magic_buffer((magic_t) outstream, buffer, size*nitems);
> if (mimetype) {
> printf("%s\n", mimetype);
> } else {
> fprintf(stderr, "Could not determine mime type\n");
> }
> /*
> For a "real" program, you would want to process the
> char *buffer, and return (size*nitems) to continue.
> Here I am just bailing out on the first chunk...
> */
> return 0;
>}
>
>
>int main(int argc, char**argv)
>{
> CURL *curl;
> CURLcode rv;
> magic_t magic;
> char err[CURL_ERROR_SIZE];
>
> if ( 2 == argc ) {
> curl = curl_easy_init();
> if ( curl ) {
> curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, err);
> curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
> curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
> curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
> magic = magic_open(MAGIC_MIME);
> magic_load(magic, NULL);
> /*
> Here I am passing the magic_t pointer straight into
> the write callback. You would probably want to make
> it a struct field or global for a "real" application.
> */
> curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) magic);
> rv = curl_easy_perform(curl);
> switch (rv) {
> case CURLE_OK:
> fprintf(stderr, "No Data!\n");
> return 1;
> case CURLE_WRITE_ERROR:
> return 0;
> default:
> fprintf(stderr, "%s\n", err);
> return rv;
> }
> } else {
> fprintf(stderr, "Failed to initialize libcurl.\n");
> return 1;
> }
> } else {
> fprintf(stderr, "Usage: %s <url>\n", argv[0]);
> return 0;
> }
>}
>
>[---------------]
>
>As another option, I think it's also possible
>to do something similar with GnomeVFS
>
>
> - Jeff
>
>
Thanks for the help mate, the reason I was more interested in using
libCURL to do this than some other library was because I plan to port to
win32 soonish and I dont want to be limited into using linux only
libraries. Do you know a more x-platform method of doing this kind of
thing which would work in all cases? libmagic sounds great, I'm gonna
see if there is a win32 version, of course, if this is the case, it
would be fantastic!
ta ra
Chris Thomas
Received on 2004-07-08