Skip to content

Commit 497f858

Browse files
Merge pull request #58 from tarantool/protocol-upgrade
protocol upgrade basic implementation
2 parents 9be3e49 + 5b2266f commit 497f858

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

http/server.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ local codes = require('http.codes')
1010

1111
local log = require('log')
1212
local socket = require('socket')
13-
local fiber = require('fiber')
1413
local json = require('json')
1514
local errno = require 'errno'
1615

@@ -659,7 +658,7 @@ local function process_client(self, s, peer)
659658
break
660659
end
661660
end
662-
661+
663662
if is_eof then
664663
break
665664
end
@@ -822,6 +821,10 @@ local function process_client(self, s, peer)
822821
end
823822
end
824823

824+
if reason and reason.detach == true then
825+
return reason
826+
end
827+
825828
if p.proto[1] ~= 1 then
826829
break
827830
end
@@ -1128,8 +1131,14 @@ local function httpd_start(self)
11281131
error("httpd: usage: httpd:start()")
11291132
end
11301133

1131-
local server = socket.tcp_server(self.host, self.port, { name = 'http',
1132-
handler = function(...) process_client(self, ...) end })
1134+
local server = socket.tcp_server(self.host, self.port,
1135+
{ name = 'http',
1136+
handler = function(...)
1137+
local res = process_client(self, ...)
1138+
if res and res.detach == true then
1139+
res.detach_handler(self, ...)
1140+
end
1141+
end})
11331142
if server == nil then
11341143
error(sprintf("Can't create tcp_server: %s", errno.strerror()))
11351144
end

test/http.test.lua

Lines changed: 36 additions & 1 deletion
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(7)
13+
test:plan(9)
1414
test:test("split_uri", function(test)
1515
test:plan(65)
1616
local function check(uri, rhs)
@@ -370,4 +370,39 @@ 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+
373408
os.exit(test:check() == true and 0 or 1)

0 commit comments

Comments
 (0)