cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: how to capture http response number?

From: Ray Satiro via curl-library <curl-library_at_cool.haxx.se>
Date: Tue, 17 May 2016 15:02:30 -0400

On 5/17/2016 10:11 AM, Kman Devnull wrote:
> Now, I see that if the server was slow to respond to a http-post, it
> appears that my whole code seems to wait on the call-back function
> "WriteMemoryCallback".
>
> My embedded system, speaks to various sensors - now because of the
> above delay, it affects the real-time processing/receiving of the
> sensor data. How would you propose for me to proceed?
>
> Is there a way I could make the receiving of server-response
> -parallel-(as in another thread etc.) to sending of the http_post?

Please don't top post it makes the conversation harder to follow. I've
omitted the rest of the conversation but I'll sum it up that you are
looking to get the server response status code to a POST.

You can't use the same handle from more than one thread [2], so you
wouldn't be able to perform a POST from one thread and then wait for the
server to reply from another. I suggest in your main thread you write to
a queue the data to be handled asynchronously in a worker thread using
libcurl. If you need to evaluate the status code in the main thread
before continuing you'll have to wait and there's very little libcurl
can do about that except wait.

Exception: It is possible (but not recommended) to terminate the
connection immediately after receiving the status code. This is bad
because usually you'd want to leave the connection open so you can keep
sending data. I would only suggest this if you are in a rare
circumstance where the server sends the status code immediately, but for
whatever reason does not send headers or body without some delay. In
that specific case you could try returning error as soon as the header
callback is called and then call CURLINFO_RESPONSE_CODE [3] to get the
code. If you allow location based redirects you'd have to account for
that in the header callback (ie don't terminate in that case). And if
you are using a proxy tunnel it's much harder because proxy tunnel
returns its own response code which IIRC is temporarily stored as the
http response code until a server response code is actually received.
See other tricks that may help at [4].

[1]: https://curl.haxx.se/mail/etiquette.html#Do_Not_Top_Post
[2]: https://curl.haxx.se/libcurl/c/threadsafe.html
[3]: https://curl.haxx.se/libcurl/c/CURLINFO_RESPONSE_CODE.html
[4]: https://curl.haxx.se/mail/lib-2016-04/0043.html

-------------------------------------------------------------------
List admin: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2016-05-17