╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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