net/websocket.lua

Thu, 25 Dec 2014 10:48:06 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 25 Dec 2014 10:48:06 +0000
changeset 0
d363a6692a10
permissions
-rw-r--r--

Initial commit. Tortoises are fun.

0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2012 Florian Zeitz
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2014 Daurnimator
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local t_concat = table.concat;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local http = require "net.http";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local frames = require "net.websocket.frames";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local base64 = require "util.encodings".base64;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local sha1 = require "util.hashes".sha1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local random_bytes = require "util.random".bytes;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local timer = require "util.timer";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local log = require "util.logger".init "websocket";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local close_timeout = 3; -- Seconds to wait after sending close frame until closing connection.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local websockets = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local websocket_listeners = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 function websocket_listeners.ondisconnect(handler, err)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local s = websockets[handler];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 websockets[handler] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 if s.close_timer then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 timer.stop(s.close_timer);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 s.close_timer = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 s.readyState = 3;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if s.close_code == nil and s.onerror then s:onerror(err); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if s.onclose then s:onclose(s.close_code, s.close_message or err); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 function websocket_listeners.ondetach(handler)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 websockets[handler] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local function fail(s, code, reason)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 module:log("warn", "WebSocket connection failed, closing. %d %s", code, reason);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 s:close(code, reason);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 s.handler:close();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return false
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function websocket_listeners.onincoming(handler, buffer, err)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local s = websockets[handler];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 s.readbuffer = s.readbuffer..buffer;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 while true do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local frame, len = frames.parse(s.readbuffer);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if frame == nil then break end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 s.readbuffer = s.readbuffer:sub(len+1);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 log("debug", "Websocket received frame: opcode=%0x, %i bytes", frame.opcode, #frame.data);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 -- Error cases
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if frame.RSV1 or frame.RSV2 or frame.RSV3 then -- Reserved bits non zero
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return fail(s, 1002, "Reserved bits not zero");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if frame.opcode < 0x8 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local databuffer = s.databuffer;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if frame.opcode == 0x0 then -- Continuation frames
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if not databuffer then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return fail(s, 1002, "Unexpected continuation frame");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 databuffer[#databuffer+1] = frame.data;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 elseif frame.opcode == 0x1 or frame.opcode == 0x2 then -- Text or Binary frame
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 if databuffer then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 return fail(s, 1002, "Continuation frame expected");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 databuffer = {type=frame.opcode, frame.data};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 s.databuffer = databuffer;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 return fail(s, 1002, "Reserved opcode");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if frame.FIN then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 s.databuffer = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if s.onmessage then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 s:onmessage(t_concat(databuffer), databuffer.type);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 else -- Control frame
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if frame.length > 125 then -- Control frame with too much payload
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return fail(s, 1002, "Payload too large");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 elseif not frame.FIN then -- Fragmented control frame
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return fail(s, 1002, "Fragmented control frame");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if frame.opcode == 0x8 then -- Close request
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if frame.length == 1 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 return fail(s, 1002, "Close frame with payload, but too short for status code");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local status_code, message = frames.parse_close(frame.data);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 if status_code == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 --[[ RFC 6455 7.4.1
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 1005 is a reserved value and MUST NOT be set as a status code in a
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 Close control frame by an endpoint. It is designated for use in
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 applications expecting a status code to indicate that no status
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 code was actually present.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 ]]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 status_code = 1005
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 elseif status_code < 1000 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 return fail(s, 1002, "Closed with invalid status code");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 elseif ((status_code > 1003 and status_code < 1007) or status_code > 1011) and status_code < 3000 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 return fail(s, 1002, "Closed with reserved status code");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 s.close_code, s.close_message = status_code, message;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 s:close(1000);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 return true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 elseif frame.opcode == 0x9 then -- Ping frame
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 frame.opcode = 0xA;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 frame.MASK = true; -- RFC 6455 6.1.5: If the data is being sent by the client, the frame(s) MUST be masked
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 handler:write(frames.build(frame));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 elseif frame.opcode == 0xA then -- Pong frame
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 log("debug", "Received unexpected pong frame: " .. tostring(frame.data));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 return fail(s, 1002, "Reserved opcode");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 return true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 local websocket_methods = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local function close_timeout_cb(now, timerid, s)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 s.close_timer = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 log("warn", "Close timeout waiting for server to close, closing manually.");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 s.handler:close();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 function websocket_methods:close(code, reason)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if self.readyState < 2 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 code = code or 1000;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 log("debug", "closing WebSocket with code %i: %s" , code , tostring(reason));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 self.readyState = 2;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local handler = self.handler;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 handler:write(frames.build_close(code, reason, true));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 -- Do not close socket straight away, wait for acknowledgement from server.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 self.close_timer = timer.add_task(close_timeout, close_timeout_cb, self);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 elseif self.readyState == 2 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 log("debug", "tried to close a closing WebSocket, closing the raw socket.");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 -- Stop timer
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 if self.close_timer then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 timer.stop(self.close_timer);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 self.close_timer = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 local handler = self.handler;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 handler:close();
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 log("debug", "tried to close a closed WebSocket, ignoring.");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 function websocket_methods:send(data, opcode)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if self.readyState < 1 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 return nil, "WebSocket not open yet, unable to send data.";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 elseif self.readyState >= 2 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 return nil, "WebSocket closed, unable to send data.";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 if opcode == "text" or opcode == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 opcode = 0x1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 elseif opcode == "binary" then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 opcode = 0x2;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 local frame = {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 FIN = true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 MASK = true; -- RFC 6455 6.1.5: If the data is being sent by the client, the frame(s) MUST be masked
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 opcode = opcode;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 data = tostring(data);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 log("debug", "WebSocket sending frame: opcode=%0x, %i bytes", frame.opcode, #frame.data);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 return self.handler:write(frames.build(frame));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 local websocket_metatable = {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 __index = websocket_methods;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 local function connect(url, ex, listeners)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 ex = ex or {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 --[[RFC 6455 4.1.7:
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 The request MUST include a header field with the name
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 |Sec-WebSocket-Key|. The value of this header field MUST be a
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 nonce consisting of a randomly selected 16-byte value that has
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 been base64-encoded (see Section 4 of [RFC4648]). The nonce
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 MUST be selected randomly for each connection.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 ]]
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 local key = base64.encode(random_bytes(16));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 -- Either a single protocol string or an array of protocol strings.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 local protocol = ex.protocol;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 if type(protocol) == "string" then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 protocol = { protocol, [protocol] = true };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 elseif type(protocol) == "table" and protocol[1] then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 for _, v in ipairs(protocol) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 protocol[v] = true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 protocol = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 local headers = {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 ["Upgrade"] = "websocket";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 ["Connection"] = "Upgrade";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 ["Sec-WebSocket-Key"] = key;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 ["Sec-WebSocket-Protocol"] = protocol and t_concat(protocol, ", ");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 ["Sec-WebSocket-Version"] = "13";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 ["Sec-WebSocket-Extensions"] = ex.extensions;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 }
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 if ex.headers then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 for k,v in pairs(ex.headers) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 headers[k] = v;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 local s = setmetatable({
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 readbuffer = "";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 databuffer = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 handler = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 close_code = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 close_message = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 close_timer = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 readyState = 0;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 protocol = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 url = url;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 onopen = listeners.onopen;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 onclose = listeners.onclose;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 onmessage = listeners.onmessage;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 onerror = listeners.onerror;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 }, websocket_metatable);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 local http_url = url:gsub("^(ws)", "http");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 local http_req = http.request(http_url, {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 method = "GET";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 headers = headers;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 sslctx = ex.sslctx;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 }, function(b, c, r, http_req)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 if c ~= 101
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 or r.headers["connection"]:lower() ~= "upgrade"
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 or r.headers["upgrade"] ~= "websocket"
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 or r.headers["sec-websocket-accept"] ~= base64.encode(sha1(key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 or (protocol and not protocol[r.headers["sec-websocket-protocol"]])
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 s.readyState = 3;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 log("warn", "WebSocket connection to %s failed: %s", url, tostring(b));
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 if s.onerror then s:onerror("connecting-failed"); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 return;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 s.protocol = r.headers["sec-websocket-protocol"];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 -- Take possession of socket from http
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 http_req.conn = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 local handler = http_req.handler;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 s.handler = handler;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 websockets[handler] = s;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 handler:setlistener(websocket_listeners);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 log("debug", "WebSocket connected successfully to %s", url);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 s.readyState = 1;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 if s.onopen then s:onopen(); end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 websocket_listeners.onincoming(handler, b);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 return s;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 return {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 connect = connect;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 };

mercurial