Skip to content

Commit 7db146d

Browse files
committedSep 20, 2011
Merge pull request #457 from 3rd-Eden/ACAO
access-control-allow-origin
2 parents 6182dff + 6df152c commit 7db146d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed
 

‎lib/manager.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -688,14 +688,18 @@ Manager.prototype.generateId = function () {
688688
*/
689689

690690
Manager.prototype.handleHandshake = function (data, req, res) {
691-
var self = this;
691+
var self = this
692+
, origin = req.headers.origin
693+
, headers = {
694+
'Content-Type': 'text/plain'
695+
};
692696

693697
function writeErr (status, message) {
694698
if (data.query.jsonp) {
695699
res.writeHead(200, { 'Content-Type': 'application/javascript' });
696700
res.end('io.j[' + data.query.jsonp + '](new Error("' + message + '"));');
697701
} else {
698-
res.writeHead(status, { 'Content-Type': 'text/plain' });
702+
res.writeHead(status, headers);
699703
res.end(message);
700704
}
701705
};
@@ -712,6 +716,15 @@ Manager.prototype.handleHandshake = function (data, req, res) {
712716

713717
var handshakeData = this.handshakeData(data);
714718

719+
if (origin) {
720+
// https://developer.mozilla.org/En/HTTP_Access_Control
721+
headers['Access-Control-Allow-Origin'] = '*';
722+
723+
if (req.headers.cookie) {
724+
headers['Access-Control-Allow-Credentials'] = 'true';
725+
}
726+
}
727+
715728
this.authorize(handshakeData, function (err, authorized, newData) {
716729
if (err) return error(err);
717730

@@ -728,7 +741,7 @@ Manager.prototype.handleHandshake = function (data, req, res) {
728741
hs = 'io.j[' + data.query.jsonp + '](' + JSON.stringify(hs) + ');';
729742
res.writeHead(200, { 'Content-Type': 'application/javascript' });
730743
} else {
731-
res.writeHead(200, { 'Content-Type': 'text/plain' });
744+
res.writeHead(200, headers);
732745
}
733746

734747
res.end(hs);

‎test/manager.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,26 @@ module.exports = {
327327
});
328328
},
329329

330+
'test handshake cross domain access control': function (done) {
331+
var port = ++ports
332+
, io = sio.listen(port)
333+
, cl = client(port)
334+
, headers = {
335+
Origin: 'http://example.org:1337'
336+
, Cookie: 'name=value'
337+
};
338+
339+
cl.get('/socket.io/{protocol}/', { headers:headers }, function (res, data) {
340+
res.statusCode.should.eql(200);
341+
res.headers['access-control-allow-origin'].should.eql('*');
342+
res.headers['access-control-allow-credentials'].should.eql('true');
343+
344+
cl.end();
345+
io.server.close();
346+
done();
347+
});
348+
},
349+
330350
'test limiting the supported transports for a manager': function (done) {
331351
var port = ++ports
332352
, io = sio.listen(port)

0 commit comments

Comments
 (0)
Please sign in to comment.