UNPKG

4.5 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2018,2020. All Rights Reserved.
3// Node module: @loopback/rest
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.RequestContext = void 0;
8const express_1 = require("@loopback/express");
9const keys_1 = require("./keys");
10/**
11 * A per-request Context combining an IoC container with handler context
12 * (request, response, etc.).
13 */
14class RequestContext extends express_1.MiddlewareContext {
15 constructor(request, response, parent, serverConfig, name) {
16 super(request, response, parent, name);
17 this.request = request;
18 this.response = response;
19 this.serverConfig = serverConfig;
20 }
21 /**
22 * Get the protocol used by the client to make the request.
23 * Please note this protocol may be different from what we are observing
24 * at HTTP/TCP level, because reverse proxies like nginx or sidecars like
25 * Envoy are switching between protocols.
26 */
27 get requestedProtocol() {
28 var _a, _b;
29 return ((_b = (((_a = this.request.get('x-forwarded-proto')) !== null && _a !== void 0 ? _a : '').split(',')[0] ||
30 this.request.protocol ||
31 this.serverConfig.protocol)) !== null && _b !== void 0 ? _b : 'http');
32 }
33 /**
34 * Get the effective base path of the incoming request. This base path
35 * combines `baseUrl` provided by Express when LB4 handler is mounted on
36 * a non-root path, with the `basePath` value configured at LB4 side.
37 */
38 get basePath() {
39 var _a;
40 const request = this.request;
41 let basePath = (_a = this.serverConfig.basePath) !== null && _a !== void 0 ? _a : '';
42 if (request.baseUrl && request.baseUrl !== '/') {
43 if (!basePath || request.baseUrl.endsWith(basePath)) {
44 // Express has already applied basePath to baseUrl
45 basePath = request.baseUrl;
46 }
47 else {
48 basePath = request.baseUrl + basePath;
49 }
50 }
51 return basePath;
52 }
53 /**
54 * Get the base URL used by the client to make the request.
55 * This URL contains the protocol, hostname, port and base path.
56 * The path of the invoked route and query string is not included.
57 *
58 * Please note these values may be different from what we are observing
59 * at HTTP/TCP level, because reverse proxies like nginx are rewriting them.
60 */
61 get requestedBaseUrl() {
62 var _a, _b, _c;
63 const request = this.request;
64 const config = this.serverConfig;
65 const protocol = this.requestedProtocol;
66 // The host can be in one of the forms
67 // [::1]:3000
68 // [::1]
69 // 127.0.0.1:3000
70 // 127.0.0.1
71 let { host, port } = parseHostAndPort((_a = request.get('x-forwarded-host')) !== null && _a !== void 0 ? _a : request.headers.host);
72 const forwardedPort = ((_b = request.get('x-forwarded-port')) !== null && _b !== void 0 ? _b : '').split(',')[0];
73 port = forwardedPort || port;
74 if (!host) {
75 // No host detected from http headers
76 // Use the configured values or the local network address
77 host = (_c = config.host) !== null && _c !== void 0 ? _c : request.socket.localAddress;
78 port = (config.port || request.socket.localPort).toString();
79 }
80 // clear default ports
81 port = protocol === 'https' && port === '443' ? '' : port;
82 port = protocol === 'http' && port === '80' ? '' : port;
83 // add port number of present
84 host += port !== '' ? ':' + port : '';
85 return protocol + '://' + host + this.basePath;
86 }
87 setupBindings() {
88 super.setupBindings();
89 this.bind(keys_1.RestBindings.Http.REQUEST).to(this.request).lock();
90 this.bind(keys_1.RestBindings.Http.RESPONSE).to(this.response).lock();
91 this.bind(keys_1.RestBindings.Http.CONTEXT).to(this).lock();
92 }
93}
94exports.RequestContext = RequestContext;
95function parseHostAndPort(host) {
96 var _a;
97 host = host !== null && host !== void 0 ? host : '';
98 host = host.split(',')[0];
99 const portPattern = /:([0-9]+)$/;
100 const port = ((_a = host.match(portPattern)) !== null && _a !== void 0 ? _a : [])[1] || '';
101 host = host.replace(portPattern, '');
102 return { host, port };
103}
104//# sourceMappingURL=request-context.js.map
\No newline at end of file