diff options
author | 2022-01-29 23:51:01 -0800 | |
---|---|---|
committer | 2022-01-29 23:51:01 -0800 | |
commit | 1de275325ee3067b3eb230ec644ed4765cb50c7d (patch) | |
tree | e51b09d2fc443d00d3a08e918799805dfdbea7b6 | |
parent | 14301284afe814261b6c5232c1209c8f9244497f (diff) | |
download | bun-1de275325ee3067b3eb230ec644ed4765cb50c7d.tar.gz bun-1de275325ee3067b3eb230ec644ed4765cb50c7d.tar.zst bun-1de275325ee3067b3eb230ec644ed4765cb50c7d.zip |
Embed a favicon when none exists
-rw-r--r-- | src/favicon.png | bin | 0 -> 9076 bytes | |||
-rw-r--r-- | src/http.zig | 30 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/favicon.png b/src/favicon.png Binary files differnew file mode 100644 index 000000000..074beae19 --- /dev/null +++ b/src/favicon.png diff --git a/src/http.zig b/src/http.zig index 491657811..f05fdb161 100644 --- a/src/http.zig +++ b/src/http.zig @@ -103,6 +103,27 @@ pub const RequestContext = struct { /// --disable-bun.js propagates here pub var fallback_only = false; + const default_favicon = @embedFile("favicon.png"); + const default_favicon_shasum = "07877ad4cdfe472cc70759d1f237d358ae1f6a9b"; + pub fn sendFavicon(ctx: *RequestContext) !void { + defer ctx.done(); + ctx.appendHeader("Content-Type", MimeType.byExtension("png").value); + ctx.appendHeader("ETag", default_favicon_shasum); + ctx.appendHeader("Age", "0"); + ctx.appendHeader("Cache-Control", "public, max-age=3600"); + + if (ctx.header("If-None-Match")) |etag_header| { + if (strings.eqlLong(default_favicon_shasum, etag_header, true)) { + try ctx.sendNotModified(); + return; + } + } + + try ctx.writeStatus(200); + try ctx.prepareToSendBody(default_favicon.len, false); + try ctx.writeBodyBuf(default_favicon); + } + fn parseOrigin(this: *RequestContext) void { var protocol: ?string = null; var host: ?string = null; @@ -3333,6 +3354,15 @@ pub const Server = struct { } if (!finished) { + // if we're about to 404 and it's the favicon, use our stand-in + if (strings.eqlComptime(req_ctx.url.path, "favicon.ico")) { + req_ctx.sendFavicon() catch |err| { + Output.printErrorln("FAIL [{s}] - {s}: {s}", .{ @errorName(err), req.method, req.path }); + did_print = true; + }; + return; + } + req_ctx.sendNotFound() catch {}; } } |