Skip to content

Commit 739bf7b

Browse files
Konstantin BurkalevKonstantin Burkalev
authored andcommitted
Created a lightweight POST processing for publish events
1 parent ce747f2 commit 739bf7b

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

build/post-handler.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Project: this project
3+
-- User: kostik
4+
-- Date: 20.06.14
5+
--
6+
7+
local wampServer = require "wiola"
8+
local realm = "hga"
9+
10+
local redisOk, redisErr = wampServer.setupRedis("unix:/tmp/redis.sock")
11+
if not redisOk then
12+
return ngx.exit(444)
13+
end
14+
15+
--local req = ngx.var.request_body
16+
ngx.req.read_body()
17+
local req = ngx.req.get_body_data()
18+
19+
local res, httpCode = wampServer.processPostData(ngx.var.connection, realm, req)
20+
21+
ngx.status = httpCode
22+
ngx.say(res)
23+
24+
-- to cause quit the whole request rather than the current phase handler
25+
ngx.exit(ngx.HTTP_OK)

build/wiola.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,45 @@ function _M.getPendingData(regId)
565565
return redis:lpop("wiSes" .. regId .. "Data")
566566
end
567567

568+
--
569+
-- Process lightweight publish POST data from client
570+
--
571+
-- sid - nginx session connection ID
572+
-- realm - WAMP Realm to operate in
573+
-- data - data, received through POST
574+
--
575+
function _M.processPostData(sid, realm, data)
576+
577+
local cjson = require "cjson"
578+
local dataObj = cjson.decode(data)
579+
local res
580+
local httpCode
581+
582+
if dataObj[1] == WAMP_MSG_SPEC.PUBLISH then
583+
local regId, dataType = _M.addConnection(sid, nil)
584+
585+
-- Make a session legal :)
586+
redis:hset("wiSes" .. regId, "isWampEstablished", 1)
587+
redis:hset("wiSes" .. regId, "realm", realm)
588+
589+
_M.receiveData(regId, data)
590+
591+
local cliData, cliErr = _M.getPendingData(regId)
592+
if cliData ~= ngx.null then
593+
res = cliData
594+
httpCode = ngx.HTTP_FORBIDDEN
595+
else
596+
res = cjson.encode({ result = true, error = nil })
597+
httpCode = ngx.HTTP_OK
598+
end
599+
600+
_M.removeConnection(regId)
601+
else
602+
res = cjson.encode({ result = false, error = "Message type not supported" })
603+
httpCode = ngx.HTTP_FORBIDDEN
604+
end
605+
606+
return res, httpCode
607+
end
608+
568609
return _M

src/wiola/post-handler.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Project: this project
3+
-- User: kostik
4+
-- Date: 20.06.14
5+
--
6+
7+
local wampServer = require "wiola"
8+
local realm = "hga"
9+
10+
local redisOk, redisErr = wampServer.setupRedis("unix:/tmp/redis.sock")
11+
if not redisOk then
12+
ngx.log(ngx.DEBUG, "Failed to connect to redis: ", redisErr)
13+
return ngx.exit(444)
14+
end
15+
16+
--local req = ngx.var.request_body
17+
ngx.req.read_body()
18+
local req = ngx.req.get_body_data()
19+
20+
local res, httpCode = wampServer.processPostData(ngx.var.connection, realm, req)
21+
22+
ngx.status = httpCode
23+
ngx.say(res)
24+
25+
-- to cause quit the whole request rather than the current phase handler
26+
ngx.exit(ngx.HTTP_OK)

src/wiola/wiola.lua

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ local function putData(session, data)
206206
dataObj = cjson.encode(data)
207207
end
208208

209-
ngx.log(ngx.DEBUG, "Preparing data for client: ", dataObj);
209+
ngx.log(ngx.DEBUG, "Preparing data for client: ", dataObj)
210210

211211
redis:rpush("wiSes" .. session.sessId .. "Data", dataObj)
212212
end
@@ -587,4 +587,47 @@ function _M.getPendingData(regId)
587587
return redis:lpop("wiSes" .. regId .. "Data")
588588
end
589589

590+
--
591+
-- Process lightweight publish POST data from client
592+
--
593+
-- sid - nginx session connection ID
594+
-- realm - WAMP Realm to operate in
595+
-- data - data, received through POST
596+
--
597+
function _M.processPostData(sid, realm, data)
598+
599+
ngx.log(ngx.DEBUG, "Received POST data for processing in realm ", realm, ":", data)
600+
601+
local cjson = require "cjson"
602+
local dataObj = cjson.decode(data)
603+
local res
604+
local httpCode
605+
606+
if dataObj[1] == WAMP_MSG_SPEC.PUBLISH then
607+
local regId, dataType = _M.addConnection(sid, nil)
608+
609+
-- Make a session legal :)
610+
redis:hset("wiSes" .. regId, "isWampEstablished", 1)
611+
redis:hset("wiSes" .. regId, "realm", realm)
612+
613+
_M.receiveData(regId, data)
614+
615+
local cliData, cliErr = _M.getPendingData(regId)
616+
if cliData ~= ngx.null then
617+
res = cliData
618+
httpCode = ngx.HTTP_FORBIDDEN
619+
else
620+
res = cjson.encode({ result = true, error = nil })
621+
httpCode = ngx.HTTP_OK
622+
end
623+
624+
_M.removeConnection(regId)
625+
else
626+
res = cjson.encode({ result = false, error = "Message type not supported" })
627+
httpCode = ngx.HTTP_FORBIDDEN
628+
end
629+
630+
return res, httpCode
631+
end
632+
590633
return _M

0 commit comments

Comments
 (0)