cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Problem with attachment based multiple form!

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Fri, 13 Dec 2002 09:24:18 +0100 (MET)

On Thu, 12 Dec 2002, Sanjeev wrote:

> Next, after going through docs and your today's mail, i suspect that, the
> html code i sent might be *CONFUSING* cURL.

HTML code doesn't confuse curl, because curl has no idea of what HTML is and
it never even tries to understand it.

> Yesterday's Problem::
> ------------------------------------------------------------------------------

> There is a libcurl.so.2 (131KB file) in the /usr/lib ( which might've come
> with the Mandrake distrubtion ) and now the current libcurl package is
> defaulted to /usr/local/lib ( curl-7.10.3-pre1's libcurl.so.2 is 580KB ).
> while running yesterday's program, it is running with 131KB file ( from gdb
> ). And hence the problem.

You can run 'ldd [program]' to get a perfect view of which libs your
particular program is using.

> Since we are checking for libcurl version with
>
> #if LIBCURL_VERSION_NUM < 0x070900
>
> which i guess might be taken from <curl/curl.h>

That's true. But if the Mandrake distribution comes with a libcurl in
/usr/lib/ it is also very likely that they have the curl/curl.h file in
the /usr/include tree...

> Since the core is in the libcurl.so, cant we check for something else from
> that libcurl.so file to check the version number, along with
> LIBCURL_VERSION_NUM from curl/curl.h. I'm saying this because there might
> be some people, who may face this problem, ( or ) my program might not work
> on other machine, with similiar problem. It will be possible for us to warn
> them.

Check for what? The LIBCURL_VERSION_NUM check is there to make sure that you
don't try to compile a program with a libcurl version that is older than 7.9.

You need to make sure that you don't have multiple versions of the library
and its associated headers in your system. Or if you have multiple versions
and want to keep on doing that, you must make sure that you setup the correct
paths etc for your tools.

> >> curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
> >> curl_easy_setopt(curl, CURLOPT_URL,
> >> "http://www.yyy.com/qqq/formspage.phtml");
> >> curl_easy_setopt(curl, CURLOPT_FILE, file);
> >> curl_easy_setopt(curl, CURLOPT_COOKIEFILE,
> mfile);
> >> curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
> >> "uid=san&pass=san");
> >> res = curl_easy_perform(curl);
>
> >This does not save any received cookies. If I were
> you, I'd consider doing
> >that.
>
> What else should I do for this Sir!

RTFM? ;-) I'd recommend you to go with CURLOPT_COOKIEJAR:

   CURLOPT_COOKIEJAR
         Pass a file name as char *, zero terminated. This will
         make libcurl write all internally known cookies to the
         specified file when curl_easy_cleanup(3) is called. If
         no cookies are known, no file will be created. Specify
         "-" to instead have the cookies written to stdout. Using
         this option also enables cookies for this session, so if
         you for example follow a location it will make matching
         cookies get sent accordingly. (Added in 7.9)

But since you seem to have your program up and running without cookies
there's no need to add any.

> May be by saying: "You cannot supply both CURLOPT_HTTPPOST and
> CURLOPT_POSTFIELDS to the same request" this you might have meant the same
> *REQUEST* i.e, only one curl_easy_perform() for both of them together!

That's exactly what I meant, yes.

> Though I'm comfortable with this, If you can please suggest me, if I can
> reduce the number of submissions from 4.

Sorry, that is beyond the scope of what I do here.

> Now that the attached code is working fine for me, i have few questions:
>
> 1) By the following Code::
>
> curl_formadd(&post, &last, CURLFORM_COPYNAME,
> "pictures",
> CURLFORM_FILE, "my-face.jpg",
> CURLFORM_FILE, "your-face.jpg",
> CURLFORM_END);
>
> Did you mean that the form had two input boxes for file input ( browse file
> ) for the same name=pictures if that is right. I dont have any further
> question regd. this.

It isn't right. You don't have two input boxes using the same field name
since that would confuse the receiver. (Alas, you *can* but you just don't.)

> But if it meant that i can submit two files with only one input box (
> practically impossible with a regular browser ), then it is not working for
> me. I can only submit one file.

You mean you can only submit one file or you mean that the receiving end can
only receive one file? It's quite a big difference.

> Is ther any alternative to submit two or more files, with one input box as
> said above as per my html page??

curl is not a magic wand. Can you do multiple file uploads with your browser?
If yes, then yes, curl should be able to do it. If no, then I think you'll
have a hard time to do it with curl. Unless you know how the other end works
or can figure it out.

> 2) While Coding I dont have any idea of how many Attachments a request is
> going to submit. So i took a way out to do this:
>
> for(i = 0; i < numatt; i++) {
>
> curl_easy_setopt(curl, CURLOPT_HTTPPOST,
> post);
> res = curl_easy_perform(curl);
> post->contents = myArray[i];
> }
>
>
> where 'numatt' is the number of attachments,
> myArray[] holds the individaul attachment
> paths.
> post is descendant of HttpPost struct;
>
> And this is working quite well for me. But 2 exceptions:
>
> a) the page is being submitted those many times there are attachments (
> for which, may be we dont have any other alternative! )

And what exactly is not working?

> b) curl_formfree(post) is segfaulting for me. may be
> because i'm manually changing the contents of post->contents.

Yes, that's why. I'd suggest that you:

1) restore the original contents in the struct before formfree()ing.

 or

2) create your own chain of post-structs and do whatever you want with it!
  (that would mean not use curl_formadd()).

-- 
 Daniel Stenberg -- curl, cURL, Curl, CURL. Groks URLs.
-------------------------------------------------------
This sf.net email is sponsored by:
With Great Power, Comes Great Responsibility 
Learn to use your power at OSDN's High Performance Computing Channel
http://hpc.devchannel.org/
Received on 2002-12-13