2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2017-03-24 16:46:44 +00:00
|
|
|
|
2017-05-04 04:07:54 +00:00
|
|
|
require('../common');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2016-12-30 23:38:06 +00:00
|
|
|
const net = require('net');
|
|
|
|
const http = require('http');
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let requests_recv = 0;
|
|
|
|
let requests_sent = 0;
|
|
|
|
let request_upgradeHead = null;
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function createTestServer() {
|
2010-04-30 13:33:11 +00:00
|
|
|
return new testServer();
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function testServer() {
|
2017-05-04 04:07:54 +00:00
|
|
|
http.Server.call(this, () => {});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
this.on('connection', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
requests_recv++;
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
this.on('request', function(req, res) {
|
2017-07-11 00:55:21 +00:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2010-12-05 22:33:52 +00:00
|
|
|
res.write('okay');
|
2010-04-30 13:33:11 +00:00
|
|
|
res.end();
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
this.on('upgrade', function(req, socket, upgradeHead) {
|
2010-12-05 22:33:52 +00:00
|
|
|
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n\r\n');
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2013-06-13 00:45:30 +00:00
|
|
|
request_upgradeHead = upgradeHead;
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2013-07-26 02:33:15 +00:00
|
|
|
socket.on('data', function(d) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const data = d.toString('utf8');
|
2016-08-17 23:14:43 +00:00
|
|
|
if (data === 'kill') {
|
2010-04-30 13:33:11 +00:00
|
|
|
socket.end();
|
|
|
|
} else {
|
2010-12-05 22:33:52 +00:00
|
|
|
socket.write(data, 'utf8');
|
2010-04-30 13:33:11 +00:00
|
|
|
}
|
2013-07-26 02:33:15 +00:00
|
|
|
});
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2018-12-28 04:10:45 +00:00
|
|
|
Object.setPrototypeOf(testServer.prototype, http.Server.prototype);
|
|
|
|
Object.setPrototypeOf(testServer, http.Server);
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function writeReq(socket, data, encoding) {
|
2010-04-30 13:33:11 +00:00
|
|
|
requests_sent++;
|
|
|
|
socket.write(data);
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
2020-10-03 20:01:57 +00:00
|
|
|
// connection: Upgrade with listener
|
2016-05-29 07:06:56 +00:00
|
|
|
function test_upgrade_with_listener() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const conn = net.createConnection(server.address().port);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
2017-01-08 13:19:00 +00:00
|
|
|
let state = 0;
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('connect', function() {
|
2010-12-05 22:33:52 +00:00
|
|
|
writeReq(conn,
|
|
|
|
'GET / HTTP/1.1\r\n' +
|
2021-07-09 07:59:35 +00:00
|
|
|
'Host: example.com\r\n' +
|
2010-12-05 22:33:52 +00:00
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n' +
|
|
|
|
'WjN}|M(6');
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('data', function(data) {
|
2010-04-30 13:33:11 +00:00
|
|
|
state++;
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2018-10-20 19:56:05 +00:00
|
|
|
assert.strictEqual(typeof data, 'string');
|
2010-06-16 01:19:25 +00:00
|
|
|
|
2016-08-17 23:14:43 +00:00
|
|
|
if (state === 1) {
|
2024-05-22 05:08:59 +00:00
|
|
|
assert.strictEqual(data.slice(0, 12), 'HTTP/1.1 101');
|
2018-10-20 19:56:05 +00:00
|
|
|
assert.strictEqual(request_upgradeHead.toString('utf8'), 'WjN}|M(6');
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.write('test', 'utf8');
|
2016-08-17 23:14:43 +00:00
|
|
|
} else if (state === 2) {
|
2018-10-20 19:56:05 +00:00
|
|
|
assert.strictEqual(data, 'test');
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.write('kill', 'utf8');
|
2010-04-30 13:33:11 +00:00
|
|
|
}
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('end', function() {
|
2018-10-20 19:56:05 +00:00
|
|
|
assert.strictEqual(state, 2);
|
2010-04-30 13:33:11 +00:00
|
|
|
conn.end();
|
2016-05-29 07:06:56 +00:00
|
|
|
server.removeAllListeners('upgrade');
|
2010-04-30 13:33:11 +00:00
|
|
|
test_upgrade_no_listener();
|
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2020-10-03 20:01:57 +00:00
|
|
|
// connection: Upgrade, no listener
|
2010-12-05 22:33:52 +00:00
|
|
|
function test_upgrade_no_listener() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const conn = net.createConnection(server.address().port);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('connect', function() {
|
2010-12-05 22:33:52 +00:00
|
|
|
writeReq(conn,
|
|
|
|
'GET / HTTP/1.1\r\n' +
|
2021-07-09 07:59:35 +00:00
|
|
|
'Host: example.com\r\n' +
|
2010-12-05 22:33:52 +00:00
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n');
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
|
|
|
|
2018-04-12 17:57:19 +00:00
|
|
|
conn.once('data', (data) => {
|
|
|
|
assert.strictEqual(typeof data, 'string');
|
2024-05-22 05:08:59 +00:00
|
|
|
assert.strictEqual(data.slice(0, 12), 'HTTP/1.1 200');
|
2010-04-30 13:33:11 +00:00
|
|
|
conn.end();
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('close', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
test_standard_http();
|
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2020-10-03 20:01:57 +00:00
|
|
|
// connection: normal
|
2010-12-05 22:33:52 +00:00
|
|
|
function test_standard_http() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const conn = net.createConnection(server.address().port);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
2010-06-16 01:19:25 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('connect', function() {
|
2021-07-09 07:59:35 +00:00
|
|
|
writeReq(conn, 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n');
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2013-04-08 12:40:31 +00:00
|
|
|
conn.once('data', function(data) {
|
2018-10-20 19:56:05 +00:00
|
|
|
assert.strictEqual(typeof data, 'string');
|
2024-05-22 05:08:59 +00:00
|
|
|
assert.strictEqual(data.slice(0, 12), 'HTTP/1.1 200');
|
2010-04-30 13:33:11 +00:00
|
|
|
conn.end();
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('close', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
server.close();
|
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = createTestServer();
|
2010-06-15 19:38:22 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
server.listen(0, function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
// All tests get chained after this:
|
2016-05-29 07:06:56 +00:00
|
|
|
test_upgrade_with_listener();
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2020-10-03 20:01:57 +00:00
|
|
|
// Fin.
|
2011-10-14 23:08:36 +00:00
|
|
|
process.on('exit', function() {
|
2018-10-20 19:56:05 +00:00
|
|
|
assert.strictEqual(requests_recv, 3);
|
|
|
|
assert.strictEqual(requests_sent, 3);
|
2010-05-02 01:01:06 +00:00
|
|
|
});
|