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 | |
16 |
|
17 |
|
18 |
|
19 |
|
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 |
|
29 |
|
30 |
|
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 |
|
39 | basePath = request.baseUrl;
|
40 | }
|
41 | else {
|
42 | basePath = request.baseUrl + basePath;
|
43 | }
|
44 | }
|
45 | return basePath;
|
46 | }
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | get requestedBaseUrl() {
|
56 | var _a, _b, _c, _d, _e;
|
57 | const request = this.request;
|
58 | const config = this.serverConfig;
|
59 | const protocol = this.requestedProtocol;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
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 |
|
70 |
|
71 | host = (_d = (_c = config.host) !== null && _c !== void 0 ? _c : request.socket.localAddress) !== null && _d !== void 0 ? _d : '';
|
72 | port = ((_e = (config.port || request.socket.localPort)) !== null && _e !== void 0 ? _e : '').toString();
|
73 | }
|
74 |
|
75 | port = protocol === 'https' && port === '443' ? '' : port;
|
76 | port = protocol === 'http' && port === '80' ? '' : port;
|
77 |
|
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 | }
|
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 |