Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
phatpaul opened this issue Oct 31, 2020 · 0 comments
Open

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

phatpaul opened this issue Oct 31, 2020 · 0 comments

Comments

@phatpaul
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant