| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e6685f9 commit 4197a9a
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1338,6 +1338,11 @@ When using [`fs.cp()`][], `src` or `dest` pointed to an invalid path. | |||
| 1338 | 1338 | ||
| 1339 | 1339 | <a id="ERR_FS_CP_FIFO_PIPE"></a> | |
| 1340 | 1340 | ||
| 1341 | + ### `ERR_HTTP_BODY_NOT_ALLOWED` | ||
| 1342 | + | ||
| 1343 | + An error is thrown when writing to an HTTP response which does not allow | ||
| 1344 | + contents. <a id="ERR_HTTP_BODY_NOT_ALLOWED"></a> | ||
| 1345 | + | ||
| 1341 | 1346 | ### `ERR_HTTP_CONTENT_LENGTH_MISMATCH` | |
| 1342 | 1347 | ||
| 1343 | 1348 | Response body size doesn't match with the specified content-length header value. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2144,9 +2144,10 @@ it will switch to implicit header mode and flush the implicit headers. | |||
| 2144 | 2144 | This sends a chunk of the response body. This method may | |
| 2145 | 2145 | be called multiple times to provide successive parts of the body. | |
| 2146 | 2146 | ||
| 2147 | - In the `node:http` module, the response body is omitted when the | ||
| 2148 | - request is a HEAD request. Similarly, the `204` and `304` responses | ||
| 2149 | - _must not_ include a message body. | ||
| 2147 | + Writing to the body is not allowed when the request method or response status | ||
| 2148 | + do not support content. If an attempt is made to write to the body for a | ||
| 2149 | + HEAD request or as part of a `204` or `304`response, a synchronous `Error` | ||
| 2150 | + with the code `ERR_HTTP_BODY_NOT_ALLOWED` is thrown. | ||
| 2150 | 2151 | ||
| 2151 | 2152 | `chunk` can be a string or a buffer. If `chunk` is a string, | |
| 2152 | 2153 | the second parameter specifies how to encode it into a byte stream. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,6 +60,7 @@ const { | |||
| 60 | 60 | ERR_HTTP_HEADERS_SENT, | |
| 61 | 61 | ERR_HTTP_INVALID_HEADER_VALUE, | |
| 62 | 62 | ERR_HTTP_TRAILER_INVALID, | |
| 63 | + ERR_HTTP_BODY_NOT_ALLOWED, | ||
| 63 | 64 | ERR_INVALID_HTTP_TOKEN, | |
| 64 | 65 | ERR_INVALID_ARG_TYPE, | |
| 65 | 66 | ERR_INVALID_ARG_VALUE, | |
@@ -85,6 +86,7 @@ const kUniqueHeaders = Symbol('kUniqueHeaders'); | |||
| 85 | 86 | const kBytesWritten = Symbol('kBytesWritten'); | |
| 86 | 87 | const kErrored = Symbol('errored'); | |
| 87 | 88 | const kHighWaterMark = Symbol('kHighWaterMark'); | |
| 89 | + const kRejectNonStandardBodyWrites = Symbol('kRejectNonStandardBodyWrites'); | ||
| 88 | 90 | ||
| 89 | 91 | const nop = () => {}; | |
| 90 | 92 | ||
@@ -150,6 +152,7 @@ function OutgoingMessage(options) { | |||
| 150 | 152 | ||
| 151 | 153 | this[kErrored] = null; | |
| 152 | 154 | this[kHighWaterMark] = options?.highWaterMark ?? getDefaultHighWaterMark(); | |
| 155 | + this[kRejectNonStandardBodyWrites] = options?.rejectNonStandardBodyWrites ?? false; | ||
| 153 | 156 | } | |
| 154 | 157 | ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype); | |
| 155 | 158 | ObjectSetPrototypeOf(OutgoingMessage, Stream); | |
@@ -884,6 +887,17 @@ function write_(msg, chunk, encoding, callback, fromEnd) { | |||
| 884 | 887 | err = new ERR_STREAM_DESTROYED('write'); | |
| 885 | 888 | } | |
| 886 | 889 | ||
| 890 | + if (!msg._hasBody) { | ||
| 891 | + if (msg[kRejectNonStandardBodyWrites]) { | ||
| 892 | + throw new ERR_HTTP_BODY_NOT_ALLOWED(); | ||
| 893 | + } else { | ||
| 894 | + debug('This type of response MUST NOT have a body. ' + | ||
| 895 | + 'Ignoring write() calls.'); | ||
| 896 | + process.nextTick(callback); | ||
| 897 | + return true; | ||
| 898 | + } | ||
| 899 | + } | ||
| 900 | + | ||
| 887 | 901 | if (err) { | |
| 888 | 902 | if (!msg.destroyed) { | |
| 889 | 903 | onError(msg, err, callback); | |
@@ -916,13 +930,6 @@ function write_(msg, chunk, encoding, callback, fromEnd) { | |||
| 916 | 930 | msg._implicitHeader(); | |
| 917 | 931 | } | |
| 918 | 932 | ||
| 919 | - if (!msg._hasBody) { | ||
| 920 | - debug('This type of response MUST NOT have a body. ' + | ||
| 921 | - 'Ignoring write() calls.'); | ||
| 922 | - process.nextTick(callback); | ||
| 923 | - return true; | ||
| 924 | - } | ||
| 925 | - | ||
| 926 | 933 | if (!fromEnd && msg.socket && !msg.socket.writableCorked) { | |
| 927 | 934 | msg.socket.cork(); | |
| 928 | 935 | process.nextTick(connectionCorkNT, msg.socket); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -487,6 +487,14 @@ function storeHTTPOptions(options) { | |||
| 487 | 487 | validateBoolean(joinDuplicateHeaders, 'options.joinDuplicateHeaders'); | |
| 488 | 488 | } | |
| 489 | 489 | this.joinDuplicateHeaders = joinDuplicateHeaders; | |
| 490 | + | ||
| 491 | + const rejectNonStandardBodyWrites = options.rejectNonStandardBodyWrites; | ||
| 492 | + if (rejectNonStandardBodyWrites !== undefined) { | ||
| 493 | + validateBoolean(rejectNonStandardBodyWrites, 'options.rejectNonStandardBodyWrites'); | ||
| 494 | + this.rejectNonStandardBodyWrites = rejectNonStandardBodyWrites; | ||
| 495 | + } else { | ||
| 496 | + this.rejectNonStandardBodyWrites = false; | ||
| 497 | + } | ||
| 490 | 498 | } | |
| 491 | 499 | ||
| 492 | 500 | function setupConnectionsTracking(server) { | |
@@ -1023,7 +1031,11 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { | |||
| 1023 | 1031 | } | |
| 1024 | 1032 | } | |
| 1025 | 1033 | ||
| 1026 | - const res = new server[kServerResponse](req, { highWaterMark: socket.writableHighWaterMark }); | ||
| 1034 | + const res = new server[kServerResponse](req, | ||
| 1035 | + { | ||
| 1036 | + highWaterMark: socket.writableHighWaterMark, | ||
| 1037 | + rejectNonStandardBodyWrites: server.rejectNonStandardBodyWrites, | ||
| 1038 | + }); | ||
| 1027 | 1039 | res._keepAliveTimeout = server.keepAliveTimeout; | |
| 1028 | 1040 | res._maxRequestsPerSocket = server.maxRequestsPerSocket; | |
| 1029 | 1041 | res._onPendingData = updateOutgoingData.bind(undefined, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,6 +55,7 @@ let maxHeaderSize; | |||
| 55 | 55 | * requireHostHeader?: boolean; | |
| 56 | 56 | * joinDuplicateHeaders?: boolean; | |
| 57 | 57 | * highWaterMark?: number; | |
| 58 | + * rejectNonStandardBodyWrites?: boolean; | ||
| 58 | 59 | * }} [opts] | |
| 59 | 60 | * @param {Function} [requestListener] | |
| 60 | 61 | * @returns {Server} | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1154,6 +1154,8 @@ E('ERR_HTTP2_TRAILERS_NOT_READY', | |||
| 1154 | 1154 | 'Trailing headers cannot be sent until after the wantTrailers event is ' + | |
| 1155 | 1155 | 'emitted', Error); | |
| 1156 | 1156 | E('ERR_HTTP2_UNSUPPORTED_PROTOCOL', 'protocol "%s" is unsupported.', Error); | |
| 1157 | + E('ERR_HTTP_BODY_NOT_ALLOWED', | ||
| 1158 | + 'Adding content for this request method or response status is not allowed.', Error); | ||
| 1157 | 1159 | E('ERR_HTTP_CONTENT_LENGTH_MISMATCH', | |
| 1158 | 1160 | 'Response body\'s content-length of %s byte(s) does not match the content-length of %s byte(s) set in header', Error); | |
| 1159 | 1161 | E('ERR_HTTP_HEADERS_SENT', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ const http = require('http'); | |||
| 29 | 29 | ||
| 30 | 30 | const server = http.createServer(function(req, res) { | |
| 31 | 31 | res.writeHead(200); | |
| 32 | - res.end('FAIL'); // broken: sends FAIL from hot path. | ||
| 32 | + res.end(); | ||
| 33 | 33 | }); | |
| 34 | 34 | server.listen(0); | |
| 35 | 35 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,102 @@ | |||
| 1 | + // Copyright Joyent, Inc. and other Node contributors. | ||
| 2 | + // | ||
| 3 | + // Permission is hereby granted, free of charge, to any person obtaining a | ||
| 4 | + // copy of this software and associated documentation files (the | ||
| 5 | + // "Software"), to deal in the Software without restriction, including | ||
| 6 | + // without limitation the rights to use, copy, modify, merge, publish, | ||
| 7 | + // distribute, sublicense, and/or sell copies of the Software, and to permit | ||
| 8 | + // persons to whom the Software is furnished to do so, subject to the | ||
| 9 | + // following conditions: | ||
| 10 | + // | ||
| 11 | + // The above copyright notice and this permission notice shall be included | ||
| 12 | + // in all copies or substantial portions of the Software. | ||
| 13 | + // | ||
| 14 | + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
| 15 | + // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 16 | + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
| 17 | + // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| 18 | + // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
| 19 | + // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
| 20 | + // USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 21 | + | ||
| 22 | + 'use strict'; | ||
| 23 | + const common = require('../common'); | ||
| 24 | + const assert = require('assert'); | ||
| 25 | + const http = require('http'); | ||
| 26 | + | ||
| 27 | + { | ||
| 28 | + const server = http.createServer((req, res) => { | ||
| 29 | + res.writeHead(200); | ||
| 30 | + res.end('this is content'); | ||
| 31 | + }); | ||
| 32 | + server.listen(0); | ||
| 33 | + | ||
| 34 | + server.on('listening', common.mustCall(function() { | ||
| 35 | + const req = http.request({ | ||
| 36 | + port: this.address().port, | ||
| 37 | + method: 'HEAD', | ||
| 38 | + path: '/' | ||
| 39 | + }, common.mustCall((res) => { | ||
| 40 | + res.resume(); | ||
| 41 | + res.on('end', common.mustCall(function() { | ||
| 42 | + server.close(); | ||
| 43 | + })); | ||
| 44 | + })); | ||
| 45 | + req.end(); | ||
| 46 | + })); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + { | ||
| 50 | + const server = http.createServer({ | ||
| 51 | + rejectNonStandardBodyWrites: true, | ||
| 52 | + }, (req, res) => { | ||
| 53 | + res.writeHead(204); | ||
| 54 | + assert.throws(() => { | ||
| 55 | + res.write('this is content'); | ||
| 56 | + }, { | ||
| 57 | + code: 'ERR_HTTP_BODY_NOT_ALLOWED', | ||
| 58 | + name: 'Error', | ||
| 59 | + message: 'Adding content for this request method or response status is not allowed.' | ||
| 60 | + }); | ||
| 61 | + res.end(); | ||
| 62 | + }); | ||
| 63 | + server.listen(0); | ||
| 64 | + | ||
| 65 | + server.on('listening', common.mustCall(function() { | ||
| 66 | + const req = http.request({ | ||
| 67 | + port: this.address().port, | ||
| 68 | + method: 'GET', | ||
| 69 | + path: '/' | ||
| 70 | + }, common.mustCall((res) => { | ||
| 71 | + res.resume(); | ||
| 72 | + res.on('end', common.mustCall(function() { | ||
| 73 | + server.close(); | ||
| 74 | + })); | ||
| 75 | + })); | ||
| 76 | + req.end(); | ||
| 77 | + })); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + { | ||
| 81 | + const server = http.createServer({ | ||
| 82 | + rejectNonStandardBodyWrites: false, | ||
| 83 | + }, (req, res) => { | ||
| 84 | + res.writeHead(200); | ||
| 85 | + res.end('this is content'); | ||
| 86 | + }); | ||
| 87 | + server.listen(0); | ||
| 88 | + | ||
| 89 | + server.on('listening', common.mustCall(function() { | ||
| 90 | + const req = http.request({ | ||
| 91 | + port: this.address().port, | ||
| 92 | + method: 'HEAD', | ||
| 93 | + path: '/' | ||
| 94 | + }, common.mustCall((res) => { | ||
| 95 | + res.resume(); | ||
| 96 | + res.on('end', common.mustCall(function() { | ||
| 97 | + server.close(); | ||
| 98 | + })); | ||
| 99 | + })); | ||
| 100 | + req.end(); | ||
| 101 | + })); | ||
| 102 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments