cURL / Mailing Lists / curl-library / Single Mail

curl-library

http status code early?

From: David Boyce <dsb_at_world.std.com>
Date: Thu, 03 Apr 2003 12:42:28 -0500

I have a Unix command-line tool based on libcurl. The program itself is
just a thin wrapper which makes an http request to a servlet and pumps the
response out to stdout. There's no need for the user to know (not that it's
a secret either) that it's an http based tool; they just run commands and
get results, in text/plain format, on stdout. This works nicely.

There's just one minor problem. A well behaved command-line tool sends data
to stdout and error messages to stderr. But HTTP doesn't have two streams,
just the unitary response body. So as currently configured all output,
including errors, is streamed to stdout. I don't use any callbacks
(CURLOPT_WRITEFUNCTION), I just let the response flow to stdout.

I'm looking for a way to divert error output to stderr. The obvious
solution is for the server to set the response code to something
representing an error (presumably in the 4xx range) and set the exit status
and output stream of the client based on that. I've set this up and it
works great for the exit status, because of course I don't need that till
exit time. But the stdout/stderr determination is a problem because the
documentation for curl_easy_getinfo() says this:

>This function is intended to get used *AFTER* a performed transfer, all
>results from this function are undefined until the transfer is completed.

So I can't get the response code till after the data has flowed to stdout.
Of course I could set a callback and buffer all the output using malloc or
a temp file but that's a long ways from elegant, esp. since the output is
unbounded. I'm wondering if there's a cleaner way to handle this that I've
missed, and in particular why the response code (and all the header data)
isn't valid until after the entire response body is read given that the
headers must arrive frst.

Thanks,
David

-------------------------------------------------------
This SF.net email is sponsored by: ValueWeb:
Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
No other company gives more support or power for your dedicated server
http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
Received on 2003-04-03