Skip to content

Commit 7416028

Browse files
jasnellMylesBorins
authored andcommitted
doc: specify encoding in text/html examples
Fixes: #29739 PR-URL: #34222 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 9339f9f commit 7416028

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

doc/api/http2.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ server.on('error', (err) => console.error(err));
5151
server.on('stream', (stream, headers) => {
5252
// stream is a Duplex
5353
stream.respond({
54-
'content-type': 'text/html',
54+
'content-type': 'text/html; charset=utf-8',
5555
':status': 200
5656
});
5757
stream.end('<h1>Hello World</h1>');
@@ -271,7 +271,7 @@ session.on('stream', (stream, headers, flags) => {
271271
// ...
272272
stream.respond({
273273
':status': 200,
274-
'content-type': 'text/plain'
274+
'content-type': 'text/plain; charset=utf-8'
275275
});
276276
stream.write('hello ');
277277
stream.end('world');
@@ -291,7 +291,7 @@ const server = http2.createServer();
291291

292292
server.on('stream', (stream, headers) => {
293293
stream.respond({
294-
'content-type': 'text/html',
294+
'content-type': 'text/html; charset=utf-8',
295295
':status': 200
296296
});
297297
stream.on('error', (error) => console.error(error));
@@ -889,6 +889,18 @@ All `Http2Stream` instances are [`Duplex`][] streams. The `Writable` side of the
889889
`Duplex` is used to send data to the connected peer, while the `Readable` side
890890
is used to receive data sent by the connected peer.
891891

892+
The default text character encoding for all `Http2Stream`s is UTF-8. As a best
893+
practice, it is recommended that when using an `Http2Stream` to send text,
894+
the `'content-type'` header should be set and should identify the character
895+
encoding used.
896+
897+
```js
898+
stream.respond({
899+
'content-type': 'text/html; charset=utf-8',
900+
':status': 200
901+
});
902+
```
903+
892904
#### `Http2Stream` Lifecycle
893905

894906
##### Creation
@@ -1509,7 +1521,7 @@ server.on('stream', (stream) => {
15091521
const headers = {
15101522
'content-length': stat.size,
15111523
'last-modified': stat.mtime.toUTCString(),
1512-
'content-type': 'text/plain'
1524+
'content-type': 'text/plain; charset=utf-8'
15131525
};
15141526
stream.respondWithFD(fd, headers);
15151527
stream.on('close', () => fs.closeSync(fd));
@@ -1554,7 +1566,7 @@ server.on('stream', (stream) => {
15541566
const headers = {
15551567
'content-length': stat.size,
15561568
'last-modified': stat.mtime.toUTCString(),
1557-
'content-type': 'text/plain'
1569+
'content-type': 'text/plain; charset=utf-8'
15581570
};
15591571
stream.respondWithFD(fd, headers, { waitForTrailers: true });
15601572
stream.on('wantTrailers', () => {
@@ -1624,7 +1636,7 @@ server.on('stream', (stream) => {
16241636
}
16251637

16261638
stream.respondWithFile('/some/file',
1627-
{ 'content-type': 'text/plain' },
1639+
{ 'content-type': 'text/plain; charset=utf-8' },
16281640
{ statCheck, onError });
16291641
});
16301642
```
@@ -1644,7 +1656,7 @@ server.on('stream', (stream) => {
16441656
return false; // Cancel the send operation
16451657
}
16461658
stream.respondWithFile('/some/file',
1647-
{ 'content-type': 'text/plain' },
1659+
{ 'content-type': 'text/plain; charset=utf-8' },
16481660
{ statCheck });
16491661
});
16501662
```
@@ -1674,7 +1686,7 @@ const http2 = require('http2');
16741686
const server = http2.createServer();
16751687
server.on('stream', (stream) => {
16761688
stream.respondWithFile('/some/file',
1677-
{ 'content-type': 'text/plain' },
1689+
{ 'content-type': 'text/plain; charset=utf-8' },
16781690
{ waitForTrailers: true });
16791691
stream.on('wantTrailers', () => {
16801692
stream.sendTrailers({ ABC: 'some value to send' });
@@ -1766,7 +1778,7 @@ server.on('stream', (stream, headers, flags) => {
17661778
// ...
17671779
stream.respond({
17681780
[HTTP2_HEADER_STATUS]: 200,
1769-
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain'
1781+
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8'
17701782
});
17711783
stream.write('hello ');
17721784
stream.end('world');
@@ -1929,7 +1941,7 @@ server.on('stream', (stream, headers, flags) => {
19291941
// ...
19301942
stream.respond({
19311943
[HTTP2_HEADER_STATUS]: 200,
1932-
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain'
1944+
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8'
19331945
});
19341946
stream.write('hello ');
19351947
stream.end('world');
@@ -2138,7 +2150,7 @@ const server = http2.createServer();
21382150

21392151
server.on('stream', (stream, headers) => {
21402152
stream.respond({
2141-
'content-type': 'text/html',
2153+
'content-type': 'text/html; charset=utf-8',
21422154
':status': 200
21432155
});
21442156
stream.end('<h1>Hello World</h1>');
@@ -2264,7 +2276,7 @@ const server = http2.createSecureServer(options);
22642276

22652277
server.on('stream', (stream, headers) => {
22662278
stream.respond({
2267-
'content-type': 'text/html',
2279+
'content-type': 'text/html; charset=utf-8',
22682280
':status': 200
22692281
});
22702282
stream.end('<h1>Hello World</h1>');
@@ -2725,7 +2737,7 @@ const http2 = require('http2');
27252737
const server = http2.createServer((req, res) => {
27262738
res.setHeader('Content-Type', 'text/html');
27272739
res.setHeader('X-Foo', 'bar');
2728-
res.writeHead(200, { 'Content-Type': 'text/plain' });
2740+
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
27292741
res.end('ok');
27302742
});
27312743
```
@@ -3310,7 +3322,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings
33103322
here to send multiple headers with the same name.
33113323

33123324
```js
3313-
response.setHeader('Content-Type', 'text/html');
3325+
response.setHeader('Content-Type', 'text/html; charset=utf-8');
33143326
```
33153327

33163328
or
@@ -3329,9 +3341,9 @@ to [`response.writeHead()`][] given precedence.
33293341
```js
33303342
// Returns content-type = text/plain
33313343
const server = http2.createServer((req, res) => {
3332-
res.setHeader('Content-Type', 'text/html');
3344+
res.setHeader('Content-Type', 'text/html; charset=utf-8');
33333345
res.setHeader('X-Foo', 'bar');
3334-
res.writeHead(200, { 'Content-Type': 'text/plain' });
3346+
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
33353347
res.end('ok');
33363348
});
33373349
```
@@ -3513,7 +3525,7 @@ will be emitted.
35133525
const body = 'hello world';
35143526
response.writeHead(200, {
35153527
'Content-Length': Buffer.byteLength(body),
3516-
'Content-Type': 'text/plain' });
3528+
'Content-Type': 'text/plain; charset=utf-8' });
35173529
```
35183530

35193531
`Content-Length` is given in bytes not characters. The
@@ -3536,9 +3548,9 @@ to [`response.writeHead()`][] given precedence.
35363548
```js
35373549
// Returns content-type = text/plain
35383550
const server = http2.createServer((req, res) => {
3539-
res.setHeader('Content-Type', 'text/html');
3551+
res.setHeader('Content-Type', 'text/html; charset=utf-8');
35403552
res.setHeader('X-Foo', 'bar');
3541-
res.writeHead(200, { 'Content-Type': 'text/plain' });
3553+
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
35423554
res.end('ok');
35433555
});
35443556
```

0 commit comments

Comments
 (0)