Skip to content

Commit 33aac0b

Browse files
upgrade: detaching mechanism reworked
1 parent 497f858 commit 33aac0b

File tree

3 files changed

+10
-43
lines changed

3 files changed

+10
-43
lines changed

http/codes.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
return {
2+
[101] = 'Switching Protocols',
23
[200] = 'Ok',
34
[201] = 'Created',
45
[202] = 'Accepted',

http/server.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ local socket = require('socket')
1313
local json = require('json')
1414
local errno = require 'errno'
1515

16+
local DETACHED = 101
17+
1618
local function errorf(fmt, ...)
1719
error(string.format(fmt, ...))
1820
end
@@ -722,6 +724,10 @@ local function process_client(self, s, peer)
722724
elseif reason == nil then
723725
status = 200
724726
hdrs = {}
727+
elseif type(reason) == 'number' then
728+
if reason == DETACHED then
729+
break
730+
end
725731
else
726732
error('invalid response')
727733
end
@@ -821,10 +827,6 @@ local function process_client(self, s, peer)
821827
end
822828
end
823829

824-
if reason and reason.detach == true then
825-
return reason
826-
end
827-
828830
if p.proto[1] ~= 1 then
829831
break
830832
end
@@ -1135,9 +1137,6 @@ local function httpd_start(self)
11351137
{ name = 'http',
11361138
handler = function(...)
11371139
local res = process_client(self, ...)
1138-
if res and res.detach == true then
1139-
res.detach_handler(self, ...)
1140-
end
11411140
end})
11421141
if server == nil then
11431142
error(sprintf("Can't create tcp_server: %s", errno.strerror()))
@@ -1151,6 +1150,8 @@ local function httpd_start(self)
11511150
end
11521151

11531152
local exports = {
1153+
DETACHED = DETACHED,
1154+
11541155
new = function(host, port, options)
11551156
if options == nil then
11561157
options = {}

test/http.test.lua

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local yaml = require 'yaml'
1010
local urilib = require('uri')
1111

1212
local test = tap.test("http")
13-
test:plan(9)
13+
test:plan(7)
1414
test:test("split_uri", function(test)
1515
test:plan(65)
1616
local function check(uri, rhs)
@@ -370,39 +370,4 @@ test:test("server requests", function(test)
370370
httpd:stop()
371371
end)
372372

373-
local function cfgservtwo()
374-
local detached = function(httpd, s, peer)
375-
test:test('Detached hook is called', function(test)
376-
test:plan(1)
377-
test:ok(true, 'hook called')
378-
end)
379-
end
380-
local path = os.getenv('LUA_SOURCE_DIR') or './'
381-
path = fio.pathjoin(path, 'test')
382-
local httpd = http_server.new('127.0.0.1', 12346, { app_dir = path,
383-
log_requests = false, log_errors = false })
384-
:route({path = '/ws', name = 'test'},
385-
function()
386-
return {status = 200,
387-
body = 'ok',
388-
detach = true,
389-
detach_handler = detached,
390-
}
391-
end)
392-
return httpd
393-
end
394-
395-
test:test("server requests", function(test)
396-
test:plan(2)
397-
398-
local httpd = cfgservtwo()
399-
httpd:start()
400-
401-
local r = http_client.get('http://127.0.0.1:12346/ws')
402-
403-
test:is(r.status, 200, 'detached 200')
404-
test:is(r.reason, 'Ok', 'detached reason')
405-
end
406-
)
407-
408373
os.exit(test:check() == true and 0 or 1)

0 commit comments

Comments
 (0)