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