Repository navigation
http: accept asterisk-form request targets #32811
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
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { once } from "node:events"; | ||
| import http from "node:http"; | ||
| import net from "node:net"; | ||
|
|
||
| // RFC 9112 3.2.4: the asterisk-form request-target ("*") identifies the | ||
| // server as a whole and is sent by `OPTIONS * HTTP/1.1`. The request-line | ||
| // parser only accepted targets beginning with "/", so asterisk-form requests | ||
| // were answered with 400 Bad Request before ever reaching the handler. Node | ||
| // (llhttp) delivers them with `req.url === "*"`, for any method, so Bun does | ||
| // the same. | ||
|
Check warning on line 11 in test/js/node/http/node-http-asterisk-form-target.test.ts
|
||
|
robobun marked this conversation as resolved.
Resolved
|
||
|
|
||
| async function writeAndCollect(port: number, requestLine: string) { | ||
| const client = net.connect(port); | ||
| try { | ||
| client.on("error", () => {}); | ||
| const chunks: Buffer[] = []; | ||
| client.on("data", chunk => chunks.push(chunk)); | ||
| const closed = once(client, "close"); | ||
| client.write(`${requestLine}\r\nHost: localhost\r\nConnection: close\r\n\r\n`); | ||
| await closed; | ||
| return Buffer.concat(chunks).toString(); | ||
| } finally { | ||
| client.destroy(); | ||
| } | ||
| } | ||
|
|
||
| describe("node:http server", () => { | ||
| // llhttp accepts asterisk-form for any method, not only OPTIONS. | ||
| test.each(["OPTIONS", "GET"])("delivers `%s * HTTP/1.1` to the handler with url '*'", async method => { | ||
| const { promise: handled, resolve } = Promise.withResolvers<{ method: string; url: string }>(); | ||
| await using server = http | ||
| .createServer((req, res) => { | ||
| resolve({ method: req.method!, url: req.url! }); | ||
| res.end("hi"); | ||
| }) | ||
| .listen(0); | ||
| await once(server, "listening"); | ||
|
|
||
| const wire = await writeAndCollect((server.address() as net.AddressInfo).port, `${method} * HTTP/1.1`); | ||
| expect(wire).toStartWith("HTTP/1.1 200 OK\r\n"); | ||
| expect(wire).toEndWith("\r\n\r\nhi"); | ||
| expect(await handled).toEqual({ method, url: "*" }); | ||
| }); | ||
|
|
||
| test("an asterisk-form target with a query is delivered verbatim, like Node", async () => { | ||
| const { promise: handled, resolve } = Promise.withResolvers<string>(); | ||
| await using server = http | ||
| .createServer((req, res) => { | ||
| resolve(req.url!); | ||
| res.end(); | ||
| }) | ||
| .listen(0); | ||
| await once(server, "listening"); | ||
|
|
||
| const wire = await writeAndCollect((server.address() as net.AddressInfo).port, "OPTIONS *?a=1 HTTP/1.1"); | ||
| expect(wire).toStartWith("HTTP/1.1 200 OK\r\n"); | ||
| expect(await handled).toBe("*?a=1"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Bun.serve", () => { | ||
| // Bun.serve shares the same request-line parser, so it was rejecting | ||
| // asterisk-form targets too. | ||
| test("delivers `OPTIONS * HTTP/1.1` to fetch()", async () => { | ||
| const { promise: handled, resolve } = Promise.withResolvers<string>(); | ||
| await using server = Bun.serve({ | ||
| port: 0, | ||
| fetch(req) { | ||
| resolve(req.method); | ||
| return new Response("hi"); | ||
| }, | ||
| }); | ||
|
|
||
| const wire = await writeAndCollect(server.port, "OPTIONS * HTTP/1.1"); | ||
| expect(wire).toStartWith("HTTP/1.1 200 OK\r\n"); | ||
| expect(await handled).toBe("OPTIONS"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.