Skip to content

Commit

Permalink
Created a lightweight POST processing for publish events
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Burkalev authored and Konstantin Burkalev committed Jun 19, 2014
1 parent ce747f2 commit 739bf7b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
25 changes: 25 additions & 0 deletions build/post-handler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--
-- Project: this project
-- User: kostik
-- Date: 20.06.14
--

local wampServer = require "wiola"
local realm = "hga"

local redisOk, redisErr = wampServer.setupRedis("unix:/tmp/redis.sock")
if not redisOk then
return ngx.exit(444)
end

--local req = ngx.var.request_body
ngx.req.read_body()
local req = ngx.req.get_body_data()

local res, httpCode = wampServer.processPostData(ngx.var.connection, realm, req)

ngx.status = httpCode
ngx.say(res)

-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
41 changes: 41 additions & 0 deletions build/wiola.lua
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,45 @@ function _M.getPendingData(regId)
return redis:lpop("wiSes" .. regId .. "Data")
end

--
-- Process lightweight publish POST data from client
--
-- sid - nginx session connection ID
-- realm - WAMP Realm to operate in
-- data - data, received through POST
--
function _M.processPostData(sid, realm, data)

local cjson = require "cjson"
local dataObj = cjson.decode(data)
local res
local httpCode

if dataObj[1] == WAMP_MSG_SPEC.PUBLISH then
local regId, dataType = _M.addConnection(sid, nil)

-- Make a session legal :)
redis:hset("wiSes" .. regId, "isWampEstablished", 1)
redis:hset("wiSes" .. regId, "realm", realm)

_M.receiveData(regId, data)

local cliData, cliErr = _M.getPendingData(regId)
if cliData ~= ngx.null then
res = cliData
httpCode = ngx.HTTP_FORBIDDEN
else
res = cjson.encode({ result = true, error = nil })
httpCode = ngx.HTTP_OK
end

_M.removeConnection(regId)
else
res = cjson.encode({ result = false, error = "Message type not supported" })
httpCode = ngx.HTTP_FORBIDDEN
end

return res, httpCode
end

return _M
26 changes: 26 additions & 0 deletions src/wiola/post-handler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- Project: this project
-- User: kostik
-- Date: 20.06.14
--

local wampServer = require "wiola"
local realm = "hga"

local redisOk, redisErr = wampServer.setupRedis("unix:/tmp/redis.sock")
if not redisOk then
ngx.log(ngx.DEBUG, "Failed to connect to redis: ", redisErr)
return ngx.exit(444)
end

--local req = ngx.var.request_body
ngx.req.read_body()
local req = ngx.req.get_body_data()

local res, httpCode = wampServer.processPostData(ngx.var.connection, realm, req)

ngx.status = httpCode
ngx.say(res)

-- to cause quit the whole request rather than the current phase handler
ngx.exit(ngx.HTTP_OK)
45 changes: 44 additions & 1 deletion src/wiola/wiola.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ local function putData(session, data)
dataObj = cjson.encode(data)
end

ngx.log(ngx.DEBUG, "Preparing data for client: ", dataObj);
ngx.log(ngx.DEBUG, "Preparing data for client: ", dataObj)

redis:rpush("wiSes" .. session.sessId .. "Data", dataObj)
end
Expand Down Expand Up @@ -587,4 +587,47 @@ function _M.getPendingData(regId)
return redis:lpop("wiSes" .. regId .. "Data")
end

--
-- Process lightweight publish POST data from client
--
-- sid - nginx session connection ID
-- realm - WAMP Realm to operate in
-- data - data, received through POST
--
function _M.processPostData(sid, realm, data)

ngx.log(ngx.DEBUG, "Received POST data for processing in realm ", realm, ":", data)

local cjson = require "cjson"
local dataObj = cjson.decode(data)
local res
local httpCode

if dataObj[1] == WAMP_MSG_SPEC.PUBLISH then
local regId, dataType = _M.addConnection(sid, nil)

-- Make a session legal :)
redis:hset("wiSes" .. regId, "isWampEstablished", 1)
redis:hset("wiSes" .. regId, "realm", realm)

_M.receiveData(regId, data)

local cliData, cliErr = _M.getPendingData(regId)
if cliData ~= ngx.null then
res = cliData
httpCode = ngx.HTTP_FORBIDDEN
else
res = cjson.encode({ result = true, error = nil })
httpCode = ngx.HTTP_OK
end

_M.removeConnection(regId)
else
res = cjson.encode({ result = false, error = "Message type not supported" })
httpCode = ngx.HTTP_FORBIDDEN
end

return res, httpCode
end

return _M

0 comments on commit 739bf7b

Please sign in to comment.