-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchunked_encoding_server.js
100 lines (87 loc) · 2.62 KB
/
chunked_encoding_server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Use with `http_api.php` to test chunked transfer encoding:
*
* ```php
* $requests = [
* new Request( "http://127.0.0.1:3000/", [
* 'http_version' => '1.1'
* ] ),
* new Request( "http://127.0.0.1:3000/", [
* 'http_version' => '1.0',
* 'headers' => [
* 'please-redirect' => 'yes',
* ],
* ] ),
* ];
*/
const http = require('http');
const zlib = require('zlib');
const server = http.createServer((req, res) => {
// Check if the client is using HTTP/1.1
const isHttp11 = req.httpVersion === '1.1';
res.useChunkedEncodingByDefault = false
// Check if the client accepts gzip encoding
const acceptEncoding = req.headers['accept-encoding'];
const useGzip = acceptEncoding && acceptEncoding.includes('gzip');
if (req.headers['please-redirect']) {
res.writeHead(301, { Location: req.url });
res.end();
return;
}
// Set headers for chunked transfer encoding if HTTP/1.1
if (isHttp11) {
res.setHeader('Transfer-Encoding', 'chunked');
}
res.setHeader('Content-Type', 'text/plain');
// Create a function to write chunks
const writeChunks = (stream) => {
stream.write(`<!DOCTYPE html>
<html lang=en>
<head>
<meta charset='utf-8'>
<title>Chunked transfer encoding test</title>
</head>\r\n`);
stream.write('<body><h1>Chunked transfer encoding test</h1>\r\n');
setTimeout(() => {
stream.write('<h5>This is a chunked response after 100 ms.</h5>\n');
setTimeout(() => {
stream.write('<h5>This is a chunked response after 1 second. The server should not close the stream before all chunks are sent to a client.</h5></body></html>\n');
stream.end();
}, 1000);
}, 100);
};
if (useGzip) {
res.setHeader('Content-Encoding', 'gzip');
const gzip = zlib.createGzip();
gzip.pipe(res);
if (isHttp11) {
writeChunks({
write(data) {
gzip.write(data);
gzip.flush();
},
end() {
gzip.end();
}
});
} else {
gzip.write('Chunked transfer encoding test\n');
gzip.write('This is a chunked response after 100 ms.\n');
gzip.write('This is a chunked response after 1 second.\n');
gzip.end();
}
} else {
if (isHttp11) {
writeChunks(res);
} else {
res.write('Chunked transfer encoding test\n');
res.write('This is a chunked response after 100 ms.\n');
res.write('This is a chunked response after 1 second.\n');
res.end();
}
}
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is listening on http://127.0.0.1:${port}`);
});