0z2i6v3u5t 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.devcontainer/devcontainer.json +4 -0
- package/.devcontainer/setup.sh +11 -0
- package/.dockerignore +2 -0
- package/.github/CONTRIBUTING.md +52 -0
- package/.github/FUNDING.yml +3 -0
- package/.github/ISSUE_TEMPLATE/bug_report.yml +59 -0
- package/.github/ISSUE_TEMPLATE/config.yml +5 -0
- package/.github/ISSUE_TEMPLATE/feature_request.yml +43 -0
- package/.github/dependabot.yml +17 -0
- package/.github/workflows/codeql.yml +76 -0
- package/.github/workflows/publish_docs.yml +25 -0
- package/.github/workflows/test.yml +78 -0
- package/.nvmrc +1 -0
- package/.prettierignore +1 -0
- package/.prettierrc +1 -0
- package/.vscode/launch.json +42 -0
- package/CODE_OF_CONDUCT.md +76 -0
- package/Dockerfile +17 -0
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/SECURITY.md +5 -0
- package/__tests__/actions/cacheTest.ts +58 -0
- package/__tests__/actions/randomNumber.ts +26 -0
- package/__tests__/actions/recursiveAction.ts +16 -0
- package/__tests__/actions/sleepTest.ts +24 -0
- package/__tests__/actions/status.ts +17 -0
- package/__tests__/actions/swagger.ts +76 -0
- package/__tests__/actions/validationTest.ts +63 -0
- package/__tests__/cli/cli.ts +126 -0
- package/__tests__/core/api.ts +632 -0
- package/__tests__/core/cache.ts +400 -0
- package/__tests__/core/chatRoom.ts +589 -0
- package/__tests__/core/cli.ts +349 -0
- package/__tests__/core/cluster.ts +132 -0
- package/__tests__/core/config.ts +78 -0
- package/__tests__/core/errors.ts +112 -0
- package/__tests__/core/log.ts +23 -0
- package/__tests__/core/middleware.ts +427 -0
- package/__tests__/core/plugins/partialPlugin.ts +94 -0
- package/__tests__/core/plugins/withPlugin.ts +88 -0
- package/__tests__/core/plugins/withoutPlugin.ts +81 -0
- package/__tests__/core/process.ts +42 -0
- package/__tests__/core/specHelper.ts +330 -0
- package/__tests__/core/staticFile/compression.ts +99 -0
- package/__tests__/core/staticFile/staticFile.ts +180 -0
- package/__tests__/core/tasks/customQueueFunction.ts +67 -0
- package/__tests__/core/tasks/fullWorkerFlow.ts +199 -0
- package/__tests__/core/tasks/tasks.ts +605 -0
- package/__tests__/integration/browser.ts +133 -0
- package/__tests__/integration/ioredis-mock.ts +194 -0
- package/__tests__/integration/sendBuffer.ts +97 -0
- package/__tests__/integration/sendFile.ts +24 -0
- package/__tests__/integration/sharedFingerprint.ts +82 -0
- package/__tests__/integration/taskFlow.ts +110 -0
- package/__tests__/jest.ts +5 -0
- package/__tests__/modules/action.ts +103 -0
- package/__tests__/modules/config.ts +19 -0
- package/__tests__/modules/utils/ensureNoTsHeaderOrSpecFiles.ts +24 -0
- package/__tests__/servers/web/allowedRequestHosts.ts +88 -0
- package/__tests__/servers/web/enableMultiples.ts +83 -0
- package/__tests__/servers/web/fileUpload.ts +79 -0
- package/__tests__/servers/web/jsonp.ts +57 -0
- package/__tests__/servers/web/nonMultiples.ts +83 -0
- package/__tests__/servers/web/rawBody.ts +208 -0
- package/__tests__/servers/web/returnErrorCodes.ts +55 -0
- package/__tests__/servers/web/routes/deepRoutes.ts +96 -0
- package/__tests__/servers/web/routes/routes.ts +579 -0
- package/__tests__/servers/web/routes/veryDeepRoutes.ts +92 -0
- package/__tests__/servers/web/web.ts +1031 -0
- package/__tests__/servers/websocket.ts +795 -0
- package/__tests__/tasks/runAction.ts +37 -0
- package/__tests__/template.ts.example +20 -0
- package/__tests__/testCliCommands/hello.ts +44 -0
- package/__tests__/testPlugin/public/plugin.html +1 -0
- package/__tests__/testPlugin/src/actions/pluginAction.ts +14 -0
- package/__tests__/testPlugin/src/bin/hello.ts +22 -0
- package/__tests__/testPlugin/src/initializers/pluginInitializer.ts +17 -0
- package/__tests__/testPlugin/src/tasks/pluginTask.ts +15 -0
- package/__tests__/testPlugin/tsconfig.json +10 -0
- package/__tests__/utils/utils.ts +492 -0
- package/app.json +23 -0
- package/bin/deploy-docs +39 -0
- package/client/ActionheroWebsocketClient.js +277 -0
- package/docker-compose.yml +73 -0
- package/package.json +24 -0
- package/public/chat.html +194 -0
- package/public/css/cosmo.css +12 -0
- package/public/favicon.ico +0 -0
- package/public/index.html +115 -0
- package/public/javascript/.gitkeep +0 -0
- package/public/linkedSession.html +80 -0
- package/public/logo/actionhero-small.png +0 -0
- package/public/logo/actionhero.png +0 -0
- package/public/pixel.gif +0 -0
- package/public/simple.html +2 -0
- package/public/swagger.html +32 -0
- package/public/websocketLoadTest.html +322 -0
- package/src/actions/cacheTest.ts +58 -0
- package/src/actions/createChatRoom.ts +20 -0
- package/src/actions/randomNumber.ts +17 -0
- package/src/actions/recursiveAction.ts +13 -0
- package/src/actions/sendFile.ts +12 -0
- package/src/actions/sleepTest.ts +40 -0
- package/src/actions/status.ts +73 -0
- package/src/actions/swagger.ts +155 -0
- package/src/actions/validationTest.ts +36 -0
- package/src/bin/actionhero.ts +225 -0
- package/src/bin/methods/actions/list.ts +30 -0
- package/src/bin/methods/console.ts +26 -0
- package/src/bin/methods/generate/action.ts +58 -0
- package/src/bin/methods/generate/cli.ts +51 -0
- package/src/bin/methods/generate/initializer.ts +54 -0
- package/src/bin/methods/generate/plugin.ts +57 -0
- package/src/bin/methods/generate/server.ts +38 -0
- package/src/bin/methods/generate/task.ts +68 -0
- package/src/bin/methods/generate.ts +176 -0
- package/src/bin/methods/task/enqueue.ts +35 -0
- package/src/classes/action.ts +98 -0
- package/src/classes/actionProcessor.ts +463 -0
- package/src/classes/api.ts +51 -0
- package/src/classes/cli.ts +67 -0
- package/src/classes/config.ts +15 -0
- package/src/classes/connection.ts +321 -0
- package/src/classes/exceptionReporter.ts +9 -0
- package/src/classes/initializer.ts +59 -0
- package/src/classes/initializers.ts +5 -0
- package/src/classes/input.ts +9 -0
- package/src/classes/inputs.ts +34 -0
- package/src/classes/process/actionheroVersion.ts +15 -0
- package/src/classes/process/env.ts +16 -0
- package/src/classes/process/id.ts +34 -0
- package/src/classes/process/pid.ts +32 -0
- package/src/classes/process/projectRoot.ts +16 -0
- package/src/classes/process/typescript.ts +47 -0
- package/src/classes/process.ts +479 -0
- package/src/classes/server.ts +251 -0
- package/src/classes/task.ts +87 -0
- package/src/config/api.ts +107 -0
- package/src/config/errors.ts +162 -0
- package/src/config/logger.ts +113 -0
- package/src/config/plugins.ts +37 -0
- package/src/config/redis.ts +78 -0
- package/src/config/routes.ts +44 -0
- package/src/config/tasks.ts +84 -0
- package/src/config/web.ts +136 -0
- package/src/config/websocket.ts +62 -0
- package/src/index.ts +46 -0
- package/src/initializers/actions.ts +125 -0
- package/src/initializers/chatRoom.ts +214 -0
- package/src/initializers/connections.ts +124 -0
- package/src/initializers/exceptions.ts +155 -0
- package/src/initializers/params.ts +52 -0
- package/src/initializers/redis.ts +191 -0
- package/src/initializers/resque.ts +248 -0
- package/src/initializers/routes.ts +229 -0
- package/src/initializers/servers.ts +134 -0
- package/src/initializers/specHelper.ts +195 -0
- package/src/initializers/staticFile.ts +253 -0
- package/src/initializers/tasks.ts +188 -0
- package/src/modules/action.ts +89 -0
- package/src/modules/cache.ts +326 -0
- package/src/modules/chatRoom.ts +321 -0
- package/src/modules/config.ts +246 -0
- package/src/modules/log.ts +62 -0
- package/src/modules/redis.ts +93 -0
- package/src/modules/route.ts +59 -0
- package/src/modules/specHelper.ts +182 -0
- package/src/modules/task.ts +527 -0
- package/src/modules/utils/argv.ts +3 -0
- package/src/modules/utils/arrayStartingMatch.ts +21 -0
- package/src/modules/utils/arrayUnique.ts +15 -0
- package/src/modules/utils/collapseObjectToArray.ts +33 -0
- package/src/modules/utils/deepCopy.ts +3 -0
- package/src/modules/utils/ensureNoTsHeaderOrSpecFiles.ts +19 -0
- package/src/modules/utils/eventLoopDelay.ts +34 -0
- package/src/modules/utils/fileUtils.ts +119 -0
- package/src/modules/utils/filterObjectForLogging.ts +51 -0
- package/src/modules/utils/filterResponseForLogging.ts +53 -0
- package/src/modules/utils/getExternalIPAddress.ts +17 -0
- package/src/modules/utils/hashMerge.ts +63 -0
- package/src/modules/utils/isPlainObject.ts +45 -0
- package/src/modules/utils/isRunning.ts +7 -0
- package/src/modules/utils/parseCookies.ts +20 -0
- package/src/modules/utils/parseHeadersForClientAddress.ts +53 -0
- package/src/modules/utils/parseIPv6URI.ts +24 -0
- package/src/modules/utils/replaceDistWithSrc.ts +9 -0
- package/src/modules/utils/safeGlob.ts +6 -0
- package/src/modules/utils/sleep.ts +8 -0
- package/src/modules/utils/sortGlobalMiddleware.ts +17 -0
- package/src/modules/utils/sourceRelativeLinkPath.ts +29 -0
- package/src/modules/utils.ts +66 -0
- package/src/server.ts +20 -0
- package/src/servers/web.ts +894 -0
- package/src/servers/websocket.ts +304 -0
- package/src/tasks/runAction.ts +29 -0
- package/tea.yaml +9 -0
- package/templates/README.md.template +17 -0
- package/templates/action.ts.template +15 -0
- package/templates/boot.js.template +9 -0
- package/templates/cli.ts.template +15 -0
- package/templates/gitignore.template +23 -0
- package/templates/initializer.ts.template +17 -0
- package/templates/package-plugin.json.template +12 -0
- package/templates/package.json.template +45 -0
- package/templates/projectMap.txt +39 -0
- package/templates/projectServer.ts.template +20 -0
- package/templates/server.ts.template +37 -0
- package/templates/task.ts.template +16 -0
- package/templates/test/action.ts.template +13 -0
- package/templates/test/task.ts.template +20 -0
- package/tsconfig.json +11 -0
@@ -0,0 +1,894 @@
|
|
1
|
+
import * as http from "http";
|
2
|
+
import * as https from "https";
|
3
|
+
import * as url from "url";
|
4
|
+
import * as qs from "qs";
|
5
|
+
import * as fs from "fs";
|
6
|
+
import * as zlib from "zlib";
|
7
|
+
import * as path from "path";
|
8
|
+
import * as formidable from "formidable";
|
9
|
+
import * as Mime from "mime";
|
10
|
+
import * as uuid from "uuid";
|
11
|
+
import * as etag from "etag";
|
12
|
+
import { BrowserFingerprint } from "browser_fingerprint";
|
13
|
+
import { api, config, utils, Server, Connection } from "../index";
|
14
|
+
import { ActionsStatus, ActionProcessor } from "../classes/actionProcessor";
|
15
|
+
|
16
|
+
export class WebServer extends Server {
|
17
|
+
server: http.Server | https.Server;
|
18
|
+
fingerPrinter: BrowserFingerprint;
|
19
|
+
sockets: { [id: string]: any };
|
20
|
+
|
21
|
+
constructor() {
|
22
|
+
super();
|
23
|
+
this.type = "web";
|
24
|
+
this.sockets = {};
|
25
|
+
|
26
|
+
this.attributes = {
|
27
|
+
canChat: false,
|
28
|
+
logConnections: false,
|
29
|
+
logExits: false,
|
30
|
+
sendWelcomeMessage: false,
|
31
|
+
verbs: [], // no verbs for connections of this type, as they are to be very short-lived
|
32
|
+
};
|
33
|
+
|
34
|
+
this.connectionCustomMethods = {
|
35
|
+
setHeader: (
|
36
|
+
connection: Connection,
|
37
|
+
key: string,
|
38
|
+
value: string | number,
|
39
|
+
) => {
|
40
|
+
connection.rawConnection.res.setHeader(key, value);
|
41
|
+
},
|
42
|
+
|
43
|
+
setStatusCode: (connection: Connection, value: number) => {
|
44
|
+
connection.rawConnection.responseHttpCode = value;
|
45
|
+
},
|
46
|
+
|
47
|
+
pipe: (
|
48
|
+
connection: Connection,
|
49
|
+
buffer: string | Buffer,
|
50
|
+
headers: Record<string, string>,
|
51
|
+
) => {
|
52
|
+
for (const k in headers) {
|
53
|
+
connection.setHeader(k, headers[k]);
|
54
|
+
}
|
55
|
+
if (typeof buffer === "string") {
|
56
|
+
buffer = Buffer.from(buffer);
|
57
|
+
}
|
58
|
+
connection.rawConnection.res.end(buffer);
|
59
|
+
},
|
60
|
+
};
|
61
|
+
}
|
62
|
+
|
63
|
+
async initialize() {
|
64
|
+
if (["api", "file"].indexOf(this.config.rootEndpointType) < 0) {
|
65
|
+
throw new Error("rootEndpointType can only be 'api' or 'file'");
|
66
|
+
}
|
67
|
+
|
68
|
+
if (
|
69
|
+
!this.config.urlPathForFiles &&
|
70
|
+
this.config.rootEndpointType === "file"
|
71
|
+
) {
|
72
|
+
throw new Error(
|
73
|
+
'rootEndpointType cannot be "file" without a urlPathForFiles',
|
74
|
+
);
|
75
|
+
}
|
76
|
+
|
77
|
+
this.fingerPrinter = new BrowserFingerprint(this.config.fingerprintOptions);
|
78
|
+
}
|
79
|
+
|
80
|
+
async start() {
|
81
|
+
let bootAttempts = 0;
|
82
|
+
if (this.config.secure === false) {
|
83
|
+
this.server = http.createServer((req, res) => {
|
84
|
+
this.handleRequest(req, res);
|
85
|
+
});
|
86
|
+
} else {
|
87
|
+
this.server = https.createServer(
|
88
|
+
this.config.serverOptions,
|
89
|
+
(req, res) => {
|
90
|
+
this.handleRequest(req, res);
|
91
|
+
},
|
92
|
+
);
|
93
|
+
}
|
94
|
+
|
95
|
+
this.server.on("error", (error) => {
|
96
|
+
bootAttempts++;
|
97
|
+
if (bootAttempts < this.config.bootAttempts) {
|
98
|
+
this.log(`cannot boot web server; trying again [${error}]`, "error");
|
99
|
+
if (bootAttempts === 1) {
|
100
|
+
this.cleanSocket(this.config.bindIP, this.config.port);
|
101
|
+
}
|
102
|
+
setTimeout(() => {
|
103
|
+
this.log("attempting to boot again..");
|
104
|
+
this.server.listen(this.config.port, this.config.bindIP);
|
105
|
+
}, 1000);
|
106
|
+
} else {
|
107
|
+
throw new Error(
|
108
|
+
`cannot start web server @ ${this.config.bindIP}:${this.config.port} => ${error}`,
|
109
|
+
);
|
110
|
+
}
|
111
|
+
});
|
112
|
+
|
113
|
+
let socketCounter = 0;
|
114
|
+
this.server.on("connection", (socket) => {
|
115
|
+
const id = socketCounter;
|
116
|
+
this.sockets[id] = socket;
|
117
|
+
socket.on("close", () => delete this.sockets[id]);
|
118
|
+
socketCounter++;
|
119
|
+
});
|
120
|
+
|
121
|
+
await new Promise((resolve) => {
|
122
|
+
this.server.listen(this.config.port, this.config.bindIP, () => {
|
123
|
+
this.chmodSocket(this.config.bindIP, this.config.port);
|
124
|
+
resolve(null);
|
125
|
+
});
|
126
|
+
});
|
127
|
+
|
128
|
+
this.on("connection", async (connection: Connection) => {
|
129
|
+
const requestMode = await this.determineRequestParams(connection);
|
130
|
+
if (requestMode === "api") {
|
131
|
+
this.processAction(connection);
|
132
|
+
} else if (requestMode === "file") {
|
133
|
+
this.processFile(connection);
|
134
|
+
} else if (requestMode === "options") {
|
135
|
+
this.respondToOptions(connection);
|
136
|
+
} else if (requestMode === "trace") {
|
137
|
+
this.respondToTrace(connection);
|
138
|
+
}
|
139
|
+
});
|
140
|
+
|
141
|
+
this.on("actionComplete", this.completeResponse);
|
142
|
+
}
|
143
|
+
|
144
|
+
async stop() {
|
145
|
+
if (!this.server) return;
|
146
|
+
|
147
|
+
await new Promise((resolve) => {
|
148
|
+
this.server.close(resolve);
|
149
|
+
for (const socket of Object.values(this.sockets)) {
|
150
|
+
socket.destroy();
|
151
|
+
}
|
152
|
+
});
|
153
|
+
}
|
154
|
+
|
155
|
+
async sendMessage(connection: Connection, message: string) {
|
156
|
+
let stringResponse = "";
|
157
|
+
if (connection.rawConnection.method !== "HEAD") {
|
158
|
+
stringResponse = String(message);
|
159
|
+
}
|
160
|
+
|
161
|
+
this.cleanHeaders(connection);
|
162
|
+
const headers = connection.rawConnection.responseHeaders;
|
163
|
+
const responseHttpCode = parseInt(
|
164
|
+
connection.rawConnection.responseHttpCode,
|
165
|
+
);
|
166
|
+
|
167
|
+
this.sendWithCompression(
|
168
|
+
connection,
|
169
|
+
responseHttpCode,
|
170
|
+
headers,
|
171
|
+
stringResponse,
|
172
|
+
);
|
173
|
+
}
|
174
|
+
|
175
|
+
async sendFile(
|
176
|
+
connection: Connection,
|
177
|
+
error: NodeJS.ErrnoException,
|
178
|
+
fileStream: any,
|
179
|
+
mime: string,
|
180
|
+
length: number,
|
181
|
+
lastModified: Date,
|
182
|
+
) {
|
183
|
+
let foundCacheControl = false;
|
184
|
+
let ifModifiedSince;
|
185
|
+
|
186
|
+
connection.rawConnection.responseHeaders.forEach((pair: string[]) => {
|
187
|
+
if (pair[0].toLowerCase() === "cache-control") {
|
188
|
+
foundCacheControl = true;
|
189
|
+
}
|
190
|
+
});
|
191
|
+
|
192
|
+
connection.rawConnection.responseHeaders.push(["Content-Type", mime]);
|
193
|
+
|
194
|
+
if (fileStream) {
|
195
|
+
if (!foundCacheControl) {
|
196
|
+
connection.rawConnection.responseHeaders.push([
|
197
|
+
"Cache-Control",
|
198
|
+
"max-age=" +
|
199
|
+
this.config.flatFileCacheDuration +
|
200
|
+
", must-revalidate, public",
|
201
|
+
]);
|
202
|
+
}
|
203
|
+
}
|
204
|
+
|
205
|
+
if (fileStream && !this.config.enableEtag) {
|
206
|
+
if (lastModified) {
|
207
|
+
connection.rawConnection.responseHeaders.push([
|
208
|
+
"Last-Modified",
|
209
|
+
new Date(lastModified).toUTCString(),
|
210
|
+
]);
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
this.cleanHeaders(connection);
|
215
|
+
const headers = connection.rawConnection.responseHeaders;
|
216
|
+
const reqHeaders = connection.rawConnection.req.headers;
|
217
|
+
|
218
|
+
const sendRequestResult = () => {
|
219
|
+
const responseHttpCode = parseInt(
|
220
|
+
connection.rawConnection.responseHttpCode,
|
221
|
+
10,
|
222
|
+
);
|
223
|
+
if (error) {
|
224
|
+
this.sendWithCompression(
|
225
|
+
connection,
|
226
|
+
responseHttpCode,
|
227
|
+
headers,
|
228
|
+
String(error),
|
229
|
+
);
|
230
|
+
} else if (responseHttpCode !== 304) {
|
231
|
+
this.sendWithCompression(
|
232
|
+
connection,
|
233
|
+
responseHttpCode,
|
234
|
+
headers,
|
235
|
+
null,
|
236
|
+
fileStream,
|
237
|
+
length,
|
238
|
+
);
|
239
|
+
} else {
|
240
|
+
connection.rawConnection.res.writeHead(
|
241
|
+
responseHttpCode,
|
242
|
+
this.transformHeaders(headers),
|
243
|
+
);
|
244
|
+
connection.rawConnection.res.end();
|
245
|
+
connection.destroy();
|
246
|
+
fileStream.close();
|
247
|
+
}
|
248
|
+
};
|
249
|
+
|
250
|
+
if (error) {
|
251
|
+
connection.rawConnection.responseHttpCode = 404;
|
252
|
+
return sendRequestResult();
|
253
|
+
}
|
254
|
+
|
255
|
+
if (reqHeaders["if-modified-since"]) {
|
256
|
+
ifModifiedSince = new Date(reqHeaders["if-modified-since"]);
|
257
|
+
lastModified.setMilliseconds(0);
|
258
|
+
if (lastModified <= ifModifiedSince) {
|
259
|
+
connection.rawConnection.responseHttpCode = 304;
|
260
|
+
}
|
261
|
+
return sendRequestResult();
|
262
|
+
}
|
263
|
+
|
264
|
+
if (this.config.enableEtag && fileStream && fileStream.path) {
|
265
|
+
const fileStats: fs.Stats = await new Promise((resolve) => {
|
266
|
+
fs.stat(fileStream.path, (error, fileStats) => {
|
267
|
+
if (error || !fileStats) {
|
268
|
+
this.log(
|
269
|
+
"Error receving file statistics: " + String(error),
|
270
|
+
"error",
|
271
|
+
);
|
272
|
+
}
|
273
|
+
return resolve(fileStats);
|
274
|
+
});
|
275
|
+
});
|
276
|
+
|
277
|
+
if (!fileStats) return sendRequestResult();
|
278
|
+
|
279
|
+
const fileEtag = etag(fileStats, { weak: true });
|
280
|
+
connection.rawConnection.responseHeaders.push(["ETag", fileEtag]);
|
281
|
+
let noneMatchHeader = reqHeaders["if-none-match"];
|
282
|
+
const cacheCtrlHeader = reqHeaders["cache-control"];
|
283
|
+
let noCache = false;
|
284
|
+
let etagMatches;
|
285
|
+
// check for no-cache cache request directive
|
286
|
+
if (cacheCtrlHeader && cacheCtrlHeader.indexOf("no-cache") !== -1) {
|
287
|
+
noCache = true;
|
288
|
+
}
|
289
|
+
// parse if-none-match
|
290
|
+
if (noneMatchHeader) {
|
291
|
+
noneMatchHeader = noneMatchHeader.split(/ *, */);
|
292
|
+
}
|
293
|
+
// if-none-match
|
294
|
+
if (noneMatchHeader) {
|
295
|
+
etagMatches = noneMatchHeader.some((match: string) => {
|
296
|
+
return (
|
297
|
+
match === "*" || match === fileEtag || match === "W/" + fileEtag
|
298
|
+
);
|
299
|
+
});
|
300
|
+
}
|
301
|
+
if (etagMatches && !noCache) {
|
302
|
+
connection.rawConnection.responseHttpCode = 304;
|
303
|
+
}
|
304
|
+
sendRequestResult();
|
305
|
+
} else {
|
306
|
+
sendRequestResult();
|
307
|
+
}
|
308
|
+
}
|
309
|
+
|
310
|
+
sendWithCompression(
|
311
|
+
connection: Connection,
|
312
|
+
responseHttpCode: number,
|
313
|
+
headers: Array<[string, string | number]>,
|
314
|
+
stringResponse: string,
|
315
|
+
fileStream?: any,
|
316
|
+
fileLength?: number,
|
317
|
+
) {
|
318
|
+
let acceptEncoding =
|
319
|
+
connection.rawConnection.req.headers["accept-encoding"];
|
320
|
+
let compressor;
|
321
|
+
let stringEncoder;
|
322
|
+
if (!acceptEncoding) {
|
323
|
+
acceptEncoding = "";
|
324
|
+
}
|
325
|
+
|
326
|
+
// Note: this is not a conforming accept-encoding parser.
|
327
|
+
// https://nodejs.org/api/zlib.html#zlib_zlib_createinflate_options
|
328
|
+
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
|
329
|
+
if (this.config.compress === true) {
|
330
|
+
const gzipMatch = acceptEncoding.match(/\bgzip\b/);
|
331
|
+
const deflateMatch = acceptEncoding.match(/\bdeflate\b/);
|
332
|
+
if (
|
333
|
+
(gzipMatch && !deflateMatch) ||
|
334
|
+
(gzipMatch && deflateMatch && gzipMatch.index < deflateMatch.index)
|
335
|
+
) {
|
336
|
+
headers.push(["Content-Encoding", "gzip"]);
|
337
|
+
compressor = zlib.createGzip();
|
338
|
+
stringEncoder = zlib.gzip;
|
339
|
+
} else if (
|
340
|
+
(!gzipMatch && deflateMatch) ||
|
341
|
+
(gzipMatch && deflateMatch && deflateMatch.index < gzipMatch.index)
|
342
|
+
) {
|
343
|
+
headers.push(["Content-Encoding", "deflate"]);
|
344
|
+
compressor = zlib.createDeflate();
|
345
|
+
stringEncoder = zlib.deflate;
|
346
|
+
}
|
347
|
+
}
|
348
|
+
|
349
|
+
// the 'finish' event denotes a successful transfer
|
350
|
+
connection.rawConnection.res.on("finish", () => {
|
351
|
+
connection.destroy();
|
352
|
+
});
|
353
|
+
|
354
|
+
// the 'close' event denotes a failed transfer, but it is probably the client's fault
|
355
|
+
connection.rawConnection.res.on("close", () => {
|
356
|
+
connection.destroy();
|
357
|
+
});
|
358
|
+
|
359
|
+
if (fileStream) {
|
360
|
+
if (compressor) {
|
361
|
+
connection.rawConnection.res.writeHead(
|
362
|
+
responseHttpCode,
|
363
|
+
this.transformHeaders(headers),
|
364
|
+
);
|
365
|
+
fileStream.pipe(compressor).pipe(connection.rawConnection.res);
|
366
|
+
} else {
|
367
|
+
if (fileLength) {
|
368
|
+
headers.push(["Content-Length", fileLength]);
|
369
|
+
}
|
370
|
+
connection.rawConnection.res.writeHead(
|
371
|
+
responseHttpCode,
|
372
|
+
this.transformHeaders(headers),
|
373
|
+
);
|
374
|
+
fileStream.pipe(connection.rawConnection.res);
|
375
|
+
}
|
376
|
+
} else {
|
377
|
+
if (stringEncoder) {
|
378
|
+
stringEncoder(stringResponse, (error, zippedString) => {
|
379
|
+
if (error) {
|
380
|
+
console.error(error);
|
381
|
+
}
|
382
|
+
headers.push(["Content-Length", zippedString.length]);
|
383
|
+
connection.rawConnection.res.writeHead(
|
384
|
+
responseHttpCode,
|
385
|
+
this.transformHeaders(headers),
|
386
|
+
);
|
387
|
+
connection.rawConnection.res.end(zippedString);
|
388
|
+
});
|
389
|
+
} else {
|
390
|
+
headers.push(["Content-Length", Buffer.byteLength(stringResponse)]);
|
391
|
+
connection.rawConnection.res.writeHead(
|
392
|
+
responseHttpCode,
|
393
|
+
this.transformHeaders(headers),
|
394
|
+
);
|
395
|
+
connection.rawConnection.res.end(stringResponse);
|
396
|
+
}
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
handleRequest(req: http.IncomingMessage, res: http.ServerResponse) {
|
401
|
+
const {
|
402
|
+
fingerprint,
|
403
|
+
headersHash,
|
404
|
+
}: { fingerprint: string; headersHash: Record<string, string> } =
|
405
|
+
this.fingerPrinter.fingerprint(req);
|
406
|
+
const responseHeaders = [];
|
407
|
+
const cookies = utils.parseCookies(req);
|
408
|
+
const responseHttpCode = 200;
|
409
|
+
const method = req.method.toUpperCase();
|
410
|
+
|
411
|
+
// waiting until URL() can handle relative paths
|
412
|
+
// https://github.com/nodejs/node/issues/12682
|
413
|
+
const parsedURL = url.parse(req.url, true);
|
414
|
+
let i;
|
415
|
+
for (i in headersHash) {
|
416
|
+
responseHeaders.push([i, headersHash[i]]);
|
417
|
+
}
|
418
|
+
|
419
|
+
// https://github.com/actionhero/actionhero/issues/189
|
420
|
+
responseHeaders.push(["Content-Type", "application/json; charset=utf-8"]);
|
421
|
+
|
422
|
+
for (i in this.config.httpHeaders) {
|
423
|
+
if (this.config.httpHeaders[i]) {
|
424
|
+
responseHeaders.push([i, this.config.httpHeaders[i]]);
|
425
|
+
}
|
426
|
+
}
|
427
|
+
|
428
|
+
// check if this request (http://other-host.com) is in allowedRequestHosts ([https://host.com])
|
429
|
+
if (
|
430
|
+
this.config.allowedRequestHosts &&
|
431
|
+
this.config.allowedRequestHosts.length > 0
|
432
|
+
) {
|
433
|
+
const requestHost = req.headers["x-forwarded-proto"]
|
434
|
+
? req.headers["x-forwarded-proto"] + "://" + req.headers.host
|
435
|
+
: (this.config.secure ? "https://" : "http://") + req.headers.host;
|
436
|
+
|
437
|
+
if (!this.config.allowedRequestHosts.includes(requestHost)) {
|
438
|
+
const newHost = this.config.allowedRequestHosts[0];
|
439
|
+
res.statusCode = 302;
|
440
|
+
res.setHeader("Location", newHost + req.url);
|
441
|
+
return res.end(`You are being redirected to ${newHost + req.url}\r\n`);
|
442
|
+
}
|
443
|
+
}
|
444
|
+
|
445
|
+
const { ip, port } = utils.parseHeadersForClientAddress(req.headers);
|
446
|
+
const messageId = uuid.v4();
|
447
|
+
|
448
|
+
this.buildConnection({
|
449
|
+
rawConnection: {
|
450
|
+
req: req,
|
451
|
+
res: res,
|
452
|
+
params: {},
|
453
|
+
method: method,
|
454
|
+
cookies: cookies,
|
455
|
+
responseHeaders: responseHeaders,
|
456
|
+
responseHttpCode: responseHttpCode,
|
457
|
+
parsedURL: parsedURL,
|
458
|
+
},
|
459
|
+
id: `${fingerprint}-${messageId}`,
|
460
|
+
messageId: messageId,
|
461
|
+
fingerprint: fingerprint,
|
462
|
+
remoteAddress: ip || req.connection.remoteAddress || "0.0.0.0",
|
463
|
+
remotePort: port || req.connection.remotePort || "0",
|
464
|
+
});
|
465
|
+
}
|
466
|
+
|
467
|
+
async completeResponse(data: ActionProcessor<any>) {
|
468
|
+
if (data.toRender !== true) {
|
469
|
+
if (data.connection.rawConnection.res.finished) {
|
470
|
+
data.connection.destroy();
|
471
|
+
} else {
|
472
|
+
data.connection.rawConnection.res.on("finish", () =>
|
473
|
+
data.connection.destroy(),
|
474
|
+
);
|
475
|
+
data.connection.rawConnection.res.on("close", () =>
|
476
|
+
data.connection.destroy(),
|
477
|
+
);
|
478
|
+
}
|
479
|
+
|
480
|
+
return;
|
481
|
+
}
|
482
|
+
|
483
|
+
if (
|
484
|
+
this.config.metadataOptions.serverInformation &&
|
485
|
+
typeof data.response !== "string"
|
486
|
+
) {
|
487
|
+
data.response.serverInformation = this.buildServerInformation(
|
488
|
+
data.connection.connectedAt,
|
489
|
+
);
|
490
|
+
}
|
491
|
+
|
492
|
+
if (
|
493
|
+
this.config.metadataOptions.requesterInformation &&
|
494
|
+
typeof data.response !== "string"
|
495
|
+
) {
|
496
|
+
data.response.requesterInformation = this.buildRequesterInformation(
|
497
|
+
data.connection,
|
498
|
+
);
|
499
|
+
}
|
500
|
+
|
501
|
+
if (data.response.error) {
|
502
|
+
if (
|
503
|
+
this.config.returnErrorCodes === true &&
|
504
|
+
data.connection.rawConnection.responseHttpCode === 200
|
505
|
+
) {
|
506
|
+
const customErrorCode = parseInt(data.response.error.code, 10);
|
507
|
+
const isValidCustomResponseCode =
|
508
|
+
customErrorCode >= 100 && customErrorCode < 600;
|
509
|
+
if (isValidCustomResponseCode) {
|
510
|
+
data.connection.rawConnection.responseHttpCode = customErrorCode;
|
511
|
+
} else if (data.actionStatus === ActionsStatus.UnknownAction) {
|
512
|
+
data.connection.rawConnection.responseHttpCode = 404;
|
513
|
+
} else if (data.actionStatus === ActionsStatus.MissingParams) {
|
514
|
+
data.connection.rawConnection.responseHttpCode = 422;
|
515
|
+
} else {
|
516
|
+
data.connection.rawConnection.responseHttpCode =
|
517
|
+
this.config.defaultErrorStatusCode ?? 500;
|
518
|
+
}
|
519
|
+
}
|
520
|
+
}
|
521
|
+
|
522
|
+
if (
|
523
|
+
!data.response.error &&
|
524
|
+
data.action &&
|
525
|
+
data.params.apiVersion &&
|
526
|
+
api.actions.actions[data.params.action][data.params.apiVersion]
|
527
|
+
.matchExtensionMimeType === true &&
|
528
|
+
data.connection.extension
|
529
|
+
) {
|
530
|
+
const mime = Mime.getType(data.connection.extension);
|
531
|
+
if (mime) {
|
532
|
+
data.connection.rawConnection.responseHeaders.push([
|
533
|
+
"Content-Type",
|
534
|
+
mime,
|
535
|
+
]);
|
536
|
+
}
|
537
|
+
}
|
538
|
+
|
539
|
+
if (data.response.error) {
|
540
|
+
data.response.error = await config.errors.serializers.servers.web(
|
541
|
+
data.response.error,
|
542
|
+
);
|
543
|
+
}
|
544
|
+
|
545
|
+
let stringResponse = "";
|
546
|
+
|
547
|
+
if (this.extractHeader(data.connection, "Content-Type").match(/json/)) {
|
548
|
+
stringResponse = JSON.stringify(data.response, null, this.config.padding);
|
549
|
+
if (data.params.callback) {
|
550
|
+
data.connection.rawConnection.responseHeaders.push([
|
551
|
+
"Content-Type",
|
552
|
+
"application/javascript",
|
553
|
+
]);
|
554
|
+
stringResponse =
|
555
|
+
this.callbackHtmlEscape(data.connection.params.callback) +
|
556
|
+
"(" +
|
557
|
+
stringResponse +
|
558
|
+
");";
|
559
|
+
}
|
560
|
+
} else {
|
561
|
+
stringResponse = data.response as unknown as string;
|
562
|
+
}
|
563
|
+
|
564
|
+
this.sendMessage(data.connection, stringResponse);
|
565
|
+
}
|
566
|
+
|
567
|
+
extractHeader(connection: Connection, match: string) {
|
568
|
+
let i = connection.rawConnection.responseHeaders.length - 1;
|
569
|
+
while (i >= 0) {
|
570
|
+
if (
|
571
|
+
connection.rawConnection.responseHeaders[i][0].toLowerCase() ===
|
572
|
+
match.toLowerCase()
|
573
|
+
) {
|
574
|
+
return connection.rawConnection.responseHeaders[i][1];
|
575
|
+
}
|
576
|
+
i--;
|
577
|
+
}
|
578
|
+
return null;
|
579
|
+
}
|
580
|
+
|
581
|
+
respondToOptions(connection: Connection) {
|
582
|
+
if (
|
583
|
+
!this.config.httpHeaders["Access-Control-Allow-Methods"] &&
|
584
|
+
!this.extractHeader(connection, "Access-Control-Allow-Methods")
|
585
|
+
) {
|
586
|
+
const methods = "HEAD, GET, POST, PATCH, PUT, DELETE, OPTIONS, TRACE";
|
587
|
+
connection.rawConnection.responseHeaders.push([
|
588
|
+
"Access-Control-Allow-Methods",
|
589
|
+
methods,
|
590
|
+
]);
|
591
|
+
}
|
592
|
+
|
593
|
+
if (
|
594
|
+
!this.config.httpHeaders["Access-Control-Allow-Origin"] &&
|
595
|
+
!this.extractHeader(connection, "Access-Control-Allow-Origin")
|
596
|
+
) {
|
597
|
+
const origin = "*";
|
598
|
+
connection.rawConnection.responseHeaders.push([
|
599
|
+
"Access-Control-Allow-Origin",
|
600
|
+
origin,
|
601
|
+
]);
|
602
|
+
}
|
603
|
+
|
604
|
+
this.sendMessage(connection, "");
|
605
|
+
}
|
606
|
+
|
607
|
+
respondToTrace(connection: Connection) {
|
608
|
+
const data = this.buildRequesterInformation(connection);
|
609
|
+
const stringResponse = JSON.stringify(data, null, this.config.padding);
|
610
|
+
this.sendMessage(connection, stringResponse);
|
611
|
+
}
|
612
|
+
|
613
|
+
async determineRequestParams(connection: Connection) {
|
614
|
+
// determine file or api request
|
615
|
+
let requestMode = this.config.rootEndpointType;
|
616
|
+
const pathname = connection.rawConnection.parsedURL.pathname;
|
617
|
+
const pathParts = pathname.split("/");
|
618
|
+
let i;
|
619
|
+
|
620
|
+
while (pathParts[0] === "") {
|
621
|
+
pathParts.shift();
|
622
|
+
}
|
623
|
+
if (pathParts[pathParts.length - 1] === "") {
|
624
|
+
pathParts.pop();
|
625
|
+
}
|
626
|
+
|
627
|
+
let urlPathForActionsParts = [];
|
628
|
+
if (this.config.urlPathForActions) {
|
629
|
+
urlPathForActionsParts = this.config.urlPathForActions.split("/");
|
630
|
+
while (urlPathForActionsParts[0] === "") {
|
631
|
+
urlPathForActionsParts.shift();
|
632
|
+
}
|
633
|
+
}
|
634
|
+
|
635
|
+
let urlPathForFilesParts = [];
|
636
|
+
if (this.config.urlPathForFiles) {
|
637
|
+
urlPathForFilesParts = this.config.urlPathForFiles.split("/");
|
638
|
+
while (urlPathForFilesParts[0] === "") {
|
639
|
+
urlPathForFilesParts.shift();
|
640
|
+
}
|
641
|
+
}
|
642
|
+
|
643
|
+
if (
|
644
|
+
pathParts[0] &&
|
645
|
+
utils.arrayStartingMatch(urlPathForActionsParts, pathParts)
|
646
|
+
) {
|
647
|
+
requestMode = "api";
|
648
|
+
for (i = 0; i < urlPathForActionsParts.length; i++) {
|
649
|
+
pathParts.shift();
|
650
|
+
}
|
651
|
+
} else if (
|
652
|
+
pathParts[0] &&
|
653
|
+
utils.arrayStartingMatch(urlPathForFilesParts, pathParts)
|
654
|
+
) {
|
655
|
+
requestMode = "file";
|
656
|
+
for (i = 0; i < urlPathForFilesParts.length; i++) {
|
657
|
+
pathParts.shift();
|
658
|
+
}
|
659
|
+
}
|
660
|
+
|
661
|
+
const extensionParts =
|
662
|
+
connection.rawConnection.parsedURL.pathname.split(".");
|
663
|
+
if (extensionParts.length > 1) {
|
664
|
+
connection.extension = extensionParts[extensionParts.length - 1];
|
665
|
+
}
|
666
|
+
|
667
|
+
// OPTIONS
|
668
|
+
if (connection.rawConnection.method === "OPTIONS") {
|
669
|
+
requestMode = "options";
|
670
|
+
return requestMode;
|
671
|
+
}
|
672
|
+
|
673
|
+
// API
|
674
|
+
if (requestMode === "api") {
|
675
|
+
if (connection.rawConnection.method === "TRACE") {
|
676
|
+
requestMode = "trace";
|
677
|
+
}
|
678
|
+
|
679
|
+
let search = "";
|
680
|
+
if (connection.rawConnection.parsedURL.search) {
|
681
|
+
search = connection.rawConnection.parsedURL.search.slice(1);
|
682
|
+
}
|
683
|
+
|
684
|
+
this.fillParamsFromWebRequest(
|
685
|
+
connection,
|
686
|
+
qs.parse(search, this.config.queryParseOptions),
|
687
|
+
);
|
688
|
+
connection.rawConnection.params.query =
|
689
|
+
connection.rawConnection.parsedURL.query;
|
690
|
+
if (
|
691
|
+
connection.rawConnection.method !== "GET" &&
|
692
|
+
connection.rawConnection.method !== "HEAD" &&
|
693
|
+
(connection.rawConnection.req.headers["content-type"] ||
|
694
|
+
connection.rawConnection.req.headers["Content-Type"])
|
695
|
+
) {
|
696
|
+
connection.rawConnection.form = new formidable.IncomingForm();
|
697
|
+
if (this.config?.formOptions) {
|
698
|
+
for (i in this.config.formOptions) {
|
699
|
+
connection.rawConnection.form.options[i] =
|
700
|
+
this.config.formOptions[i];
|
701
|
+
}
|
702
|
+
}
|
703
|
+
|
704
|
+
let rawBody = Promise.resolve(Buffer.alloc(0));
|
705
|
+
if (this.config.saveRawBody) {
|
706
|
+
rawBody = new Promise((resolve, reject) => {
|
707
|
+
let fullBody = Buffer.alloc(0);
|
708
|
+
connection.rawConnection.req
|
709
|
+
.on("data", (chunk: Uint8Array) => {
|
710
|
+
fullBody = Buffer.concat([fullBody, chunk]);
|
711
|
+
})
|
712
|
+
.on("end", () => {
|
713
|
+
resolve(fullBody);
|
714
|
+
});
|
715
|
+
});
|
716
|
+
}
|
717
|
+
|
718
|
+
const { fields, files } = (await new Promise((resolve) => {
|
719
|
+
connection.rawConnection.form.parse(
|
720
|
+
connection.rawConnection.req,
|
721
|
+
(
|
722
|
+
error: NodeJS.ErrnoException,
|
723
|
+
fields: string[],
|
724
|
+
files: string[],
|
725
|
+
) => {
|
726
|
+
if (error) {
|
727
|
+
this.log("error processing form: " + String(error), "error");
|
728
|
+
connection.error = new Error(
|
729
|
+
"There was an error processing this form.",
|
730
|
+
);
|
731
|
+
}
|
732
|
+
resolve({ fields, files });
|
733
|
+
},
|
734
|
+
);
|
735
|
+
})) as { fields: string[]; files: string[] };
|
736
|
+
|
737
|
+
connection.rawConnection.params.body = fields;
|
738
|
+
connection.rawConnection.params.rawBody = await rawBody;
|
739
|
+
connection.rawConnection.params.files = files;
|
740
|
+
this.fillParamsFromWebRequest(connection, files);
|
741
|
+
this.fillParamsFromWebRequest(connection, fields);
|
742
|
+
connection.params.action = null;
|
743
|
+
api.routes.processRoute(connection, pathParts);
|
744
|
+
return requestMode;
|
745
|
+
} else {
|
746
|
+
connection.params.action = null;
|
747
|
+
api.routes.processRoute(connection, pathParts);
|
748
|
+
return requestMode;
|
749
|
+
}
|
750
|
+
}
|
751
|
+
|
752
|
+
// FILE
|
753
|
+
if (requestMode === "file") {
|
754
|
+
api.routes.processRoute(connection, pathParts);
|
755
|
+
if (!connection.params.file) {
|
756
|
+
connection.params.file = pathParts.join(path.sep);
|
757
|
+
}
|
758
|
+
if (
|
759
|
+
connection.params.file === "" ||
|
760
|
+
connection.params.file[connection.params.file.length - 1] === "/"
|
761
|
+
) {
|
762
|
+
connection.params.file =
|
763
|
+
connection.params.file + config.general.directoryFileType;
|
764
|
+
}
|
765
|
+
try {
|
766
|
+
connection.params.file = decodeURIComponent(connection.params.file);
|
767
|
+
} catch (e) {
|
768
|
+
connection.error = new Error("There was an error decoding URI: " + e);
|
769
|
+
}
|
770
|
+
return requestMode;
|
771
|
+
}
|
772
|
+
}
|
773
|
+
|
774
|
+
fillParamsFromWebRequest(
|
775
|
+
connection: Connection,
|
776
|
+
varsHash: Record<string, any>,
|
777
|
+
) {
|
778
|
+
// helper for JSON posts
|
779
|
+
const collapsedVarsHash = utils.collapseObjectToArray(varsHash);
|
780
|
+
if (collapsedVarsHash !== false) {
|
781
|
+
varsHash = { payload: collapsedVarsHash }; // post was an array, lets call it "payload"
|
782
|
+
}
|
783
|
+
|
784
|
+
for (const v in varsHash) {
|
785
|
+
connection.params[v] = varsHash[v];
|
786
|
+
}
|
787
|
+
}
|
788
|
+
|
789
|
+
transformHeaders(headersArray: Array<[string, string | number]>) {
|
790
|
+
return headersArray.reduce(
|
791
|
+
(headers: Record<string, string[]>, currentHeader) => {
|
792
|
+
const currentHeaderKey = currentHeader[0].toLowerCase();
|
793
|
+
// we have a set-cookie, let's see what we have to do
|
794
|
+
if (currentHeaderKey === "set-cookie") {
|
795
|
+
if (headers[currentHeaderKey]) {
|
796
|
+
headers[currentHeaderKey].push(currentHeader[1].toString());
|
797
|
+
} else {
|
798
|
+
headers[currentHeaderKey] = [currentHeader[1].toString()];
|
799
|
+
}
|
800
|
+
} else {
|
801
|
+
headers[currentHeaderKey] = [currentHeader[1].toString()];
|
802
|
+
}
|
803
|
+
|
804
|
+
return headers;
|
805
|
+
},
|
806
|
+
{},
|
807
|
+
);
|
808
|
+
}
|
809
|
+
|
810
|
+
buildServerInformation(connectedAt: number) {
|
811
|
+
const stopTime = new Date().getTime();
|
812
|
+
return {
|
813
|
+
serverName: config.general.serverName,
|
814
|
+
apiVersion: config.general.apiVersion,
|
815
|
+
requestDuration: stopTime - connectedAt,
|
816
|
+
currentTime: stopTime,
|
817
|
+
};
|
818
|
+
}
|
819
|
+
|
820
|
+
buildRequesterInformation(connection: Connection) {
|
821
|
+
const requesterInformation = {
|
822
|
+
id: connection.id,
|
823
|
+
fingerprint: connection.fingerprint,
|
824
|
+
messageId: connection.messageId,
|
825
|
+
remoteIP: connection.remoteIP,
|
826
|
+
receivedParams: {} as { [key: string]: any },
|
827
|
+
};
|
828
|
+
|
829
|
+
for (const p in connection.params) {
|
830
|
+
if (
|
831
|
+
config.general.disableParamScrubbing === true ||
|
832
|
+
api.params.postVariables.indexOf(p) >= 0
|
833
|
+
) {
|
834
|
+
requesterInformation.receivedParams[p] = connection.params[p];
|
835
|
+
}
|
836
|
+
}
|
837
|
+
|
838
|
+
return requesterInformation;
|
839
|
+
}
|
840
|
+
|
841
|
+
cleanHeaders(connection: Connection) {
|
842
|
+
const originalHeaders = connection.rawConnection.responseHeaders.reverse();
|
843
|
+
const foundHeaders = [];
|
844
|
+
const cleanedHeaders = [];
|
845
|
+
for (const i in originalHeaders) {
|
846
|
+
const key = originalHeaders[i][0];
|
847
|
+
const value = originalHeaders[i][1];
|
848
|
+
if (
|
849
|
+
foundHeaders.indexOf(key.toLowerCase()) >= 0 &&
|
850
|
+
key.toLowerCase().indexOf("set-cookie") < 0
|
851
|
+
) {
|
852
|
+
// ignore, it's a duplicate
|
853
|
+
} else if (
|
854
|
+
connection.rawConnection.method === "HEAD" &&
|
855
|
+
key === "Transfer-Encoding"
|
856
|
+
) {
|
857
|
+
// ignore, we can't send this header for HEAD requests
|
858
|
+
} else {
|
859
|
+
foundHeaders.push(key.toLowerCase());
|
860
|
+
cleanedHeaders.push([key, value]);
|
861
|
+
}
|
862
|
+
}
|
863
|
+
connection.rawConnection.responseHeaders = cleanedHeaders;
|
864
|
+
}
|
865
|
+
|
866
|
+
cleanSocket(bindIP: string | number, port: string | number) {
|
867
|
+
if (!bindIP && typeof port === "string" && port.indexOf("/") >= 0) {
|
868
|
+
fs.unlink(port, (error) => {
|
869
|
+
if (error) {
|
870
|
+
this.log(`cannot remove stale socket @ ${port}: ${error}`, "error");
|
871
|
+
} else {
|
872
|
+
this.log(`removed stale unix socket @ ${port}`);
|
873
|
+
}
|
874
|
+
});
|
875
|
+
}
|
876
|
+
}
|
877
|
+
|
878
|
+
chmodSocket(bindIP: string | number, port: string | number) {
|
879
|
+
if (!bindIP && typeof port === "string" && port.indexOf("/") >= 0) {
|
880
|
+
fs.chmodSync(port, "0777");
|
881
|
+
}
|
882
|
+
}
|
883
|
+
|
884
|
+
callbackHtmlEscape(s: string) {
|
885
|
+
return s
|
886
|
+
.replace(/&/g, "&")
|
887
|
+
.replace(/"/g, """)
|
888
|
+
.replace(/'/g, "'")
|
889
|
+
.replace(/</g, "<")
|
890
|
+
.replace(/>/g, ">")
|
891
|
+
.replace(/\)/g, "")
|
892
|
+
.replace(/\(/g, "");
|
893
|
+
}
|
894
|
+
}
|