@@ -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
@@ -1499,7 +1511,7 @@ server.on('stream', (stream) => {
1499
1511
const headers = {
1500
1512
' content-length' : stat .size ,
1501
1513
' last-modified' : stat .mtime .toUTCString (),
1502
- ' content-type' : ' text/plain'
1514
+ ' content-type' : ' text/plain; charset=utf-8 '
1503
1515
};
1504
1516
stream .respondWithFD (fd, headers);
1505
1517
stream .on (' close' , () => fs .closeSync (fd));
@@ -1544,7 +1556,7 @@ server.on('stream', (stream) => {
1544
1556
const headers = {
1545
1557
' content-length' : stat .size ,
1546
1558
' last-modified' : stat .mtime .toUTCString (),
1547
- ' content-type' : ' text/plain'
1559
+ ' content-type' : ' text/plain; charset=utf-8 '
1548
1560
};
1549
1561
stream .respondWithFD (fd, headers, { waitForTrailers: true });
1550
1562
stream .on (' wantTrailers' , () => {
@@ -1611,7 +1623,7 @@ server.on('stream', (stream) => {
1611
1623
}
1612
1624
1613
1625
stream .respondWithFile (' /some/file' ,
1614
- { ' content-type' : ' text/plain' },
1626
+ { ' content-type' : ' text/plain; charset=utf-8 ' },
1615
1627
{ statCheck, onError });
1616
1628
});
1617
1629
```
@@ -1631,7 +1643,7 @@ server.on('stream', (stream) => {
1631
1643
return false ; // Cancel the send operation
1632
1644
}
1633
1645
stream .respondWithFile (' /some/file' ,
1634
- { ' content-type' : ' text/plain' },
1646
+ { ' content-type' : ' text/plain; charset=utf-8 ' },
1635
1647
{ statCheck });
1636
1648
});
1637
1649
```
@@ -1661,7 +1673,7 @@ const http2 = require('http2');
1661
1673
const server = http2 .createServer ();
1662
1674
server .on (' stream' , (stream ) => {
1663
1675
stream .respondWithFile (' /some/file' ,
1664
- { ' content-type' : ' text/plain' },
1676
+ { ' content-type' : ' text/plain; charset=utf-8 ' },
1665
1677
{ waitForTrailers: true });
1666
1678
stream .on (' wantTrailers' , () => {
1667
1679
stream .sendTrailers ({ ABC : ' some value to send' });
@@ -1753,7 +1765,7 @@ server.on('stream', (stream, headers, flags) => {
1753
1765
// ...
1754
1766
stream .respond ({
1755
1767
[HTTP2_HEADER_STATUS ]: 200 ,
1756
- [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain'
1768
+ [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain; charset=utf-8 '
1757
1769
});
1758
1770
stream .write (' hello ' );
1759
1771
stream .end (' world' );
@@ -1895,7 +1907,7 @@ server.on('stream', (stream, headers, flags) => {
1895
1907
// ...
1896
1908
stream .respond ({
1897
1909
[HTTP2_HEADER_STATUS ]: 200 ,
1898
- [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain'
1910
+ [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain; charset=utf-8 '
1899
1911
});
1900
1912
stream .write (' hello ' );
1901
1913
stream .end (' world' );
@@ -2084,7 +2096,7 @@ const server = http2.createServer();
2084
2096
2085
2097
server .on (' stream' , (stream , headers ) => {
2086
2098
stream .respond ({
2087
- ' content-type' : ' text/html' ,
2099
+ ' content-type' : ' text/html; charset=utf-8 ' ,
2088
2100
' :status' : 200
2089
2101
});
2090
2102
stream .end (' <h1>Hello World</h1>' );
@@ -2209,7 +2221,7 @@ const server = http2.createSecureServer(options);
2209
2221
2210
2222
server .on (' stream' , (stream , headers ) => {
2211
2223
stream .respond ({
2212
- ' content-type' : ' text/html' ,
2224
+ ' content-type' : ' text/html; charset=utf-8 ' ,
2213
2225
' :status' : 200
2214
2226
});
2215
2227
stream .end (' <h1>Hello World</h1>' );
@@ -2697,7 +2709,7 @@ const http2 = require('http2');
2697
2709
const server = http2 .createServer ((req , res ) => {
2698
2710
res .setHeader (' Content-Type' , ' text/html' );
2699
2711
res .setHeader (' X-Foo' , ' bar' );
2700
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
2712
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
2701
2713
res .end (' ok' );
2702
2714
});
2703
2715
```
@@ -3265,7 +3277,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings
3265
3277
here to send multiple headers with the same name.
3266
3278
3267
3279
``` js
3268
- response .setHeader (' Content-Type' , ' text/html' );
3280
+ response .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3269
3281
```
3270
3282
3271
3283
or
@@ -3284,9 +3296,9 @@ to [`response.writeHead()`][] given precedence.
3284
3296
``` js
3285
3297
// Returns content-type = text/plain
3286
3298
const server = http2 .createServer ((req , res ) => {
3287
- res .setHeader (' Content-Type' , ' text/html' );
3299
+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3288
3300
res .setHeader (' X-Foo' , ' bar' );
3289
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3301
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
3290
3302
res .end (' ok' );
3291
3303
});
3292
3304
```
@@ -3466,7 +3478,7 @@ will be emitted.
3466
3478
const body = ' hello world' ;
3467
3479
response .writeHead (200 , {
3468
3480
' Content-Length' : Buffer .byteLength (body),
3469
- ' Content-Type' : ' text/plain' });
3481
+ ' Content-Type' : ' text/plain; charset=utf-8 ' });
3470
3482
```
3471
3483
3472
3484
` Content-Length ` is given in bytes not characters. The
@@ -3489,9 +3501,9 @@ to [`response.writeHead()`][] given precedence.
3489
3501
``` js
3490
3502
// Returns content-type = text/plain
3491
3503
const server = http2 .createServer ((req , res ) => {
3492
- res .setHeader (' Content-Type' , ' text/html' );
3504
+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
3493
3505
res .setHeader (' X-Foo' , ' bar' );
3494
- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3506
+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
3495
3507
res .end (' ok' );
3496
3508
});
3497
3509
```
0 commit comments