-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathserver.coffee
717 lines (556 loc) · 21.6 KB
/
server.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
_ = require("lodash")
url = require("url")
http = require("http")
concatStream = require("concat-stream")
stream = require("stream")
express = require("express")
Promise = require("bluebird")
evilDns = require("evil-dns")
isHtml = require("is-html")
httpProxy = require("http-proxy")
la = require("lazy-ass")
check = require("check-more-types")
httpsProxy = require("@packages/https-proxy")
compression = require("compression")
debug = require("debug")("cypress:server:server")
agent = require("@packages/network").agent
cors = require("./util/cors")
uri = require("./util/uri")
origin = require("./util/origin")
ensureUrl = require("./util/ensure-url")
appData = require("./util/app_data")
buffers = require("./util/buffers")
blacklist = require("./util/blacklist")
statusCode = require("./util/status_code")
headersUtil = require("./util/headers")
allowDestroy = require("./util/server_destroy")
cwd = require("./cwd")
errors = require("./errors")
logger = require("./logger")
Socket = require("./socket")
Request = require("./request")
fileServer = require("./file_server")
XhrServer = require("./xhr_ws_server")
templateEngine = require("./template_engine")
DEFAULT_DOMAIN_NAME = "localhost"
fullyQualifiedRe = /^https?:\/\//
isResponseHtml = (contentType, responseBuffer) ->
if contentType
return contentType is "text/html"
if body = _.invoke(responseBuffer, 'toString')
return isHtml(body)
return false
setProxiedUrl = (req) ->
## bail if we've already proxied the url
return if req.proxiedUrl
## backup the original proxied url
## and slice out the host/origin
## and only leave the path which is
## how browsers would normally send
## use their url
req.proxiedUrl = uri.removeDefaultPort(req.url).format()
req.url = uri.getPath(req.url)
notSSE = (req, res) ->
req.headers.accept isnt "text/event-stream" and compression.filter(req, res)
## currently not making use of event emitter
## but may do so soon
class Server
constructor: ->
if not (@ instanceof Server)
return new Server()
@_request = null
@_middleware = null
@_server = null
@_socket = null
@_baseUrl = null
@_nodeProxy = null
@_fileServer = null
@_httpsProxy = null
@_urlResolver = null
createExpressApp: (morgan) ->
app = express()
## set the cypress config from the cypress.json file
app.set("view engine", "html")
## since we use absolute paths, configure express-handlebars to not automatically find layouts
## https://github.com/cypress-io/cypress/issues/2891
app.engine("html", templateEngine.render)
## handle the proxied url in case
## we have not yet started our websocket server
app.use (req, res, next) =>
setProxiedUrl(req)
## if we've defined some middlware
## then call this. useful in tests
if m = @_middleware
m(req, res)
## always continue on
next()
app.use require("cookie-parser")()
app.use compression({filter: notSSE})
app.use require("morgan")("dev") if morgan
## errorhandler
app.use require("errorhandler")()
## remove the express powered-by header
app.disable("x-powered-by")
return app
createRoutes: ->
require("./routes").apply(null, arguments)
getHttpServer: -> @_server
portInUseErr: (port) ->
e = errors.get("PORT_IN_USE_SHORT", port)
e.port = port
e.portInUse = true
e
open: (config = {}, project, onWarning) ->
debug("server open")
la(_.isPlainObject(config), "expected plain config object", config)
Promise.try =>
## always reset any buffers
## TODO: change buffers to be an instance
## here and pass this dependency around
buffers.reset()
app = @createExpressApp(config.morgan)
logger.setSettings(config)
## generate our request instance
## and set the responseTimeout
@_request = Request({timeout: config.responseTimeout})
@_nodeProxy = httpProxy.createProxyServer()
@_xhrServer = XhrServer.create()
getRemoteState = => @_getRemoteState()
@createHosts(config.hosts)
@createRoutes(app, config, @_request, getRemoteState, @_xhrServer.getDeferredResponse, project, @_nodeProxy)
@createServer(app, config, project, @_request, onWarning)
createHosts: (hosts = {}) ->
_.each hosts, (ip, host) ->
evilDns.add(host, ip)
createServer: (app, config, project, request, onWarning) ->
new Promise (resolve, reject) =>
{port, fileServerFolder, socketIoRoute, baseUrl, blacklistHosts} = config
@_server = http.createServer(app)
allowDestroy(@_server)
onError = (err) =>
## if the server bombs before starting
## and the err no is EADDRINUSE
## then we know to display the custom err message
if err.code is "EADDRINUSE"
reject @portInUseErr(port)
onUpgrade = (req, socket, head) =>
debug("Got UPGRADE request from %s", req.url)
@proxyWebsockets(@_nodeProxy, socketIoRoute, req, socket, head)
callListeners = (req, res) =>
listeners = @_server.listeners("request").slice(0)
@_callRequestListeners(@_server, listeners, req, res)
onSniUpgrade = (req, socket, head) =>
upgrades = @_server.listeners("upgrade").slice(0)
for upgrade in upgrades
upgrade.call(@_server, req, socket, head)
@_server.on "connect", (req, socket, head) =>
debug("Got CONNECT request from %s", req.url)
@_httpsProxy.connect(req, socket, head, {
onDirectConnection: (req) =>
urlToCheck = "https://" + req.url
isMatching = cors.urlMatchesOriginPolicyProps(urlToCheck, @_remoteProps)
word = if isMatching then "does" else "does not"
debug("HTTPS request #{word} match URL: #{urlToCheck} with props: %o", @_remoteProps)
## if we are currently matching then we're
## not making a direct connection anyway
## so we only need to check this if we
## have blacklist hosts and are not matching.
##
## if we have blacklisted hosts lets
## see if this matches - if so then
## we cannot allow it to make a direct
## connection
if blacklistHosts and not isMatching
isMatching = blacklist.matches(urlToCheck, blacklistHosts)
debug("HTTPS request #{urlToCheck} matches blacklist?", isMatching)
## make a direct connection only if
## our req url does not match the origin policy
## which is the superDomain + port
return not isMatching
})
@_server.on "upgrade", onUpgrade
@_server.once "error", onError
@_listen(port, onError)
.then (port) =>
Promise.all([
httpsProxy.create(appData.path("proxy"), port, {
onRequest: callListeners
onUpgrade: onSniUpgrade
}),
fileServer.create(fileServerFolder)
])
.spread (httpsProxy, fileServer) =>
@_httpsProxy = httpsProxy
@_fileServer = fileServer
## if we have a baseUrl let's go ahead
## and make sure the server is connectable!
if baseUrl
@_baseUrl = baseUrl
if config.isTextTerminal
return @_retryBaseUrlCheck(baseUrl, onWarning)
.return(null)
.catch (e) ->
debug(e)
reject(errors.get("CANNOT_CONNECT_BASE_URL", baseUrl))
ensureUrl.isListening(baseUrl)
.return(null)
.catch (err) ->
errors.get("CANNOT_CONNECT_BASE_URL_WARNING", baseUrl)
.then (warning) =>
## once we open set the domain
## to root by default
## which prevents a situation where navigating
## to http sites redirects to /__/ cypress
@_onDomainSet(baseUrl ? "<root>")
resolve([port, warning])
_port: ->
_.chain(@_server).invoke("address").get("port").value()
_listen: (port, onError) ->
new Promise (resolve) =>
listener = =>
address = @_server.address()
@isListening = true
debug("Server listening on ", address)
@_server.removeListener "error", onError
resolve(address.port)
@_server.listen(port || 0, '127.0.0.1', listener)
_getRemoteState: ->
# {
# origin: "http://localhost:2020"
# fileServer:
# strategy: "file"
# domainName: "localhost"
# props: null
# }
# {
# origin: "https://foo.google.com"
# strategy: "http"
# domainName: "google.com"
# props: {
# port: 443
# tld: "com"
# domain: "google"
# }
# }
props = _.extend({}, {
auth: @_remoteAuth
props: @_remoteProps
origin: @_remoteOrigin
strategy: @_remoteStrategy
visiting: @_remoteVisitingUrl
domainName: @_remoteDomainName
fileServer: @_remoteFileServer
})
debug("Getting remote state: %o", props)
return props
_onRequest: (headers, automationRequest, options) ->
@_request.sendPromise(headers, automationRequest, options)
_onResolveUrl: (urlStr, headers, automationRequest, options = {}) ->
debug("resolving visit %o", {
url: urlStr
headers
options
})
startTime = new Date()
## if we have an existing url resolver
## in flight then cancel it
if @_urlResolver
@_urlResolver.cancel()
request = @_request
handlingLocalFile = false
previousState = _.clone @_getRemoteState()
## nuke any hashes from our url since
## those those are client only and do
## not apply to http requests
urlStr = url.parse(urlStr)
urlStr.hash = null
urlStr = urlStr.format()
originalUrl = urlStr
reqStream = null
currentPromisePhase = null
runPhase = (fn) ->
return currentPromisePhase = fn()
return @_urlResolver = p = new Promise (resolve, reject, onCancel) =>
onCancel ->
p.currentPromisePhase = currentPromisePhase
p.reqStream = reqStream
_.invoke(reqStream, "abort")
_.invoke(currentPromisePhase, "cancel")
## if we have a buffer for this url
## then just respond with its details
## so we are idempotant and do not make
## another request
if obj = buffers.getByOriginalUrl(urlStr)
debug("got previous request buffer for url:", urlStr)
## reset the cookies from the buffer on the browser
return runPhase ->
resolve(
Promise.map obj.details.cookies, (cookie) ->
## prevent prepending a . to the cookie domain if top-level
## navigation occurs as a result of a cy.visit
if _.isUndefined(cookie.hostOnly) && !cookie.domain?.startsWith('.')
cookie.hostOnly = true
automationRequest('set:cookie', cookie)
.return(obj.details)
)
redirects = []
newUrl = null
if not fullyQualifiedRe.test(urlStr)
handlingLocalFile = true
@_remoteVisitingUrl = true
@_onDomainSet(urlStr, options)
## TODO: instead of joining remoteOrigin here
## we can simply join our fileServer origin
## and bypass all the remoteState.visiting shit
urlFile = url.resolve(@_remoteFileServer, urlStr)
urlStr = url.resolve(@_remoteOrigin, urlStr)
onReqError = (err) =>
## only restore the previous state
## if our promise is still pending
if p.isPending()
restorePreviousState()
reject(err)
onReqStreamReady = (str) =>
reqStream = str
str
.on("error", onReqError)
.on "response", (incomingRes) =>
debug(
"resolve:url headers received, buffering response %o",
_.pick(incomingRes, "headers", "statusCode")
)
newUrl ?= urlStr
runPhase =>
## get the cookies that would be sent with this request so they can be rehydrated
automationRequest("get:cookies", {
domain: cors.getSuperDomain(newUrl)
})
.then (cookies) =>
@_remoteVisitingUrl = false
statusIs2xxOrAllowedFailure = ->
## is our status code in the 2xx range, or have we disabled failing
## on status code?
statusCode.isOk(incomingRes.statusCode) or (options.failOnStatusCode is false)
isOk = statusIs2xxOrAllowedFailure()
contentType = headersUtil.getContentType(incomingRes)
details = {
isOkStatusCode: isOk
contentType
url: newUrl
status: incomingRes.statusCode
cookies
statusText: statusCode.getText(incomingRes.statusCode)
redirects
originalUrl
}
## does this response have this cypress header?
if fp = incomingRes.headers["x-cypress-file-path"]
## if so we know this is a local file request
details.filePath = fp
debug("setting details resolving url %o", details)
concatStr = concatStream (responseBuffer) =>
## buffer the entire response before resolving.
## this allows us to detect & reject ETIMEDOUT errors
## where the headers have been sent but the
## connection hangs before receiving a body.
if !_.get(responseBuffer, 'length')
## concatStream can yield an empty array, which is
## not a valid chunk
responseBuffer = undefined
## if there is not a content-type, try to determine
## if the response content is HTML-like
## https://github.com/cypress-io/cypress/issues/1727
details.isHtml = isResponseHtml(contentType, responseBuffer)
debug("resolve:url response ended, setting buffer %o", { newUrl, details })
details.totalTime = new Date() - startTime
## TODO: think about moving this logic back into the
## frontend so that the driver can be in control of
## when the server should cache the request buffer
## and set the domain vs not
if isOk and details.isHtml
## reset the domain to the new url if we're not
## handling a local file
@_onDomainSet(newUrl, options) if not handlingLocalFile
responseBufferStream = new stream.PassThrough({
highWaterMark: Number.MAX_SAFE_INTEGER
})
responseBufferStream.end(responseBuffer)
buffers.set({
url: newUrl
stream: responseBufferStream
details
originalUrl: originalUrl
response: incomingRes
})
else
## TODO: move this logic to the driver too for
## the same reasons listed above
restorePreviousState()
resolve(details)
str.pipe(concatStr)
.catch(onReqError)
restorePreviousState = =>
@_remoteAuth = previousState.auth
@_remoteProps = previousState.props
@_remoteOrigin = previousState.origin
@_remoteStrategy = previousState.strategy
@_remoteFileServer = previousState.fileServer
@_remoteDomainName = previousState.domainName
@_remoteVisitingUrl = previousState.visiting
# if they're POSTing an object, querystringify their POST body
if options.method == 'POST' and _.isObject(options.body)
options.form = options.body
delete options.body
_.assign(options, {
## turn off gzip since we need to eventually
## rewrite these contents
gzip: false
url: urlFile ? urlStr
headers: _.assign({
accept: "text/html,*/*"
}, options.headers)
onBeforeReqInit: runPhase
followRedirect: (incomingRes) ->
status = incomingRes.statusCode
next = incomingRes.headers.location
curr = newUrl ? urlStr
newUrl = url.resolve(curr, next)
redirects.push([status, newUrl].join(": "))
return true
})
debug('sending request with options %o', options)
runPhase ->
request.sendStream(headers, automationRequest, options)
.then (createReqStream) ->
onReqStreamReady(createReqStream())
.catch(onReqError)
_onDomainSet: (fullyQualifiedUrl, options = {}) ->
l = (type, val) ->
debug("Setting", type, val)
@_remoteAuth = options.auth
l("remoteAuth", @_remoteAuth)
## if this isn't a fully qualified url
## or if this came to us as <root> in our tests
## then we know to go back to our default domain
## which is the localhost server
if fullyQualifiedUrl is "<root>" or not fullyQualifiedRe.test(fullyQualifiedUrl)
@_remoteOrigin = "http://#{DEFAULT_DOMAIN_NAME}:#{@_port()}"
@_remoteStrategy = "file"
@_remoteFileServer = "http://#{DEFAULT_DOMAIN_NAME}:#{@_fileServer?.port()}"
@_remoteDomainName = DEFAULT_DOMAIN_NAME
@_remoteProps = null
l("remoteOrigin", @_remoteOrigin)
l("remoteStrategy", @_remoteStrategy)
l("remoteHostAndPort", @_remoteProps)
l("remoteDocDomain", @_remoteDomainName)
l("remoteFileServer", @_remoteFileServer)
else
@_remoteOrigin = origin(fullyQualifiedUrl)
@_remoteStrategy = "http"
@_remoteFileServer = null
## set an object with port, tld, and domain properties
## as the remoteHostAndPort
@_remoteProps = cors.parseUrlIntoDomainTldPort(@_remoteOrigin)
@_remoteDomainName = _.compact([@_remoteProps.domain, @_remoteProps.tld]).join(".")
l("remoteOrigin", @_remoteOrigin)
l("remoteHostAndPort", @_remoteProps)
l("remoteDocDomain", @_remoteDomainName)
return @_getRemoteState()
_callRequestListeners: (server, listeners, req, res) ->
for listener in listeners
listener.call(server, req, res)
_normalizeReqUrl: (server) ->
## because socket.io removes all of our request
## events, it forces the socket.io traffic to be
## handled first.
## however we need to basically do the same thing
## it does and after we call into socket.io go
## through and remove all request listeners
## and change the req.url by slicing out the host
## because the browser is in proxy mode
listeners = server.listeners("request").slice(0)
server.removeAllListeners("request")
server.on "request", (req, res) =>
setProxiedUrl(req)
@_callRequestListeners(server, listeners, req, res)
_retryBaseUrlCheck: (baseUrl, onWarning) ->
ensureUrl.retryIsListening(baseUrl, {
retryIntervals: [3000, 3000, 4000],
onRetry: ({ attempt, delay, remaining }) ->
warning = errors.get("CANNOT_CONNECT_BASE_URL_RETRYING", {
remaining
attempt
delay
baseUrl
})
onWarning(warning)
})
proxyWebsockets: (proxy, socketIoRoute, req, socket, head) ->
## bail if this is our own namespaced socket.io request
return if req.url.startsWith(socketIoRoute)
if (host = req.headers.host) and @_remoteProps and (remoteOrigin = @_remoteOrigin)
## get the port from @_remoteProps
## get the protocol from remoteOrigin
## get the hostname from host header
{port} = @_remoteProps
{protocol} = url.parse(remoteOrigin)
{hostname} = url.parse("http://#{host}")
onProxyErr = (err, req, res) ->
debug("Got ERROR proxying websocket connection", { err, port, protocol, hostname, req })
proxy.ws(req, socket, head, {
secure: false
target: {
host: hostname
port: port
protocol: protocol
}
agent: agent
}, onProxyErr)
else
## we can't do anything with this socket
## since we don't know how to proxy it!
socket.end() if socket.writable
reset: ->
buffers.reset()
@_onDomainSet(@_baseUrl ? "<root>")
_close: ->
@reset()
logger.unsetSettings()
evilDns.clear()
## bail early we dont have a server or we're not
## currently listening
return Promise.resolve() if not @_server or not @isListening
@_server.destroyAsync()
.then =>
@isListening = false
close: ->
Promise.join(
@_close()
@_socket?.close()
@_fileServer?.close()
@_httpsProxy?.close()
)
.then =>
## reset any middleware
@_middleware = null
end: ->
@_socket and @_socket.end()
changeToUrl: (url) ->
@_socket and @_socket.changeToUrl(url)
onTestFileChange: (filePath) ->
@_socket and @_socket.onTestFileChange(filePath)
onRequest: (fn) ->
@_middleware = fn
onNextRequest: (fn) ->
@onRequest =>
fn.apply(@, arguments)
@_middleware = null
startWebsockets: (automation, config, options = {}) ->
options.onResolveUrl = @_onResolveUrl.bind(@)
options.onRequest = @_onRequest.bind(@)
options.onIncomingXhr = @_xhrServer.onIncomingXhr
options.onResetXhrServer = @_xhrServer.reset
@_socket = Socket(config)
@_socket.startListening(@_server, automation, config, options)
@_normalizeReqUrl(@_server)
module.exports = Server