How to set response header to "Connection:keep-alive"

2,963 views
Skip to first unread message

璋璇徐

unread,
Jan 13, 2013, 7:55:52 PM1/13/13
to clojure-...@googlegroups.com
Hi, all
    I want to set the response header to "Connection:keep-alive". I've tried to return a map like "{:body xxx :header {"connection" "keep-alive"}}" in handler-ok. But it seems not work. Any suggestion?

璋璇徐

unread,
Jan 13, 2013, 8:41:34 PM1/13/13
to clojure-...@googlegroups.com
I mean I tried return map   "{:body xxx :headers {"connection" "keep-alive"}}" . Sorry for the typo.

在 2013年1月14日星期一UTC+8上午8时55分52秒,璋璇徐写道:

Malcolm Sparks

unread,
Jan 14, 2013, 5:02:18 AM1/14/13
to clojure-...@googlegroups.com
Hi,

You are right. I have tested and get the same behaviour. However, if I change 'connection' to 'connection2', it works as expected. But if I change back to 'connection', the header disappears as you point out.

There are times when components in the stack below Liberator will modify headers - I have noticed that Compojure, for example, will add a charset declaration to the Content-Type header if it is not already set. Obviously Liberator cannot do anything about that. I have run 'git grep' on both Compojure and Ring, looking for anything that might refuse to propagate the Connection header, but found nothing. Which web server are you using? (eg. jetty, aleph, tomcat...) - I've tested with Jetty and reproduced this behaviour so I expect it may be doing something to quash the Connection header, but I am not currently able to download its source code to check. 

I suspect the reason for Jetty behaving in this way is because it is not correct to set this header in HTTP 1.1 - Connection: keep-alive was something in HTTP 1.0 which is deprecated in HTTP 1.1 (I believe). What are you trying to achieve (I say this only to be able to help you further, not to question your motives :)

Regards,

Malcolm

Malcolm Sparks

unread,
Jan 14, 2013, 5:04:36 AM1/14/13
to clojure-...@googlegroups.com
From http://en.wikipedia.org/wiki/HTTP_persistent_connection

HTTP 1.0

Under HTTP 1.0, there is no official specification for how keepalive operates. It was, in essence, added to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request:

Connection: Keep-Alive

Then, when the server receives this request and generates a response, it also adds a header to the response:

Connection: Keep-Alive

Following this, the connection is not dropped, but is instead kept open. When the client sends another request, it uses the same connection. This will continue until either the client or the server decides that the conversation is over, and one of them drops the connection.

[edit]HTTP 1.1

In HTTP 1.1, all connections are considered persistent unless declared otherwise.[1]

Philipp Meier

unread,
Jan 14, 2013, 4:37:46 PM1/14/13
to clojure-...@googlegroups.com
Hi,


Am Montag, 14. Januar 2013 11:02:18 UTC+1 schrieb Malcolm Sparks:
I suspect the reason for Jetty behaving in this way is because it is not correct to set this header in HTTP 1.1 - Connection: keep-alive was something in HTTP 1.0 which is deprecated in HTTP 1.1 (I believe). What are you trying to achieve (I say this only to be able to help you further, not to question your motives :) 

Connection keep-alive must be done by the actual http server implementation. This is technically outside of liberator's or even ring's scope. Jetty when used with run-jetty offers keep-alive over http 1.0 as expected and holds the connection open on 1.1:

~ ⮀ curl -v http://localhost:3000/ 
* About to connect() to localhost port 3000 (#0)
*   Trying ::1...
* connected
* Connected to localhost (::1) port 3000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:3000
> Accept: */*
< HTTP/1.1 200 OK
< Date: Mon, 14 Jan 2013 21:35:44 GMT
< Content-Type: text/html;charset=UTF-8
< Content-Length: 2
< Server: Jetty(7.6.1.v20120215)
* Connection #0 to host localhost left intact
OK* Closing connection #0

And for HTTP 1.0:

~ ⮀ curl -0v -H 'Connection: keep-alive' http://localhost:3000/
* About to connect() to localhost port 3000 (#0)
*   Trying ::1...
* connected
* Connected to localhost (::1) port 3000 (#0)
> GET / HTTP/1.0
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:3000
> Accept: */*
> Connection: keep-alive
< HTTP/1.1 200 OK
< Date: Mon, 14 Jan 2013 21:36:50 GMT
< Content-Type: text/html;charset=UTF-8
< Content-Length: 2
< Connection: keep-alive
< Server: Jetty(7.6.1.v20120215)
* Connection #0 to host localhost left intact
OK* Closing connection #0 

Malcolm's research on the HTTP spec was correct and jetty works as expected here.

-billy.
Reply all
Reply to author
Forward
0 new messages