⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.18.0-dev.1272+2ff397f75What's newOn an older Zig?Blograylib-zig

Routes Over a Seam

Updated · Code verified against Zig 0.18.0-dev.1272+2ff397f75

All five CRUD routes of a URL shortener fit in one Zig function. handle takes request bytes and a store, and writes a complete HTTP response. An in-memory store is behind the seam. Thus the tests of each route need no server, no socket and no database.

The problem

In the HTTP surface, create, read, update and delete become routes. This layer also has the most cases that you must make correct:

  • The slug that does not exist.
  • The URL that fails validation.
  • The method that no code handles.

If you test those cases against a live server and database, you must start the server and the database for each case.

The solution is the same seam that the ORM’s Repo chapter built. Put the storage behind an interface. Then give the handler the implementation that it needs at that time. The in-memory store needs no server and no database. Thus this chapter runs in your browser.

The plan

  1. A Store that holds links in a fixed array. The store has only the methods that the routes can use: create and find.
  2. handle: parse the request line, dispatch on the method and the path, and write a complete HTTP response. The request parsing is the minimum that works. The web server track shows the full parsing.
  3. A demo that runs all five routes and the failure cases, and prints each exchange.
const std = @import("std");

// From the slugs chapter, without a change.
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

fn encode(id: u64, buf: *[11]u8) []const u8 {
    var rest = id;
    var i: usize = buf.len;
    while (true) {
        i -= 1;
        buf[i] = alphabet[@intCast(rest % 62)];
        rest /= 62;
        if (rest == 0) break;
    }
    return buf[i..];
}

fn checkTarget(url: []const u8) !void {
    const has_scheme = std.mem.startsWith(u8, url, "https://") or
        std.mem.startsWith(u8, url, "http://");
    if (!has_scheme) return error.SchemeNotAllowed;
    for (url) |ch| {
        if (ch <= ' ' or ch == 0x7f) return error.BadCharacter;
    }
}

// ---------------------------------------------------------------- store

/// All the operations that the routes can ask of storage, and no other
/// operations. The final chapter replaces this struct with a struct whose
/// methods write SQL, and no line of the handler changes. The recording
/// driver of the ORM uses the same split for the same reason: you can test
/// the route logic without a database.
const Store = struct {
    const Link = struct {
        id: u64 = 0,
        slug_buf: [11]u8 = undefined,
        slug_len: usize = 0,
        target_buf: [256]u8 = undefined,
        target_len: usize = 0,
        hits: u64 = 0,
        live: bool = false,

        fn slug(self: *const Link) []const u8 {
            return self.slug_buf[0..self.slug_len];
        }
        fn target(self: *const Link) []const u8 {
            return self.target_buf[0..self.target_len];
        }
    };

    links: [16]Link = @splat(.{}),
    next_id: u64 = 1,

    fn create(self: *Store, target: []const u8) ?*Link {
        for (&self.links) |*link| {
            if (link.live) continue;
            const id = self.next_id;
            self.next_id += 1;
            link.* = .{ .id = id, .live = true };
            // encode fills the end of its buffer, and the slug must be at
            // the front. The ranges can overlap, and @memmove handles that.
            const s = encode(id, &link.slug_buf);
            @memmove(link.slug_buf[0..s.len], s);
            link.slug_len = s.len;
            @memcpy(link.target_buf[0..target.len], target);
            link.target_len = target.len;
            return link;
        }
        return null;
    }

    fn find(self: *Store, slug: []const u8) ?*Link {
        for (&self.links) |*link| {
            if (link.live and std.mem.eql(u8, link.slug(), slug)) return link;
        }
        return null;
    }
};

// -------------------------------------------------------------- handler

fn respond(w: *std.Io.Writer, status: []const u8, extra_header: []const u8, body: []const u8) !void {
    try w.print("HTTP/1.1 {s}\r\n", .{status});
    if (extra_header.len > 0) try w.print("{s}\r\n", .{extra_header});
    try w.print("Content-Length: {d}\r\n\r\n{s}", .{ body.len, body });
}

