@ohos-ports/http-deceiver 1.2.7-beta.1 → 1.2.7-beta.2
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/lib/deceiver.js +60 -24
- package/package.json +2 -2
- package/test/api-test.js +8 -3
package/lib/deceiver.js
CHANGED
|
@@ -14,39 +14,33 @@ var HTTPParser;
|
|
|
14
14
|
var methods;
|
|
15
15
|
var reverseMethods;
|
|
16
16
|
|
|
17
|
+
// llhttp-era Node (>= 13) gates upgrade delivery on request-body completion
|
|
18
|
+
// (see _http_server.js onParserExecuteCommon); real parsers complete upgrade
|
|
19
|
+
// requests at headers, and the migrated code must do the same.
|
|
20
|
+
var llhttp = false;
|
|
21
|
+
|
|
17
22
|
var kOnHeaders;
|
|
18
23
|
var kOnHeadersComplete;
|
|
19
24
|
var kOnMessageComplete;
|
|
20
25
|
var kOnBody;
|
|
21
26
|
if (mode === 'normal' || mode === 'modern') {
|
|
22
|
-
// Node.js >= 12 replaced the internal `http_parser` binding with llhttp and
|
|
23
|
-
// `process.binding('http_parser')` throws. The llhttp parser instances
|
|
24
|
-
// still use the same numeric callback-slot constants and the same 9-argument
|
|
25
|
-
// `onHeadersComplete` signature as the old binding-based parser, and the
|
|
26
|
-
// method-id table is exported as the public `http.METHODS` array (ordered by
|
|
27
|
-
// method id), so fall back to those when the binding is unavailable.
|
|
28
|
-
var httpParserBinding = null;
|
|
29
27
|
try {
|
|
30
|
-
|
|
31
|
-
} catch (e) {
|
|
32
|
-
httpParserBinding = null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (httpParserBinding) {
|
|
36
|
-
HTTPParser = httpParserBinding.HTTPParser;
|
|
28
|
+
HTTPParser = process.binding('http_parser').HTTPParser;
|
|
37
29
|
methods = HTTPParser.methods;
|
|
38
30
|
|
|
39
31
|
// v6
|
|
40
32
|
if (!methods)
|
|
41
|
-
methods =
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
33
|
+
methods = process.binding('http_parser').methods;
|
|
34
|
+
} catch (e) {
|
|
35
|
+
// Node >= 13 replaced the `http_parser` binding with llhttp. The
|
|
36
|
+
// internal `_http_common` module exposes the parser class (with the
|
|
37
|
+
// kOn* callback-slot constants) and the llhttp enum-ordered method
|
|
38
|
+
// table that numeric method ids map through. `http.METHODS` is
|
|
39
|
+
// alphabetically sorted and does NOT match method ids.
|
|
40
|
+
var common = require('_http_common');
|
|
41
|
+
methods = common.methods;
|
|
42
|
+
HTTPParser = common.HTTPParser;
|
|
43
|
+
llhttp = true;
|
|
50
44
|
}
|
|
51
45
|
|
|
52
46
|
reverseMethods = {};
|
|
@@ -107,8 +101,21 @@ if (mode === 'modern') {
|
|
|
107
101
|
|
|
108
102
|
parser.execute = null;
|
|
109
103
|
|
|
104
|
+
// Node >= 24 http servers natively consume StreamBase-backed sockets
|
|
105
|
+
// (parser.consume(socket._handle)) - raw bytes then go straight to the
|
|
106
|
+
// C++ llhttp and never reach the JS-level parser.execute that is
|
|
107
|
+
// replaced below. Undo the native consumption (Node runs the same
|
|
108
|
+
// unconsume() whenever user code adds a data/readable listener) so
|
|
109
|
+
// that socket data flows through the JS parser path again.
|
|
110
|
+
if (parser._consumed) {
|
|
111
|
+
var unconsumeListener = function unconsumeListener() {};
|
|
112
|
+
this.socket.on('data', unconsumeListener);
|
|
113
|
+
this.socket.removeListener('data', unconsumeListener);
|
|
114
|
+
}
|
|
115
|
+
|
|
110
116
|
var self = this;
|
|
111
117
|
var method = reverseMethods[request.method];
|
|
118
|
+
var upgrade = self._isUpgrade(request);
|
|
112
119
|
parser.execute = function execute() {
|
|
113
120
|
self._skipExecute(this);
|
|
114
121
|
this[kOnHeadersComplete](1,
|
|
@@ -118,8 +125,13 @@ if (mode === 'modern') {
|
|
|
118
125
|
request.path,
|
|
119
126
|
0,
|
|
120
127
|
'',
|
|
121
|
-
|
|
128
|
+
upgrade,
|
|
122
129
|
true);
|
|
130
|
+
// Mimic the real parser: upgrade requests complete at headers, so the
|
|
131
|
+
// server hands the raw socket (not a gated UpgradeStream wrapper) to
|
|
132
|
+
// the 'upgrade'/'connect' listener. llhttp-era Node only.
|
|
133
|
+
if (llhttp && upgrade)
|
|
134
|
+
this[kOnMessageComplete]();
|
|
123
135
|
return 0;
|
|
124
136
|
};
|
|
125
137
|
|
|
@@ -246,6 +258,30 @@ Deceiver.prototype._skipExecute = function _skipExecute(parser) {
|
|
|
246
258
|
this.execute = oldExecute;
|
|
247
259
|
this.finish = oldFinish;
|
|
248
260
|
self.emitMessageComplete();
|
|
261
|
+
|
|
262
|
+
// Deceived requests complete at socket EOF (this method), i.e. after
|
|
263
|
+
// Node's socketOnEnd received the FIN but right before it calls
|
|
264
|
+
// socket.end(). A response written from the async req.on('end')
|
|
265
|
+
// handler would be dropped by writeAfterFIN (EPIPE) if the socket
|
|
266
|
+
// ended now; real requests complete during data processing, so
|
|
267
|
+
// responses still go out first. Mirror http.Server's
|
|
268
|
+
// httpAllowHalfOpen handling for this socket: make socketOnEnd mark
|
|
269
|
+
// the pending response _last (Node then ends the socket once that
|
|
270
|
+
// response finishes) instead of ending it right away. The flag is
|
|
271
|
+
// only read synchronously by socketOnEnd; restore it on nextTick.
|
|
272
|
+
var sock = self.socket;
|
|
273
|
+
var res = sock._httpMessage;
|
|
274
|
+
var srv = sock.server;
|
|
275
|
+
if (srv && res && !res.finished) {
|
|
276
|
+
var flipped = !srv.httpAllowHalfOpen;
|
|
277
|
+
if (flipped) {
|
|
278
|
+
srv.httpAllowHalfOpen = true;
|
|
279
|
+
process.nextTick(function restoreHalfOpen() {
|
|
280
|
+
if (srv.httpAllowHalfOpen)
|
|
281
|
+
srv.httpAllowHalfOpen = false;
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
249
285
|
};
|
|
250
286
|
};
|
|
251
287
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/http-deceiver",
|
|
3
|
-
"version": "1.2.7-beta.
|
|
3
|
+
"version": "1.2.7-beta.2",
|
|
4
4
|
"description": "Deceive HTTP parser",
|
|
5
5
|
"main": "lib/deceiver.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/indutny/http-deceiver#readme",
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"handle-thing": "
|
|
26
|
+
"handle-thing": "npm:@ohos-ports/handle-thing@^2.0.1-beta.0",
|
|
27
27
|
"mocha": "^2.2.5",
|
|
28
28
|
"readable-stream": "^2.0.1",
|
|
29
29
|
"stream-pair": "^1.0.0"
|
package/test/api-test.js
CHANGED
|
@@ -31,7 +31,7 @@ describe('HTTP Deceiver', function() {
|
|
|
31
31
|
server.on('request', function(req, res) {
|
|
32
32
|
assert.equal(req.method, 'PUT');
|
|
33
33
|
assert.equal(req.url, '/hello');
|
|
34
|
-
assert.deepEqual(req.headers, { a: 'b' });
|
|
34
|
+
assert.deepEqual(req.headers, { host: 'localhost', a: 'b' });
|
|
35
35
|
|
|
36
36
|
done();
|
|
37
37
|
});
|
|
@@ -40,6 +40,7 @@ describe('HTTP Deceiver', function() {
|
|
|
40
40
|
method: 'PUT',
|
|
41
41
|
path: '/hello',
|
|
42
42
|
headers: {
|
|
43
|
+
host: 'localhost',
|
|
43
44
|
a: 'b'
|
|
44
45
|
}
|
|
45
46
|
});
|
|
@@ -79,7 +80,7 @@ describe('HTTP Deceiver', function() {
|
|
|
79
80
|
server.on('request', function(req, res) {
|
|
80
81
|
assert.equal(req.method, 'PUT');
|
|
81
82
|
assert.equal(req.url, '/hello');
|
|
82
|
-
assert.deepEqual(req.headers, { a: 'b' });
|
|
83
|
+
assert.deepEqual(req.headers, { host: 'localhost', a: 'b' });
|
|
83
84
|
|
|
84
85
|
var actual = '';
|
|
85
86
|
req.on('data', function(chunk) {
|
|
@@ -95,6 +96,7 @@ describe('HTTP Deceiver', function() {
|
|
|
95
96
|
method: 'PUT',
|
|
96
97
|
path: '/hello',
|
|
97
98
|
headers: {
|
|
99
|
+
host: 'localhost',
|
|
98
100
|
a: 'b'
|
|
99
101
|
}
|
|
100
102
|
});
|
|
@@ -118,7 +120,7 @@ describe('HTTP Deceiver', function() {
|
|
|
118
120
|
|
|
119
121
|
server.emit('connection', socket);
|
|
120
122
|
|
|
121
|
-
pair.end('PUT /second HTTP/1.1\r\nContent-Length:11\r\n\r\nhello world');
|
|
123
|
+
pair.end('PUT /second HTTP/1.1\r\nHost: localhost\r\nContent-Length:11\r\n\r\nhello world');
|
|
122
124
|
}
|
|
123
125
|
|
|
124
126
|
server.on('request', function(req, res) {
|
|
@@ -140,6 +142,7 @@ describe('HTTP Deceiver', function() {
|
|
|
140
142
|
method: 'PUT',
|
|
141
143
|
path: '/first',
|
|
142
144
|
headers: {
|
|
145
|
+
host: 'localhost',
|
|
143
146
|
a: 'b'
|
|
144
147
|
}
|
|
145
148
|
});
|
|
@@ -163,6 +166,7 @@ describe('HTTP Deceiver', function() {
|
|
|
163
166
|
method: 'CONNECT',
|
|
164
167
|
path: '/hello',
|
|
165
168
|
headers: {
|
|
169
|
+
host: 'localhost'
|
|
166
170
|
}
|
|
167
171
|
});
|
|
168
172
|
});
|
|
@@ -185,6 +189,7 @@ describe('HTTP Deceiver', function() {
|
|
|
185
189
|
method: 'POST',
|
|
186
190
|
path: '/hello',
|
|
187
191
|
headers: {
|
|
192
|
+
host: 'localhost',
|
|
188
193
|
'upgrade': 'websocket'
|
|
189
194
|
}
|
|
190
195
|
});
|