Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-pkg-server

Source of release 0.6.0

0.6.0 / src / registry.lucb

## Loopback registry API. Reverse-proxy headers never establish identity.
from luce_server import http
import interop
from luce_db import db
from luce_auth import auth
import ownership
from luce_json import json
import os
import c
import strings
import repositories
from luce_git import git
import memory
import io
from luce_pkg import pkg
import http_auth as transport_auth
from luce_crypto import native as crypto
import time
import rate_limit
import site as site_files
import gzip
import sync
from luce_prism import prism

let max_release_upload: usize = repositories.max_object_bytes + 24576 + 3309 + 8

func failed(code: ErrorCode, message: str) -> interop.Outcome[interop.Reference[http.Response]]:
    let copied = interop.copy_text(message) catch failure:
        return interop.Outcome[interop.Reference[http.Response]].failure(failure.code, interop.Owned[str]("internal error"))
    return interop.Outcome[interop.Reference[http.Response]].failure(code, copied)

func reply(text: str, status: i64 = 200) -> interop.Outcome[interop.Reference[http.Response]]:
    return binary_reply(text.bytes, "text/plain; charset=utf-8", status)

func binary_reply(bytes: const u8[], content_type: str, status: i64 = 200) -> interop.Outcome[interop.Reference[http.Response]]:
    let value = http.Response.bytes(bytes, content_type, status) catch failure:
        return failed(failure.code, "internal error")
    value.value().header("Cache-Control", "no-store") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    return interop.Outcome[interop.Reference[http.Response]].success(interop.Owned[interop.Reference[http.Response]](value, &value.owner.header))

func basic_challenge() -> interop.Outcome[interop.Reference[http.Response]]:
    let value = http.Response.bytes(b"authentication required", "text/plain; charset=utf-8", 401) catch failure:
        return failed(failure.code, "internal error")
    value.value().header("Cache-Control", "no-store") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    value.value().header("WWW-Authenticate", "Basic realm=\"pkg.luciaos.com\", charset=\"UTF-8\"") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    return interop.Outcome[interop.Reference[http.Response]].success(interop.Owned[interop.Reference[http.Response]](value, &value.owner.header))

func rate_limited() -> interop.Outcome[interop.Reference[http.Response]]:
    let value = http.Response.bytes(b"rate limited", "text/plain; charset=utf-8", 429) catch failure:
        return failed(failure.code, "internal error")
    value.value().header("Cache-Control", "no-store") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    value.value().header("Retry-After", "60") catch failure:
        value.release()
        return failed(failure.code, "internal error")
    return interop.Outcome[interop.Reference[http.Response]].success(interop.Owned[interop.Reference[http.Response]](value, &value.owner.header))

func valid_token(token: str) -> bool:
    if token.length != 32: return false
    for byte in token.bytes:
        if not ((byte >= '0' and byte <= '9') or (byte >= 'a' and byte <= 'f')): return false
    return true

func valid_credential_token(token: str) -> bool:
    if token.length != 64: return false
    for byte in token.bytes:
        if not ((byte >= '0' and byte <= '9') or (byte >= 'a' and byte <= 'f')): return false
    return true

func bearer(request: const http.Request*) -> str!:
    let header = (try request.header("Authorization")) else error(auth.invalid, "authentication required")
    if not strings.starts_with(header, "Bearer "): error(auth.invalid, "authentication required")
    let token = (str)header.bytes[7..]
    if not valid_token(token): error(auth.invalid, "authentication required")
    return token

func credential_bearer(request: const http.Request*) -> str!:
    let header = (try request.header("Authorization")) else error(auth.invalid, "authentication required")
    if not strings.starts_with(header, "Bearer "): error(auth.invalid, "authentication required")
    let token = (str)header.bytes[7..]
    if not valid_credential_token(token): error(auth.invalid, "authentication required")
    return token

func field(root: json.View, name: str) -> str!:
    let value = root.member(name) else error(auth.invalid, "missing field")
    return value.text() else error(auth.invalid, "string field required")

