Skip to content

Commit

Permalink
Add support for HTTP2 pseudo headers (#55)
Browse files Browse the repository at this point in the history
* fix(headers): do not depend on `http` for header name validation

Node internally uses undici instead anyway

* fix(headers): support HTTP2 pseudo-headers

which begin with `:` e.g. `:authority`, `:method`, etc.

* test(headers): check that pseudo-headers are allowed
  • Loading branch information
pcattori authored Dec 5, 2023
1 parent d4cdb8f commit 3986fb3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-dogs-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/web-fetch": patch
---

Support HTTP2 pseudo-headers like `:authority`, `:method`, etc.
26 changes: 12 additions & 14 deletions packages/fetch/src/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@ import {types} from 'util';
import http from 'http';
import { isIterable } from './utils/is.js'

const validators = /** @type {{validateHeaderName?:(name:string) => any, validateHeaderValue?:(name:string, value:string) => any}} */
(http)
/** @type {{validateHeaderValue?:(name:string, value:string) => any}} */
const validators = (http)

const validateHeaderName = typeof validators.validateHeaderName === 'function' ?
validators.validateHeaderName :
/**
* @param {string} name
*/
name => {
if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(name)) {
const err = new TypeError(`Header name must be a valid HTTP token [${name}]`);
Object.defineProperty(err, 'code', {value: 'ERR_INVALID_HTTP_TOKEN'});
throw err;
}
};
/**
* @param {string} name
*/
const validateHeaderName = name => {
if (!/^[\^`\-\w!#$%&'*+.|~:]+$/.test(name)) {
const err = new TypeError(`Header name must be a valid HTTP token [${name}]`);
Object.defineProperty(err, 'code', {value: 'ERR_INVALID_HTTP_TOKEN'});
throw err;
}
};

const validateHeaderValue = typeof validators.validateHeaderValue === 'function' ?
validators.validateHeaderValue :
Expand Down
13 changes: 13 additions & 0 deletions packages/fetch/test/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ describe('Headers', () => {
expect(() => headers.append('', 'ok')).to.throw(TypeError);
});

it('should allow HTTP2 pseudo-headers', () => {
let headers = new Headers({':authority': 'something'});
headers.append(":method", "something else")

const result = [];
for (const pair of headers) {
result.push(pair);
}

expect(result).to.deep.equal([[':authority', 'something'], [':method', 'something else']]);

})

it('should ignore unsupported attributes while reading headers', () => {
const FakeHeader = function () { };
// Prototypes are currently ignored
Expand Down

0 comments on commit 3986fb3

Please sign in to comment.