From a2b0c1933036852cf2cc3600eed4fc77407f1e40 Mon Sep 17 00:00:00 2001 From: Cecchi MacNaughton Date: Mon, 6 Apr 2020 22:08:33 -0700 Subject: [PATCH 1/2] doc: remove warning from `response.writeHead` The example referenced as being potentially unsafe specifies Content-Length correctly. --- doc/api/http.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index fb2f2b32399121..5d91175f262620 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1713,12 +1713,9 @@ const server = http.createServer((req, res) => { }); ``` -`Content-Length` is given in bytes not characters. The above example -works because the string `'hello world'` contains only single byte characters. -If the body contains higher coded characters then `Buffer.byteLength()` -should be used to determine the number of bytes in a given encoding. -And Node.js does not check whether `Content-Length` and the length of the body -which has been transmitted are equal or not. +`Content-Length` is given in bytes, not characters. Node.js does not check +whether `Content-Length` and the length of the body which has been transmitted +are equal or not. Attempting to set a header field name or value that contains invalid characters will result in a [`TypeError`][] being thrown. From 49d1a0f62637755a33e869f990ee5017a6c53d1a Mon Sep 17 00:00:00 2001 From: Cecchi MacNaughton Date: Tue, 7 Apr 2020 09:10:39 -0700 Subject: [PATCH 2/2] fixup: include link to `Buffer.byteLength()` --- doc/api/http.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 5d91175f262620..a683eebdab5d26 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1713,9 +1713,10 @@ const server = http.createServer((req, res) => { }); ``` -`Content-Length` is given in bytes, not characters. Node.js does not check -whether `Content-Length` and the length of the body which has been transmitted -are equal or not. +`Content-Length` is given in bytes, not characters. Use +[`Buffer.byteLength()`][] to determine the length of the body in bytes. Node.js +does not check whether `Content-Length` and the length of the body which has +been transmitted are equal or not. Attempting to set a header field name or value that contains invalid characters will result in a [`TypeError`][] being thrown. @@ -2385,6 +2386,7 @@ not abort the request or do anything besides add a `'timeout'` event. [`'response'`]: #http_event_response [`'upgrade'`]: #http_event_upgrade [`Agent`]: #http_class_http_agent +[`Buffer.byteLength()`]: buffer.html#buffer_class_method_buffer_bytelength_string_encoding [`Duplex`]: stream.html#stream_class_stream_duplex [`TypeError`]: errors.html#errors_class_typeerror [`URL`]: url.html#url_the_whatwg_url_api