Skip to content

Commit 12b9ec0

Browse files
committed
http2: remove regular-file-only restriction
Requiring `respondWithFile()` to only work with regular files is an artificial restriction on Node’s side and has become unnecessary. Offsets or lengths cannot be specified for those files, but that is an inherent property of other file types. PR-URL: #18936 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 1eb6b01 commit 12b9ec0

7 files changed

+164
-14
lines changed

doc/api/errors.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,14 @@ client.
971971
### ERR_HTTP2_SEND_FILE
972972

973973
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.
975982

976983
<a id="ERR_HTTP2_SESSION_ERROR"></a>
977984
### ERR_HTTP2_SESSION_ERROR

doc/api/http2.md

+10
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ if the `getTrailers` callback attempts to set such header fields.
12231223
#### http2stream.respondWithFD(fd[, headers[, options]])
12241224
<!-- YAML
12251225
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.
12261231
-->
12271232

12281233
* `fd` {number} A readable file descriptor.
@@ -1313,6 +1318,11 @@ if the `getTrailers` callback attempts to set such header fields.
13131318
#### http2stream.respondWithFile(path[, headers[, options]])
13141319
<!-- YAML
13151320
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.
13161326
-->
13171327

13181328
* `path` {string|Buffer|URL}

lib/internal/errors.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ E('ERR_HTTP2_PING_LENGTH', 'HTTP2 ping payload must be 8 bytes', RangeError);
714714
E('ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED',
715715
'Cannot set HTTP/2 pseudo-headers', Error);
716716
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);
718720
E('ERR_HTTP2_SESSION_ERROR', 'Session closed with error code %s', Error);
719721
E('ERR_HTTP2_SOCKET_BOUND',
720722
'The socket is already bound to an Http2Session', Error);

lib/internal/http2/core.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
ERR_HTTP2_PING_LENGTH,
4343
ERR_HTTP2_PUSH_DISABLED,
4444
ERR_HTTP2_SEND_FILE,
45+
ERR_HTTP2_SEND_FILE_NOSEEK,
4546
ERR_HTTP2_SESSION_ERROR,
4647
ERR_HTTP2_SETTINGS_CANCEL,
4748
ERR_HTTP2_SOCKET_BOUND,
@@ -2045,12 +2046,21 @@ function doSendFileFD(session, options, fd, headers, streamOptions, err, stat) {
20452046
}
20462047

20472048
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;
20542064
}
20552065

20562066
if (this.destroyed || this.closed) {
@@ -2075,12 +2085,14 @@ function doSendFileFD(session, options, fd, headers, streamOptions, err, stat) {
20752085
return;
20762086
}
20772087

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);
20822093

2083-
headers[HTTP2_HEADER_CONTENT_LENGTH] = statOptions.length;
2094+
headers[HTTP2_HEADER_CONTENT_LENGTH] = statOptions.length;
2095+
}
20842096

20852097
processRespondWithFD(this, fd, headers,
20862098
options.offset | 0,

test/parallel/test-http2-respond-file-error-dir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ server.on('stream', (stream) => {
1515
common.expectsError({
1616
code: 'ERR_HTTP2_SEND_FILE',
1717
type: Error,
18-
message: 'Only regular files can be sent'
18+
message: 'Directories cannot be sent'
1919
})(err);
2020

2121
stream.respond({ ':status': 404 });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}));

0 commit comments

Comments
 (0)