nitroshare / qhttpengine

HTTP server for Qt applications
MIT License
159 stars 76 forks source link

Socket is not closed at the end of serving a file request #8

Closed dov closed 7 years ago

dov commented 8 years ago

When the QHttpServer is done serving a file it leaves the socket open. This causes some clients to wait infinitely for the more data, and not realize that the entire file contents has been transfered.

Note, that in contrast, on error, the socket is closed as expected.

The following example shows the problem:

.1. Create a root directory, populate it, and start the http server:

cd examples/fileserver
mkdir http_root
echo aaaa > http_root/a.txt
env LD_LIBRARY_PATH=../../src ./fileserver -p 8899 -d http_root 

.2. Connect to the server by telnet and first trigger an error. Note that the connection is closed at the end of the error message.

> telnet localhost 8899
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /foo.txt HTTP/1.1

HTTP/1.0 404 NOT FOUND
Content-Length: 366
Content-Type: text/html

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" \
content="width=device-width, initial-scale=1.0"><title>404 NOT FOUND</title></head><body>\
<h1>404 NOT FOUND</h1><p>An error has occurred while trying to display the requested \
resource. Please contact the website owner if this error persists.</p><hr><p><em>QHttpEngine \
0.1.0</em></p></body></html>\
Connection closed by foreign host.

.3. Request a file. The connection is never closed.

> telnet localhost 8899
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /a.txt HTTP/1.1

HTTP/1.0 200 OK
Content-Length: 5
Content-Type: text/plain

aaaa
^]               ←   Done manually!
telnet> quit
Connection closed.
nathan-osman commented 8 years ago

Good catch. I've confirmed that this is happening and I'll take a look at it.

nathan-osman commented 7 years ago

This has now been fixed.