Skip to content

Commit ee8f90f

Browse files
committed
Merge pull request lipp#35 from ntd/pr1
Depend on struct instead of lpack
2 parents 8d7c423 + dae9203 commit ee8f90f

File tree

9 files changed

+22
-31
lines changed

9 files changed

+22
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var echoWs = new WebSocket('ws://127.0.0.1:8002','echo');
111111
The client and server modules depend on:
112112

113113
- luasocket
114-
- lpack
114+
- struct
115115
- luabitop (if not using Lua 5.2 nor luajit)
116116
- copas (optionally)
117117
- lua-ev (optionally)

lua-websockets.rockspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ description = {
1515

1616
dependencies = {
1717
"lua >= 5.1",
18-
"lpack",
18+
"struct",
1919
"luasocket",
2020
"luabitop",
2121
"copas"

rockspecs/lua-websockets-scm-1.rockspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description = {
1414

1515
dependencies = {
1616
"lua >= 5.1",
17-
"lpack",
17+
"struct",
1818
"luasocket",
1919
"luabitop",
2020
"lua-ev",

spec/frame_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package.path = package.path..'../src'
22

33
local frame = require'websocket.frame'
4-
require'pack'
54

65
local bytes = string.char
76

spec/handshake_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ local url = 'ws://127.0.0.1:'..port
66

77
local handshake = require'websocket.handshake'
88
local socket = require'socket'
9-
require'pack'
109

1110
local request_lines = {
1211
'GET /chat HTTP/1.1',

spec/tools_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package.path = package.path..'../src'
22

33
local tools = require'websocket.tools'
4-
require'pack'
54

65
local bytes = string.char
76

src/websocket/frame.lua

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
-- Following Websocket RFC: http://tools.ietf.org/html/rfc6455
2-
require'pack'
2+
local struct = require'struct'
33
local bit = require'websocket.bit'
44
local band = bit.band
55
local bxor = bit.bxor
66
local bor = bit.bor
7-
local sunpack = string.unpack
87
local tremove = table.remove
9-
local spack = string.pack
108
local srep = string.rep
119
local ssub = string.sub
1210
local sbyte = string.byte
@@ -38,7 +36,7 @@ local xor_mask = function(encoded,mask,payload)
3836
local original = {sbyte(encoded,p,last)}
3937
for i=1,#original do
4038
local j = (i-1) % 4 + 1
41-
transformed[i] = bxor(original[i],mask[j])
39+
transformed[i] = band(bxor(original[i],mask[j]), 0xFF)
4240
end
4341
local xored = schar(unpack(transformed))
4442
tinsert(transformed_arr,xored)
@@ -59,15 +57,15 @@ local encode = function(data,opcode,masked,fin)
5957
local len = #data
6058
if len < 126 then
6159
payload = bor(payload,len)
62-
encoded = spack('bb',header,payload)
60+
encoded = struct.pack('bb',header,payload)
6361
elseif len < 0xffff then
6462
payload = bor(payload,126)
65-
encoded = spack('bb>H',header,payload,len)
63+
encoded = struct.pack('bb>H',header,payload,len)
6664
elseif len < 2^53 then
6765
local high = math.floor(len/2^32)
6866
local low = len - high*2^32
6967
payload = bor(payload,127)
70-
encoded = spack('bb>I>I',header,payload,high,low)
68+
encoded = struct.pack('bb>I>I',header,payload,high,low)
7169
end
7270
if not masked then
7371
encoded = encoded..data
@@ -77,7 +75,7 @@ local encode = function(data,opcode,masked,fin)
7775
local m3 = math.random(0,0xff)
7876
local m4 = math.random(0,0xff)
7977
local mask = {m1,m2,m3,m4}
80-
encoded = encoded..spack('bbbb',m1,m2,m3,m4)
78+
encoded = encoded..struct.pack('bbbb',m1,m2,m3,m4)
8179
encoded = encoded..xor_mask(data,mask,#data)
8280
end
8381
return encoded
@@ -88,7 +86,7 @@ local decode = function(encoded)
8886
if #encoded < 2 then
8987
return nil,2
9088
end
91-
local pos,header,payload = sunpack(encoded,'bb')
89+
local header,payload,pos = struct.unpack('bb',encoded)
9290
local high,low
9391
encoded = ssub(encoded,pos)
9492
local bytes = 2
@@ -101,12 +99,12 @@ local decode = function(encoded)
10199
if #encoded < 2 then
102100
return nil,2
103101
end
104-
pos,payload = sunpack(encoded,'>H')
102+
payload,pos = struct.unpack('>H',encoded)
105103
elseif payload == 127 then
106104
if #encoded < 8 then
107105
return nil,8
108106
end
109-
pos,high,low = sunpack(encoded,'>I>I')
107+
high,low,pos = struct.unpack('>I>I',encoded)
110108
payload = high*2^32 + low
111109
if payload < 0xffff or payload > 2^53 then
112110
assert(false,'INVALID PAYLOAD '..payload)
@@ -123,7 +121,7 @@ local decode = function(encoded)
123121
if bytes_short > 0 then
124122
return nil,bytes_short
125123
end
126-
local pos,m1,m2,m3,m4 = sunpack(encoded,'bbbb')
124+
local m1,m2,m3,m4,pos = struct.unpack('bbbb',encoded)
127125
encoded = ssub(encoded,pos)
128126
local mask = {
129127
m1,m2,m3,m4
@@ -147,7 +145,7 @@ end
147145

148146
local encode_close = function(code,reason)
149147
if code then
150-
local data = spack('>H',code)
148+
local data = struct.pack('>H',code)
151149
if reason then
152150
data = data..tostring(reason)
153151
end
@@ -160,7 +158,7 @@ local decode_close = function(data)
160158
local _,code,reason
161159
if data then
162160
if #data > 1 then
163-
_,code = sunpack(data,'>H')
161+
code = struct.unpack('>H',data)
164162
end
165163
if #data > 2 then
166164
reason = data:sub(3)

src/websocket/handshake.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
require'pack'
3-
41
local sha1 = require'websocket.tools'.sha1
52
local base64 = require'websocket.tools'.base64
63
local tinsert = table.insert

src/websocket/tools.lua

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require'pack'
1+
local struct = require'struct'
22
local socket = require'socket'
33
local bit = require'websocket.bit'
44
local rol = bit.rol
@@ -8,7 +8,6 @@ local band = bit.band
88
local bnot = bit.bnot
99
local lshift = bit.lshift
1010
local rshift = bit.rshift
11-
local spack = string.pack
1211
local sunpack = string.unpack
1312
local srep = string.rep
1413
local schar = string.char
@@ -45,16 +44,16 @@ local sha1 = function(msg)
4544
-- append 64 big endian length
4645
local high = math.floor(bits/2^32)
4746
local low = bits - high*2^32
48-
msg = msg..spack('>I>I',high,low)
47+
msg = msg..struct.pack('>I>I',high,low)
4948

5049
assert(#msg % 64 == 0,#msg % 64)
5150

5251
for j=1,#msg,64 do
5352
local chunk = msg:sub(j,j+63)
5453
assert(#chunk==64,#chunk)
55-
local words = {sunpack(chunk,srep('>I',16))}
56-
-- index 1 contains fragment from unpack
57-
tremove(words,1)
54+
local words = {struct.unpack(srep('>I',16),chunk)}
55+
-- last item contains the index in chunk where it stopped reading
56+
tremove(words,17)
5857
assert(#words==16)
5958
for i=17,80 do
6059
words[i] = bxor(words[i-3],words[i-8],words[i-14],words[i-16])
@@ -105,7 +104,7 @@ local sha1 = function(msg)
105104
h3 = band(h3,0xffffffff)
106105
h4 = band(h4,0xffffffff)
107106

108-
return spack('>i>i>i>i>i',h0,h1,h2,h3,h4)
107+
return struct.pack('>i>i>i>i>i',h0,h1,h2,h3,h4)
109108
end
110109

111110
local base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
@@ -163,7 +162,7 @@ local generate_key = function()
163162
local r2 = mrandom(0,0xfffffff)
164163
local r3 = mrandom(0,0xfffffff)
165164
local r4 = mrandom(0,0xfffffff)
166-
local key = spack('IIII',r1,r2,r3,r4)
165+
local key = struct.pack('IIII',r1,r2,r3,r4)
167166
assert(#key==16,#key)
168167
return base64_encode(key)
169168
end

0 commit comments

Comments
 (0)