Skip to content

serveStaticFile (espfs) hangs if filesize multiple of 1024 #93

@phatpaul

Description

@phatpaul

Seems I got unlucky and stumbled on this bug. I guess my minimized and gzip compressed javascript is an exact multiple of the chunk size defined in serveStaticFile() = 1024.

My browser hangs forever trying to read that file. Wireshark shows that the file is indeed sent, but there is no \r\n at the end and the TCP connection is never closed (I'm using header: Connection: close, so I expect the connection closed after each file).

https://github.com/chmorgan/libesphttpd/blob/master/core/httpd-espfs.c#L202
(this code is mostly unchanged from the original author @Spritetm)

	len=espFsRead(file, buff, FILE_CHUNK_LEN);
	if (len>0) httpdSend(connData, buff, len);
	if (len!=FILE_CHUNK_LEN) {
		//We're done.
		espFsClose(file);
		return HTTPD_CGI_DONE;
	} else {
		//Ok, till next time.
		return HTTPD_CGI_MORE;
	}

It seems if the file is an exact multiple of FILE_CHUNK_LEN = 1024, then the final call will be with len==0. That should close the file and return HTTPD_CGI_DONE, but without sending any data.
It seems like that should work, but maybe httpd doesn't handle that case.

I hacked it for now by sending a dummy line-break if len==0 and it is now working for me:

	len=espFsRead(file, buff, FILE_CHUNK_LEN);
	if (len>0) httpdSend(connData, buff, len);
	// workaround if file length was exact multiple of FILE_CHUNK_LEN then we should send some dummy data in the last chunk
	if (len == 0){
		httpdSend(connData,"\r\n",2);
	}
	if (len!=FILE_CHUNK_LEN) {
		//We're done.
		espFsClose(file);
		return HTTPD_CGI_DONE;
	} else {
		//Ok, till next time.
		return HTTPD_CGI_MORE;
	}

But I think it needs a proper fix in httpd.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions