-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
sockjs.coffee
174 lines (149 loc) · 6.75 KB
/
sockjs.coffee
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
# ***** BEGIN LICENSE BLOCK *****
# Copyright (c) 2011-2012 VMware, Inc.
#
# For the license see COPYING.
# ***** END LICENSE BLOCK *****
events = require('events')
fs = require('fs')
webjs = require('./webjs')
utils = require('./utils')
trans_websocket = require('./trans-websocket')
trans_jsonp = require('./trans-jsonp')
trans_xhr = require('./trans-xhr')
iframe = require('./iframe')
trans_eventsource = require('./trans-eventsource')
trans_htmlfile = require('./trans-htmlfile')
chunking_test = require('./chunking-test')
sockjsVersion = ->
try
pkg = fs.readFileSync(__dirname + '/../package.json', 'utf-8')
catch x
return if pkg then JSON.parse(pkg).version else null
class App extends webjs.GenericApp
welcome_screen: (req, res) ->
res.setHeader('content-type', 'text/plain; charset=UTF-8')
res.writeHead(200)
res.end("Welcome to SockJS!\n")
return true
handle_404: (req, res) ->
res.setHeader('content-type', 'text/plain; charset=UTF-8')
res.writeHead(404)
res.end('404 Error: Page not found\n')
return true
disabled_transport: (req, res, data) ->
return @handle_404(req, res, data)
h_sid: (req, res, data) ->
# Some load balancers do sticky sessions, but only if there is
# a JSESSIONID cookie. If this cookie isn't yet set, we shall
# set it to a dummy value. It doesn't really matter what, as
# session information is usually added by the load balancer.
req.cookies = utils.parseCookie(req.headers.cookie)
if typeof @options.jsessionid is 'function'
# Users can supply a function
@options.jsessionid(req, res)
else if (@options.jsessionid and res.setHeader)
# We need to set it every time, to give the loadbalancer
# opportunity to attach its own cookies.
jsid = req.cookies['JSESSIONID'] or 'dummy'
res.setHeader('Set-Cookie', 'JSESSIONID=' + jsid + '; path=/')
return data
log: (severity, line) ->
@options.log(severity, line)
utils.objectExtend(App.prototype, iframe.app)
utils.objectExtend(App.prototype, chunking_test.app)
utils.objectExtend(App.prototype, trans_websocket.app)
utils.objectExtend(App.prototype, trans_jsonp.app)
utils.objectExtend(App.prototype, trans_xhr.app)
utils.objectExtend(App.prototype, trans_eventsource.app)
utils.objectExtend(App.prototype, trans_htmlfile.app)
generate_dispatcher = (options) ->
p = (s) => new RegExp('^' + options.prefix + s + '[/]?$')
t = (s) => [p('/([^/.]+)/([^/.]+)' + s), 'server', 'session']
opts_filters = (options_filter='xhr_options') ->
return ['h_sid', 'xhr_cors', 'cache_for', options_filter, 'expose']
prefix_dispatcher = [
['GET', p(''), ['welcome_screen']],
['GET', p('/iframe[0-9-.a-z_]*.html'), ['iframe', 'cache_for', 'expose']],
['OPTIONS', p('/info'), opts_filters('info_options')],
['GET', p('/info'), ['xhr_cors', 'h_no_cache', 'info', 'expose']],
['OPTIONS', p('/chunking_test'), opts_filters()],
['POST', p('/chunking_test'), ['xhr_cors', 'expect_xhr', 'chunking_test']]
]
transport_dispatcher = [
['GET', t('/jsonp'), ['h_sid', 'h_no_cache', 'jsonp']],
['POST', t('/jsonp_send'), ['h_sid', 'h_no_cache', 'expect_form', 'jsonp_send']],
['POST', t('/xhr'), ['h_sid', 'h_no_cache', 'xhr_cors', 'xhr_poll']],
['OPTIONS', t('/xhr'), opts_filters()],
['POST', t('/xhr_send'), ['h_sid', 'h_no_cache', 'xhr_cors', 'expect_xhr', 'xhr_send']],
['OPTIONS', t('/xhr_send'), opts_filters()],
['POST', t('/xhr_streaming'), ['h_sid', 'h_no_cache', 'xhr_cors', 'xhr_streaming']],
['OPTIONS', t('/xhr_streaming'), opts_filters()],
['GET', t('/eventsource'), ['h_sid', 'h_no_cache', 'eventsource']],
['GET', t('/htmlfile'), ['h_sid', 'h_no_cache', 'htmlfile']],
]
# TODO: remove this code on next major release
if options.websocket
prefix_dispatcher.push(
['GET', p('/websocket'), ['raw_websocket']])
transport_dispatcher.push(
['GET', t('/websocket'), ['sockjs_websocket']])
else
# modify urls to return 404
prefix_dispatcher.push(
['GET', p('/websocket'), ['cache_for', 'disabled_transport']])
transport_dispatcher.push(
['GET', t('/websocket'), ['cache_for', 'disabled_transport']])
return prefix_dispatcher.concat(transport_dispatcher)
class Listener
constructor: (@options, emit) ->
@app = new App()
@app.options = @options
@app.emit = emit
@app.log('debug', 'SockJS v' + sockjsVersion() + ' ' +
'bound to ' + JSON.stringify(@options.prefix))
@dispatcher = generate_dispatcher(@options)
@webjs_handler = webjs.generateHandler(@app, @dispatcher)
@path_regexp = new RegExp('^' + @options.prefix + '([/].+|[/]?)$')
handler: (req, res, extra) =>
# All urls that match the prefix must be handled by us.
if not req.url.match(@path_regexp)
return false
@webjs_handler(req, res, extra)
return true
getHandler: () ->
return (a,b,c) => @handler(a,b,c)
class Server extends events.EventEmitter
constructor: (user_options) ->
@options =
prefix: ''
response_limit: 128*1024
websocket: true
faye_server_options: null
jsessionid: false
heartbeat_delay: 25000
disconnect_delay: 5000
log: (severity, line) -> console.log(line)
sockjs_url: 'https://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js'
if user_options
utils.objectExtend(@options, user_options)
listener: (handler_options) ->
options = utils.objectExtend({}, @options)
if handler_options
utils.objectExtend(options, handler_options)
return new Listener(options, => @emit.apply(@, arguments))
installHandlers: (http_server, handler_options) ->
handler = @listener(handler_options).getHandler()
utils.overshadowListeners(http_server, 'request', handler)
utils.overshadowListeners(http_server, 'upgrade', handler)
return true
middleware: (handler_options) ->
handler = @listener(handler_options).getHandler()
handler.upgrade = handler
return handler
exports.createServer = (options) ->
return new Server(options)
exports.listen = (http_server, options) ->
srv = exports.createServer(options)
if http_server
srv.installHandlers(http_server)
return srv