UNPKG

4.53 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 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 /**
16 * Get the protocol used by the client to make the request.
17 * Please note this protocol may be different from what we are observing
18 * at HTTP/TCP level, because reverse proxies like nginx or sidecars like
19 * Envoy are switching between protocols.
20 */
21 get requestedProtocol() {
22 var _a, _b;
23 return ((_b = (((_a = this.request.get('x-forwarded-proto')) !== null && _a !== void 0 ? _a : '').split(',')[0] ||
24 this.request.protocol ||
25 this.serverConfig.protocol)) !== null && _b !== void 0 ? _b : 'http');
26 }
27 /**
28 * Get the effective base path of the incoming request. This base path
29 * combines `baseUrl` provided by Express when LB4 handler is mounted on
30 * a non-root path, with the `basePath` value configured at LB4 side.
31 */
32 get basePath() {
33 var _a;
34 const request = this.request;
35 let basePath = (_a = this.serverConfig.basePath) !== null && _a !== void 0 ? _a : '';
36 if (request.baseUrl && request.baseUrl !== '/') {
37 if (!basePath || request.baseUrl.endsWith(basePath)) {
38 // Express has already applied basePath to baseUrl
39 basePath = request.baseUrl;
40 }
41 else {
42 basePath = request.baseUrl + basePath;
43 }
44 }
45 return basePath;
46 }
47 /**
48 * Get the base URL used by the client to make the request.
49 * This URL contains the protocol, hostname, port and base path.
50 * The path of the invoked route and query string is not included.
51 *
52 * Please note these values may be different from what we are observing
53 * at HTTP/TCP level, because reverse proxies like nginx are rewriting them.
54 */
55 get requestedBaseUrl() {
56 var _a, _b, _c;
57 const request = this.request;
58 const config = this.serverConfig;
59 const protocol = this.requestedProtocol;
60 // The host can be in one of the forms
61 // [::1]:3000
62 // [::1]
63 // 127.0.0.1:3000
64 // 127.0.0.1
65 let { host, port } = parseHostAndPort((_a = request.get('x-forwarded-host')) !== null && _a !== void 0 ? _a : request.headers.host);
66 const forwardedPort = ((_b = request.get('x-forwarded-port')) !== null && _b !== void 0 ? _b : '').split(',')[0];
67 port = forwardedPort || port;
68 if (!host) {
69 // No host detected from http headers
70 // Use the configured values or the local network address
71 host = (_c = config.host) !== null && _c !== void 0 ? _c : request.socket.localAddress;
72 port = (config.port || request.socket.localPort).toString();
73 }
74 // clear default ports
75 port = protocol === 'https' && port === '443' ? '' : port;
76 port = protocol === 'http' && port === '80' ? '' : port;
77 // add port number of present
78 host += port !== '' ? ':' + port : '';
79 return protocol + '://' + host + this.basePath;
80 }
81 constructor(request, response, parent, serverConfig, name) {
82 super(request, response, parent, name);
83 this.request = request;
84 this.response = response;
85 this.serverConfig = serverConfig;
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