Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166(* Copyright (c) 2024-2026 Thomas Gazagnaire <thomas@gazagnaire.org>
Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. *)
open! Hermeticmodule Reader = Bytesrw.Bytes.Reader
let src = Logs.Src.create "git.server" ~doc:"Git smart-HTTP server"
module L = (val Logs.src_log src : Logs.LOG)
(* git's smart-HTTP content types ([service] already includes the [git-] prefix, e.g. "git-upload-pack"). *)let adv_ct service = Fmt.str "application/x-%s-advertisement" servicelet result_ct service = Fmt.str "application/x-%s-result" service
(* Smart-HTTP responses must not be cached, or a client may replay a stale advertisement. *)let no_cache = [ ("Cache-Control", "no-cache, max-age=0, must-revalidate") ]
let result ~content_type body = Respond.Response.v ~headers:no_cache ~status:200 ~content_type body
(* A Git client prints a text/plain refusal body to its user, so a refusal is text, not the server's problem document. *)let bad_request m = Respond.Response.v ~status:400 ~content_type:"text/plain" m
type request_probe = { operation : string; protocol : string; bytes : int }
let request_span = Probe.span "git.server.request.span" ~doc:"Git smart-HTTP request lifetime" Probe.Fields.( obj (fun operation protocol bytes -> { operation; protocol; bytes }) |> field "operation" string ~enc:(fun t -> t.operation) |> field "protocol" string ~enc:(fun t -> t.protocol) |> field "bytes" int ~enc:(fun t -> t.bytes) |> seal)
let with_request ~operation ~protocol ~bytes f = Probe.with_span request_span { operation; protocol; bytes } f
(* A stock git client compresses the upload-pack / receive-pack request body with [Content-Encoding: gzip] (git's [gzip_request_buffer], on by default for smart HTTP). Decompress it before parsing pkt-lines; an unencoded body passes through untouched. *)let request_body limits (req : Respond.post_request) = Git.Serve.Limits.read limits (fun body -> Ok (Reader.to_string body)) @@ match Http.Headers.string "content-encoding" req.headers with | Some enc when String.lowercase_ascii (String.trim enc) = "gzip" -> Zlib.Gzip.decompress_reads () (Reader.of_string req.body) | _ -> Reader.of_string req.body
(* Recomputing a clone's advertisement and packfile on every request dominates server allocation (the pack is one independent zlib stream per object). Cache the whole {!Respond.Response.t} for both info/refs and upload-pack. Caching the response value (not just its bytes) also lets Respond render its head+body wire once, so every later clone is a single writev of the cached Cstruct with no per-request copy.
The server is the only writer of refs (all mutations go through receive-pack), so the caches are cleared exactly there rather than by re-reading the refs on every request -- a per-request ref scan would re-open the ref files through Eio.Path each time, swamping the cache-hit path. The server is single-domain, so no lock is needed. *)let memoize cache key build = match Hashtbl.find_opt cache key with | Some resp -> resp | None -> let resp = build () in Hashtbl.replace cache key resp; resp
(* Modern git requests protocol v2 with a [Git-Protocol: version=2] header on info/refs, and sends v2 command requests (bodies starting [command=]) to git-upload-pack. We serve v2 when asked, v0 otherwise. *)let wants_v2 headers = match Http.Headers.string "git-protocol" headers with | Some v -> List.exists (fun s -> String.trim s = "version=2") (String.split_on_char ':' v) | None -> false
(* A v2 command request begins with a [command=...] pkt-line, whose payload after the 4-hex length starts "command="; a v0 request begins with "want ". *)let is_v2_request body = String.length body >= 12 && String.sub body 4 8 = "command="
let info_refs repo adv_cache (req : Respond.get_request) = match List.assoc_opt "service" req.params with | Some (("git-upload-pack" | "git-receive-pack") as service) -> with_request ~operation:"info_refs" ~protocol:service ~bytes:0 @@ fun () -> if service = "git-upload-pack" && wants_v2 req.headers then memoize adv_cache ("v2:" ^ service) (fun () -> result ~content_type:(adv_ct service) (Git_eio.Serve.v2_advertise ~service repo)) else memoize adv_cache service (fun () -> match Git_eio.Serve.advertise ~service repo with | Ok advertisement -> result ~content_type:(adv_ct service) advertisement | Error (`Msg m) -> L.warn (fun m' -> m' "advertise %s: %s" service m); Respond.Response.internal_server_error) | Some other -> Fmt.kstr bad_request "unknown service %s" other | None -> bad_request "missing service"
let upload_pack limits repo pack_cache (req : Respond.post_request) = match request_body limits req with | Error (`Msg m) -> bad_request m | Ok body -> with_request ~operation:"upload_pack" ~protocol:"git-upload-pack" ~bytes:(String.length body) @@ fun () -> let serve r = if is_v2_request body then Git_eio.Serve.v2_serve repo r else Git_eio.Serve.upload_pack repo r in memoize pack_cache body (fun () -> match serve (Reader.of_string body) with | Ok body -> result ~content_type:(result_ct "git-upload-pack") body | Error (`Msg m) -> L.warn (fun m' -> m' "upload-pack: %s" m); Respond.Response.internal_server_error)
let receive_pack ?context limits repo adv_cache pack_cache (req : Respond.post_request) = match request_body limits req with | Error (`Msg m) -> bad_request m | Ok body -> ( with_request ~operation:"receive_pack" ~protocol:"git-receive-pack" ~bytes:(String.length body) @@ fun () -> match Git_eio.Serve.receive_pack ?context ~limits repo (Reader.of_string body) with | Ok body -> (* Refs may have moved; drop the cached advertisement and packs. *) Hashtbl.clear adv_cache; Hashtbl.clear pack_cache; result ~content_type:(result_ct "git-receive-pack") body | Error (`Msg m) -> L.warn (fun m' -> m' "receive-pack: %s" m); Respond.Response.internal_server_error)
let routes ?context ?(limits = Git.Serve.Limits.default) repo = let adv_cache : (string, Respond.Response.t) Hashtbl.t = Hashtbl.create 4 in let pack_cache : (string, Respond.Response.t) Hashtbl.t = Hashtbl.create 16 in [ Respond.get "/info/refs" (info_refs repo adv_cache); Respond.post "/git-upload-pack" (upload_pack limits repo pack_cache); Respond.post "/git-receive-pack" (receive_pack ?context limits repo adv_cache pack_cache); ]
let run ?context ?(limits = Git.Serve.Limits.default) ?stop ?on_listen ~max_connections ~mono ~net ~addr repo = Respond_eio.run ?stop ?on_listen ~max_connections ~mono ~net ~addr ~max_body_size:limits.request_bytes (routes ?context ~limits repo)