[Libwebsockets] Connection failed with code 304

Andy Green andy at warmcat.com
Fri Oct 21 17:19:36 CEST 2016

On Fri, 2016-10-21 at 12:32 +0000, Peter Brett wrote:

> Don't worry, I'm not about to ask what a 304 error is.

Well, it's not supposed to be an 'error', but reading the whole thing I
see why that's what it is atm.

> I am deliberately causing this by injecting a "If-Modified-Since"
> header in my callback under

Right... lws itself doesn't add that header itself, so the client http
stuff has not had to deal with 304 until now.  It supports 3xx
redirects already transparently, but 304 is "Not Modified".

> about what happens next. I see the log line lwsl_notice("Connection
> failed with code %d", n); from lib/client.c (needs a '\n' in my
> opinion, but that's not important) after which I see a 


> LOCK_POLL, CHANGE_MODE_POLL_FD, UNLOCK_POLL sequence and then just a
> bunch of GET_THREAD_ID's, which were much faster than my lws_service
> interval.
> sequences, since I understand they are only used where an external
> thread is involved.

They're part of lws changing the POLLIN|POLLOUT wait for a wsi.  They
aren't significant in themselves and don't have to be handled unless
you have an external poll() wait you are integrating with; these pass
through the wait change for the wsi socket so you can shadow it in your
real pollfd array in the code that actually does the wait.

> I would have expected to see a CLIENT_CONNECTION_ERROR (perhaps
> that's for tcp connection errors?) or some other indication via my
> callback. How should I be detecting the 304 error - actually it looks
> like all HTTP error responses are treated the same looking at the
> code in client.c.

Well no it handles redirects

	if (n == 301 || n == 302 || n == 303 || n == 307 || n == 308) {
		p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_LOCATION);

And you should get a LWS_CALLBACK_CLIENT_CONNECTION_ERROR (at bail2: in
the code)... wsi->protocol should still be set to protocols[0] at that

> Also, I may be in a minority of 1 here, but it would be useful to me
> to be able to see the full HTTP response even in the case where the
> response code is not 200. Basically, I want to be able to poll a
> server, with 2 aims: to retrieve the latest version of a file when it
> changes and to synchronise to server time based on the Date header
> (my application is to run in a device with no RTC). I am seeing a
> expected when I do not inject the If-Modified-Since code.
> My code is based on test-client.c with few changes other than logging
> every reason in the callback, injecting the extra header as described
> above and in my main, I only ever call lws_client_connect_via_info
> once and then use a for loop to call lws_service.

Lws adds things because I or somebody else wanted them... this is a
small piece of new territory.

I pushed a little patch on master that legitimizes the 304 to be
treated like a 200 for the test client.

And a second patch which adds this

 * lws_http_client_http_response() - get last HTTP response code
 * \param wsi: client connection
 * Returns the last server response code, eg, 200 for client http
lws_http_client_http_response(struct lws *wsi);

You can also call it from LWS_CALLBACK_CLIENT_CONNECTION_ERROR but it's
first available at LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP if it's one of
200 or 304.

Does that help?


> Thanks,
> Peter
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list