http-proxy-middleware 3.0.4 → 3.0.5
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.
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type * as http from 'http';
|
|
2
|
-
import type { Options } from '../types';
|
|
3
2
|
export type BodyParserLikeRequest = http.IncomingMessage & {
|
|
4
3
|
body?: any;
|
|
5
4
|
};
|
|
6
5
|
/**
|
|
7
6
|
* Fix proxied body if bodyParser is involved.
|
|
8
7
|
*/
|
|
9
|
-
export declare function fixRequestBody<TReq extends BodyParserLikeRequest = BodyParserLikeRequest>(proxyReq: http.ClientRequest, req: TReq
|
|
8
|
+
export declare function fixRequestBody<TReq extends BodyParserLikeRequest = BodyParserLikeRequest>(proxyReq: http.ClientRequest, req: TReq): void;
|
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fixRequestBody = fixRequestBody;
|
|
4
4
|
const querystring = require("querystring");
|
|
5
|
-
const logger_1 = require("../logger");
|
|
6
5
|
/**
|
|
7
6
|
* Fix proxied body if bodyParser is involved.
|
|
8
7
|
*/
|
|
9
|
-
function fixRequestBody(proxyReq, req
|
|
8
|
+
function fixRequestBody(proxyReq, req) {
|
|
9
|
+
// skip fixRequestBody() when req.readableLength not 0 (bodyParser failure)
|
|
10
|
+
if (req.readableLength !== 0) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
10
13
|
const requestBody = req.body;
|
|
11
14
|
if (!requestBody) {
|
|
12
15
|
return;
|
|
@@ -15,19 +18,6 @@ function fixRequestBody(proxyReq, req, res, options) {
|
|
|
15
18
|
if (!contentType) {
|
|
16
19
|
return;
|
|
17
20
|
}
|
|
18
|
-
const logger = (0, logger_1.getLogger)(options);
|
|
19
|
-
// Handle bad request when unexpected "Connect: Upgrade" header is provided
|
|
20
|
-
if (/upgrade/gi.test(proxyReq.getHeader('Connection'))) {
|
|
21
|
-
handleBadRequest({ proxyReq, req, res });
|
|
22
|
-
logger.error(`[HPM] HPM_UNEXPECTED_CONNECTION_UPGRADE_HEADER. Aborted request: ${req.url}`);
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
// Handle bad request when invalid request body is provided
|
|
26
|
-
if (hasInvalidKeys(requestBody)) {
|
|
27
|
-
handleBadRequest({ proxyReq, req, res });
|
|
28
|
-
logger.error(`[HPM] HPM_INVALID_REQUEST_DATA. Aborted request: ${req.url}`);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
21
|
const writeBody = (bodyData) => {
|
|
32
22
|
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
|
|
33
23
|
proxyReq.write(bodyData);
|
|
@@ -58,12 +48,3 @@ function handlerFormDataBodyData(contentType, data) {
|
|
|
58
48
|
}
|
|
59
49
|
return str;
|
|
60
50
|
}
|
|
61
|
-
function hasInvalidKeys(obj) {
|
|
62
|
-
return Object.keys(obj).some((key) => /[\n\r]/.test(key));
|
|
63
|
-
}
|
|
64
|
-
function handleBadRequest({ proxyReq, req, res }) {
|
|
65
|
-
res.writeHead(400);
|
|
66
|
-
res.end('Bad Request');
|
|
67
|
-
proxyReq.destroy();
|
|
68
|
-
req.destroy();
|
|
69
|
-
}
|
package/package.json
CHANGED