# HG changeset patch # User Florian Zeitz # Date 1358008010 -3600 # Node ID fcb1be0b4a5c92de261f4283fb2d5604163c3b3e # Parent cc9d460aa779fa6cbbdb37f2b81b6c5f6c237a6c net.http.server: Properly handle persistent connections diff -r cc9d460aa779 -r fcb1be0b4a5c net/http/server.lua --- a/net/http/server.lua Sat Jan 12 16:55:39 2013 +0100 +++ b/net/http/server.lua Sat Jan 12 17:26:50 2013 +0100 @@ -156,12 +156,23 @@ local date_header = os_date('!%a, %d %b %Y %H:%M:%S GMT'); -- FIXME use local conn_header = request.headers.connection; - local keep_alive = conn_header == "Keep-Alive" or (request.httpversion == "1.1" and conn_header ~= "close"); + conn_header = conn_header and ","..conn_header:gsub("[ \t]", ""):lower().."," or "" + local httpversion = request.httpversion + local persistent = conn_header:find(",keep-alive,", 1, true) + or (httpversion == "1.1" and not conn_header:find(",close,", 1, true)); + + local response_conn_header; + if persistent then + response_conn_header = "Keep-Alive"; + else + response_conn_header = httpversion == "1.1" and "close" or nil + end local response = { request = request; status_code = 200; - headers = { date = date_header, connection = (keep_alive and "Keep-Alive" or "close") }; + headers = { date = date_header, connection = response_conn_header }; + persistent = persistent; conn = conn; send = _M.send_response; finish_cb = finish_cb; @@ -241,7 +252,7 @@ response:on_destroy(); response.on_destroy = nil; end - if headers.connection == "Keep-Alive" then + if response.persistent then response:finish_cb(); else response.conn:close();