From 33aac0b27caf6ee6f6599d49876245616a28081b Mon Sep 17 00:00:00 2001 From: Michael Filonenko Date: Mon, 3 Sep 2018 15:14:59 +0300 Subject: [PATCH] upgrade: detaching mechanism reworked --- http/codes.lua | 1 + http/server.lua | 15 ++++++++------- test/http.test.lua | 37 +------------------------------------ 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/http/codes.lua b/http/codes.lua index 68b6e57..18bd9dd 100644 --- a/http/codes.lua +++ b/http/codes.lua @@ -1,4 +1,5 @@ return { + [101] = 'Switching Protocols', [200] = 'Ok', [201] = 'Created', [202] = 'Accepted', diff --git a/http/server.lua b/http/server.lua index 8e3f255..f8d7866 100644 --- a/http/server.lua +++ b/http/server.lua @@ -13,6 +13,8 @@ local socket = require('socket') local json = require('json') local errno = require 'errno' +local DETACHED = 101 + local function errorf(fmt, ...) error(string.format(fmt, ...)) end @@ -722,6 +724,10 @@ local function process_client(self, s, peer) elseif reason == nil then status = 200 hdrs = {} + elseif type(reason) == 'number' then + if reason == DETACHED then + break + end else error('invalid response') end @@ -821,10 +827,6 @@ 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 @@ -1135,9 +1137,6 @@ local function httpd_start(self) { 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())) @@ -1151,6 +1150,8 @@ local function httpd_start(self) end local exports = { + DETACHED = DETACHED, + new = function(host, port, options) if options == nil then options = {} diff --git a/test/http.test.lua b/test/http.test.lua index 20e9cd1..4f57762 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(9) +test:plan(7) test:test("split_uri", function(test) test:plan(65) local function check(uri, rhs) @@ -370,39 +370,4 @@ 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)