This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BOSHHttp.js
139 lines (121 loc) · 3.68 KB
/
BOSHHttp.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
'use strict';
var EventEmitter = require('events').EventEmitter
, util = require('util')
, ltx = require('ltx')
, BOSHServerSession = require('./BOSHServerSession')
, Connection = require('node-xmpp-core').Connection
var winston = require('winston'),
logger = winston.loggers.get('bosh');
var NS_HTTPBIND = 'http://jabber.org/protocol/httpbind'
function parseBody(stream, cb) {
var parser = new ltx.Parser()
stream.on('data', function(data) {
parser.write(data)
})
stream.on('end', function() {
parser.end()
})
stream.on('error', function(e) {
cb(e)
})
parser.on('tree', function(bodyEl) {
cb(null, bodyEl)
})
parser.on('error', function(e) {
cb(e)
})
}
/**
* Sessions are stored *in memory!* You wouldn't want that for very
* large setups.
*
* This implementation is based on
* https://github.com/node-xmpp/node-xmpp-server/blob/master/lib/bosh/bosh_server.js
*
* License: MIT
*/
function BOSHServer(options) {
this.options = options || {};
// set default cors properties
if (!this.options.cors) {
this.options.cors = {
origin : '*'
};
}
this.sessions = {}
}
util.inherits(BOSHServer, EventEmitter)
BOSHServer.prototype.setCorsHeader= function(res, options) {
res.setHeader("Access-Control-Allow-Origin", options.origin)
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS')
res.setHeader("Access-Control-Allow-Headers", "Authorization, X-Requested-With, Content-Type, Content-Length")
res.setHeader('Access-Control-Allow-Credentials', true)
}
/**
* *YOU* need to check the path before passing to this function.
*/
BOSHServer.prototype.handleHTTP = function(req, res) {
this.setCorsHeader(res, this.options.cors );
if (req.method === 'POST') {
this._handlePostRequest(req, res)
//} else if (false && req.method === 'PROPFIND') {
/* TODO: CORS preflight request */
} else {
res.writeHead(400)
res.end()
}
}
BOSHServer.prototype._handlePostRequest = function(req, res) {
var self = this
parseBody(req, function(error, bodyEl) {
if (error ||
!bodyEl || !bodyEl.attrs || !bodyEl.is ||
!bodyEl.is('body', NS_HTTPBIND)
) {
self._sendErrorResponse(res, error)
}
if (bodyEl.attrs.sid) {
logger.debug('send to session: ' + bodyEl.root().toString())
return self._useExistingSession(req, res, bodyEl)
}
self._createSession(req, res, bodyEl)
})
}
BOSHServer.prototype._sendErrorResponse = function(res, error) {
res.writeHead(400, { 'Content-Type': 'text/plain' })
res.end(error.message || error.stack || 'Error')
}
BOSHServer.prototype._useExistingSession = function(req, res, bodyEl) {
var session = this.sessions[bodyEl.attrs.sid]
if (session) {
session.handleHTTP(
{ req: req, res: res, bodyEl: bodyEl }
)
} else {
res.writeHead(
404, { 'Content-Type': 'text/plain' }
)
res.end('BOSH session not found')
}
}
BOSHServer.prototype._createSession = function(req, res, bodyEl) {
/* No sid: create session */
var self = this
var session
do {
session = new BOSHServerSession(
{ req: req, res: res, bodyEl: bodyEl }
)
} while (this.sessions.hasOwnProperty(session.sid))
this.sessions[session.sid] = session
/* Hook for destruction */
session.on('close', function() {
delete self.sessions[session.sid]
})
// emit new connection
var conn = new Connection({
socket: session
})
this.emit('connect', conn)
}
module.exports = BOSHServer