╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
386 387 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOpts(), io, allocator, repo_path, true); 388 defer repo.deinit(io, allocator); 389 try service.serve(repo_kind, any_repo_opts.toRepoOpts(), &repo, io, allocator); 390 return; 391 }, 392 else => |e| return e, 393 }; 394 defer any_repo.deinit(io, allocator); 395 396 switch (any_repo) { 397 inline else => |*repo| { 398 try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator); 399 }, 400 } 401 } 402 } 403 404 fn openRepo( 405 comptime repo_kind: rp.RepoKind, 406 comptime repo_opts: rp.RepoOpts(repo_kind), 407 io: std.Io, 408 allocator: std.mem.Allocator, 409 repo_path: []const u8, 410 create_if_missing: bool, 411 ) !rp.Repo(repo_kind, repo_opts) { 412 return rp.Repo(repo_kind, repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 413 error.RepoNotFound => { 414 if (!create_if_missing) return open_err; 415 416 var repo = try rp.Repo(repo_kind, repo_opts).init(io, allocator, .{ .path = repo_path }); 417 errdefer repo.deinit(io, allocator); 418 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 419 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 420 return repo; 421 }, 422 else => |e| return e, 423 }; 424 } 425 426 const HttpGitService = struct { 427 body_reader: *std.Io.Reader, 428 writer: *std.Io.Writer, 429 options: xit.net_server_http_backend.Options, 430 431 fn serve( 432 self: @This(), 433 comptime repo_kind: rp.RepoKind, 434 comptime repo_opts: rp.RepoOpts(repo_kind), 435 repo: *rp.Repo(repo_kind, repo_opts), 436 io: std.Io, 437 allocator: std.mem.Allocator, 438 ) !void { 439 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 440 } 441 }; 442 443 fn isReceivePack( 444 handler: xit.net_server_http_backend.HandlerKind, 445 suffix: []const u8, 446 query: ?std.Uri.Component, 447 ) bool { 448 return switch (handler) { 449 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 450 .get_info_refs => if (query) |q| 451 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 452 else 453 false, 454 }; 455 } 456 457 fn parseListenAddress(value: []const u8) !ListenAddress { 458 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 459 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 460 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 461 return .{ .host = value[0..colon], .port = port }; 462 } 463 464 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 465 if (encoded.len == 0) return error.InvalidRepoPath; 466 467 const mutable = try allocator.dupe(u8, encoded); 468 errdefer allocator.free(mutable); 469 const decoded = std.Uri.percentDecodeInPlace(mutable); 470 471 var iter = std.fs.path.componentIterator(decoded); 472 while (iter.next()) |component| { 473 if (component.name.len == 0 or std.mem.eql(u8, component.name, ".") or std.mem.eql(u8, component.name, "..")) { 474 return error.InvalidRepoPath; 475 } 476 } 477 478 return try allocator.realloc(mutable, decoded.len); 479 } 480 481 fn isSubPath(parent: []const u8, child: []const u8) bool { 482 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 483 if (!std.mem.startsWith(u8, child, parent)) return false; 484 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 485 } 486 487 fn normalizeMethod(method: std.http.Method) std.http.Method { 488 return if (method == .HEAD) .GET else method; 489 } 490 491 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 492 const git_protocol = header orelse return .v0; 493 var version: xit.net_server_common.ProtocolVersion = .v0; 494 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 495 while (iter.next()) |entry| { 496 const value = std.mem.trimStart(u8, entry, " "); 497 if (std.mem.startsWith(u8, value, "version=")) { 498 const v = value["version=".len..]; 499 if (std.mem.eql(u8, v, "2")) { 500 version = .v2; 501 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 502 version = .v1; 503 } 504 } 505 } 506 return version; 507 } 508 509 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 510 var it = request.iterateHeaders(); 511 while (it.next()) |header| { 512 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 513 } 514 return null; 515 } 516 517 fn writeSimpleResponse( 518 http_server: *std.http.Server, 519 code: u16, 520 message: []const u8, 521 content_type: []const u8, 522 body: []const u8, 523 ) !void { 524 try http_server.out.print( 525 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 526 .{ code, message, content_type, body.len, body }, 527 ); 528 } 529 530 fn logError(err: *std.Io.Writer, comptime fmt: []const u8, args: anytype) void { 531 err.print(fmt, args) catch return; 532 err.flush() catch {}; 533 } 534