nicolasff / webdis

A Redis HTTP interface with JSON output
https://webd.is
BSD 2-Clause "Simplified" License
2.82k stars 307 forks source link

Handle Connection:close header #118

Closed boothj5 closed 9 years ago

boothj5 commented 9 years ago

Currently if the Connection: close header is sent, the response includes a Connection: Keep-Alive.

$ curl -v http://127.0.0.1:7379/GET/hello --header "Connection: close"
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 7379 (#0)
> GET /GET/hello HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 127.0.0.1:7379
> Accept: */*
> Connection: close
> 
< HTTP/1.1 200 OK
* Server Webdis is not blacklisted
< Server: Webdis
< Allow: GET,POST,PUT,OPTIONS
< Access-Control-Allow-Methods: GET,POST,PUT,OPTIONS
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization
< Content-Type: application/json
< ETag: "8cf38afc245b7a6a88696566483d1390"
< Connection: Keep-Alive
< Content-Length: 15
< 
* Connection #0 to host 127.0.0.1 left intact
{"GET":"world"}

If another request is made using the same connection a 400 Bad Request is returned.

With this patch, a Connection: close header is included in the response instead, so the client will know to close it and use a new connection.

$ curl -v http://127.0.0.1:7379/GET/hello --header "Connection: close"
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 7379 (#0)
> GET /GET/hello HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 127.0.0.1:7379
> Accept: */*
> Connection: close
> 
< HTTP/1.1 200 OK
* Server Webdis is not blacklisted
< Server: Webdis
< Allow: GET,POST,PUT,OPTIONS
< Access-Control-Allow-Methods: GET,POST,PUT,OPTIONS
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization
< Content-Type: application/json
< ETag: "8cf38afc245b7a6a88696566483d1390"
< Connection: Close
< Content-Length: 15
< 
* Closing connection 0
{"GET":"world"}

The socket is also closed so the client should get a socket read error if it was to attempt another request.

I'm not familiar with the code base, so there might be a better solution, but this works with curl, and my own http client library.

nicolasff commented 9 years ago

Thanks for the patch!