annotate mod_websocket/mod_websocket.lua @ 497:94756b0657b4

mod_ircd: added USERHOST command.
author Marco Cirillo <maranda@lightwitch.org>
date Mon, 05 Dec 2011 21:11:40 +0000
parents 5fc00a3e47b5
children 54fa9d6d7809
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
129
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
1 module.host = "*" -- Global module
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
2
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
3 local logger = require "util.logger";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
4 local log = logger.init("mod_websocket");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
5 local httpserver = require "net.httpserver";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
6 local lxp = require "lxp";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
7 local init_xmlhandlers = require "core.xmlhandlers";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
8 local st = require "util.stanza";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
9 local sm = require "core.sessionmanager";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
10
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
11 local sessions = {};
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
12 local default_headers = { };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
13
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
14
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
15 local stream_callbacks = { default_ns = "jabber:client",
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
16 streamopened = sm.streamopened,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
17 streamclosed = sm.streamclosed,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
18 handlestanza = core_process_stanza };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
19 function stream_callbacks.error(session, error, data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
20 if error == "no-stream" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
21 session.log("debug", "Invalid opening stream header");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
22 session:close("invalid-namespace");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
23 elseif session.close then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
24 (session.log or log)("debug", "Client XML parse error: %s", tostring(error));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
25 session:close("xml-not-well-formed");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
26 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
27 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
28
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
29
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
30 local function session_reset_stream(session)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
31 local parser = lxp.new(init_xmlhandlers(session, stream_callbacks), "\1");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
32 session.parser = parser;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
33
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
34 session.notopen = true;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
35
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
36 function session.data(conn, data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
37 data, _ = data:gsub("[%z\255]", "")
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
38 log("debug", "Parsing: %s", data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
39
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
40 local ok, err = parser:parse(data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
41 if not ok then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
42 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
43 data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
44 session:close("xml-not-well-formed");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
45 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
46 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
47 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
48
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
49 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
50 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
51 local function session_close(session, reason)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
52 local log = session.log or log;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
53 if session.conn then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
54 if session.notopen then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
55 session.send("<?xml version='1.0'?>");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
56 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
57 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
58 if reason then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
59 if type(reason) == "string" then -- assume stream error
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
60 log("info", "Disconnecting client, <stream:error> is: %s", reason);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
61 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
62 elseif type(reason) == "table" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
63 if reason.condition then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
64 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
65 if reason.text then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
66 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
67 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
68 if reason.extra then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
69 stanza:add_child(reason.extra);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
70 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
71 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
72 session.send(stanza);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
73 elseif reason.name then -- a stanza
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
74 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
75 session.send(reason);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
76 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
77 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
78 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
79 session.send("</stream:stream>");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
80 session.conn:close();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
81 websocket_listener.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
82 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
83 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
84
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
85
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
86 local websocket_listener = { default_mode = "*a" };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
87 function websocket_listener.onincoming(conn, data)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
88 local session = sessions[conn];
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
89 if not session then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
90 session = { type = "c2s_unauthed",
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
91 conn = conn,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
92 reset_stream = session_reset_stream,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
93 close = session_close,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
94 dispatch_stanza = stream_callbacks.handlestanza,
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
95 log = logger.init("websocket"),
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
96 secure = conn.ssl };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
97
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
98 function session.send(s)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
99 conn:write("\00" .. tostring(s) .. "\255");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
100 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
101
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
102 sessions[conn] = session;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
103 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
104
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
105 session_reset_stream(session);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
106
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
107 if data then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
108 session.data(conn, data);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
109 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
110 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
111
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
112 function websocket_listener.ondisconnect(conn, err)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
113 local session = sessions[conn];
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
114 if session then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
115 (session.log or log)("info", "Client disconnected: %s", err);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
116 sm.destroy_session(session, err);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
117 sessions[conn] = nil;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
118 session = nil;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
119 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
120 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
121
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
122
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
123 function handle_request(method, body, request)
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
124 if request.method ~= "GET" or request.headers["upgrade"] ~= "WebSocket" or request.headers["connection"] ~= "Upgrade" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
125 if request.method == "OPTIONS" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
126 return { headers = default_headers, body = "" };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
127 else
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
128 return "<html><body>You really don't look like a Websocket client to me... what do you want?</body></html>";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
129 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
130 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
131
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
132 local subprotocol = request.headers["Websocket-Protocol"];
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
133 if subprotocol ~= nil and subprotocol ~= "XMPP" then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
134 return "<html><body>You really don't look like an XMPP Websocket client to me... what do you want?</body></html>";
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
135 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
136
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
137 if not method then
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
138 log("debug", "Request %s suffered error %s", tostring(request.id), body);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
139 return;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
140 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
141
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
142 request.conn:setlistener(websocket_listener);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
143 request.write("HTTP/1.1 101 Web Socket Protocol Handshake\r\n");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
144 request.write("Upgrade: WebSocket\r\n");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
145 request.write("Connection: Upgrade\r\n");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
146 request.write("WebSocket-Origin: file://\r\n"); -- FIXME
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
147 request.write("WebSocket-Location: ws://localhost:5281/xmpp-websocket\r\n"); -- FIXME
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
148 request.write("WebSocket-Protocol: XMPP\r\n");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
149 request.write("\r\n");
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
150
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
151 return true;
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
152 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
153
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
154 local function setup()
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
155 local ports = module:get_option("websocket_ports") or { 5281 };
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
156 httpserver.new_from_config(ports, handle_request, { base = "xmpp-websocket" });
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
157 end
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
158 if prosody.start_time then -- already started
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
159 setup();
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
160 else
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
161 prosody.events.add_handler("server-started", setup);
5fc00a3e47b5 mod_websocket: Initial commit
Ali Sabil <ali.sabil@gmail.com>
parents:
diff changeset
162 end