@@ -51,7 +51,7 @@ server.on('error', (err) => console.error(err));
51
51
server .on (' stream' , (stream , headers ) => {
52
52
// stream is a Duplex
53
53
stream .respond ({
54
- ' content-type' : ' text/html' ,
54
+ ' content-type' : ' text/html; charset=utf-8 ' ,
55
55
' :status' : 200
56
56
});
57
57
stream .end (' <h1>Hello World</h1>' );
@@ -271,7 +271,7 @@ session.on('stream', (stream, headers, flags) => {
271
271
// ...
272
272
stream .respond ({
273
273
' :status' : 200 ,
274
- ' content-type' : ' text/plain'
274
+ ' content-type' : ' text/plain; charset=utf-8 '
275
275
});
276
276
stream .write (' hello ' );
277
277
stream .end (' world' );
@@ -291,7 +291,7 @@ const server = http2.createServer();
291
291
292
292
server .on (' stream' , (stream , headers ) => {
293
293
stream .respond ({
294
- ' content-type' : ' text/html' ,
294
+ ' content-type' : ' text/html; charset=utf-8 ' ,
295
295
' :status' : 200
296
296
});
297
297
stream .on (' error' , (error ) => console .error (error));
@@ -889,6 +889,18 @@ All `Http2Stream` instances are [`Duplex`][] streams. The `Writable` side of the
889
889
` Duplex ` is used to send data to the connected peer, while the ` Readable ` side
890
890
is used to receive data sent by the connected peer.
891
891
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
+
892
904
#### ` Http2Stream ` Lifecycle
893
905
894
906
##### Creation
@@ -1509,7 +1521,7 @@ server.on('stream', (stream) => {
1509
1521
const headers = {
1510
1522
' content-length' : stat .size ,
1511
1523
' last-modified' : stat .mtime .toUTCString (),
1512
- ' content-type' : ' text/plain'
1524
+ ' content-type' : ' text/plain; charset=utf-8 '
1513
1525
};
1514
1526
stream .respondWithFD (fd, headers);
1515
1527
stream .on (' close' , () => fs .closeSync (fd));
@@ -1554,7 +1566,7 @@ server.on('stream', (stream) => {
1554
1566
const headers = {
1555
1567
' content-length' : stat .size ,
1556
1568
' last-modified' : stat .mtime .toUTCString (),
1557
- ' content-type' : ' text/plain'
1569
+ ' content-type' : ' text/plain; charset=utf-8 '
1558
1570
};
1559
1571
stream .respondWithFD (fd, headers, { waitForTrailers: true });
1560
1572
stream .on (' wantTrailers' , () => {
@@ -1624,7 +1636,7 @@ server.on('stream', (stream) => {
1624
1636
}
1625
1637
1626
1638
stream .respondWithFile (' /some/file' ,
1627
- { ' content-type' : ' text/plain' },
1639
+ { ' content-type' : ' text/plain; charset=utf-8 ' },
1628
1640
{ statCheck, onError });
1629
1641
});
1630
1642
```
@@ -1644,7 +1656,7 @@ server.on('stream', (stream) => {
1644
1656
return false ; // Cancel the send operation
1645
1657
}
1646
1658
stream .respondWithFile (' /some/file' ,
1647
- { ' content-type' : ' text/plain' },
1659
+ { ' content-type' : ' text/plain; charset=utf-8 ' },
1648
1660
{ statCheck });
1649
1661
});
1650
1662
```
@@ -1674,7 +1686,7 @@ const http2 = require('http2');
1674
1686
const server = http2 .createServer ();
1675
1687
server .on (' stream' , (stream ) => {
1676
1688
stream .respondWithFile (' /some/file' ,
1677
- { ' content-type' : ' text/plain' },
1689
+ { ' content-type' : ' text/plain; charset=utf-8 ' },
1678
1690
{ waitForTrailers: true });
1679
1691
stream .on (' wantTrailers' , () => {
1680
1692
stream .sendTrailers ({ ABC : ' some value to send' });
@@ -1766,7 +1778,7 @@ server.on('stream', (stream, headers, flags) => {
1766
1778
// ...
1767
1779
stream .respond ({
1768
1780
[HTTP2_HEADER_STATUS ]: 200 ,
1769
- [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain'
1781
+ [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain; charset=utf-8 '
1770
1782
});
1771
1783
stream .write (' hello ' );
1772
1784
stream .end (' world' );
@@ -1929,7 +1941,7 @@ server.on('stream', (stream, headers, flags) => {
1929
1941
// ...
1930
1942
stream .respond ({
1931
1943
[HTTP2_HEADER_STATUS ]: 200 ,
1932
- [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain'
1944
+ [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain; charset=utf-8 '
1933
1945
});
1934
1946
stream .write (' hello ' );
1935
1947
stream .end (' world' );
@@ -2138,7 +2150,7 @@ const server = http2.createServer();
2138
2150
2139
2151
server .on (' stream' , (stream , headers ) => {
2140
2152
stream .respond ({
2141
- ' content-type' : ' text/html' ,
2153
+ ' content-type' : ' text/html; charset=utf-8 ' ,
2142
2154
' :status' : 200
2143
2155
});
2144
2156
stream .end (' <h1>Hello World</h1>' );
@@ -2264,7 +2276,7 @@ const server = http2.createSecureServer(options);
2264
2276
2265
2277
server .on (' stream' , (stream , headers ) => {
2266
2278
stream .respond ({
2267
- ' content-type' : ' text/html' ,
2279
+ ' content-type' : ' text/html; charset=utf-8 ' ,
2268
2280
' :status' : 200
2269
2281
});
2270
2282
stream .end (' <h1>Hello World</h1>' );
@@ -2725,7 +2737,7 @@ const http2 = require('http2');
2725
2737
const server = http2 .createServer ((req , res ) => {
2726
2738
res .setHeader (' Content-Type' , ' text/html' );
2727
2739
res .setHeader (' X-Foo' , ' bar' );
2728
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
2740
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
2729
2741
res .end (' ok' );
2730
2742
});
2731
2743
```
@@ -3310,7 +3322,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings
3310
3322
here to send multiple headers with the same name.
3311
3323
3312
3324
``` js
3313
- response .setHeader (' Content-Type' , ' text/html' );
3325
+ response .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3314
3326
```
3315
3327
3316
3328
or
@@ -3329,9 +3341,9 @@ to [`response.writeHead()`][] given precedence.
3329
3341
``` js
3330
3342
// Returns content-type = text/plain
3331
3343
const server = http2 .createServer ((req , res ) => {
3332
- res .setHeader (' Content-Type' , ' text/html' );
3344
+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3333
3345
res .setHeader (' X-Foo' , ' bar' );
3334
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3346
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
3335
3347
res .end (' ok' );
3336
3348
});
3337
3349
```
@@ -3513,7 +3525,7 @@ will be emitted.
3513
3525
const body = ' hello world' ;
3514
3526
response .writeHead (200 , {
3515
3527
' Content-Length' : Buffer .byteLength (body),
3516
- ' Content-Type' : ' text/plain' });
3528
+ ' Content-Type' : ' text/plain; charset=utf-8 ' });
3517
3529
```
3518
3530
3519
3531
` Content-Length ` is given in bytes not characters. The
@@ -3536,9 +3548,9 @@ to [`response.writeHead()`][] given precedence.
3536
3548
``` js
3537
3549
// Returns content-type = text/plain
3538
3550
const server = http2 .createServer ((req , res ) => {
3539
- res .setHeader (' Content-Type' , ' text/html' );
3551
+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3540
3552
res .setHeader (' X-Foo' , ' bar' );
3541
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3553
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
3542
3554
res .end (' ok' );
3543
3555
});
3544
3556
```
0 commit comments