@@ -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
@@ -1499,7 +1511,7 @@ server.on('stream', (stream) => {
14991511 const headers = {
15001512 ' content-length' : stat .size ,
15011513 ' last-modified' : stat .mtime .toUTCString (),
1502- ' content-type' : ' text/plain'
1514+ ' content-type' : ' text/plain; charset=utf-8 '
15031515 };
15041516 stream .respondWithFD (fd, headers);
15051517 stream .on (' close' , () => fs .closeSync (fd));
@@ -1544,7 +1556,7 @@ server.on('stream', (stream) => {
15441556 const headers = {
15451557 ' content-length' : stat .size ,
15461558 ' last-modified' : stat .mtime .toUTCString (),
1547- ' content-type' : ' text/plain'
1559+ ' content-type' : ' text/plain; charset=utf-8 '
15481560 };
15491561 stream .respondWithFD (fd, headers, { waitForTrailers: true });
15501562 stream .on (' wantTrailers' , () => {
@@ -1611,7 +1623,7 @@ server.on('stream', (stream) => {
16111623 }
16121624
16131625 stream .respondWithFile (' /some/file' ,
1614- { ' content-type' : ' text/plain' },
1626+ { ' content-type' : ' text/plain; charset=utf-8 ' },
16151627 { statCheck, onError });
16161628});
16171629```
@@ -1631,7 +1643,7 @@ server.on('stream', (stream) => {
16311643 return false ; // Cancel the send operation
16321644 }
16331645 stream .respondWithFile (' /some/file' ,
1634- { ' content-type' : ' text/plain' },
1646+ { ' content-type' : ' text/plain; charset=utf-8 ' },
16351647 { statCheck });
16361648});
16371649```
@@ -1661,7 +1673,7 @@ const http2 = require('http2');
16611673const server = http2 .createServer ();
16621674server .on (' stream' , (stream ) => {
16631675 stream .respondWithFile (' /some/file' ,
1664- { ' content-type' : ' text/plain' },
1676+ { ' content-type' : ' text/plain; charset=utf-8 ' },
16651677 { waitForTrailers: true });
16661678 stream .on (' wantTrailers' , () => {
16671679 stream .sendTrailers ({ ABC : ' some value to send' });
@@ -1753,7 +1765,7 @@ server.on('stream', (stream, headers, flags) => {
17531765 // ...
17541766 stream .respond ({
17551767 [HTTP2_HEADER_STATUS ]: 200 ,
1756- [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain'
1768+ [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain; charset=utf-8 '
17571769 });
17581770 stream .write (' hello ' );
17591771 stream .end (' world' );
@@ -1895,7 +1907,7 @@ server.on('stream', (stream, headers, flags) => {
18951907 // ...
18961908 stream .respond ({
18971909 [HTTP2_HEADER_STATUS ]: 200 ,
1898- [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain'
1910+ [HTTP2_HEADER_CONTENT_TYPE ]: ' text/plain; charset=utf-8 '
18991911 });
19001912 stream .write (' hello ' );
19011913 stream .end (' world' );
@@ -2084,7 +2096,7 @@ const server = http2.createServer();
20842096
20852097server .on (' stream' , (stream , headers ) => {
20862098 stream .respond ({
2087- ' content-type' : ' text/html' ,
2099+ ' content-type' : ' text/html; charset=utf-8 ' ,
20882100 ' :status' : 200
20892101 });
20902102 stream .end (' <h1>Hello World</h1>' );
@@ -2209,7 +2221,7 @@ const server = http2.createSecureServer(options);
22092221
22102222server .on (' stream' , (stream , headers ) => {
22112223 stream .respond ({
2212- ' content-type' : ' text/html' ,
2224+ ' content-type' : ' text/html; charset=utf-8 ' ,
22132225 ' :status' : 200
22142226 });
22152227 stream .end (' <h1>Hello World</h1>' );
@@ -2697,7 +2709,7 @@ const http2 = require('http2');
26972709const server = http2 .createServer ((req , res ) => {
26982710 res .setHeader (' Content-Type' , ' text/html' );
26992711 res .setHeader (' X-Foo' , ' bar' );
2700- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
2712+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
27012713 res .end (' ok' );
27022714});
27032715```
@@ -3265,7 +3277,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings
32653277here to send multiple headers with the same name.
32663278
32673279``` js
3268- response .setHeader (' Content-Type' , ' text/html' );
3280+ response .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
32693281```
32703282
32713283or
@@ -3284,9 +3296,9 @@ to [`response.writeHead()`][] given precedence.
32843296``` js
32853297// Returns content-type = text/plain
32863298const server = http2 .createServer ((req , res ) => {
3287- res .setHeader (' Content-Type' , ' text/html' );
3299+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
32883300 res .setHeader (' X-Foo' , ' bar' );
3289- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3301+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
32903302 res .end (' ok' );
32913303});
32923304```
@@ -3466,7 +3478,7 @@ will be emitted.
34663478const body = ' hello world' ;
34673479response .writeHead (200 , {
34683480 ' Content-Length' : Buffer .byteLength (body),
3469- ' Content-Type' : ' text/plain' });
3481+ ' Content-Type' : ' text/plain; charset=utf-8 ' });
34703482```
34713483
34723484` Content-Length ` is given in bytes not characters. The
@@ -3489,9 +3501,9 @@ to [`response.writeHead()`][] given precedence.
34893501``` js
34903502// Returns content-type = text/plain
34913503const server = http2 .createServer ((req , res ) => {
3492- res .setHeader (' Content-Type' , ' text/html' );
3504+ res .setHeader (' Content-Type' , ' text/html; charset=utf-8 ' );
34933505 res .setHeader (' X-Foo' , ' bar' );
3494- res .writeHead (200 , { ' Content-Type' : ' text/plain' });
3506+ res .writeHead (200 , { ' Content-Type' : ' text/plain; charset=utf-8 ' });
34953507 res .end (' ok' );
34963508});
34973509```
0 commit comments