Skip to content

Commit

Permalink
fix goaway to match nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Oct 12, 2024
1 parent 9167863 commit e9791e1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/bun.js/api/bun/h2_frame_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1925,15 +1925,10 @@ pub const H2FrameParser = struct {

if (handleIncommingPayload(this, data, frame.streamIdentifier)) |content| {
const payload = content.data;
const last_stream_id: u32 = @intCast(UInt31WithReserved.fromBytes(payload[0..4]).uint31);
const error_code = UInt31WithReserved.fromBytes(payload[4..8]).toUInt32();
const chunk = this.handlers.binary_type.toJS(payload[8..], this.handlers.globalObject);
this.readBuffer.reset();
if (error_code != @intFromEnum(ErrorCode.NO_ERROR)) {
this.dispatchWith2Extra(.onGoAway, JSC.JSValue.jsNumber(error_code), JSC.JSValue.jsNumber(last_stream_id), chunk);
} else {
this.dispatchWithExtra(.onGoAway, JSC.JSValue.jsNumber(last_stream_id), chunk);
}
this.dispatchWith2Extra(.onGoAway, JSC.JSValue.jsNumber(error_code), JSC.JSValue.jsNumber(this.lastStreamID), chunk);
return content.end;
}
return data.len;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//#FILE: test-http2-compat-write-early-hints-invalid-argument-type.js
//#SHA1: 8ae2eba59668a38b039a100d3ad26f88e54be806
//-----------------
"use strict";

const http2 = require("node:http2");
const util = require("node:util");
const debug = util.debuglog("test");

const testResBody = "response content";

// Check if crypto is available
let hasCrypto = false;
try {
require("crypto");
hasCrypto = true;
} catch (err) {
// crypto not available
}

(hasCrypto ? describe : describe.skip)("HTTP2 compat writeEarlyHints invalid argument type", () => {
let server;
let client;

beforeAll(done => {
server = http2.createServer();
server.listen(0, () => {
done();
});
});

afterAll(() => {
if (client) {
client.close();
}
server.close();
});

test("should throw ERR_INVALID_ARG_TYPE for invalid object value", done => {
server.on("request", (req, res) => {
debug("Server sending early hints...");
expect(() => {
res.writeEarlyHints("this should not be here");
}).toThrow(
expect.objectContaining({
code: "ERR_INVALID_ARG_TYPE",
name: "TypeError",
}),
);

debug("Server sending full response...");
res.end(testResBody);
});

client = http2.connect(`http://localhost:${server.address().port}`);
const req = client.request();

debug("Client sending request...");

req.on("headers", () => {
done(new Error("Should not receive headers"));
});

req.on("response", () => {
done();
});

req.end();
});
});

//<#END_FILE: test-http2-compat-write-early-hints-invalid-argument-type.js
58 changes: 58 additions & 0 deletions test/js/node/test/parallel/http2-goaway-opaquedata.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//#FILE: test-http2-goaway-opaquedata.js
//#SHA1: 5ad5b6a64cb0e7419753dcd88d59692eb97973ed
//-----------------
'use strict';

const http2 = require('http2');

let server;
let serverPort;

beforeAll((done) => {
server = http2.createServer();
server.listen(0, () => {
serverPort = server.address().port;
done();
});
});

afterAll((done) => {
server.close(done);
});

test('HTTP/2 GOAWAY with opaque data', (done) => {
const data = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]);
let session;

server.once('stream', (stream) => {
session = stream.session;
session.on('close', () => {
expect(true).toBe(true); // Session closed
});
session.goaway(0, 0, data);
stream.respond();
stream.end();
});

const client = http2.connect(`http://localhost:${serverPort}`);
client.once('goaway', (code, lastStreamID, buf) => {
expect(code).toBe(0);
expect(lastStreamID).toBe(1);
expect(buf).toEqual(data);
session.close();
client.close();
done();
});

const req = client.request();
req.resume();
req.on('end', () => {
expect(true).toBe(true); // Request ended
});
req.on('close', () => {
expect(true).toBe(true); // Request closed
});
req.end();
});

//<#END_FILE: test-http2-goaway-opaquedata.js

0 comments on commit e9791e1

Please sign in to comment.