diff options
author | James M Snell <jasnell@gmail.com> | 2020-07-06 22:27:57 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2020-07-09 18:49:50 +0300 |
commit | 99f0404646e9e264c11bedd2bdf70fa0a1247199 (patch) | |
tree | e2af7977f48dd2b9a500c8b2784025602478508a /doc/api/http2.md | |
parent | f98a441bf75f2b0b698a0f9dace218f56386ec4a (diff) |
doc: specify encoding in text/html examples
Fixes: https://github.com/nodejs/node/issues/29739
PR-URL: https://github.com/nodejs/node/pull/34222
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'doc/api/http2.md')
-rw-r--r-- | doc/api/http2.md | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/doc/api/http2.md b/doc/api/http2.md index 21ce7739fbf..c24e43f12b0 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -51,7 +51,7 @@ server.on('error', (err) => console.error(err)); server.on('stream', (stream, headers) => { // stream is a Duplex stream.respond({ - 'content-type': 'text/html', + 'content-type': 'text/html; charset=utf-8', ':status': 200 }); stream.end('<h1>Hello World</h1>'); @@ -271,7 +271,7 @@ session.on('stream', (stream, headers, flags) => { // ... stream.respond({ ':status': 200, - 'content-type': 'text/plain' + 'content-type': 'text/plain; charset=utf-8' }); stream.write('hello '); stream.end('world'); @@ -291,7 +291,7 @@ const server = http2.createServer(); server.on('stream', (stream, headers) => { stream.respond({ - 'content-type': 'text/html', + 'content-type': 'text/html; charset=utf-8', ':status': 200 }); stream.on('error', (error) => console.error(error)); @@ -889,6 +889,18 @@ All `Http2Stream` instances are [`Duplex`][] streams. The `Writable` side of the `Duplex` is used to send data to the connected peer, while the `Readable` side is used to receive data sent by the connected peer. +The default text character encoding for all `Http2Stream`s is UTF-8. As a best +practice, it is recommended that when using an `Http2Stream` to send text, +the `'content-type'` header should be set and should identify the character +encoding used. + +```js +stream.respond({ + 'content-type': 'text/html; charset=utf-8', + ':status': 200 +}); +``` + #### `Http2Stream` Lifecycle ##### Creation @@ -1509,7 +1521,7 @@ server.on('stream', (stream) => { const headers = { 'content-length': stat.size, 'last-modified': stat.mtime.toUTCString(), - 'content-type': 'text/plain' + 'content-type': 'text/plain; charset=utf-8' }; stream.respondWithFD(fd, headers); stream.on('close', () => fs.closeSync(fd)); @@ -1554,7 +1566,7 @@ server.on('stream', (stream) => { const headers = { 'content-length': stat.size, 'last-modified': stat.mtime.toUTCString(), - 'content-type': 'text/plain' + 'content-type': 'text/plain; charset=utf-8' }; stream.respondWithFD(fd, headers, { waitForTrailers: true }); stream.on('wantTrailers', () => { @@ -1624,7 +1636,7 @@ server.on('stream', (stream) => { } stream.respondWithFile('/some/file', - { 'content-type': 'text/plain' }, + { 'content-type': 'text/plain; charset=utf-8' }, { statCheck, onError }); }); ``` @@ -1644,7 +1656,7 @@ server.on('stream', (stream) => { return false; // Cancel the send operation } stream.respondWithFile('/some/file', - { 'content-type': 'text/plain' }, + { 'content-type': 'text/plain; charset=utf-8' }, { statCheck }); }); ``` @@ -1674,7 +1686,7 @@ const http2 = require('http2'); const server = http2.createServer(); server.on('stream', (stream) => { stream.respondWithFile('/some/file', - { 'content-type': 'text/plain' }, + { 'content-type': 'text/plain; charset=utf-8' }, { waitForTrailers: true }); stream.on('wantTrailers', () => { stream.sendTrailers({ ABC: 'some value to send' }); @@ -1766,7 +1778,7 @@ server.on('stream', (stream, headers, flags) => { // ... stream.respond({ [HTTP2_HEADER_STATUS]: 200, - [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' + [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8' }); stream.write('hello '); stream.end('world'); @@ -1929,7 +1941,7 @@ server.on('stream', (stream, headers, flags) => { // ... stream.respond({ [HTTP2_HEADER_STATUS]: 200, - [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' + [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain; charset=utf-8' }); stream.write('hello '); stream.end('world'); @@ -2140,7 +2152,7 @@ const server = http2.createServer(); server.on('stream', (stream, headers) => { stream.respond({ - 'content-type': 'text/html', + 'content-type': 'text/html; charset=utf-8', ':status': 200 }); stream.end('<h1>Hello World</h1>'); @@ -2268,7 +2280,7 @@ const server = http2.createSecureServer(options); server.on('stream', (stream, headers) => { stream.respond({ - 'content-type': 'text/html', + 'content-type': 'text/html; charset=utf-8', ':status': 200 }); stream.end('<h1>Hello World</h1>'); @@ -2731,7 +2743,7 @@ const http2 = require('http2'); const server = http2.createServer((req, res) => { res.setHeader('Content-Type', 'text/html'); res.setHeader('X-Foo', 'bar'); - res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('ok'); }); ``` @@ -3316,7 +3328,7 @@ in the to-be-sent headers, its value will be replaced. Use an array of strings here to send multiple headers with the same name. ```js -response.setHeader('Content-Type', 'text/html'); +response.setHeader('Content-Type', 'text/html; charset=utf-8'); ``` or @@ -3335,9 +3347,9 @@ to [`response.writeHead()`][] given precedence. ```js // Returns content-type = text/plain const server = http2.createServer((req, res) => { - res.setHeader('Content-Type', 'text/html'); + res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.setHeader('X-Foo', 'bar'); - res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('ok'); }); ``` @@ -3519,7 +3531,7 @@ will be emitted. const body = 'hello world'; response.writeHead(200, { 'Content-Length': Buffer.byteLength(body), - 'Content-Type': 'text/plain' }); + 'Content-Type': 'text/plain; charset=utf-8' }); ``` `Content-Length` is given in bytes not characters. The @@ -3542,9 +3554,9 @@ to [`response.writeHead()`][] given precedence. ```js // Returns content-type = text/plain const server = http2.createServer((req, res) => { - res.setHeader('Content-Type', 'text/html'); + res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.setHeader('X-Foo', 'bar'); - res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' }); res.end('ok'); }); ``` |