Skip to content

Commit

Permalink
Merge pull request #168 from sockjs/cors-eventsource
Browse files Browse the repository at this point in the history
Add CORS headers to eventsource
  • Loading branch information
brycekahle committed Oct 22, 2014
2 parents b1d86b1 + d3b6c06 commit 259f0eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/trans-eventsource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ class EventSourceReceiver extends transport.ResponseReceiver

exports.app =
eventsource: (req, res) ->
res.setHeader('Content-Type', 'text/event-stream; charset=UTF-8')
if !req.headers['origin'] or req.headers['origin'] is 'null'
origin = '*'
else
origin = req.headers['origin']
res.setHeader('Access-Control-Allow-Credentials', 'true')
res.setHeader('Content-Type', 'text/event-stream')
res.setHeader('Access-Control-Allow-Origin', origin)
res.setHeader('Vary', 'Origin')
headers = req.headers['access-control-request-headers']
if headers
res.setHeader('Access-Control-Allow-Headers', headers)

res.writeHead(200)
# Opera needs one more new line at the start.
res.write('\r\n')
Expand Down
2 changes: 1 addition & 1 deletion src/trans-xhr.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ exports.app =
origin = '*'
else
origin = req.headers['origin']
res.setHeader('Access-Control-Allow-Credentials', 'true')
res.setHeader('Access-Control-Allow-Origin', origin)
res.setHeader('Vary', 'Origin')
headers = req.headers['access-control-request-headers']
if headers
res.setHeader('Access-Control-Allow-Headers', headers)
res.setHeader('Access-Control-Allow-Credentials', 'true')
return content

xhr_poll: (req, res, _, next_filter) ->
Expand Down

0 comments on commit 259f0eb

Please sign in to comment.