115 lines
5.5 KiB
JavaScript
115 lines
5.5 KiB
JavaScript
|
"use strict";
|
||
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
||
|
}
|
||
|
Object.defineProperty(o, k2, desc);
|
||
|
}) : (function(o, m, k, k2) {
|
||
|
if (k2 === undefined) k2 = k;
|
||
|
o[k2] = m[k];
|
||
|
}));
|
||
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
|
}) : function(o, v) {
|
||
|
o["default"] = v;
|
||
|
});
|
||
|
var __importStar = (this && this.__importStar) || function (mod) {
|
||
|
if (mod && mod.__esModule) return mod;
|
||
|
var result = {};
|
||
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||
|
__setModuleDefault(result, mod);
|
||
|
return result;
|
||
|
};
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const builtin_header_names_1 = __importDefault(require("./builtin-header-names"));
|
||
|
const headerTransforms = __importStar(require("./header-transforms"));
|
||
|
const upload_1 = require("../upload");
|
||
|
const match_url_wildcard_1 = __importDefault(require("match-url-wildcard"));
|
||
|
const generate_unique_id_1 = __importDefault(require("../utils/generate-unique-id"));
|
||
|
const headers_1 = require("../utils/headers");
|
||
|
const DEFAULT_REQUEST_OPTIONS = {
|
||
|
isAjax: false,
|
||
|
rawHeaders: [],
|
||
|
isWebSocket: false,
|
||
|
get requestId() {
|
||
|
return (0, generate_unique_id_1.default)();
|
||
|
},
|
||
|
};
|
||
|
class RequestOptions {
|
||
|
constructor(params) {
|
||
|
Object.assign(this, DEFAULT_REQUEST_OPTIONS, params);
|
||
|
this._applyExternalProxySettings();
|
||
|
this.prepare();
|
||
|
}
|
||
|
static createFrom(ctx) {
|
||
|
const bodyWithUploads = (0, upload_1.inject)(ctx.req.headers[builtin_header_names_1.default.contentType], ctx.reqBody);
|
||
|
// NOTE: First, we should rewrite the request body, because the 'content-length' header will be built based on it.
|
||
|
if (bodyWithUploads)
|
||
|
ctx.reqBody = bodyWithUploads;
|
||
|
// NOTE: We should save authorization and proxyAuthorization headers for API requests.
|
||
|
if (ctx.req.headers[builtin_header_names_1.default.isApiRequest]) {
|
||
|
if (ctx.req.headers[builtin_header_names_1.default.authorization])
|
||
|
ctx.req.headers[builtin_header_names_1.default.authorization] = (0, headers_1.addAuthorizationPrefix)(ctx.req.headers[builtin_header_names_1.default.authorization]);
|
||
|
if (ctx.req.headers[builtin_header_names_1.default.proxyAuthorization])
|
||
|
ctx.req.headers[builtin_header_names_1.default.proxyAuthorization] = (0, headers_1.addAuthorizationPrefix)(ctx.req.headers[builtin_header_names_1.default.proxyAuthorization]);
|
||
|
}
|
||
|
return new RequestOptions({
|
||
|
// NOTE: All headers, including 'content-length', are built here.
|
||
|
headers: headerTransforms.forRequest(ctx),
|
||
|
externalProxySettings: ctx.session.externalProxySettings || void 0,
|
||
|
url: ctx.dest.url,
|
||
|
protocol: ctx.dest.protocol,
|
||
|
hostname: ctx.dest.hostname,
|
||
|
host: ctx.dest.host,
|
||
|
port: ctx.dest.port,
|
||
|
path: ctx.dest.partAfterHost,
|
||
|
auth: ctx.dest.auth,
|
||
|
method: ctx.req.method || '',
|
||
|
credentials: ctx.session.getAuthCredentials(),
|
||
|
body: ctx.reqBody,
|
||
|
isAjax: ctx.isAjax,
|
||
|
rawHeaders: ctx.req.rawHeaders,
|
||
|
requestId: ctx.requestId,
|
||
|
requestTimeout: ctx.session.options.requestTimeout,
|
||
|
isWebSocket: ctx.isWebSocket,
|
||
|
disableHttp2: ctx.session.isHttp2Disabled(),
|
||
|
});
|
||
|
}
|
||
|
_applyExternalProxySettings() {
|
||
|
if (!this.externalProxySettings || (0, match_url_wildcard_1.default)(this.url, this.externalProxySettings.bypassRules))
|
||
|
return;
|
||
|
this.proxy = this.externalProxySettings;
|
||
|
if (this.protocol === 'http:') {
|
||
|
this.path = this.protocol + '//' + this.host + this.path;
|
||
|
this.host = this.externalProxySettings.host;
|
||
|
this.hostname = this.externalProxySettings.hostname;
|
||
|
this.port = this.externalProxySettings.port;
|
||
|
if (this.externalProxySettings.authHeader)
|
||
|
this.headers[builtin_header_names_1.default.proxyAuthorization] = this.externalProxySettings.authHeader;
|
||
|
}
|
||
|
}
|
||
|
get isHttps() {
|
||
|
return this.protocol === 'https:';
|
||
|
}
|
||
|
ignoreSSLAuth() {
|
||
|
this.rejectUnauthorized = false;
|
||
|
this.ecdhCurve = 'auto';
|
||
|
}
|
||
|
prepare() {
|
||
|
// NOTE: The headers are converted to raw headers because some sites ignore headers in a lower case. (GH-1380)
|
||
|
// We also need to restore the request option headers to a lower case because headers may change
|
||
|
// if a request is unauthorized, so there can be duplicated headers, for example, 'www-authenticate' and 'WWW-Authenticate'.
|
||
|
const transformedHeaders = headerTransforms.transformHeadersCaseToRaw(this.headers, this.rawHeaders);
|
||
|
const clonedReqOptions = Object.assign({}, this);
|
||
|
clonedReqOptions['headers'] = transformedHeaders;
|
||
|
return clonedReqOptions;
|
||
|
}
|
||
|
}
|
||
|
exports.default = RequestOptions;module.exports = exports.default;
|
||
|
|