func positive_number(value: str, maximum: i64) -> i64!:
    if value.length == 0: error(auth.invalid, "positive integer required")
    var result: i64 = 0
    for byte in value.bytes:
        if byte < '0' or byte > '9': error(auth.invalid, "positive integer required")
        result = result * 10 + (i64)(byte - '0')
        if result > maximum: error(auth.invalid, "positive integer exceeds limit")
    if result <= 0: error(auth.invalid, "positive integer required")
    return result

func json_object(maximum: usize = 1048576) -> interop.Reference[json.Value]!:
    let native = try new json.Value(maximum) in memory.heap
    return interop.Reference[json.Value].adopt(native, json.value_type, memory.heap) catch failure:
        native.close()
        free(native) in memory.heap
        error(failure.code, failure.message)

func pull_request_document(record: repositories.PullRequest*) -> interop.Reference[json.Value]!:
    let result = try json_object(16384)
    errdefer result.release()
    let value = result.value()
    var base_id: u8[40]
    var head_id: u8[40]
    var merge_id: u8[40]
    _ = try git.hex_id(record.base_id, base_id)
    _ = try git.hex_id(record.head_id, head_id)
    _ = try git.hex_id(record.merge_id, merge_id)
    try value.set_integer("number", record.number)
    try value.set_text("author", (str)record.author[..<record.author_length])
    try value.set_text("title", (str)record.title[..<record.title_length])
    try value.set_text("body", (str)record.body[..<record.body_length])
    try value.set_text("base", (str)record.base[..<record.base_length])
    try value.set_text("head", (str)record.head[..<record.head_length])
    try value.set_text("base_commit", (str)base_id)
    try value.set_text("head_commit", (str)head_id)
    try value.set_text("merge_commit", "" if record.state != .merged else (str)merge_id)
    try value.set_text("state", "open" if record.state == .open else ("closed" if record.state == .closed else "merged"))
    try value.set_integer("created_at", record.created_at)
    try value.set_integer("updated_at", record.updated_at)
    try value.set_text("closed_by", (str)record.closed_by[..<record.closed_by_length])
    return result

func json_reply(value: interop.Reference[json.Value], status: i64 = 200) -> interop.Outcome[interop.Reference[http.Response]]:
    let text = value.value().encode()
    return binary_reply(text.bytes, "application/json", status)

