From 33e678af32075244ab84edbba717fd82b2ecd739 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 19 Feb 2015 14:00:39 +0400 Subject: [PATCH 1/7] Add separate rockspecs for each backend. --- rockspecs/lua-websockets-copas-scm-1.rockspec | 32 +++++++++++++++++ rockspecs/lua-websockets-core-scm-1.rockspec | 35 +++++++++++++++++++ rockspecs/lua-websockets-ev-scm-1.rockspec | 32 +++++++++++++++++ rockspecs/lua-websockets-scm-1.rockspec | 33 +++-------------- rockspecs/lua-websockets-sync-scm-1.rockspec | 30 ++++++++++++++++ 5 files changed, 133 insertions(+), 29 deletions(-) create mode 100644 rockspecs/lua-websockets-copas-scm-1.rockspec create mode 100644 rockspecs/lua-websockets-core-scm-1.rockspec create mode 100644 rockspecs/lua-websockets-ev-scm-1.rockspec create mode 100644 rockspecs/lua-websockets-sync-scm-1.rockspec diff --git a/rockspecs/lua-websockets-copas-scm-1.rockspec b/rockspecs/lua-websockets-copas-scm-1.rockspec new file mode 100644 index 0000000..5efe8f0 --- /dev/null +++ b/rockspecs/lua-websockets-copas-scm-1.rockspec @@ -0,0 +1,32 @@ +package = "lua-websockets-copas" +version = "scm-1" + +source = { + url = "git://github.com/lipp/lua-websockets.git", +} + +description = { + summary = "copas backend for websockets for Lua", + homepage = "http://github.com/lipp/lua-websockets", + license = "MIT/X11", + detailed = "Provides async client and server for copas." +} + +dependencies = { + "lua >= 5.1", + "lua-websockets-core", + "luasocket", + "copas" +} + +build = { + type = 'none', + install = { + lua = { + ['websocket.sync'] = 'src/websocket/sync.lua', + ['websocket.client_copas'] = 'src/websocket/client_copas.lua', + ['websocket.server_copas'] = 'src/websocket/server_copas.lua', + } + } +} + diff --git a/rockspecs/lua-websockets-core-scm-1.rockspec b/rockspecs/lua-websockets-core-scm-1.rockspec new file mode 100644 index 0000000..d4dc7bb --- /dev/null +++ b/rockspecs/lua-websockets-core-scm-1.rockspec @@ -0,0 +1,35 @@ +package = "lua-websockets-core" +version = "scm-1" + +source = { + url = "git://github.com/lipp/lua-websockets.git", +} + +description = { + summary = "Websockets for Lua", + homepage = "http://github.com/lipp/lua-websockets", + license = "MIT/X11", + detailed = "Provides base functionality to implement websocket protocol." +} + +dependencies = { + "lua >= 5.1", + "struct", + "bit32", +} + +build = { + type = 'none', + install = { + lua = { + ['websocket'] = 'src/websocket.lua', + ['websocket.client'] = 'src/websocket/client.lua', + ['websocket.server'] = 'src/websocket/server.lua', + ['websocket.handshake'] = 'src/websocket/handshake.lua', + ['websocket.tools'] = 'src/websocket/tools.lua', + ['websocket.frame'] = 'src/websocket/frame.lua', + ['websocket.bit'] = 'src/websocket/bit.lua', + } + } +} + diff --git a/rockspecs/lua-websockets-ev-scm-1.rockspec b/rockspecs/lua-websockets-ev-scm-1.rockspec new file mode 100644 index 0000000..2ca95e1 --- /dev/null +++ b/rockspecs/lua-websockets-ev-scm-1.rockspec @@ -0,0 +1,32 @@ +package = "lua-websockets-ev" +version = "scm-1" + +source = { + url = "git://github.com/lipp/lua-websockets.git", +} + +description = { + summary = "libev backend for websockets for Lua", + homepage = "http://github.com/lipp/lua-websockets", + license = "MIT/X11", + detailed = "Provides async client and server for lua-ev." +} + +dependencies = { + "lua >= 5.1", + "lua-websockets-core", + "luasocket", + "lua-ev", +} + +build = { + type = 'none', + install = { + lua = { + ['websocket.client_ev'] = 'src/websocket/client_ev.lua', + ['websocket.ev_common'] = 'src/websocket/ev_common.lua', + ['websocket.server_ev'] = 'src/websocket/server_ev.lua', + } + } +} + diff --git a/rockspecs/lua-websockets-scm-1.rockspec b/rockspecs/lua-websockets-scm-1.rockspec index f72b44b..562c774 100644 --- a/rockspecs/lua-websockets-scm-1.rockspec +++ b/rockspecs/lua-websockets-scm-1.rockspec @@ -13,33 +13,8 @@ description = { } dependencies = { - "lua >= 5.1", - "struct", - "luasocket", - "luabitop", - "lua-ev", - "copas" + "lua-websockets-core", + "lua-websockets-sync", + "lua-websockets-copas", + "lua-websockets-ev", } - -build = { - type = 'none', - install = { - lua = { - ['websocket'] = 'src/websocket.lua', - ['websocket.sync'] = 'src/websocket/sync.lua', - ['websocket.client'] = 'src/websocket/client.lua', - ['websocket.client_sync'] = 'src/websocket/client_sync.lua', - ['websocket.client_ev'] = 'src/websocket/client_ev.lua', - ['websocket.client_copas'] = 'src/websocket/client_copas.lua', - ['websocket.ev_common'] = 'src/websocket/ev_common.lua', - ['websocket.server'] = 'src/websocket/server.lua', - ['websocket.server_ev'] = 'src/websocket/server_ev.lua', - ['websocket.server_copas'] = 'src/websocket/server_copas.lua', - ['websocket.handshake'] = 'src/websocket/handshake.lua', - ['websocket.tools'] = 'src/websocket/tools.lua', - ['websocket.frame'] = 'src/websocket/frame.lua', - ['websocket.bit'] = 'src/websocket/bit.lua', - } - } -} - diff --git a/rockspecs/lua-websockets-sync-scm-1.rockspec b/rockspecs/lua-websockets-sync-scm-1.rockspec new file mode 100644 index 0000000..cda3e87 --- /dev/null +++ b/rockspecs/lua-websockets-sync-scm-1.rockspec @@ -0,0 +1,30 @@ +package = "lua-websockets-sync" +version = "scm-1" + +source = { + url = "git://github.com/lipp/lua-websockets.git", +} + +description = { + summary = "Sunc backend for websockets for Lua", + homepage = "http://github.com/lipp/lua-websockets", + license = "MIT/X11", + detailed = "Provides sync client" +} + +dependencies = { + "lua >= 5.1", + "lua-websockets-core", + "luasocket", +} + +build = { + type = 'none', + install = { + lua = { + ['websocket.sync'] = 'src/websocket/sync.lua', + ['websocket.client_sync'] = 'src/websocket/client_sync.lua', + } + } +} + From 86ba5f7884bdd7ab60fc3f2af109e79e57bc334b Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Thu, 19 Feb 2015 15:14:56 +0400 Subject: [PATCH 2/7] Move `websocket.sync` to `websocket.core` module --- rockspecs/lua-websockets-copas-scm-1.rockspec | 1 - rockspecs/lua-websockets-core-scm-1.rockspec | 1 + rockspecs/lua-websockets-sync-scm-1.rockspec | 1 - 3 files changed, 1 insertion(+), 2 deletions(-) diff --git a/rockspecs/lua-websockets-copas-scm-1.rockspec b/rockspecs/lua-websockets-copas-scm-1.rockspec index 5efe8f0..361850a 100644 --- a/rockspecs/lua-websockets-copas-scm-1.rockspec +++ b/rockspecs/lua-websockets-copas-scm-1.rockspec @@ -23,7 +23,6 @@ build = { type = 'none', install = { lua = { - ['websocket.sync'] = 'src/websocket/sync.lua', ['websocket.client_copas'] = 'src/websocket/client_copas.lua', ['websocket.server_copas'] = 'src/websocket/server_copas.lua', } diff --git a/rockspecs/lua-websockets-core-scm-1.rockspec b/rockspecs/lua-websockets-core-scm-1.rockspec index d4dc7bb..1d8989a 100644 --- a/rockspecs/lua-websockets-core-scm-1.rockspec +++ b/rockspecs/lua-websockets-core-scm-1.rockspec @@ -23,6 +23,7 @@ build = { install = { lua = { ['websocket'] = 'src/websocket.lua', + ['websocket.sync'] = 'src/websocket/sync.lua', ['websocket.client'] = 'src/websocket/client.lua', ['websocket.server'] = 'src/websocket/server.lua', ['websocket.handshake'] = 'src/websocket/handshake.lua', diff --git a/rockspecs/lua-websockets-sync-scm-1.rockspec b/rockspecs/lua-websockets-sync-scm-1.rockspec index cda3e87..e685d1b 100644 --- a/rockspecs/lua-websockets-sync-scm-1.rockspec +++ b/rockspecs/lua-websockets-sync-scm-1.rockspec @@ -22,7 +22,6 @@ build = { type = 'none', install = { lua = { - ['websocket.sync'] = 'src/websocket/sync.lua', ['websocket.client_sync'] = 'src/websocket/client_sync.lua', } } From e2c3186757f0eae97c8cec988ac764fec2b0dae6 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 25 Feb 2015 15:45:08 +0400 Subject: [PATCH 3/7] Install on Travis. --- .travis.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8e247b5..6da0895 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: env: - LUA="luajit" -install: +before_install: - sudo pip install autobahntestsuite - sudo apt-get install libev-dev - sudo apt-get install luajit @@ -12,7 +12,17 @@ install: - sudo luarocks install luacov-coveralls - sudo luarocks install busted 1.11.1-1 -script: "sudo luarocks make rockspecs/lua-websockets-scm-1.rockspec && ./test.sh" +install: + - sudo luarocks make lua-websockets-core-scm-1.rockspec + - sudo luarocks make lua-websockets-sync-scm-1.rockspec + - sudo luarocks make lua-websockets-copas-scm-1.rockspec + - sudo luarocks make lua-websockets-ev-scm-1.rockspec + - sudo luarocks make lua-websockets-scm-1.rockspec + +script: + - ./test.sh after_success: - luacov-coveralls + + From 2c0f2066361c03f663a2c57f9c5cbdf290581a53 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 25 Feb 2015 15:53:44 +0400 Subject: [PATCH 4/7] Fix. Path to rockspecs in `.travis.yml` --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6da0895..f8ff652 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,11 +13,11 @@ before_install: - sudo luarocks install busted 1.11.1-1 install: - - sudo luarocks make lua-websockets-core-scm-1.rockspec - - sudo luarocks make lua-websockets-sync-scm-1.rockspec - - sudo luarocks make lua-websockets-copas-scm-1.rockspec - - sudo luarocks make lua-websockets-ev-scm-1.rockspec - - sudo luarocks make lua-websockets-scm-1.rockspec + - sudo luarocks make rockspecs/lua-websockets-core-scm-1.rockspec + - sudo luarocks make rockspecs/lua-websockets-sync-scm-1.rockspec + - sudo luarocks make rockspecs/lua-websockets-copas-scm-1.rockspec + - sudo luarocks make rockspecs/lua-websockets-ev-scm-1.rockspec + - sudo luarocks make rockspecs/lua-websockets-scm-1.rockspec script: - ./test.sh From 8d20159eb9b08e1e543a46d278f1ec545c012131 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 25 Feb 2015 16:03:05 +0400 Subject: [PATCH 5/7] Fix. Specify empty build table in rockspec. --- rockspecs/lua-websockets-scm-1.rockspec | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rockspecs/lua-websockets-scm-1.rockspec b/rockspecs/lua-websockets-scm-1.rockspec index 562c774..85a6077 100644 --- a/rockspecs/lua-websockets-scm-1.rockspec +++ b/rockspecs/lua-websockets-scm-1.rockspec @@ -18,3 +18,5 @@ dependencies = { "lua-websockets-copas", "lua-websockets-ev", } + +build = {type = "builtin", modules = {}} From ee3eb93270674e44376517572c6c6475ea847750 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Sun, 1 Mar 2015 17:12:49 +0400 Subject: [PATCH 6/7] Add. Support `lbase64` and `basexx` libraries to make base64 encode. Add. Support `ldigest` library to make sha1 digest --- rockspecs/lua-websockets-core-scm-1.rockspec | 1 + src/websocket/tools.lua | 60 +++++++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/rockspecs/lua-websockets-core-scm-1.rockspec b/rockspecs/lua-websockets-core-scm-1.rockspec index 1d8989a..9bcec15 100644 --- a/rockspecs/lua-websockets-core-scm-1.rockspec +++ b/rockspecs/lua-websockets-core-scm-1.rockspec @@ -16,6 +16,7 @@ dependencies = { "lua >= 5.1", "struct", "bit32", + "basexx", } build = { diff --git a/src/websocket/tools.lua b/src/websocket/tools.lua index 38a76e8..c169c3f 100644 --- a/src/websocket/tools.lua +++ b/src/websocket/tools.lua @@ -15,20 +15,43 @@ local tinsert = table.insert local tconcat = table.concat local mrandom = math.random +local function prequire(m) + local ok, err = pcall(require, m) + if ok then return err, m end + return nil, err +end + +local function orequire(...) + for _, name in ipairs{...} do + local mod = prequire(name) + if mod then return mod, name end + end +end + +local function vrequire(...) + local m, n = orequire(...) + if m then return m, n end + error("Can not fine any of this modules: " .. table.concat({...}, "/"), 2) +end + -- used for generate key random ops math.randomseed(os.time()) --- SHA1 hashing from luacrypto, if available -local sha1_crypto -local done,crypto = pcall(require,'crypto') -if done then - sha1_crypto = function(msg) - return crypto.digest('sha1',msg,true) +-- SHA1 hashing from luacrypto, ldigest if available +local shalib, name = orequire('crypto', 'sha1', 'digest') +local sha1_digest if name == 'sha1' then + sha1_digest = function(str) return shalib.digest(str, true) end +elseif name == 'crypto' then + sha1_digest = function(str) return shalib.digest('sha1', str, true) end +elseif name == 'digest' then + if _G.sha1 and _G.sha1.digest then + shalib = _G.sha1 + sha1_digest = function(str) return shalib.digest(str, true) end end end - +if not sha1_digest then -- from wiki article, not particularly clever impl -local sha1_wiki = function(msg) +sha1_digest = function(msg) local h0 = 0x67452301 local h1 = 0xEFCDAB89 local h2 = 0x98BADCFE @@ -113,10 +136,19 @@ local sha1_wiki = function(msg) return struct.pack('>i>i>i>i>i',h0,h1,h2,h3,h4) end +end + +local base, name = vrequire("base64", "mime", "basexx") -local base64_encode = function(data) - local mime = require'mime' - return (mime.b64(data)) +local base64 = {} if name == 'basexx' then + base64.encode = function(str) return base.to_base64(str) end + base64.decode = function(str) return base.from_base64(str) end +elseif name == 'mime' then + base64.encode = function(str) return base.b64(str) end + base64.decode = function(str) return base.ub64(str) end +elseif name == 'base64' then + base64.encode = function(str) return base.encode(str) end + base64.decode = function(str) return base.decode(str) end end local parse_url = function(url) @@ -147,10 +179,8 @@ local generate_key = function() end return { - sha1 = sha1_crypto or sha1_wiki, - base64 = { - encode = base64_encode - }, + sha1 = sha1_digest, + base64 = base64, parse_url = parse_url, generate_key = generate_key, } From 77f924594408af46588cf1133f48b6188ce399c6 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Mon, 2 Mar 2015 09:34:40 +0400 Subject: [PATCH 7/7] Fix. Use base64 encode function introduced in preview commit. --- src/websocket/tools.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websocket/tools.lua b/src/websocket/tools.lua index c169c3f..3ee8846 100644 --- a/src/websocket/tools.lua +++ b/src/websocket/tools.lua @@ -175,7 +175,7 @@ local generate_key = function() local r4 = mrandom(0,0xfffffff) local key = struct.pack('IIII',r1,r2,r3,r4) assert(#key==16,#key) - return base64_encode(key) + return base64.encode(key) end return {