Skip to content

Commit 169ca85

Browse files
committed
more
1 parent 883de94 commit 169ca85

File tree

4 files changed

+280
-0
lines changed

4 files changed

+280
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
const bodySent = 'This is my request';
9+
10+
function assertResponse(headers, body, expectClosed) {
11+
if (expectClosed) {
12+
assert.match(headers, /Connection: close\r\n/m);
13+
assert.strictEqual(headers.search(/Keep-Alive: timeout=5, max=3\r\n/m), -1);
14+
assert.match(body, /Hello World!/m);
15+
} else {
16+
assert.match(headers, /Connection: keep-alive\r\n/m);
17+
assert.match(headers, /Keep-Alive: timeout=5, max=3\r\n/m);
18+
assert.match(body, /Hello World!/m);
19+
}
20+
}
21+
22+
function writeRequest(socket) {
23+
socket.write('POST / HTTP/1.1\r\n');
24+
socket.write('Host: localhost\r\n');
25+
socket.write('Connection: keep-alive\r\n');
26+
socket.write('Content-Type: text/plain\r\n');
27+
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`);
28+
socket.write(`${bodySent}\r\n`);
29+
socket.write('\r\n\r\n');
30+
}
31+
32+
const server = http.createServer((req, res) => {
33+
let body = '';
34+
req.on('data', (data) => {
35+
body += data;
36+
});
37+
38+
req.on('end', () => {
39+
if (req.method === 'POST') {
40+
assert.strictEqual(bodySent, body);
41+
}
42+
43+
res.writeHead(200, { 'Content-Type': 'text/plain' });
44+
res.write('Hello World!');
45+
res.end();
46+
});
47+
});
48+
49+
server.maxRequestsPerSocket = 3;
50+
51+
server.listen(0, common.mustCall((res) => {
52+
const socket = new net.Socket();
53+
54+
socket.on('end', common.mustCall(() => {
55+
server.close();
56+
}));
57+
58+
socket.on('ready', common.mustCall(() => {
59+
writeRequest(socket);
60+
writeRequest(socket);
61+
writeRequest(socket);
62+
writeRequest(socket);
63+
}));
64+
65+
let buffer = '';
66+
67+
socket.on('data', (data) => {
68+
buffer += data;
69+
70+
const responseParts = buffer.trim().split('\r\n\r\n');
71+
if (responseParts.length === 8) {
72+
assertResponse(responseParts[0], responseParts[1]);
73+
assertResponse(responseParts[2], responseParts[3]);
74+
assertResponse(responseParts[4], responseParts[5], true);
75+
76+
assert.match(responseParts[6], /HTTP\/1\.1 503 Service Unavailable/m);
77+
assert.match(responseParts[6], /Connection: close\r\n/m);
78+
assert.strictEqual(responseParts[6].search(/Keep-Alive: timeout=5\r\n/m), -1);
79+
assert.strictEqual(responseParts[7].search(/Hello World!/m), -1);
80+
81+
socket.end();
82+
}
83+
});
84+
85+
socket.connect({ port: server.address().port });
86+
}));
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
const bodySent = 'This is my request';
9+
10+
function assertResponse(headers, body, expectClosed) {
11+
assert.match(headers, /Connection: keep-alive\r\n/m);
12+
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
13+
assert.match(body, /Hello World!/m);
14+
}
15+
16+
function writeRequest(socket) {
17+
socket.write('POST / HTTP/1.1\r\n');
18+
socket.write('Connection: keep-alive\r\n');
19+
socket.write('Host: localhost\r\n');
20+
socket.write('Content-Type: text/plain\r\n');
21+
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`);
22+
socket.write(`${bodySent}\r\n`);
23+
socket.write('\r\n\r\n');
24+
}
25+
26+
const server = http.createServer((req, res) => {
27+
let body = '';
28+
req.on('data', (data) => {
29+
body += data;
30+
});
31+
32+
req.on('end', () => {
33+
if (req.method === 'POST') {
34+
assert.strictEqual(bodySent, body);
35+
}
36+
37+
res.writeHead(200, { 'Content-Type': 'text/plain' });
38+
res.write('Hello World!');
39+
res.end();
40+
});
41+
});
42+
43+
server.listen(0, common.mustCall((res) => {
44+
assert.strictEqual(server.maxRequestsPerSocket, 0);
45+
46+
const socket = new net.Socket();
47+
48+
socket.on('end', common.mustCall(() => {
49+
server.close();
50+
}));
51+
52+
socket.on('ready', common.mustCall(() => {
53+
writeRequest(socket);
54+
writeRequest(socket);
55+
writeRequest(socket);
56+
writeRequest(socket);
57+
}));
58+
59+
let buffer = '';
60+
61+
socket.on('data', (data) => {
62+
buffer += data;
63+
64+
const responseParts = buffer.trim().split('\r\n\r\n');
65+
66+
if (responseParts.length === 8) {
67+
assertResponse(responseParts[0], responseParts[1]);
68+
assertResponse(responseParts[2], responseParts[3]);
69+
assertResponse(responseParts[4], responseParts[5]);
70+
assertResponse(responseParts[6], responseParts[7]);
71+
72+
socket.end();
73+
}
74+
});
75+
76+
socket.connect({ port: server.address().port });
77+
}));
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
const bodySent = 'This is my request';
9+
10+
function assertResponse(headers, body, expectClosed) {
11+
assert.match(headers, /Connection: keep-alive\r\n/m);
12+
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
13+
assert.match(body, /Hello World!/m);
14+
}
15+
16+
function writeRequest(socket) {
17+
socket.write('POST / HTTP/1.1\r\n');
18+
socket.write('Connection: keep-alive\r\n');
19+
socket.write('Host: localhost\r\n');
20+
socket.write('Content-Type: text/plain\r\n');
21+
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`);
22+
socket.write(`${bodySent}\r\n`);
23+
socket.write('\r\n\r\n');
24+
}
25+
26+
const server = http.createServer((req, res) => {
27+
let body = '';
28+
req.on('data', (data) => {
29+
body += data;
30+
});
31+
32+
req.on('end', () => {
33+
if (req.method === 'POST') {
34+
assert.strictEqual(bodySent, body);
35+
}
36+
37+
res.writeHead(200, { 'Content-Type': 'text/plain' });
38+
res.write('Hello World!');
39+
res.end();
40+
});
41+
});
42+
43+
server.maxRequestsPerSocket = null;
44+
server.listen(0, common.mustCall((res) => {
45+
const socket = new net.Socket();
46+
47+
socket.on('end', common.mustCall(() => {
48+
server.close();
49+
}));
50+
51+
socket.on('ready', common.mustCall(() => {
52+
writeRequest(socket);
53+
writeRequest(socket);
54+
writeRequest(socket);
55+
writeRequest(socket);
56+
}));
57+
58+
let buffer = '';
59+
60+
socket.on('data', (data) => {
61+
buffer += data;
62+
63+
const responseParts = buffer.trim().split('\r\n\r\n');
64+
65+
if (responseParts.length === 8) {
66+
assertResponse(responseParts[0], responseParts[1]);
67+
assertResponse(responseParts[2], responseParts[3]);
68+
assertResponse(responseParts[4], responseParts[5]);
69+
assertResponse(responseParts[6], responseParts[7]);
70+
71+
socket.end();
72+
}
73+
});
74+
75+
socket.connect({ port: server.address().port });
76+
}));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Flags: --expose-gc
2+
'use strict';
3+
const common = require('../common');
4+
const { onGC } = require('../common/gc');
5+
const { createServer } = require('http');
6+
const { connect } = require('net');
7+
8+
// Make sure that for HTTP keepalive requests, the req object can be
9+
// garbage collected once the request is finished.
10+
// Refs: https://github.com/nodejs/node/issues/9668
11+
12+
let client;
13+
const server = createServer(common.mustCall((req, res) => {
14+
onGC(req, { ongc: common.mustCall(() => { server.close(); }) });
15+
req.resume();
16+
req.on('end', common.mustCall(() => {
17+
setImmediate(async () => {
18+
client.end();
19+
await globalThis.gc({ type: 'major', execution: 'async' });
20+
await globalThis.gc({ type: 'major', execution: 'async' });
21+
});
22+
}));
23+
res.end('hello world');
24+
}));
25+
26+
server.listen(0, common.mustCall(() => {
27+
client = connect(server.address().port);
28+
29+
const req = [
30+
'POST / HTTP/1.1',
31+
`Host: localhost:${server.address().port}`,
32+
'Connection: keep-alive',
33+
'Content-Length: 11',
34+
'',
35+
'hello world',
36+
'',
37+
].join('\r\n');
38+
39+
client.write(req);
40+
client.unref();
41+
}));

0 commit comments

Comments
 (0)