struct App:
    var database: interop.Reference[db.Database]
    var authority: interop.Reference[auth.Authority]
    var origin: u8[255]
    var origin_size: usize = 0
    ## Directory of static public release files; empty disables tag releases.
    var site: u8[255]
    var site_size: usize = 0

    func close():
        self.authority.release()
        self.database.release()

    func health(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        _ = self
        _ = view
        return reply("ok")

    func identity(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(token) catch: return reply("unauthorized", 401)
        defer who.release()
        let name = who.value().text_at() catch: return reply("internal error", 500)
        return reply(name)

    func redeem(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 3: return reply("invalid fields", 400)
        let code = field(root, "code") catch: return reply("invalid fields", 400)
        let name = field(root, "name") catch: return reply("invalid fields", 400)
        let password = field(root, "password") catch: return reply("invalid fields", 400)
        if not valid_token(code): return reply("invalid invitation", 400)
        if registration_closed(): return reply("registration is closed", 403)
        if not rate_limit.admit_registration(time.unix()): return rate_limited()
        self.authority.value().accept(code, name, password) catch failure:
            if failure.code == auth.conflict: return reply("registration conflict", 409)
            if failure.code == auth.invalid: return reply("invalid registration", 400)
            return reply("registration failed", 503)
        return reply("registered", 201)

    func login(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 2: return reply("invalid fields", 400)
        let name = field(root, "name") catch: return reply("invalid fields", 400)
        let password = field(root, "password") catch: return reply("invalid fields", 400)
        if not rate_limit.admit_login(time.unix()): return rate_limited()
        let token = self.authority.value().login(name, password) catch failure:
            if failure.code == auth.busy: return reply("authentication busy", 503)
            return reply("unauthorized", 401)
        defer token.release()
        let text = token.value().text_at() catch: return reply("internal error", 500)
        return reply(text)

    func logout(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        self.authority.value().revoke(token) catch: return reply("unauthorized", 401)
        return reply("revoked")

    func issue_credential(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(token) catch: return reply("unauthorized", 401)
        defer who.release()
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 3: return reply("invalid fields", 400)
        let scope = field(root, "scope") catch: return reply("invalid fields", 400)
        let repository = field(root, "repository") catch: return reply("invalid fields", 400)
        let lifetime_field = root.member("lifetime_seconds") else return reply("invalid fields", 400)
        let lifetime = lifetime_field.integer() else return reply("invalid fields", 400)
        let credential = self.authority.value().issue_credential(token, scope, repository, lifetime) catch failure:
            if failure.code == auth.invalid: return reply("invalid credential request", 400)
            return reply("credential issuance unavailable", 503)
        defer credential.release()
        let text = credential.value().text_at() catch: return reply("internal error", 500)
        return reply(text, 201)

    func revoke_credential(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let session = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(session) catch: return reply("unauthorized", 401)
        defer who.release()
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 1: return reply("invalid fields", 400)
        let credential = field(root, "token") catch: return reply("invalid fields", 400)
        if not valid_credential_token(credential): return reply("invalid fields", 400)
        self.authority.value().revoke_credential(session, credential) catch: return reply("credential not found", 404)
        return reply("revoked")

    func create_repository(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let token = bearer(view.value()) catch: return reply("unauthorized", 401)
        let who = self.authority.value().verify(token) catch: return reply("unauthorized", 401)
        defer who.release()
        let principal = who.value().text_at() catch: return reply("internal error", 500)
        let body = view.value().body() catch: return reply("invalid body", 400)
        defer body.release()
        let payload = body.value().bytes(4096) catch: return reply("invalid body", 400)
        let document = json.decode((str)payload, 4096) catch: return reply("invalid JSON", 400)
        defer document.release()
        let root = document.value().root()
        if root.kind() != json.Kind.object or root.length() != 1: return reply("invalid fields", 400)
        let name = field(root, "name") catch: return reply("invalid fields", 400)
        repositories.create(self.database.value(), principal, name) catch failure:
            if failure.code == auth.invalid: return reply("invalid repository", 400)
            if failure.code == auth.conflict: return reply("repository conflict", 409)
            return reply("repository creation failed", 503)
        return reply("created", 201)

    func git_http(view: interop.View[http.Request]) -> interop.Outcome[interop.Reference[http.Response]]:
        let request = view.value()
        let owner = request.parameter("owner") else return reply("invalid path", 400)
        let name = request.parameter("name") else return reply("invalid path", 400)
        if not repositories.valid_name(name): return reply("invalid repository", 400)
        var upload = false
        if request.method() == "GET" or request.method() == "HEAD":
            let service = (request.query("service") catch: return reply("invalid service", 400)) else return reply("service required", 400)
            if service != "git-receive-pack" and service != "git-upload-pack": return reply("unsupported Git service", 403)
            upload = service == "git-upload-pack"
        elif strings.ends_with(request.path(), "/git-upload-pack"):
            upload = true
        # Everything is public: clone and fetch need no account and read as the owner.
        # Only a push authenticates, with a git:write credential for this repository.
        var principal = owner
        var credential_buffer: u8[128]
        defer crypto.wipe(credential_buffer)
        var who: interop.Reference[prism.Value]? = none
        defer release_principal(who)
        if upload:
            if not readers.enter(): return busy()
        else:
            let header = (request.header("Authorization") catch: return basic_challenge()) else return basic_challenge()
            let credential = transport_auth.decode_basic(header, credential_buffer) catch: return basic_challenge()
            if credential.username != owner: return basic_challenge()
            let authorized = self.authority.value().authorize_credential(credential.token, auth.scope_git_write, owner, name) catch: return basic_challenge()
            who = authorized
            principal = authorized.value().text_at() catch: return reply("internal error", 500)
        defer if_upload_leave(upload)
        if request.method() == "GET" or request.method() == "HEAD":
            let output = new u8[4194304] --- in memory.heap catch: return reply("internal error", 500)
            defer free(output) in memory.heap
            let n = repositories.advertise_receive(self.database.value(), principal, owner, name, output, upload) catch failure:
                if failure.code == auth.missing: return reply("repository not found", 404)
                return reply("discovery failed", 503)
            return binary_reply(output[..<n], "application/x-git-upload-pack-advertisement" if upload else "application/x-git-receive-pack-advertisement")
        let content_type = (request.header("Content-Type") catch: return reply("invalid content type", 415)) else return reply("content type required", 415)
        if upload:
            if content_type != "application/x-git-upload-pack-request": return reply("unsupported content type", 415)
            let body = request.body() catch: return reply("invalid body", 400)
            defer body.release()
            let received = body.value().bytes(1048576) catch: return reply("invalid fetch body", 400)
            let inflated = inflated_body(request, received, 1048576) catch failure:
                return reply(failure.message, 415 if failure.code == prism.unsupported else 400)
            defer release_inflated(inflated)
            var wire: const u8[] = received
            if let bytes = inflated: wire = bytes
            _ = git.decode_fetch(wire) catch: return reply("invalid fetch request", 400)
            let output = new u8[67108864] --- in memory.heap catch: return reply("internal error", 500)
            defer free(output) in memory.heap
            let n = repositories.upload_pack(self.database.value(), principal, owner, name, wire, output) catch:
                return reply("fetch rejected", 403)
            return binary_reply(output[..<n], "application/x-git-upload-pack-result")
        if content_type != "application/x-git-receive-pack-request": return reply("unsupported content type", 415)
        let body = request.body() catch: return reply("invalid body", 400)
        defer body.release()
        let received = body.value().bytes((i64)repositories.max_object_bytes) catch: return reply("invalid body", 400)
        let inflated = inflated_body(request, received, repositories.max_object_bytes) catch failure:
            return reply(failure.message, 415 if failure.code == prism.unsupported else 400)
        defer release_inflated(inflated)
        var wire: const u8[] = received
        if let bytes = inflated: wire = bytes
        # Before a push larger than its buffer, Git sends a lone flush packet to check that
        # it is authorized. It already is by this point; answer with an empty result.
        if wire.length == 4 and (str)wire == "0000": return binary_reply(b"", "application/x-git-receive-pack-result")
        let commands = git.decode_push(wire) catch: return reply("invalid push request", 400)
        var report = false
        var start: usize = 0
        for i in 0..<commands.capabilities.length + 1:
            if i == commands.capabilities.length or commands.capabilities.bytes[i] == ' ':
                if (str)commands.capabilities.bytes[start..<i] == "report-status": report = true
                start = i + 1
        # Reserve bounded status storage before any ref mutation. The parser can
        # return 64 names of up to 4096 bytes, even when policy rejects them.
        let output = new u8[270336] --- in memory.heap catch: return reply("internal error", 500)
        defer free(output) in memory.heap
        var accepted = true
        # Release-rule failures and the limits a push exceeded are reported to the pusher, who
        # can act on them (`luc publish` pushes a long history in steps); anything else stays
        # generic.
        var reason_buffer: u8[160]
        var reason_size: usize = 0
        _ = repositories.receive_pack(self.database.value(), principal, owner, name, wire, (str)self.site[..<self.site_size]) catch failure:
            accepted = false
            # The pusher sees only those reasons; the journal keeps every one, as
            # one line of plain text whatever bytes the message holds.
            var log_line: u8[512]
            var used = (strings.format(log_line, f"push to {owner}/{name} refused: ") catch: recover "").length
            for byte in failure.message.bytes:
                if used == log_line.length - 1: break
                log_line[used] = byte if byte >= 0x20 and byte < 0x7f else '?'
                used += 1
            log_line[used] = '\n'
            _ = io.stderr().write(log_line[..<used + 1]) catch: recover 0
            let explained = failure.message if strings.starts_with(failure.message, "release: ") or strings.ends_with(failure.message, " limit exceeded") else ""
            for byte in explained.bytes:
                if reason_size == reason_buffer.length: break
                reason_buffer[reason_size] = byte if byte >= 0x20 and byte < 0x7f else ' '
                reason_size += 1
            recover 0
        let reason = (str)reason_buffer[..<reason_size] if reason_size > 0 else "transaction rejected"
        if not report:
            if not accepted: return reply("push rejected", 409)
            return binary_reply(b"", "application/x-git-receive-pack-result")
        var used = git.encode_packet(git.PacketKind.data, b"unpack ok\n" if accepted else b"unpack rejected\n", output) catch: return reply("internal error", 500)
        for i in 0..<commands.count:
            let command = commands.at(i) catch: return reply("invalid push", 400)
            var line: u8[4352]
            let text = strings.format(line, f"ok {command.name}\n" if accepted else f"ng {command.name} {reason}\n") catch: return reply("internal error", 500)
            used += git.encode_packet(git.PacketKind.data, text.bytes, output[used..]) catch: return reply("internal error", 500)
        used += git.encode_packet(git.PacketKind.flush, b"", output[used..]) catch: return reply("internal error", 500)
        return binary_reply(output[..<used], "application/x-git-receive-pack-result")

## A smart-HTTP request body Git sent with `Content-Encoding: gzip`, inflated into a
## heap allocation the caller releases and capped at the same `limit` as an
## uncompressed body; none when the body is not encoded.
func inflated_body(request: const http.Request*, received: const u8[], limit: usize) -> u8[]?!:
    let encoding = (request.header("Content-Encoding") catch: error(auth.invalid, "invalid content encoding")) else return none
    if encoding.length == 0 or encoding == "identity": return none
    if encoding != "gzip" and encoding != "x-gzip": error(prism.unsupported, "unsupported content encoding")
    return gzip.inflate(received, limit) catch: error(auth.invalid, "invalid gzip body")

func release_inflated(inflated: u8[]?):
    if let bytes = inflated: free(bytes) in memory.heap

## Anonymous clone and fetch do real work per request, so only a few run at once.
struct Readers:
    var mutex: sync.Mutex
    var active: u32

    func enter() -> bool:
        self.mutex.lock()
        defer self.mutex.unlock()
        if self.active >= 6: return false
        self.active += 1
        return true

    func leave():
        self.mutex.lock()
        defer self.mutex.unlock()
        if self.active > 0: self.active -= 1

var readers: Readers

func if_upload_leave(upload: bool):
    if upload: readers.leave()

func release_principal(who: interop.Reference[prism.Value]?):
    if let held = who: held.release()

func busy() -> interop.Outcome[interop.Reference[http.Response]]:
    return reply("too many concurrent fetches; retry shortly", 503)

let app_type = interop.Type[App]("registry", App.close)

func store_token() -> str!:
    let token = os.env((c.str)"LUCE_REGISTRY_STORE_TOKEN") else error(auth.invalid, "LUCE_REGISTRY_STORE_TOKEN is required")
    if token.length == 0: error(auth.invalid, "empty store token")
    return token

func configured_origin() -> str!:
    let origin = os.env((c.str)"LUCE_REGISTRY_ORIGIN") else ""
    if origin.length > 255: error(auth.invalid, "registry origin exceeds 255 bytes")
    for byte in origin.bytes:
        if byte < 33 or byte > 126: error(auth.invalid, "invalid registry origin")
    return origin

## LUCE_REGISTRY_REGISTRATION=closed refuses every invitation, even a valid one.
func registration_closed() -> bool:
    return (os.env((c.str)"LUCE_REGISTRY_REGISTRATION") else "") == "closed"

func configured_site() -> str!:
    let site = os.env((c.str)"LUCE_REGISTRY_SITE") else ""
    if site.length > 255: error(auth.invalid, "registry site path exceeds 255 bytes")
    if site.length > 0 and site.bytes[0] != '/': error(auth.invalid, "registry site path must be absolute")
    return site

func configure(socket: str) -> interop.Outcome[http.Application]:
    let database = db.Database.connect(socket, store_token() catch failure: trap(failure.message)) catch failure: trap(failure.message)
    let authority = auth.Authority.attach(database.value().store() catch failure: trap(failure.message)) catch failure:
        database.release()
        trap(failure.message)
    let native = new App(database = database, authority = authority) catch failure:
        authority.release()
        database.release()
        trap(failure.message)
    let origin = configured_origin() catch failure: trap(failure.message)
    for i in 0..<origin.length:
        native.origin[i] = origin.bytes[i]
    native.origin_size = origin.length
    let site = configured_site() catch failure: trap(failure.message)
    for i in 0..<site.length:
        native.site[i] = site.bytes[i]
    native.site_size = site.length
    if site.length > 0: site_files.ensure_front_page(site) catch failure: trap(failure.message)
    if site.length > 0: repositories.regenerate_site(native.database.value(), site) catch failure: trap(failure.message)
    let app = interop.Reference[App].adopt(native, app_type) catch failure: trap(failure.message)
    defer app.release()
    var router = http.Router() catch failure: trap(failure.message)
    defer router.close()
    let health = http.Handler.bind(app, App.health) catch failure: trap(failure.message)
    defer health.release()
    router.get("/health", health) catch failure: trap(failure.message)
    let identity = http.Handler.bind(app, App.identity) catch failure: trap(failure.message)
    defer identity.release()
    router.get("/v1/identity", identity) catch failure: trap(failure.message)
    let redeem = http.Handler.bind(app, App.redeem) catch failure: trap(failure.message)
    defer redeem.release()
    router.post("/v1/invites/redeem", redeem) catch failure: trap(failure.message)
    let login = http.Handler.bind(app, App.login) catch failure: trap(failure.message)
    defer login.release()
    router.post("/v1/sessions", login) catch failure: trap(failure.message)
    let logout = http.Handler.bind(app, App.logout) catch failure: trap(failure.message)
    defer logout.release()
    router.post("/v1/sessions/revoke", logout) catch failure: trap(failure.message)
    let issue_credential = http.Handler.bind(app, App.issue_credential) catch failure: trap(failure.message)
    defer issue_credential.release()
    router.post("/v1/credentials", issue_credential) catch failure: trap(failure.message)
    let revoke_credential = http.Handler.bind(app, App.revoke_credential) catch failure: trap(failure.message)
    defer revoke_credential.release()
    router.post("/v1/credentials/revoke", revoke_credential) catch failure: trap(failure.message)
    let create_repository = http.Handler.bind(app, App.create_repository) catch failure: trap(failure.message)
    defer create_repository.release()
    router.post("/v1/repositories", create_repository) catch failure: trap(failure.message)
    let git_http = http.Handler.bind(app, App.git_http) catch failure: trap(failure.message)
    defer git_http.release()
    router.get("/git/{owner}/{name}/info/refs", git_http) catch failure: trap(failure.message)
    router.post("/git/{owner}/{name}/git-receive-pack", git_http) catch failure: trap(failure.message)
    router.post("/git/{owner}/{name}/git-upload-pack", git_http) catch failure: trap(failure.message)
    let result = router.application() catch failure: trap(failure.message)
    return interop.Outcome[http.Application].success(interop.Owned[http.Application](result, result.owner))

func run(arguments: str[]) -> i32!:
    if arguments.length != 3: return 2
    _ = try configured_origin()
    var port: i64 = 0
    for byte in arguments[2].bytes:
        if byte < '0' or byte > '9': return 2
        port = port * 10 + (i64)(byte - '0')
        if port > 65535: return 2
    if arguments[2].length == 0: return 2
    let owner = try db.Database.open(arguments[1], try store_token())
    defer owner.release()
    let authority = try auth.Authority.attach(try owner.value().store())
    defer authority.release()
    try repositories.initialize(owner.value())
    try repositories.use_objects(arguments[1])
    try owner.value().listen()
    var socket_buffer: u8[4096]
    let socket = try strings.format(socket_buffer, f"{arguments[1]}.sock")
    var options = http.ServerConfig(address = "127.0.0.1", port = port, network_workers = 2, application_workers = 2,
                                    body_limit = (i64)max_release_upload, memory_body_limit = 65536,
                                    response_limit = (i64)repositories.max_object_bytes,
                                    request_timeout_ms = 300000, idle_timeout_ms = 15000,
                                    response_timeout_ms = 300000, application_timeout_ms = 300000,
                                    shutdown_timeout_ms = 30000)
    var server = try http.Server(options, interop.WorkerEntry[str, unit, unit].native(configure), socket)
    defer server.close()
    try server.shutdown_on_signals()
    try server.start()
    print(f"READY {try server.port()}")
    try server.run()
    return 0

pub func main(arguments: str[]) -> i32!:
    let result = try run(arguments)
    return ownership.finish_run(result)