Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/lib/rammerhead/src/util/fixCorsHeader.js
6530 views
1
const urlUtils = require('testcafe-hammerhead/lib/utils/url');
2
const RequestPipelineContext = require('testcafe-hammerhead/lib/request-pipeline/context');
3
4
/**
5
* if a non-crossdomain origin makes a request to a crossdomain port, the ports are flipped. this is to fix that issue.
6
* there is also another issue with https://domain and https://domain:443 not matching. port 443/80 are automatically
7
* removed if https and 443, and http and 80.
8
* original: https://github.com/DevExpress/testcafe-hammerhead/blob/f5b0508d10614bf39a75c772dc6bd01c24f29417/src/request-pipeline/context.ts#L436
9
*/
10
RequestPipelineContext.prototype.getProxyOrigin = function getProxyOrigin(isCrossDomain = false) {
11
// if we receive a request that has a proxy origin header, (ctx.getProxyOrigin(!!ctx.dest.reqOrigin),
12
// https://github.com/DevExpress/testcafe-hammerhead/blob/f5b0508d10614bf39a75c772dc6bd01c24f29417/src/request-pipeline/header-transforms/transforms.ts#L128),
13
// then we must return the other port over. however, the issue with this is we don't know if the incoming request is actually a
14
// crossdomain port (a simple check for reqOrigin cannot suffice, as a request from a non-crossdomain origin to a crossdomain port and
15
// vice versa can happen),
16
// so this will fix the issue from non-crossdomain port to crossdomain-port but will NOT fix crosdomain-port to non-crossdomain port.
17
// However, the latter case will never happen because hammerhead made all client rewriting cross-domain requests to always use the
18
// cross-domain ports, even if the origin is from a cross-domain port.
19
const port = isCrossDomain ? this.serverInfo.port : this.serverInfo.crossDomainPort;
20
21
// don't add a port if port is 443 and protocol is https:, and don't add a port if port is 80 and protocol is http:.
22
// note that this isn't supported by the client rewriting, so client hammerhead's port.toString() will throw an error
23
const hostPort =
24
(this.serverInfo.protocol == 'https:' && port == 443) || (this.serverInfo.protocol == 'http:' && port == 80)
25
? null
26
: port;
27
28
return urlUtils.getDomain({
29
protocol: this.serverInfo.protocol,
30
// use host instead of hostname so we can manually add in the port
31
host: this.serverInfo.hostname + (hostPort ? ':' + hostPort : '')
32
});
33
};
34
35