╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
166 }; 167 168 // serve the existing on-disk repo at its event-id directory; HTTP only 169 // handles fetch/clone, so a missing repo is simply not found 170 if (!try serveIfExists(repo_kind, any_repo_opts, io, allocator, repo_path, GitService{ 171 .body_reader = &body_reader, 172 .writer = http_server.out, 173 .options = http_backend_options, 174 })) return error.RepoNotFound; 175 } 176 177 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 178 for (&xit.net_server_http_backend.routes) |*route| { 179 if (std.mem.endsWith(u8, path, route.suffix)) { 180 return .{ route.handler, route.suffix }; 181 } 182 } 183 return null; 184 } 185 186 // serve an existing repo at `repo_path`, or return false if none is there 187 fn serveIfExists( 188 comptime repo_kind: rp.RepoKind, 189 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 190 io: std.Io, 191 allocator: std.mem.Allocator, 192 repo_path: []const u8, 193 service: anytype, 194 ) !bool { 195 // a bare open() creates the directory while probing for the repo, so only 196 // attempt it when the path already exists 197 std.Io.Dir.accessAbsolute(io, repo_path, .{}) catch return false; 198 199 if (any_repo_opts.hash) |hash_kind| { 200 var repo = rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 201 error.RepoNotFound => return false, 202 else => |e| return e, 203 }; 204 defer repo.deinit(io, allocator); 205 try service.serve(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator); 206 } else { 207 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 208 error.RepoNotFound => return false, 209 else => |e| return e, 210 }; 211 defer any_repo.deinit(io, allocator); 212 switch (any_repo) { 213 inline else => |*repo| try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator), 214 } 215 } 216 return true; 217 } 218 219 const GitService = struct { 220 body_reader: *std.Io.Reader, 221 writer: *std.Io.Writer, 222 options: xit.net_server_http_backend.Options, 223 224 fn serve( 225 self: @This(), 226 comptime repo_kind: rp.RepoKind, 227 comptime repo_opts: rp.RepoOpts(repo_kind), 228 repo: *rp.Repo(repo_kind, repo_opts), 229 io: std.Io, 230 allocator: std.mem.Allocator, 231 ) !void { 232 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 233 } 234 }; 235 236 fn isReceivePack( 237 handler: xit.net_server_http_backend.HandlerKind, 238 suffix: []const u8, 239 query: ?std.Uri.Component, 240 ) bool { 241 return switch (handler) { 242 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 243 .get_info_refs => if (query) |q| 244 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 245 else 246 false, 247 }; 248 } 249 250 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 251 if (encoded.len == 0) return error.InvalidRepoPath; 252 253 const mutable = try allocator.dupe(u8, encoded); 254 errdefer allocator.free(mutable); 255 const decoded = std.Uri.percentDecodeInPlace(mutable); 256 257 var iter = std.Io.Dir.path.componentIterator(decoded); 258 while (iter.next()) |component| { 259 if (component.name.len == 0 or std.mem.eql(u8, component.name, ".") or std.mem.eql(u8, component.name, "..")) { 260 return error.InvalidRepoPath; 261 } 262 } 263 264 return try allocator.realloc(mutable, decoded.len); 265 } 266 267 fn normalizeMethod(method: std.http.Method) std.http.Method { 268 return if (method == .HEAD) .GET else method; 269 } 270 271 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 272 var it = request.iterateHeaders(); 273 while (it.next()) |header| { 274 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 275 } 276 return null; 277 } 278 279 fn writeSimpleResponse( 280 http_server: *std.http.Server, 281 code: u16, 282 message: []const u8, 283 content_type: []const u8, 284 body: []const u8, 285 ) !void { 286 try http_server.out.print( 287 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 288 .{ code, message, content_type, body.len, body }, 289 ); 290 } 291