GeezerVison is showing github.com — http2: remove regular-file-only restriction · nodejs/node@12b9ec0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/errors.md +8-1Lines changed: 8 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -971,7 +971,14 @@ client. | 971 | 971 | ### ERR_HTTP2_SEND_FILE | 972 | 972 |
| 973 | 973 | An attempt was made to use the `Http2Stream.prototype.responseWithFile()` API to | 974 | | -send something other than a regular file. | | 974 | +send a directory. | | 975 | + | | 976 | +<a id="ERR_HTTP2_SEND_FILE_NOSEEK"></a> | | 977 | +### ERR_HTTP2_SEND_FILE_NOSEEK | | 978 | + | | 979 | +An attempt was made to use the `Http2Stream.prototype.responseWithFile()` API to | | 980 | +send something other than a regular file, but `offset` or `length` options were | | 981 | +provided. | 975 | 982 |
| 976 | 983 | <a id="ERR_HTTP2_SESSION_ERROR"></a> | 977 | 984 | ### ERR_HTTP2_SESSION_ERROR | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/http2.md +10Lines changed: 10 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1223,6 +1223,11 @@ if the `getTrailers` callback attempts to set such header fields. | 1223 | 1223 | #### http2stream.respondWithFD(fd[, headers[, options]]) | 1224 | 1224 | <!-- YAML | 1225 | 1225 | added: v8.4.0 | | 1226 | +changes: | | 1227 | + - version: REPLACEME | | 1228 | + pr-url: https://github.com/nodejs/node/pull/18936 | | 1229 | + description: Any readable file descriptor, not necessarily for a | | 1230 | + regular file, is supported now. | 1226 | 1231 | --> | 1227 | 1232 |
| 1228 | 1233 | * `fd` {number} A readable file descriptor. | @@ -1313,6 +1318,11 @@ if the `getTrailers` callback attempts to set such header fields. | 1313 | 1318 | #### http2stream.respondWithFile(path[, headers[, options]]) | 1314 | 1319 | <!-- YAML | 1315 | 1320 | added: v8.4.0 | | 1321 | +changes: | | 1322 | + - version: REPLACEME | | 1323 | + pr-url: https://github.com/nodejs/node/pull/18936 | | 1324 | + description: Any readable file, not necessarily a | | 1325 | + regular file, is supported now. | 1316 | 1326 | --> | 1317 | 1327 |
| 1318 | 1328 | * `path` {string|Buffer|URL} | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -714,7 +714,9 @@ E('ERR_HTTP2_PING_LENGTH', 'HTTP2 ping payload must be 8 bytes', RangeError); | 714 | 714 | E('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED', | 715 | 715 | 'Cannot set HTTP/2 pseudo-headers', Error); | 716 | 716 | E('ERR_HTTP2_PUSH_DISABLED', 'HTTP/2 client has disabled push streams', Error); | 717 | | -E('ERR_HTTP2_SEND_FILE', 'Only regular files can be sent', Error); | | 717 | +E('ERR_HTTP2_SEND_FILE', 'Directories cannot be sent', Error); | | 718 | +E('ERR_HTTP2_SEND_FILE_NOSEEK', | | 719 | + 'Offset or length can only be specified for regular files', Error); | 718 | 720 | E('ERR_HTTP2_SESSION_ERROR', 'Session closed with error code %s', Error); | 719 | 721 | E('ERR_HTTP2_SOCKET_BOUND', | 720 | 722 | 'The socket is already bound to an Http2Session', Error); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/http2/core.js +23-11Lines changed: 23 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,7 @@ const { | 42 | 42 | ERR_HTTP2_PING_LENGTH, | 43 | 43 | ERR_HTTP2_PUSH_DISABLED, | 44 | 44 | ERR_HTTP2_SEND_FILE, | | 45 | + ERR_HTTP2_SEND_FILE_NOSEEK, | 45 | 46 | ERR_HTTP2_SESSION_ERROR, | 46 | 47 | ERR_HTTP2_SETTINGS_CANCEL, | 47 | 48 | ERR_HTTP2_SOCKET_BOUND, | @@ -2045,12 +2046,21 @@ function doSendFileFD(session, options, fd, headers, streamOptions, err, stat) { | 2045 | 2046 | } | 2046 | 2047 |
| 2047 | 2048 | if (!stat.isFile()) { | 2048 | | - const err = new ERR_HTTP2_SEND_FILE(); | 2049 | | - if (onError) | 2050 | | - onError(err); | 2051 | | - else | 2052 | | - this.destroy(err); | 2053 | | - return; | | 2049 | + const isDirectory = stat.isDirectory(); | | 2050 | + if (options.offset !== undefined || options.offset > 0 || | | 2051 | + options.length !== undefined || options.length >= 0 || | | 2052 | + isDirectory) { | | 2053 | + const err = isDirectory ? | | 2054 | + new ERR_HTTP2_SEND_FILE() : new ERR_HTTP2_SEND_FILE_NOSEEK(); | | 2055 | + if (onError) | | 2056 | + onError(err); | | 2057 | + else | | 2058 | + this.destroy(err); | | 2059 | + return; | | 2060 | + } | | 2061 | + | | 2062 | + options.offset = -1; | | 2063 | + options.length = -1; | 2054 | 2064 | } | 2055 | 2065 |
| 2056 | 2066 | if (this.destroyed || this.closed) { | @@ -2075,12 +2085,14 @@ function doSendFileFD(session, options, fd, headers, streamOptions, err, stat) { | 2075 | 2085 | return; | 2076 | 2086 | } | 2077 | 2087 |
| 2078 | | - statOptions.length = | 2079 | | - statOptions.length < 0 ? stat.size - (+statOptions.offset) : | 2080 | | - Math.min(stat.size - (+statOptions.offset), | 2081 | | - statOptions.length); | | 2088 | + if (stat.isFile()) { | | 2089 | + statOptions.length = | | 2090 | + statOptions.length < 0 ? stat.size - (+statOptions.offset) : | | 2091 | + Math.min(stat.size - (+statOptions.offset), | | 2092 | + statOptions.length); | 2082 | 2093 |
| 2083 | | - headers[HTTP2_HEADER_CONTENT_LENGTH] = statOptions.length; | | 2094 | + headers[HTTP2_HEADER_CONTENT_LENGTH] = statOptions.length; | | 2095 | + } | 2084 | 2096 |
| 2085 | 2097 | processRespondWithFD(this, fd, headers, | 2086 | 2098 | options.offset | 0, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-http2-respond-file-error-dir.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,7 @@ server.on('stream', (stream) => { | 15 | 15 | common.expectsError({ | 16 | 16 | code: 'ERR_HTTP2_SEND_FILE', | 17 | 17 | type: Error, | 18 | | - message: 'Only regular files can be sent' | | 18 | + message: 'Directories cannot be sent' | 19 | 19 | })(err); | 20 | 20 |
| 21 | 21 | stream.respond({ ':status': 404 }); | |
Collapse file Copy file name to clipboard +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | +if (common.isWindows) | | 7 | + common.skip('no mkfifo on Windows'); | | 8 | +const child_process = require('child_process'); | | 9 | +const path = require('path'); | | 10 | +const fs = require('fs'); | | 11 | +const http2 = require('http2'); | | 12 | +const assert = require('assert'); | | 13 | + | | 14 | +const tmpdir = require('../common/tmpdir'); | | 15 | +tmpdir.refresh(); | | 16 | + | | 17 | +const pipeName = path.join(tmpdir.path, 'pipe'); | | 18 | + | | 19 | +const mkfifo = child_process.spawnSync('mkfifo', [ pipeName ]); | | 20 | +if (mkfifo.error && mkfifo.error.code === 'ENOENT') { | | 21 | + common.skip('missing mkfifo'); | | 22 | +} | | 23 | + | | 24 | +process.on('exit', () => fs.unlinkSync(pipeName)); | | 25 | + | | 26 | +const server = http2.createServer(); | | 27 | +server.on('stream', (stream) => { | | 28 | + stream.respondWithFile(pipeName, { | | 29 | + 'content-type': 'text/plain' | | 30 | + }, { | | 31 | + offset: 10, | | 32 | + onError(err) { | | 33 | + common.expectsError({ | | 34 | + code: 'ERR_HTTP2_SEND_FILE_NOSEEK', | | 35 | + type: Error, | | 36 | + message: 'Offset or length can only be specified for regular files' | | 37 | + })(err); | | 38 | + | | 39 | + stream.respond({ ':status': 404 }); | | 40 | + stream.end(); | | 41 | + }, | | 42 | + statCheck: common.mustNotCall() | | 43 | + }); | | 44 | +}); | | 45 | +server.listen(0, () => { | | 46 | + | | 47 | + const client = http2.connect(`http://localhost:${server.address().port}`); | | 48 | + const req = client.request(); | | 49 | + | | 50 | + req.on('response', common.mustCall((headers) => { | | 51 | + assert.strictEqual(headers[':status'], 404); | | 52 | + })); | | 53 | + req.on('data', common.mustNotCall()); | | 54 | + req.on('end', common.mustCall(() => { | | 55 | + client.close(); | | 56 | + server.close(); | | 57 | + })); | | 58 | + req.end(); | | 59 | +}); | | 60 | + | | 61 | +fs.writeFile(pipeName, 'Hello, world!\n', common.mustCall()); |
Collapse file Copy file name to clipboard +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | +if (common.isWindows) | | 7 | + common.skip('no mkfifo on Windows'); | | 8 | +const child_process = require('child_process'); | | 9 | +const path = require('path'); | | 10 | +const fs = require('fs'); | | 11 | +const http2 = require('http2'); | | 12 | +const assert = require('assert'); | | 13 | + | | 14 | +const tmpdir = require('../common/tmpdir'); | | 15 | +tmpdir.refresh(); | | 16 | + | | 17 | +const pipeName = path.join(tmpdir.path, 'pipe'); | | 18 | + | | 19 | +const mkfifo = child_process.spawnSync('mkfifo', [ pipeName ]); | | 20 | +if (mkfifo.error && mkfifo.error.code === 'ENOENT') { | | 21 | + common.skip('missing mkfifo'); | | 22 | +} | | 23 | + | | 24 | +process.on('exit', () => fs.unlinkSync(pipeName)); | | 25 | + | | 26 | +const server = http2.createServer(); | | 27 | +server.on('stream', (stream) => { | | 28 | + stream.respondWithFile(pipeName, { | | 29 | + 'content-type': 'text/plain' | | 30 | + }, { | | 31 | + onError: common.mustNotCall(), | | 32 | + statCheck: common.mustCall() | | 33 | + }); | | 34 | +}); | | 35 | + | | 36 | +server.listen(0, () => { | | 37 | + const client = http2.connect(`http://localhost:${server.address().port}`); | | 38 | + const req = client.request(); | | 39 | + | | 40 | + req.on('response', common.mustCall((headers) => { | | 41 | + assert.strictEqual(headers[':status'], 200); | | 42 | + })); | | 43 | + let body = ''; | | 44 | + req.setEncoding('utf8'); | | 45 | + req.on('data', (chunk) => body += chunk); | | 46 | + req.on('end', common.mustCall(() => { | | 47 | + assert.strictEqual(body, 'Hello, world!\n'); | | 48 | + client.close(); | | 49 | + server.close(); | | 50 | + })); | | 51 | + req.end(); | | 52 | +}); | | 53 | + | | 54 | +fs.open(pipeName, 'w', common.mustCall((err, fd) => { | | 55 | + assert.ifError(err); | | 56 | + fs.writeSync(fd, 'Hello, world!\n'); | | 57 | + fs.closeSync(fd); | | 58 | +})); |
|