@@ -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' );
@@ -2140,7 +2152,7 @@ const server = http2.createServer();
2140
2152
2141
2153
server .on (' stream' , (stream , headers ) => {
2142
2154
stream .respond ({
2143
- ' content-type' : ' text/html' ,
2155
+ ' content-type' : ' text/html; charset=utf-8 ' ,
2144
2156
' :status' : 200
2145
2157
});
2146
2158
stream .end (' <h1>Hello World</h1>' );
@@ -2268,7 +2280,7 @@ const server = http2.createSecureServer(options);
2268
2280
2269
2281
server .on (' stream' , (stream , headers ) => {
2270
2282
stream .respond ({
2271
- ' content-type' : ' text/html' ,
2283
+ ' content-type' : ' text/html; charset=utf-8 ' ,
2272
2284
' :status' : 200
2273
2285
});
2274
2286
stream .end (' <h1>Hello World</h1>' );
@@ -2731,7 +2743,7 @@ const http2 = require('http2');
2731
2743
const server = http2 .createServer ((req , res ) => {
2732
2744
res .setHeader (' Content-Type' , ' text/html' );
2733
2745
res .setHeader (' X-Foo' , ' bar' );
2734
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
2746
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
2735
2747
res .end (' ok' );
2736
2748
});
2737
2749
```
@@ -3316,7 +3328,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings
3316
3328
here to send multiple headers with the same name.
3317
3329
3318
3330
``` js
3319
- response .setHeader (' Content-Type' , ' text/html' );
3331
+ response .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3320
3332
```
3321
3333
3322
3334
or
@@ -3335,9 +3347,9 @@ to [`response.writeHead()`][] given precedence.
3335
3347
``` js
3336
3348
// Returns content-type = text/plain
3337
3349
const server = http2 .createServer ((req , res ) => {
3338
- res .setHeader (' Content-Type' , ' text/html' );
3350
+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3339
3351
res .setHeader (' X-Foo' , ' bar' );
3340
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3352
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
3341
3353
res .end (' ok' );
3342
3354
});
3343
3355
```
@@ -3519,7 +3531,7 @@ will be emitted.
3519
3531
const body = ' hello world' ;
3520
3532
response .writeHead (200 , {
3521
3533
' Content-Length' : Buffer .byteLength (body),
3522
- ' Content-Type' : ' text/plain' });
3534
+ ' Content-Type' : ' text/plain; charset=utf-8 ' });
3523
3535
```
3524
3536
3525
3537
` Content-Length ` is given in bytes not characters. The
@@ -3542,9 +3554,9 @@ to [`response.writeHead()`][] given precedence.
3542
3554
``` js
3543
3555
// Returns content-type = text/plain
3544
3556
const server = http2 .createServer ((req , res ) => {
3545
- res .setHeader (' Content-Type' , ' text/html' );
3557
+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3546
3558
res .setHeader (' X-Foo' , ' bar' );
3547
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3559
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
3548
3560
res .end (' ok' );
3549
3561
});
3550
3562
```
0 commit comments