Skip to content

Commit

Permalink
Explicitly raise an error when server returns http 403 and dont continue
Browse files Browse the repository at this point in the history
Previously if the server returned a 403, eventsource would begin a retry
loop to connect to the server over and over. This loop will start before
a client can attach to the 'error' event because eventsource connects
right away in the constructor. This prevents client code from closing
the eventsource connection. This commit addresses this issue and fails
fast instead of allowing eventsource to enter a retry loop when a server
returns a 403.
  • Loading branch information
Scott Moak committed Feb 5, 2013
1 parent ebdd9bd commit 7e49554
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ function EventSource(url) {

if (res.statusCode == 204) return self.close();

if (res.statusCode == 403) {
_emit('error', 'Access denied');
return self.close();
}

readyState = EventSource.OPEN;
res.on('close', onConnectionClosed);
res.on('end', onConnectionClosed);
Expand Down
18 changes: 18 additions & 0 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,24 @@ exports['HTTP Request'] = {
});
},

'http 403 causes error event': function(test) {
var headers;
var url = 'http://localhost:' + port;
createServer(["id: 1\ndata: hello world\n\n"],
function(close) {
var es = new EventSource(url);
es.onerror = function() {
test.ok(true, 'got error');
es.close();
close(test.done);
};
},
function(req, res) {
res.writeHead(403, {'Content-Type': 'text/html'});
res.end();
});
},

'http 301 with missing location causes error event': function(test) {
var headers;
var url = 'http://localhost:' + port;
Expand Down

0 comments on commit 7e49554

Please sign in to comment.