[Libwebsockets] Error When Serving Large Files via HTTP

Andy Green andy at warmcat.com
Tue Dec 16 03:57:05 CET 2014



On 16 December 2014 10:18:54 GMT+08:00, Chad Cravens <chad.cravens at ossys.com> wrote:
>When attempting to serve large files (~ >500K) using the
>libwebsockets_serve_http_file() method, most of the time the call will
>hang
>causing chrome to issue a ERR_CONTENT_LENGTH_MISMATCH error.
>
>This happens most of the time, but about every 8-10 browser refreshes
>the
>entire file is served without a problem. I noticed that this same error
>occurs in the example application libwebsockets-test-server that is
>built
>when compiling libwebsockets from source on my MacOSX.
>
>The following is the code I am using to serve the file:
>
>Log::instance()->logDebug("SERVING HTTP FILE: " + resource);
>ret = libwebsockets_serve_http_file(context, wsi, resource.c_str(),
>mime.c_str(), "");
>Log::instance()->logDebug("SERVED HTTP FILE: " + resource);
>
>I also put debug statements for every callback made. I noticed that
>when
>properly serving large files the debug output looks like the following:
>
>[LOG_DEBUG] SERVING HTTP FILE: deps.js
>LWS_CALLBACK_HTTP_FILE_COMPLETION
>[LOG_DEBUG] SERVED HTTP FILE: deps.js
>RESULT OF HTTP: 1
>
>However, when there is an error serving the file as stated above, the
>callback output looks like the following:
>[LOG_DEBUG] SERVING HTTP FILE: deps.js
>LWS_CALLBACK_LOCK_POLL
>LWS_CALLBACK_CHANGE_MODE_POLL_FD
>LWS_CALLBACK_UNLOCK_POLL
>LWS_CALLBACK_LOCK_POLL
>LWS_CALLBACK_CHANGE_MODE_POLL_FD
>LWS_CALLBACK_UNLOCK_POLL
>[LOG_DEBUG] SERVED HTTP FILE: deps.js
>LWS_CALLBACK_LOCK_POLL
>LWS_CALLBACK_CHANGE_MODE_POLL_FD
>LWS_CALLBACK_UNLOCK_POLL
>RESULT OF HTTP: 0
>
>When there is an error there additional callbacks that are called an
>apparently I am not handling the buffering properly. For a while I have
>been trying to work the issue and figure out how to properly handle
>buffering and serving large files via HTTP.
>
>Any guidance in my approach or examples / documentation would be
>GREATLY
>appreciated!!

I added leaf.jpg in the test server ("click here to get sent a large file" on the test server page) to provide a test case for exactly this.  It's like 2.5MB IIRC.

What happens with everything else the same but give Chrome leaf.jpg from the test server?

-Andy

>Kindest Regards,
>Chad




More information about the Libwebsockets mailing list