@@ -51,7 +51,7 @@ server.on('error', (err) => console.error(err));
5151server .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
292292server .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
890890is 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');
16741686const server = http2 .createServer ();
16751687server .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' );
@@ -2140,7 +2152,7 @@ const server = http2.createServer();
21402152
21412153server .on (' stream' , (stream , headers ) => {
21422154 stream .respond ({
2143- ' content-type' : ' text/html' ,
2155+ ' content-type' : ' text/html; charset=utf-8 ' ,
21442156 ' :status' : 200
21452157 });
21462158 stream .end (' <h1>Hello World</h1>' );
@@ -2268,7 +2280,7 @@ const server = http2.createSecureServer(options);
22682280
22692281server .on (' stream' , (stream , headers ) => {
22702282 stream .respond ({
2271- ' content-type' : ' text/html' ,
2283+ ' content-type' : ' text/html; charset=utf-8 ' ,
22722284 ' :status' : 200
22732285 });
22742286 stream .end (' <h1>Hello World</h1>' );
@@ -2731,7 +2743,7 @@ const http2 = require('http2');
27312743const server = http2 .createServer ((req , res ) => {
27322744 res .setHeader (' Content-Type' , ' text/html' );
27332745 res .setHeader (' X-Foo' , ' bar' );
2734- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
2746+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
27352747 res .end (' ok' );
27362748});
27372749```
@@ -3316,7 +3328,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings
33163328here to send multiple headers with the same name.
33173329
33183330``` js
3319- response .setHeader (' Content-Type' , ' text/html' );
3331+ response .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
33203332```
33213333
33223334or
@@ -3335,9 +3347,9 @@ to [`response.writeHead()`][] given precedence.
33353347``` js
33363348// Returns content-type = text/plain
33373349const server = http2 .createServer ((req , res ) => {
3338- res .setHeader (' Content-Type' , ' text/html' );
3350+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
33393351 res .setHeader (' X-Foo' , ' bar' );
3340- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3352+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
33413353 res .end (' ok' );
33423354});
33433355```
@@ -3519,7 +3531,7 @@ will be emitted.
35193531const body = ' hello world' ;
35203532response .writeHead (200 , {
35213533 ' Content-Length' : Buffer .byteLength (body),
3522- ' Content-Type' : ' text/plain' });
3534+ ' Content-Type' : ' text/plain; charset=utf-8 ' });
35233535```
35243536
35253537` Content-Length ` is given in bytes not characters. The
@@ -3542,9 +3554,9 @@ to [`response.writeHead()`][] given precedence.
35423554``` js
35433555// Returns content-type = text/plain
35443556const server = http2 .createServer ((req , res ) => {
3545- res .setHeader (' Content-Type' , ' text/html' );
3557+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
35463558 res .setHeader (' X-Foo' , ' bar' );
3547- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3559+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
35483560 res .end (' ok' );
35493561});
35503562```
0 commit comments