From 4a88203dfd186733bdfb31225641af3e8e51d19b Mon Sep 17 00:00:00 2001 From: Pierre De Lancre Date: Tue, 30 Jun 2026 01:05:59 +0300 Subject: [PATCH] first try baby --- .gitignore | 6 +++ build.zig | 22 +++++++++ build.zig.zon | 7 +++ main.zig | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ new-pots.py | 104 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 259 insertions(+) create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 main.zig create mode 100644 new-pots.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1241f6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# Zig stuff + +.zig-cache/ +zig-out/ +*.o +teapots/* diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..f3594ed --- /dev/null +++ b/build.zig @@ -0,0 +1,22 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "bukubukuchagama", + .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + }), + }); + + b.installArtifact(exe); + + const run_exe = b.addRunArtifact(exe); + + const run_step = b.step("run", "Run the application"); + run_step.dependOn(&run_exe.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..c95ad14 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,7 @@ +.{ + .name = .bukubukuchagama, + .version = "0.0.1", + .minimum_zig_version = "0.16.0", + .paths = .{""}, + .fingerprint = 0xdb83f73785cc7570, +} diff --git a/main.zig b/main.zig new file mode 100644 index 0000000..c921100 --- /dev/null +++ b/main.zig @@ -0,0 +1,120 @@ +//! Reference implementation of compliance with RFC 2324 +//! / RFC 7168 section 2.3.2 — HTCPCP "I'm a teapot" +//! +//! Build: zig build -Doptimize=ReleaseFast +//! Run: ./zig-out/bin/bukubukuchagama (listens on 127.0.0.1:8418) +//! +//! Caddyfile: +//! @garbage { +//! path *.php *.asp *.aspx *.cgi /wp-login.php /wp-admin/* /.env /.git/* /phpmyadmin/* /xmlrpc.php +//! } +//! reverse_proxy @garbage 127.0.0.1:8418 +//! + +const std = @import("std"); +const log = std.log.scoped(.teapot); + +const TEAPOT_DIR = "./teapots"; +const LISTEN_ADDR = "127.0.0.1"; +const LISTEN_PORT = 8418; + +/// Populated once at startup, read-only for the lifetime of the process — +/// safe to read from concurrent connection tasks without synchronization. +var images: std.ArrayList([]const u8) = undefined; + +fn loadImageList(allocator: std.mem.Allocator, io: std.Io) !void { + images = .empty; + + var dir = try std.Io.Dir.cwd().openDir(io, TEAPOT_DIR, .{ .iterate = true }); + defer dir.close(io); + + var it = try dir.walk(allocator); + while (try it.next(io)) |entry| { + if (entry.kind != .file) continue; + if (!std.mem.endsWith(u8, entry.basename, ".webp")) continue; + try images.append(allocator, try allocator.dupe(u8, entry.basename)); + } + + if (images.items.len == 0) return error.NoTeapotImagesFound; +} + +fn serveTeapot(allocator: std.mem.Allocator, io: std.Io, rand: std.Random, req: *std.http.Server.Request) !void { + const idx = rand.intRangeLessThan(u64, 0, images.items.len); + const name = images.items[idx]; + + const path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ TEAPOT_DIR, name }); + defer allocator.free(path); + + log.info("{s} {s} -> 418 ({s})", .{ @tagName(req.head.method), req.head.target, name }); + + const body = std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .unlimited) catch |err| { + log.err("failed to read {s}: {t}", .{ path, err }); + try req.respond("I'm a teapot.\n", .{ .status = .teapot }); + return; + }; + defer allocator.free(body); + + try req.respond(body, .{ + .status = .teapot, + .reason = "I'm a teapot", + .extra_headers = &.{ + .{ .name = "content-type", .value = "image/webp" }, + .{ .name = "x-htcpcp", .value = "RFC 2324 / RFC 7168 section 2.3.2" }, + }, + }); +} + +fn handleStream(io: std.Io, stream: std.Io.net.Stream, rand: std.Random) void { + defer stream.close(io); + + // Per-connection arena: cheap, and guarantees cleanup even if something + // in the request path errors out partway through. + var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena_state.deinit(); + const allocator = arena_state.allocator(); + + var read_buffer: [4096]u8 = undefined; + var write_buffer: [4096]u8 = undefined; + var reader = stream.reader(io, &read_buffer); + var writer = stream.writer(io, &write_buffer); + + var http_server = std.http.Server.init(&reader.interface, &writer.interface); + + while (true) { + var req = http_server.receiveHead() catch |err| switch (err) { + error.HttpConnectionClosing => return, + else => return, + }; + + serveTeapot(allocator, io, rand, &req) catch |err| { + log.err("failed to respond: {t}", .{err}); + return; + }; + + _ = arena_state.reset(.retain_capacity); + } +} + +pub fn main(init: std.process.Init) !void { + const io = init.io; + const gpa = init.gpa; + const ioSource: std.Random.IoSource = .{ .io = io }; + const rand = ioSource.interface(); + + try loadImageList(gpa, init.io); + log.info("loaded {d} teapot image(s) from {s}/", .{ images.items.len, TEAPOT_DIR }); + + const addr = std.Io.net.IpAddress.parseIp4(LISTEN_ADDR, LISTEN_PORT) catch unreachable; + var server = try addr.listen(io, .{ .reuse_address = true }); + defer server.deinit(io); + + log.info("teapot responder listening on http://{s}:{d}", .{ LISTEN_ADDR, LISTEN_PORT }); + + var group: std.Io.Group = .init; + defer group.cancel(io); + + while (true) { + const stream = try server.accept(io); + group.async(io, handleStream, .{ io, stream, rand }); + } +} diff --git a/new-pots.py b/new-pots.py new file mode 100644 index 0000000..3175822 --- /dev/null +++ b/new-pots.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +"""Download Danbooru posts tagged 'holding_teapot' and convert them to WebP. + +Requires ImageMagick's `mogrify` to be installed and on PATH. +""" + +import json +import os +import subprocess +import sys +import time +import urllib.parse +import urllib.request + +BASE_URL = "https://danbooru.donmai.us/posts" +TAGS = "holding_teapot -animated rating:general score:>=4" +OUT_DIR = "teapots" +IMAGE_EXTS = {"jpg", "jpeg", "png", "gif", "bmp"} +HEADERS = {"User-Agent": "teapot-scraper/1.0"} + +os.makedirs(OUT_DIR, exist_ok=True) + + +def fetch_page(page: int): + query = urllib.parse.urlencode( + {"tags": TAGS, "format": "json", "page": page}) + req = urllib.request.Request(f"{BASE_URL}?{query}", headers=HEADERS) + with urllib.request.urlopen(req) as resp: + return json.loads(resp.read()) + + +def download(url: str, dest: str): + req = urllib.request.Request(url, headers=HEADERS) + with urllib.request.urlopen(req) as resp, open(dest, "wb") as f: + f.write(resp.read()) + + +def main(): + page = 64 # last page I got, will figure out how to get only posts we don't have later + while True: + print(f"Fetching page {page}...") + try: + posts = fetch_page(page) + except Exception as e: + print(f"Error fetching page {page}: {e}", file=sys.stderr) + break + + if not posts: + print("Empty page, stopping.") + break + + downloaded = [] + for post in posts: + post_id = post.get("id") + file_ext = (post.get("file_ext") or "").lower() + file_url = post.get("file_url") + + if file_ext not in IMAGE_EXTS: + continue + if not file_url: + print(f" Skipping post { + post_id}: no file_url (likely gated/login-only)") + continue + + dest = os.path.join(OUT_DIR, f"{post_id}.{file_ext}") + try: + download(file_url, dest) + downloaded.append(dest) + print(f" Saved {dest}") + except Exception as e: + print(f" Failed to download post { + post_id}: {e}", file=sys.stderr) + + if downloaded: + print(f"Converting {len(downloaded)} file(s) to webp...") + # WebP's bitstream format hard-caps width/height at 16383px, so + # downscale anything bigger before converting (the trailing '>' + # means "only resize if larger than this", smaller images are + # left untouched). Don't use check=True: one oversized/corrupt + # input shouldn't take down the whole batch. + result = subprocess.run( + ["mogrify", "-resize", "16383x16383>", + "-format", "webp", *downloaded], + capture_output=True, + text=True, + ) + if result.returncode != 0: + print(f" mogrify reported errors:\n{ + result.stderr}", file=sys.stderr) + + for path in downloaded: + webp_path = os.path.splitext(path)[0] + ".webp" + if os.path.exists(webp_path): + os.remove(path) + else: + print(f" Keeping original, conversion failed: { + path}", file=sys.stderr) + + page += 1 + time.sleep(1) # be polite to the API + + +if __name__ == "__main__": + main()