/
helper.js
232 lines (207 loc) · 7.49 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// -*- tab-width:4 -*-
/*
* Copyright (c) 2011 Dhruv Matani, Anup Kalbalia
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
"use strict";
var url = require('url');
var ltx = require('ltx');
var dutil = require('./dutil.js');
var us = require('underscore');
var path = require('path');
var toNumber = us.toNumber;
var BOSH_XMLNS = 'http://jabber.org/protocol/httpbind';
var filename = "[" + path.basename(path.normalize(__filename)) + "]";
var log = require('./log.js').getLogger(filename);
// Begin packet builders
function $body(attrs) {
attrs = attrs || { };
var _attrs = {
xmlns: BOSH_XMLNS
};
dutil.extend(_attrs, attrs);
return new ltx.Element('body', _attrs);
}
function $terminate(attrs) {
attrs = attrs || { };
attrs.type = 'terminate';
return $body(attrs);
}
// End packet builders
// Begin HTTP header helpers
function add_to_headers(dest, src) {
var acah = dest['Access-Control-Allow-Headers'].split(', ');
var k;
for (k in src) {
if (src.hasOwnProperty(k)) {
dest[k] = src[k];
acah.push(k);
}
}
dest['Access-Control-Allow-Headers'] = acah.join(', ');
}
function JSONPResponseProxy(req, res) {
this.req_ = req;
this.res_ = res;
this.wrote_ = false;
this.has_content_length_header_ = false;
this.write_buffer_ = [ ];
this.headers_ = { };
this.status_code_ = 200;
var _url = url.parse(req.url, true);
this.jsonp_cb_ = _url.query.callback || '';
// console.log("DATA:", _url.query.data);
// console.log("JSONP CB:", this.jsonp_cb_);
// The proxy is used only if this is a JSONP response
if (!this.jsonp_cb_) {
return res;
}
}
JSONPResponseProxy.prototype = {
on: function () {
return this.res_.on.apply(this.res_, arguments);
},
writeHead: function (status_code, headers) {
dutil.copy(this.headers_, headers);
this.status_code_ = status_code;
this.headers_['Content-Type'] = 'application/json; charset=utf-8';
},
write: function (data) {
if (!this.wrote_) {
this.write_buffer_.push(this.jsonp_cb_ + '({"reply":"');
this.wrote_ = true;
}
data = data || '';
data = data.replace(/\n/g, '\\n').replace(/"/g, '\\"');
return this.write_buffer_.push(data);
},
end: function (data) {
this.write(data);
this.write('"});');
if (this.has_content_length_header_) {
var content_length = this.write_buffer_.map(function(entry) {
return entry.length;
}).reduce(function(prev, curr, index, array) {
return prev + curr;
});
this.headers_['Content-Length'] = content_length;
}
this.res_.writeHead(this.status_code_, this.headers_);
var data_to_write = this.write_buffer_.join('');
this.write_buffer_ = null;
return this.res_.end(data_to_write);
},
setHeader: function(name, value) {
if (name.toLowerCase() == 'content-length') {
this.has_content_length_header_ = true;
} else {
return this.res_.setHeader(name, value);
}
}
};
// End HTTP header helpers
// Begin misc. helpers
function route_parse(route) {
/* Parse the 'route' attribute, which is expected to be of the
* form: xmpp:domain:port.
*
* Returns null or a hash of the form:
* { protocol: <PROTOCOL>, host: <HOST NAME>, port: <PORT> }
*
* TODO: Move this out of bosh.js and into lookup_service.js
*/
var m = route.match(/^(\S+):(\S+):([0-9]+)$/) || [ ];
log.trace("route_parse: %s", m);
if (m && m.length === 4) {
return {protocol: m[1], host: m[2], port: toNumber(m[3])};
} else {
return null;
}
}
function save_terminate_condition_for_wait_time(obj, attr, condition, wait) {
obj[attr] = {
condition: condition,
timer: setTimeout(function () {
if (obj.hasOwnProperty(attr)) {
delete obj[attr];
}
}, (wait + 5) * 1000)
};
}
function get_stream_name(node) {
return node.attrs.stream;
}
// Sanitize all attributes in node.attr that the BOSH server cares
// about.
function sanitize_request_node(node) {
// TODO: Implement
if (node.attrs.rid) {
node.attrs.rid = toNumber(node.attrs.rid);
}
if (node.attrs.ack) {
node.attrs.ack = toNumber(node.attrs.ack);
}
return node;
}
// Coded according to the rules mentioned here:
// http://xmpp.org/extensions/xep-0206.html#create and
// http://xmpp.org/extensions/xep-0206.html#preconditions-sasl
function is_stream_restart_packet(node) {
var ia = dutil.inflated_attrs(node);
return ia['urn:xmpp:xbosh:restart'] === 'true';
}
// Coded according to the rules mentioned here:
// http://xmpp.org/extensions/xep-0124.html#multi-add
function is_stream_add_request(node, options) {
if (options.PIDGIN_COMPATIBLE) {
return false;
}
return node.attrs.to && node.attrs.sid && node.attrs.rid &&
!node.attrs.ver && !node.attrs.hold && !node.attrs.wait;
}
// Coded according to the rules mentioned here:
// http://xmpp.org/extensions/xep-0124.html#terminate
function is_stream_terminate_request(node) {
return node.attrs.sid && node.attrs.rid && node.attrs.type === 'terminate';
}
// Coded according to the rules mentioned here:
// http://xmpp.org/extensions/xep-0124.html#session-request
// Even though it says SHOULD for everything we expect, we violate the XEP.
function is_session_creation_packet(node) {
var ia = dutil.inflated_attrs(node);
return (node.attrs.rid &&
node.attrs.to && node.attrs.wait &&
node.attrs.hold && !node.attrs.sid &&
ia.hasOwnProperty('urn:xmpp:xbosh:version'));
}
// End misc. helpers
exports.add_to_headers = add_to_headers;
exports.JSONPResponseProxy = JSONPResponseProxy;
exports.route_parse = route_parse;
exports.save_terminate_condition_for_wait_time = save_terminate_condition_for_wait_time;
exports.$terminate = $terminate;
exports.$body = $body;
exports.get_stream_name = get_stream_name;
exports.is_stream_restart_packet = is_stream_restart_packet;
exports.is_stream_add_request = is_stream_add_request;
exports.is_stream_terminate_request = is_stream_terminate_request;
exports.is_session_creation_packet = is_session_creation_packet;
exports.sanitize_request_node = sanitize_request_node;