diff --git a/http/server.lua b/http/server.lua index 3cf6f22..8e3f255 100644 --- a/http/server.lua +++ b/http/server.lua @@ -10,7 +10,6 @@ local codes = require('http.codes') local log = require('log') local socket = require('socket') -local fiber = require('fiber') local json = require('json') local errno = require 'errno' @@ -659,7 +658,7 @@ local function process_client(self, s, peer) break end end - + if is_eof then break end @@ -822,6 +821,10 @@ local function process_client(self, s, peer) end end + if reason and reason.detach == true then + return reason + end + if p.proto[1] ~= 1 then break end @@ -1128,8 +1131,14 @@ local function httpd_start(self) error("httpd: usage: httpd:start()") end - local server = socket.tcp_server(self.host, self.port, { name = 'http', - handler = function(...) process_client(self, ...) end }) + local server = socket.tcp_server(self.host, self.port, + { name = 'http', + handler = function(...) + local res = process_client(self, ...) + if res and res.detach == true then + res.detach_handler(self, ...) + end + end}) if server == nil then error(sprintf("Can't create tcp_server: %s", errno.strerror())) end diff --git a/test/http.test.lua b/test/http.test.lua index 4f57762..20e9cd1 100755 --- a/test/http.test.lua +++ b/test/http.test.lua @@ -10,7 +10,7 @@ local yaml = require 'yaml' local urilib = require('uri') local test = tap.test("http") -test:plan(7) +test:plan(9) test:test("split_uri", function(test) test:plan(65) local function check(uri, rhs) @@ -370,4 +370,39 @@ test:test("server requests", function(test) httpd:stop() end) +local function cfgservtwo() + local detached = function(httpd, s, peer) + test:test('Detached hook is called', function(test) + test:plan(1) + test:ok(true, 'hook called') + end) + end + local path = os.getenv('LUA_SOURCE_DIR') or './' + path = fio.pathjoin(path, 'test') + local httpd = http_server.new('127.0.0.1', 12346, { app_dir = path, + log_requests = false, log_errors = false }) + :route({path = '/ws', name = 'test'}, + function() + return {status = 200, + body = 'ok', + detach = true, + detach_handler = detached, + } + end) + return httpd +end + +test:test("server requests", function(test) + test:plan(2) + + local httpd = cfgservtwo() + httpd:start() + + local r = http_client.get('http://127.0.0.1:12346/ws') + + test:is(r.status, 200, 'detached 200') + test:is(r.reason, 'Ok', 'detached reason') + end +) + os.exit(test:check() == true and 0 or 1)