Skip to content

Commit

Permalink
fix: add handlerProtocol key explicitly in handler metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Dec 19, 2019
1 parent 2664c85 commit 4c8361d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/middleware-host-header/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export const hostHeaderMiddleware = <
): BuildMiddleware<Input, Output> => next => async args => {
if (!HttpRequest.isInstance(args.request)) return next(args);
const { request } = args;
const { metadata = [] } = options.requestHandler;
const { handlerProtocol = "" } = options.requestHandler.metadata || {};
//For H2 request, remove 'host' header and use ':authority' header instead
//reference: https://nodejs.org/dist/latest-v13.x/docs/api/errors.html#ERR_HTTP2_INVALID_CONNECTION_HEADERS
if (metadata.includes("h2") && !request.headers[":authority"]) {
if (handlerProtocol.indexOf("h2") >= 0 && !request.headers[":authority"]) {
delete request.headers["host"];
request.headers[":authority"] = "";
//non-H2 request and 'host' header is not set, set the 'host' header to request's hostname.
Expand Down
2 changes: 1 addition & 1 deletion packages/node-http-handler/src/node-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("NodeHttpHandler", () => {
});
it("has metadata", () => {
const nodeHttpHandler = new NodeHttpHandler();
expect(nodeHttpHandler.metadata).toContain("h1");
expect(nodeHttpHandler.metadata.handlerProtocol).toContain("http/1.1");
});
it("can send http requests", async () => {
const mockResponse = {
Expand Down
3 changes: 2 additions & 1 deletion packages/node-http-handler/src/node-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export class NodeHttpHandler implements HttpHandler {
private readonly httpsAgent: https.Agent;
private readonly connectionTimeout?: number;
private readonly socketTimeout?: number;
public readonly metadata = ["h1"];
// Node http handler is hard-coded to http/1.1: https://github.com/nodejs/node/blob/ff5664b83b89c55e4ab5d5f60068fb457f1f5872/lib/_http_server.js#L286
public readonly metadata = { handlerProtocol: "http/1.1" };

constructor({
connectionTimeout,
Expand Down
2 changes: 1 addition & 1 deletion packages/node-http-handler/src/node-http2-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("NodeHttp2Handler", () => {
});

it("has metadata", () => {
expect(nodeH2Handler.metadata).toContain("h2");
expect(nodeH2Handler.metadata.handlerProtocol).toContain("h2");
});

describe("connectionPool", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/node-http-handler/src/node-http2-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface NodeHttp2Options {

export class NodeHttp2Handler implements HttpHandler {
private readonly connectionPool: Map<string, ClientHttp2Session>;
public readonly metadata = ["h2"];
public readonly metadata = { handlerProtocol: "h2" };

constructor(private readonly http2Options: NodeHttp2Options = {}) {
this.connectionPool = new Map<string, ClientHttp2Session>();
Expand Down
8 changes: 7 additions & 1 deletion packages/types/src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ export interface RequestHandler<
* 'h2' refers this handler is for handling HTTP/2 requests,
* whereas 'h1' refers handling HTTP1 requests
*/
metadata?: Array<string>;
metadata?: RequestHandlerMetadata;
destroy?: () => void;
handle: (
request: RequestType,
handlerOptions: HandlerOptions
) => Promise<RequestHandlerOutput<ResponseType>>;
}

export interface RequestHandlerMetadata {
// This infers request handler's protocol
// valid values are stated: https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
handlerProtocol: string;
}

0 comments on commit 4c8361d

Please sign in to comment.