Path: blob/master/lib/rammerhead/src/util/fixCorsHeader.js
6530 views
const urlUtils = require('testcafe-hammerhead/lib/utils/url');1const RequestPipelineContext = require('testcafe-hammerhead/lib/request-pipeline/context');23/**4* if a non-crossdomain origin makes a request to a crossdomain port, the ports are flipped. this is to fix that issue.5* there is also another issue with https://domain and https://domain:443 not matching. port 443/80 are automatically6* removed if https and 443, and http and 80.7* original: https://github.com/DevExpress/testcafe-hammerhead/blob/f5b0508d10614bf39a75c772dc6bd01c24f29417/src/request-pipeline/context.ts#L4368*/9RequestPipelineContext.prototype.getProxyOrigin = function getProxyOrigin(isCrossDomain = false) {10// if we receive a request that has a proxy origin header, (ctx.getProxyOrigin(!!ctx.dest.reqOrigin),11// https://github.com/DevExpress/testcafe-hammerhead/blob/f5b0508d10614bf39a75c772dc6bd01c24f29417/src/request-pipeline/header-transforms/transforms.ts#L128),12// then we must return the other port over. however, the issue with this is we don't know if the incoming request is actually a13// crossdomain port (a simple check for reqOrigin cannot suffice, as a request from a non-crossdomain origin to a crossdomain port and14// vice versa can happen),15// so this will fix the issue from non-crossdomain port to crossdomain-port but will NOT fix crosdomain-port to non-crossdomain port.16// However, the latter case will never happen because hammerhead made all client rewriting cross-domain requests to always use the17// cross-domain ports, even if the origin is from a cross-domain port.18const port = isCrossDomain ? this.serverInfo.port : this.serverInfo.crossDomainPort;1920// 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:.21// note that this isn't supported by the client rewriting, so client hammerhead's port.toString() will throw an error22const hostPort =23(this.serverInfo.protocol == 'https:' && port == 443) || (this.serverInfo.protocol == 'http:' && port == 80)24? null25: port;2627return urlUtils.getDomain({28protocol: this.serverInfo.protocol,29// use host instead of hostname so we can manually add in the port30host: this.serverInfo.hostname + (hostPort ? ':' + hostPort : '')31});32};333435