From 8f3bbd9b68801ac884c33657a7838e485bd6c7e2 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 22 Jul 2017 09:20:53 -0700 Subject: [PATCH] http2: add range support for respondWith{File|FD} * respondWithFD now supports optional statCheck * respondWithFD and respondWithFile both support offset/length for range requests * Fix linting nits following most recent update Backport-PR-URL: https://github.com/nodejs/node/pull/14813 Backport-Reviewed-By: Anna Henningsen Backport-Reviewed-By: Timothy Gu PR-URL: https://github.com/nodejs/node/pull/14239 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Matteo Collina --- doc/api/http2.md | 22 +++- lib/internal/http2/compat.js | 2 +- lib/internal/http2/core.js | 104 ++++++++++++++++-- src/node_http2.cc | 12 +- src/node_http2_core-inl.h | 8 +- src/node_http2_core.cc | 22 ++-- src/node_http2_core.h | 8 +- ...st-http2-client-settings-before-connect.js | 22 ++-- ...st-http2-client-shutdown-before-connect.js | 2 +- ...ttp2-compat-serverresponse-flushheaders.js | 2 +- ...est-http2-compat-serverresponse-headers.js | 2 +- ...tp2-compat-serverresponse-statusmessage.js | 4 +- ...t-http2-compat-serverresponse-writehead.js | 2 +- .../test-http2-create-client-connect.js | 12 +- ...test-http2-create-client-secure-session.js | 6 +- test/parallel/test-http2-getpackedsettings.js | 2 +- test/parallel/test-http2-info-headers.js | 2 +- .../test-http2-max-concurrent-streams.js | 2 +- .../test-http2-misused-pseudoheaders.js | 2 +- .../test-http2-multi-content-length.js | 2 +- test/parallel/test-http2-multiplex.js | 2 +- test/parallel/test-http2-priority-event.js | 2 +- .../test-http2-respond-file-fd-range.js | 94 ++++++++++++++++ .../parallel/test-http2-respond-file-range.js | 52 +++++++++ test/parallel/test-http2-serve-file.js | 4 +- .../test-http2-server-push-disabled.js | 2 +- ...st-http2-server-shutdown-before-respond.js | 2 +- test/parallel/test-http2-session-settings.js | 2 +- test/parallel/test-http2-timeouts.js | 2 +- test/parallel/test-http2-trailers.js | 2 +- test/parallel/test-http2-util-headers-list.js | 6 +- .../parallel/test-http2-write-empty-string.js | 2 +- .../test-tls-disable-renegotiation.js | 10 +- 33 files changed, 350 insertions(+), 72 deletions(-) create mode 100644 test/parallel/test-http2-respond-file-fd-range.js create mode 100644 test/parallel/test-http2-respond-file-range.js diff --git a/doc/api/http2.md b/doc/api/http2.md index 6bc1543c079f8a..cc7f3ea3b501e2 100755 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -998,13 +998,17 @@ server.on('stream', (stream) => { }); ``` -#### http2stream.respondWithFD(fd[, headers]) +#### http2stream.respondWithFD(fd[, headers[, options]]) * `fd` {number} A readable file descriptor * `headers` {[Headers Object][]} +* `options` {Object} + * `statCheck` {Function} + * `offset` {number} The offset position at which to begin reading + * `length` {number} The amount of data from the fd to send Initiates a response whose data is read from the given file descriptor. No validation is performed on the given file descriptor. If an error occurs while @@ -1034,6 +1038,16 @@ server.on('stream', (stream) => { server.on('close', () => fs.closeSync(fd)); ``` +The optional `options.statCheck` function may be specified to give user code +an opportunity to set additional content headers based on the `fs.Stat` details +of the given fd. If the `statCheck` function is provided, the +`http2stream.respondWithFD()` method will perform an `fs.fstat()` call to +collect details on the provided file descriptor. + +The `offset` and `length` options may be used to limit the response to a +specific range subset. This can be used, for instance, to support HTTP Range +requests. + #### http2stream.respondWithFile(path[, headers[, options]])