Skip to content
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

Exclude cURL from pretty error page #210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/http-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,12 @@ export function createRequestListener<Plugins extends HTTPPluginSignatures>(
} catch (e: any) {
// MIME types aren't case sensitive
const accept = req.headers.accept?.toLowerCase() ?? "";
const userAgent = req.headers["user-agent"]?.toLowerCase() ?? "";
if (
accept.includes("text/html") ||
accept.includes("*/*") ||
accept.includes("text/*")
!userAgent.includes("curl/") &&
(accept.includes("text/html") ||
accept.includes("*/*") ||
accept.includes("text/*"))
) {
// Send pretty HTML error page if client accepts it
const Youch: typeof import("youch").default = require("youch");
Expand Down
7 changes: 7 additions & 0 deletions packages/http-server/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ test("createRequestListener: displays appropriately-formatted error page", async
t.is(headers["content-type"], "text/html; charset=UTF-8");
[, headers] = await request(port, "/", { accept: "image/png, */*" });
t.is(headers["content-type"], "text/html; charset=UTF-8");

// Check pretty HTML error page isn't returned for cURL
[, headers] = await request(port, "/", {
accept: "*/*",
"user-agent": "curl/7.77.0",
});
t.is(headers["content-type"], "text/plain; charset=UTF-8");
});
test("createRequestListener: includes live reload script in html responses if enabled", async (t) => {
const mf = useMiniflareWithHandler({ HTTPPlugin }, {}, (globals) => {
Expand Down