Skip to content

Commit

Permalink
Exclude cURL from pretty error page (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble authored Mar 8, 2022
1 parent 7280c52 commit 870b401
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/http-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,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 @@ -410,6 +410,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

0 comments on commit 870b401

Please sign in to comment.