@ohos-ports/http-deceiver 0.0.0-stage → 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/.travis.yml +5 -0
- package/README.md +30 -2
- package/lib/deceiver.js +309 -0
- package/package.json +29 -4
- package/test/api-test.js +231 -0
package/.travis.yml
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# HTTP Deceiver
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](http://travis-ci.org/indutny/http-deceiver)
|
|
4
|
+
[](http://badge.fury.io/js/http-deceiver)
|
|
5
|
+
|
|
6
|
+
Deceive!
|
|
7
|
+
|
|
8
|
+
## LICENSE
|
|
9
|
+
|
|
10
|
+
This software is licensed under the MIT License.
|
|
11
|
+
|
|
12
|
+
Copyright Fedor Indutny, 2015.
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
|
15
|
+
copy of this software and associated documentation files (the
|
|
16
|
+
"Software"), to deal in the Software without restriction, including
|
|
17
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
18
|
+
distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
19
|
+
persons to whom the Software is furnished to do so, subject to the
|
|
20
|
+
following conditions:
|
|
21
|
+
|
|
22
|
+
The above copyright notice and this permission notice shall be included
|
|
23
|
+
in all copies or substantial portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
26
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
27
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
28
|
+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
29
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
30
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
31
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/lib/deceiver.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var util = require('util');
|
|
3
|
+
|
|
4
|
+
var Buffer = require('buffer').Buffer;
|
|
5
|
+
|
|
6
|
+
// Node.js version
|
|
7
|
+
var mode = /^v0\.8\./.test(process.version) ? 'rusty' :
|
|
8
|
+
/^v0\.(9|10)\./.test(process.version) ? 'old' :
|
|
9
|
+
/^v0\.12\./.test(process.version) ? 'normal' :
|
|
10
|
+
'modern';
|
|
11
|
+
|
|
12
|
+
var HTTPParser;
|
|
13
|
+
|
|
14
|
+
var methods;
|
|
15
|
+
var reverseMethods;
|
|
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
|
+
|
|
22
|
+
var kOnHeaders;
|
|
23
|
+
var kOnHeadersComplete;
|
|
24
|
+
var kOnMessageComplete;
|
|
25
|
+
var kOnBody;
|
|
26
|
+
if (mode === 'normal' || mode === 'modern') {
|
|
27
|
+
try {
|
|
28
|
+
HTTPParser = process.binding('http_parser').HTTPParser;
|
|
29
|
+
methods = HTTPParser.methods;
|
|
30
|
+
|
|
31
|
+
// v6
|
|
32
|
+
if (!methods)
|
|
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;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
reverseMethods = {};
|
|
47
|
+
|
|
48
|
+
methods.forEach(function(method, index) {
|
|
49
|
+
reverseMethods[method] = index;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
53
|
+
kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
54
|
+
kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
|
55
|
+
kOnBody = HTTPParser.kOnBody | 0;
|
|
56
|
+
} else {
|
|
57
|
+
kOnHeaders = 'onHeaders';
|
|
58
|
+
kOnHeadersComplete = 'onHeadersComplete';
|
|
59
|
+
kOnMessageComplete = 'onMessageComplete';
|
|
60
|
+
kOnBody = 'onBody';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function Deceiver(socket, options) {
|
|
64
|
+
this.socket = socket;
|
|
65
|
+
this.options = options || {};
|
|
66
|
+
this.isClient = this.options.isClient;
|
|
67
|
+
}
|
|
68
|
+
module.exports = Deceiver;
|
|
69
|
+
|
|
70
|
+
Deceiver.create = function create(stream, options) {
|
|
71
|
+
return new Deceiver(stream, options);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
Deceiver.prototype._toHeaderList = function _toHeaderList(object) {
|
|
75
|
+
var out = [];
|
|
76
|
+
var keys = Object.keys(object);
|
|
77
|
+
|
|
78
|
+
for (var i = 0; i < keys.length; i++)
|
|
79
|
+
out.push(keys[i], object[keys[i]]);
|
|
80
|
+
|
|
81
|
+
return out;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
Deceiver.prototype._isUpgrade = function _isUpgrade(request) {
|
|
85
|
+
return request.method === 'CONNECT' ||
|
|
86
|
+
request.headers.upgrade ||
|
|
87
|
+
request.headers.connection &&
|
|
88
|
+
/(^|\W)upgrade(\W|$)/i.test(request.headers.connection);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// TODO(indutny): support CONNECT
|
|
92
|
+
if (mode === 'modern') {
|
|
93
|
+
/*
|
|
94
|
+
function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
|
|
95
|
+
url, statusCode, statusMessage, upgrade,
|
|
96
|
+
shouldKeepAlive) {
|
|
97
|
+
*/
|
|
98
|
+
Deceiver.prototype.emitRequest = function emitRequest(request) {
|
|
99
|
+
var parser = this.socket.parser;
|
|
100
|
+
assert(parser, 'No parser present');
|
|
101
|
+
|
|
102
|
+
parser.execute = null;
|
|
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
|
+
|
|
116
|
+
var self = this;
|
|
117
|
+
var method = reverseMethods[request.method];
|
|
118
|
+
var upgrade = self._isUpgrade(request);
|
|
119
|
+
parser.execute = function execute() {
|
|
120
|
+
self._skipExecute(this);
|
|
121
|
+
this[kOnHeadersComplete](1,
|
|
122
|
+
1,
|
|
123
|
+
self._toHeaderList(request.headers),
|
|
124
|
+
method,
|
|
125
|
+
request.path,
|
|
126
|
+
0,
|
|
127
|
+
'',
|
|
128
|
+
upgrade,
|
|
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]();
|
|
135
|
+
return 0;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
this._emitEmpty();
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
Deceiver.prototype.emitResponse = function emitResponse(response) {
|
|
142
|
+
var parser = this.socket.parser;
|
|
143
|
+
assert(parser, 'No parser present');
|
|
144
|
+
|
|
145
|
+
parser.execute = null;
|
|
146
|
+
|
|
147
|
+
var self = this;
|
|
148
|
+
parser.execute = function execute() {
|
|
149
|
+
self._skipExecute(this);
|
|
150
|
+
this[kOnHeadersComplete](1,
|
|
151
|
+
1,
|
|
152
|
+
self._toHeaderList(response.headers),
|
|
153
|
+
response.path,
|
|
154
|
+
response.code,
|
|
155
|
+
response.status,
|
|
156
|
+
response.reason || '',
|
|
157
|
+
self._isUpgrade(response),
|
|
158
|
+
true);
|
|
159
|
+
return 0;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
this._emitEmpty();
|
|
163
|
+
};
|
|
164
|
+
} else {
|
|
165
|
+
/*
|
|
166
|
+
`function parserOnHeadersComplete(info) {`
|
|
167
|
+
|
|
168
|
+
info = { .versionMajor, .versionMinor, .url, .headers, .method,
|
|
169
|
+
.statusCode, .statusMessage, .upgrade, .shouldKeepAlive }
|
|
170
|
+
*/
|
|
171
|
+
Deceiver.prototype.emitRequest = function emitRequest(request) {
|
|
172
|
+
var parser = this.socket.parser;
|
|
173
|
+
assert(parser, 'No parser present');
|
|
174
|
+
|
|
175
|
+
var method = request.method;
|
|
176
|
+
if (reverseMethods)
|
|
177
|
+
method = reverseMethods[method];
|
|
178
|
+
|
|
179
|
+
var info = {
|
|
180
|
+
versionMajor: 1,
|
|
181
|
+
versionMinor: 1,
|
|
182
|
+
url: request.path,
|
|
183
|
+
headers: this._toHeaderList(request.headers),
|
|
184
|
+
method: method,
|
|
185
|
+
statusCode: 0,
|
|
186
|
+
statusMessage: '',
|
|
187
|
+
upgrade: this._isUpgrade(request),
|
|
188
|
+
shouldKeepAlive: true
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
var self = this;
|
|
192
|
+
parser.execute = function execute() {
|
|
193
|
+
self._skipExecute(this);
|
|
194
|
+
this[kOnHeadersComplete](info);
|
|
195
|
+
return 0;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
this._emitEmpty();
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
Deceiver.prototype.emitResponse = function emitResponse(response) {
|
|
202
|
+
var parser = this.socket.parser;
|
|
203
|
+
assert(parser, 'No parser present');
|
|
204
|
+
|
|
205
|
+
var info = {
|
|
206
|
+
versionMajor: 1,
|
|
207
|
+
versionMinor: 1,
|
|
208
|
+
url: response.path,
|
|
209
|
+
headers: this._toHeaderList(response.headers),
|
|
210
|
+
method: false,
|
|
211
|
+
statusCode: response.status,
|
|
212
|
+
statusMessage: response.reason || '',
|
|
213
|
+
upgrade: this._isUpgrade(response),
|
|
214
|
+
shouldKeepAlive: true
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
var self = this;
|
|
218
|
+
parser.execute = function execute() {
|
|
219
|
+
self._skipExecute(this);
|
|
220
|
+
this[kOnHeadersComplete](info);
|
|
221
|
+
return 0;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
this._emitEmpty();
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
Deceiver.prototype._skipExecute = function _skipExecute(parser) {
|
|
229
|
+
var self = this;
|
|
230
|
+
var oldExecute = parser.constructor.prototype.execute;
|
|
231
|
+
var oldFinish = parser.constructor.prototype.finish;
|
|
232
|
+
|
|
233
|
+
parser.execute = null;
|
|
234
|
+
parser.finish = null;
|
|
235
|
+
|
|
236
|
+
parser.execute = function execute(buffer, start, len) {
|
|
237
|
+
// Parser reuse
|
|
238
|
+
if (this.socket !== self.socket) {
|
|
239
|
+
this.execute = oldExecute;
|
|
240
|
+
this.finish = oldFinish;
|
|
241
|
+
return this.execute(buffer, start, len);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (start !== undefined)
|
|
245
|
+
buffer = buffer.slice(start, start + len);
|
|
246
|
+
self.emitBody(buffer);
|
|
247
|
+
return len;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
parser.finish = function finish() {
|
|
251
|
+
// Parser reuse
|
|
252
|
+
if (this.socket !== self.socket) {
|
|
253
|
+
this.execute = oldExecute;
|
|
254
|
+
this.finish = oldFinish;
|
|
255
|
+
return this.finish();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.execute = oldExecute;
|
|
259
|
+
this.finish = oldFinish;
|
|
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
|
+
}
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
Deceiver.prototype.emitBody = function emitBody(buffer) {
|
|
289
|
+
var parser = this.socket.parser;
|
|
290
|
+
assert(parser, 'No parser present');
|
|
291
|
+
|
|
292
|
+
parser[kOnBody](buffer, 0, buffer.length);
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
Deceiver.prototype._emitEmpty = function _emitEmpty() {
|
|
296
|
+
// Emit data to force out handling of UPGRADE
|
|
297
|
+
var empty = new Buffer(0);
|
|
298
|
+
if (this.socket.ondata)
|
|
299
|
+
this.socket.ondata(empty, 0, 0);
|
|
300
|
+
else
|
|
301
|
+
this.socket.emit('data', empty);
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
Deceiver.prototype.emitMessageComplete = function emitMessageComplete() {
|
|
305
|
+
var parser = this.socket.parser;
|
|
306
|
+
assert(parser, 'No parser present');
|
|
307
|
+
|
|
308
|
+
parser[kOnMessageComplete]();
|
|
309
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/http-deceiver",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.2.7-beta.2",
|
|
4
|
+
"description": "Deceive HTTP parser",
|
|
5
|
+
"main": "lib/deceiver.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "mocha --reporter=spec test/*-test.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/ohos-ports/ohos-ports.git",
|
|
12
|
+
"directory": "ports/http-deceiver/1.2.7"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"http",
|
|
16
|
+
"net",
|
|
17
|
+
"deceive"
|
|
18
|
+
],
|
|
19
|
+
"author": "Fedor Indutny <fedor@indutny.com>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/indutny/http-deceiver#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"handle-thing": "npm:@ohos-ports/handle-thing@^2.0.1-beta.0",
|
|
27
|
+
"mocha": "^2.2.5",
|
|
28
|
+
"readable-stream": "^2.0.1",
|
|
29
|
+
"stream-pair": "^1.0.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/test/api-test.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var net = require('net');
|
|
3
|
+
var http = require('http');
|
|
4
|
+
var streamPair = require('stream-pair');
|
|
5
|
+
var thing = require('handle-thing');
|
|
6
|
+
|
|
7
|
+
var httpDeceiver = require('../');
|
|
8
|
+
|
|
9
|
+
describe('HTTP Deceiver', function() {
|
|
10
|
+
var handle;
|
|
11
|
+
var pair;
|
|
12
|
+
var socket;
|
|
13
|
+
var deceiver;
|
|
14
|
+
|
|
15
|
+
beforeEach(function() {
|
|
16
|
+
pair = streamPair.create();
|
|
17
|
+
handle = thing.create(pair.other);
|
|
18
|
+
socket = new net.Socket({ handle: handle });
|
|
19
|
+
|
|
20
|
+
// For v0.8
|
|
21
|
+
socket.readable = true;
|
|
22
|
+
socket.writable = true;
|
|
23
|
+
|
|
24
|
+
deceiver = httpDeceiver.create(socket);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should emit request', function(done) {
|
|
28
|
+
var server = http.createServer();
|
|
29
|
+
server.emit('connection', socket);
|
|
30
|
+
|
|
31
|
+
server.on('request', function(req, res) {
|
|
32
|
+
assert.equal(req.method, 'PUT');
|
|
33
|
+
assert.equal(req.url, '/hello');
|
|
34
|
+
assert.deepEqual(req.headers, { host: 'localhost', a: 'b' });
|
|
35
|
+
|
|
36
|
+
done();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
deceiver.emitRequest({
|
|
40
|
+
method: 'PUT',
|
|
41
|
+
path: '/hello',
|
|
42
|
+
headers: {
|
|
43
|
+
host: 'localhost',
|
|
44
|
+
a: 'b'
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should emit response', function(done) {
|
|
50
|
+
var agent = new http.Agent();
|
|
51
|
+
agent.createConnection = function createConnection() {
|
|
52
|
+
return socket;
|
|
53
|
+
};
|
|
54
|
+
var client = http.request({
|
|
55
|
+
method: 'POST',
|
|
56
|
+
path: '/ok',
|
|
57
|
+
agent: agent
|
|
58
|
+
}, function(res) {
|
|
59
|
+
assert.equal(res.statusCode, 421);
|
|
60
|
+
assert.deepEqual(res.headers, { a: 'b' });
|
|
61
|
+
|
|
62
|
+
done();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
process.nextTick(function() {
|
|
66
|
+
deceiver.emitResponse({
|
|
67
|
+
status: 421,
|
|
68
|
+
reason: 'F',
|
|
69
|
+
headers: {
|
|
70
|
+
a: 'b'
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should override .execute and .finish', function(done) {
|
|
77
|
+
var server = http.createServer();
|
|
78
|
+
server.emit('connection', socket);
|
|
79
|
+
|
|
80
|
+
server.on('request', function(req, res) {
|
|
81
|
+
assert.equal(req.method, 'PUT');
|
|
82
|
+
assert.equal(req.url, '/hello');
|
|
83
|
+
assert.deepEqual(req.headers, { host: 'localhost', a: 'b' });
|
|
84
|
+
|
|
85
|
+
var actual = '';
|
|
86
|
+
req.on('data', function(chunk) {
|
|
87
|
+
actual += chunk;
|
|
88
|
+
});
|
|
89
|
+
req.once('end', function() {
|
|
90
|
+
assert.equal(actual, 'hello world');
|
|
91
|
+
done();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
deceiver.emitRequest({
|
|
96
|
+
method: 'PUT',
|
|
97
|
+
path: '/hello',
|
|
98
|
+
headers: {
|
|
99
|
+
host: 'localhost',
|
|
100
|
+
a: 'b'
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
pair.write('hello');
|
|
105
|
+
pair.end(' world');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should work with reusing parser', function(done) {
|
|
109
|
+
var server = http.createServer();
|
|
110
|
+
server.emit('connection', socket);
|
|
111
|
+
|
|
112
|
+
function secondRequest() {
|
|
113
|
+
pair = streamPair.create();
|
|
114
|
+
handle = thing.create(pair.other);
|
|
115
|
+
socket = new net.Socket({ handle: handle });
|
|
116
|
+
|
|
117
|
+
// For v0.8
|
|
118
|
+
socket.readable = true;
|
|
119
|
+
socket.writable = true;
|
|
120
|
+
|
|
121
|
+
server.emit('connection', socket);
|
|
122
|
+
|
|
123
|
+
pair.end('PUT /second HTTP/1.1\r\nHost: localhost\r\nContent-Length:11\r\n\r\nhello world');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
server.on('request', function(req, res) {
|
|
127
|
+
var actual = '';
|
|
128
|
+
req.on('data', function(chunk) {
|
|
129
|
+
actual += chunk;
|
|
130
|
+
});
|
|
131
|
+
req.once('end', function() {
|
|
132
|
+
assert.equal(actual, 'hello world');
|
|
133
|
+
|
|
134
|
+
if (req.url === '/first')
|
|
135
|
+
secondRequest();
|
|
136
|
+
else
|
|
137
|
+
done();
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
deceiver.emitRequest({
|
|
142
|
+
method: 'PUT',
|
|
143
|
+
path: '/first',
|
|
144
|
+
headers: {
|
|
145
|
+
host: 'localhost',
|
|
146
|
+
a: 'b'
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
pair.write('hello');
|
|
151
|
+
pair.end(' world');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should emit CONNECT request', function(done) {
|
|
155
|
+
var server = http.createServer();
|
|
156
|
+
server.emit('connection', socket);
|
|
157
|
+
|
|
158
|
+
server.on('connect', function(req, socket, bodyHead) {
|
|
159
|
+
assert.equal(req.method, 'CONNECT');
|
|
160
|
+
assert.equal(req.url, '/hello');
|
|
161
|
+
|
|
162
|
+
done();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
deceiver.emitRequest({
|
|
166
|
+
method: 'CONNECT',
|
|
167
|
+
path: '/hello',
|
|
168
|
+
headers: {
|
|
169
|
+
host: 'localhost'
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should emit Upgrade request', function(done) {
|
|
175
|
+
var server = http.createServer();
|
|
176
|
+
server.emit('connection', socket);
|
|
177
|
+
|
|
178
|
+
server.on('upgrade', function(req, socket, bodyHead) {
|
|
179
|
+
assert.equal(req.method, 'POST');
|
|
180
|
+
assert.equal(req.url, '/hello');
|
|
181
|
+
|
|
182
|
+
socket.on('data', function(chunk) {
|
|
183
|
+
assert.equal(chunk + '', 'hm');
|
|
184
|
+
done();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
deceiver.emitRequest({
|
|
189
|
+
method: 'POST',
|
|
190
|
+
path: '/hello',
|
|
191
|
+
headers: {
|
|
192
|
+
host: 'localhost',
|
|
193
|
+
'upgrade': 'websocket'
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
pair.write('hm');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should emit Upgrade response', function(done) {
|
|
201
|
+
var agent = new http.Agent();
|
|
202
|
+
agent.createConnection = function createConnection() {
|
|
203
|
+
return socket;
|
|
204
|
+
};
|
|
205
|
+
var client = http.request({
|
|
206
|
+
method: 'POST',
|
|
207
|
+
path: '/ok',
|
|
208
|
+
headers: {
|
|
209
|
+
connection: 'upgrade',
|
|
210
|
+
upgrade: 'websocket'
|
|
211
|
+
},
|
|
212
|
+
agent: agent
|
|
213
|
+
}, function(res) {
|
|
214
|
+
assert(false);
|
|
215
|
+
});
|
|
216
|
+
client.on('upgrade', function(res, socket) {
|
|
217
|
+
assert.equal(res.statusCode, 421);
|
|
218
|
+
done();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
process.nextTick(function() {
|
|
222
|
+
deceiver.emitResponse({
|
|
223
|
+
status: 421,
|
|
224
|
+
reason: 'F',
|
|
225
|
+
headers: {
|
|
226
|
+
upgrade: 'websocket'
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
});
|