1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.RequestContext = void 0;
|
8 | const express_1 = require("@loopback/express");
|
9 | const keys_1 = require("./keys");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | class 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 |
|
23 |
|
24 |
|
25 |
|
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 |
|
35 |
|
36 |
|
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 |
|
45 | basePath = request.baseUrl;
|
46 | }
|
47 | else {
|
48 | basePath = request.baseUrl + basePath;
|
49 | }
|
50 | }
|
51 | return basePath;
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
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 |
|
67 |
|
68 |
|
69 |
|
70 |
|
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 |
|
76 |
|
77 | host = (_c = config.host) !== null && _c !== void 0 ? _c : request.socket.localAddress;
|
78 | port = (config.port || request.socket.localPort).toString();
|
79 | }
|
80 |
|
81 | port = protocol === 'https' && port === '443' ? '' : port;
|
82 | port = protocol === 'http' && port === '80' ? '' : port;
|
83 |
|
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 | }
|
94 | exports.RequestContext = RequestContext;
|
95 | function 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 |
|
\ | No newline at end of file |