/// One request in, one response out, and no other effects. The request
/// parsing here is the minimum that works. The web server track shows the
/// full parsing, with headers.
fn handle(store: *Store, request: []const u8, w: *std.Io.Writer) !void {
    const line_end = std.mem.find(u8, request, "\r\n") orelse return error.BadRequest;
    var line = std.mem.tokenizeScalar(u8, request[0..line_end], ' ');
    const method = line.next() orelse return error.BadRequest;
    const path = line.next() orelse return error.BadRequest;
    const body_start = std.mem.find(u8, request, "\r\n\r\n") orelse return error.BadRequest;
    const body = request[body_start + 4 ..];

    var scratch: [300]u8 = undefined;

    // Create: the body is the URL to shorten, and the answer is the slug.
    if (std.mem.eql(u8, method, "POST") and std.mem.eql(u8, path, "/links")) {
        checkTarget(body) catch |err| {
            const msg = try std.mem.print(&scratch, "{t}\n", .{err});
            return respond(w, "400 Bad Request", "", msg);
        };
        const link = store.create(body) orelse
            return respond(w, "507 Insufficient Storage", "", "full\n");
        const msg = try std.mem.print(&scratch, "{s}\n", .{link.slug()});
        return respond(w, "201 Created", "", msg);
    }

    // The management routes use the same prefix and the same lookup.
    if (std.mem.startsWith(u8, path, "/links/")) {
        const link = store.find(path["/links/".len..]) orelse
            return respond(w, "404 Not Found", "", "no such link\n");
        if (std.mem.eql(u8, method, "GET")) {
            const msg = try std.mem.print(&scratch, "{s} -> {s} ({d} hits)\n", .{
                link.slug(), link.target(), link.hits,
            });
            return respond(w, "200 OK", "", msg);
        }
        if (std.mem.eql(u8, method, "PUT")) {
            checkTarget(body) catch |err| {
                const msg = try std.mem.print(&scratch, "{t}\n", .{err});
                return respond(w, "400 Bad Request", "", msg);
            };
            @memcpy(link.target_buf[0..body.len], body);
            link.target_len = body.len;
            return respond(w, "200 OK", "", "updated\n");
        }
        if (std.mem.eql(u8, method, "DELETE")) {
            link.live = false;
            return respond(w, "204 No Content", "", "");
        }
        return respond(w, "405 Method Not Allowed", "", "");
    }

    // The main route of the service: all other paths are slugs.
    if (std.mem.eql(u8, method, "GET")) {
        if (store.find(path[1..])) |link| {
            link.hits += 1;
            const loc = try std.mem.print(&scratch, "Location: {s}", .{link.target()});
            return respond(w, "302 Found", loc, "");
        }
    }
    return respond(w, "404 Not Found", "", "no such link\n");
}

// ----------------------------------------------------------------- demo

pub fn main(init: std.process.Init) !void {
    var out_buf: [4096]u8 = undefined;
    var file_writer = std.Io.File.stdout().writerStreaming(init.io, &out_buf);
    const out = &file_writer.interface;

    var store = Store{};

    const requests = [_][]const u8{
        "POST /links HTTP/1.1\r\nHost: s\r\n\r\nhttps://ziglang.org",
        "POST /links HTTP/1.1\r\nHost: s\r\n\r\njavascript:alert(1)",
        "GET /1 HTTP/1.1\r\nHost: s\r\n\r\n",
        "GET /links/1 HTTP/1.1\r\nHost: s\r\n\r\n",
        "PUT /links/1 HTTP/1.1\r\nHost: s\r\n\r\nhttps://ziglang.org/download/",
        "GET /1 HTTP/1.1\r\nHost: s\r\n\r\n",
        "DELETE /links/1 HTTP/1.1\r\nHost: s\r\n\r\n",
        "GET /1 HTTP/1.1\r\nHost: s\r\n\r\n",
    };

    for (requests) |request| {
        const line_end = std.mem.find(u8, request, "\r\n").?;
        try out.print("> {s}\n", .{request[0..line_end]});

        var response_buf: [512]u8 = undefined;
        var response: std.Io.Writer = .fixed(&response_buf);
        try handle(&store, request, &response);

        // Print the response without the HTTP \r, with a prefix on each line.
        var lines = std.mem.splitSequence(u8, response.buffered(), "\r\n");
        while (lines.next()) |l| {
            const line = std.mem.trimEnd(u8, l, "\n");
            if (line.len > 0) try out.print("< {s}\n", .{line});
        }
        try out.writeAll("\n");
    }

    try out.flush();
}

Read the trace as a contract

The demo is the specification of the service, with one exchange for each case:

  • Create answers 201 with the slug.
  • The bad scheme answers 400, before the storage gets the request.
  • The redirect answers 302 and counts the hit.
  • Update changes the target of the same slug.
  • Delete answers 204.
  • A slug that does not exist now answers 404.

The final chapter replaces the store with SQL. After that change, this trace must stay true.

The order of the routes is important

The handler matches /links and /links/{slug} before the general slug route. Thus a link with the slug links is not reachable. The alphabet from the slugs chapter makes that collision impossible now: links decodes to a very large id, not to an early id.

But route shadowing occurs with no warning. Suppose a person adds /stats to a slug space that grows. No part of the system gives a warning. Keep all reserved paths under one prefix, as /links/... does. This is an easy way to prevent the problem.

Variations

  • No allocation: the store is a fixed array, and the handler builds each response in a buffer that the caller owns. A service of this size can decide its limits at the start. When the array is full, the service answers 507.
  • Add a route: a GET /links listing is one more branch and one more store method. The handler cannot go around the seam. It must ask the store for all the data that it needs.