Skip to content

http2: add raw header array support to h2Session.request() #57917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

pimterry
Copy link
Member

This change allows you to call h2Session.request(['k1', 'v1', 'k2', 'v2']), just like in the HTTP/1 APIs.

Raw header support like this is useful for intermediaries, who generally already have headers in raw format already and would really like to preserve them exactly as received (without any unnecessary reordering or duplicate handling), and for anybody would wants to avoid the effort/perf to objectify headers unnecessarily.

I've added this just to request() for now, but I'd like to add support in the various other HTTP/2 methods - this is a first step to discuss any debatable points here up front.

There's a few notable details in here:

  • This changes error behaviour in some cases: previously, passing some invalid HTTP header values to request() would destroy the entire underlying session with an error (and would not throw an error)). Not all though - e.g. key header::name would destroy the session with an error, but :unknown-pseudo-header, multiple :method headers or setting HTTP/2-invalid connection/keep-alive/etc headers would throw (all without destroying the session - just failing to create a new stream). AFAICT there is no good reason for this, and it's very awkward & surprising behaviour. I've removed it, and the parts of the tests (test/parallel/test-http2-invalidheaderfield.js) which checked it - this is arguably a breaking change in error handling, let me know what you think.
  • This does not set stream[kSentHeaders], used by request.sentHeaders. That means that property remains undefined - this matches the behaviour of other APIs like http.request, which doesn't set kOutHeaders for HTTP/1.1 if you pass raw headers. That avoids some work and keeps things consistent, but it's a bit ugly. Opinions welcome - we could easily lazily instantiate sentHeaders instead to avoid any perf hit on the fast path anyway. We could even consider changing HTTP/1.1 to do the same!
  • There's a fair bit of refactoring here, which notably drops the header name validation at the start of request() - that's because I think the same checks are all run again afterwards within buildNgHeaderString (previously called mapToHeaders) so I'm fairly sure this was adding nothing.

This also notably changes error handling for request(). Previously some
invalid header values (but not all) would cause the session to be
unnecessarily destroyed automatically, e.g. passing an unparseable
header name to request(). This is no longer the case: header validation
failures will throw an error, but will not destroy the session or emit
'error' events.
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http2 Issues or PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC implementation / HTTP/3. labels Apr 17, 2025
Copy link

codecov bot commented Apr 17, 2025

Codecov Report

Attention: Patch coverage is 89.91228% with 23 lines in your changes missing coverage. Please review.

Project coverage is 90.24%. Comparing base (33d8e03) to head (f20cf7d).
Report is 56 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/http2/util.js 90.44% 17 Missing ⚠️
lib/internal/http2/core.js 88.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57917      +/-   ##
==========================================
- Coverage   90.25%   90.24%   -0.01%     
==========================================
  Files         630      630              
  Lines      185880   186072     +192     
  Branches    36434    36461      +27     
==========================================
+ Hits       167757   167921     +164     
- Misses      11010    11034      +24     
- Partials     7113     7117       +4     
Files with missing lines Coverage Δ
src/node_http_common.h 81.11% <ø> (ø)
lib/internal/http2/core.js 95.34% <88.00%> (-0.22%) ⬇️
lib/internal/http2/util.js 92.35% <90.44%> (-0.65%) ⬇️

... and 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@pimterry pimterry added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 22, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 22, 2025
@nodejs-github-bot
Copy link
Collaborator

@pimterry
Copy link
Member Author

This has a passing CI & one approval, and it's been open for more than 7 days, so it's now mergable! It is a bit fiddly & sensitive, and I want to check for any disagreement before I open more PRs like this for the other header API methods, so I'll leave it a few more days in case anybody else in @nodejs/http2 has opinions - if not, I'll land it sometime next week.

jasnell pushed a commit that referenced this pull request Apr 26, 2025
This also notably changes error handling for request(). Previously some
invalid header values (but not all) would cause the session to be
unnecessarily destroyed automatically, e.g. passing an unparseable
header name to request(). This is no longer the case: header validation
failures will throw an error, but will not destroy the session or emit
'error' events.

PR-URL: #57917
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@jasnell
Copy link
Member

jasnell commented Apr 26, 2025

Landed in 4cd8e19

@jasnell jasnell closed this Apr 26, 2025
RafaelGSS pushed a commit that referenced this pull request May 1, 2025
This also notably changes error handling for request(). Previously some
invalid header values (but not all) would cause the session to be
unnecessarily destroyed automatically, e.g. passing an unparseable
header name to request(). This is no longer the case: header validation
failures will throw an error, but will not destroy the session or emit
'error' events.

PR-URL: #57917
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. http2 Issues or PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC implementation / HTTP/3.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants