Skip to content

Commit

Permalink
upgrade: detaching mechanism reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
filonenko-mikhail committed Sep 3, 2018
1 parent 497f858 commit 33aac0b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 43 deletions.
1 change: 1 addition & 0 deletions http/codes.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
return {
[101] = 'Switching Protocols',
[200] = 'Ok',
[201] = 'Created',
[202] = 'Accepted',
Expand Down
15 changes: 8 additions & 7 deletions http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand All @@ -1151,6 +1150,8 @@ local function httpd_start(self)
end

local exports = {
DETACHED = DETACHED,

new = function(host, port, options)
if options == nil then
options = {}
Expand Down
37 changes: 1 addition & 36 deletions test/http.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 33aac0b

Please sign in to comment.