╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
409 else => |e| return e, 410 }; 411 defer any_repo.deinit(io, allocator); 412 413 switch (any_repo) { 414 inline else => |*repo| { 415 try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator); 416 }, 417 } 418 } 419 } 420 421 fn openRepo( 422 comptime repo_kind: rp.RepoKind, 423 comptime repo_opts: rp.RepoOpts(repo_kind), 424 io: std.Io, 425 allocator: std.mem.Allocator, 426 repo_path: []const u8, 427 create_if_missing: bool, 428 ) !rp.Repo(repo_kind, repo_opts) { 429 return rp.Repo(repo_kind, repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 430 error.RepoNotFound => { 431 if (!create_if_missing) return open_err; 432 433 var repo = try rp.Repo(repo_kind, repo_opts).init(io, allocator, .{ .path = repo_path }); 434 errdefer repo.deinit(io, allocator); 435 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 436 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 437 return repo; 438 }, 439 else => |e| return e, 440 }; 441 } 442 443 const HttpGitService = struct { 444 body_reader: *std.Io.Reader, 445 writer: *std.Io.Writer, 446 options: xit.net_server_http_backend.Options, 447 448 fn serve( 449 self: @This(), 450 comptime repo_kind: rp.RepoKind, 451 comptime repo_opts: rp.RepoOpts(repo_kind), 452 repo: *rp.Repo(repo_kind, repo_opts), 453 io: std.Io, 454 allocator: std.mem.Allocator, 455 ) !void { 456 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 457 } 458 }; 459 460 const SshGitService = struct { 461 reader: *std.Io.Reader, 462 writer: *std.Io.Writer, 463 request: SshRequest, 464 465 fn serve( 466 self: @This(), 467 comptime repo_kind: rp.RepoKind, 468 comptime repo_opts: rp.RepoOpts(repo_kind), 469 repo: *rp.Repo(repo_kind, repo_opts), 470 io: std.Io, 471 allocator: std.mem.Allocator, 472 ) !void { 473 switch (self.request.service) { 474 .upload_pack => try repo.uploadPack(io, allocator, self.reader, self.writer, .{ 475 .protocol_version = self.request.protocol_version, 476 }), 477 .receive_pack => try repo.receivePack(io, allocator, self.reader, self.writer, .{ 478 .protocol_version = self.request.protocol_version, 479 }), 480 } 481 } 482 }; 483 484 fn isReceivePack( 485 handler: xit.net_server_http_backend.HandlerKind, 486 suffix: []const u8, 487 query: ?std.Uri.Component, 488 ) bool { 489 return switch (handler) { 490 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 491 .get_info_refs => if (query) |q| 492 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 493 else 494 false, 495 }; 496 } 497 498 fn readSshRequest(allocator: std.mem.Allocator, reader: *std.Io.Reader) !SshRequest { 499 const magic = try readSshPreludeLine(reader); 500 if (!std.mem.eql(u8, magic, "haxy-ssh-git-v1")) return error.InvalidSshMagic; 501 502 const service_line = try readSshPreludeLine(reader); 503 const service = parseSshService(stripPrefix(service_line, "service=") orelse return error.InvalidSshServiceLine) orelse return error.InvalidSshService; 504 505 const protocol_line = try readSshPreludeLine(reader); 506 const protocol_version = std.meta.stringToEnum( 507 xit.net_server_common.ProtocolVersion, 508 stripPrefix(protocol_line, "protocol=") orelse return error.InvalidSshProtocolLine, 509 ) orelse return error.InvalidSshProtocol; 510 511 const repo_length_line = try readSshPreludeLine(reader); 512 const repo_len = try std.fmt.parseInt(usize, stripPrefix(repo_length_line, "repo-length=") orelse return error.InvalidSshRepoLengthLine, 10); 513 514 const empty_line = try readSshPreludeLine(reader); 515 if (empty_line.len != 0) return error.InvalidSshPreludeTerminator; 516 517 const repo = try reader.readAlloc(allocator, repo_len); 518 errdefer allocator.free(repo); 519 520 return .{ 521 .service = service, 522 .protocol_version = protocol_version, 523 .repo = repo, 524 }; 525 } 526 527 fn readSshPreludeLine(reader: *std.Io.Reader) ![]const u8 { 528 return (try reader.takeDelimiter('\n')) orelse error.InvalidSshHelperRequest; 529 } 530 531 fn parseSshService(value: []const u8) ?SshService { 532 if (std.mem.eql(u8, value, "upload-pack")) return .upload_pack; 533 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 534 return null; 535 } 536 537 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 538 if (!std.mem.startsWith(u8, value, prefix)) return null; 539 return value[prefix.len..]; 540 } 541 542 fn resolveSshRepoPath( 543 allocator: std.mem.Allocator, 544 repo_root_path: []const u8, 545 repo: []const u8, 546 ) ![]const u8 { 547 if (std.fs.path.isAbsolute(repo)) { 548 return try std.fs.path.resolve(allocator, &.{repo}); 549 } 550 return try std.fs.path.resolve(allocator, &.{ repo_root_path, repo }); 551 } 552 553 fn parseListenAddress(value: []const u8) !ListenAddress { 554 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 555 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 556 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 557 return .{ .host = value[0..colon], .port = port }; 558 } 559 560 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 561 if (encoded.len == 0) return error.InvalidRepoPath; 562 563 const mutable = try allocator.dupe(u8, encoded); 564 errdefer allocator.free(mutable); 565 const decoded = std.Uri.percentDecodeInPlace(mutable); 566 567 var iter = std.mem.splitScalar(u8, decoded, '/'); 568 while (iter.next()) |segment| { 569 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 570 return error.InvalidRepoPath; 571 } 572 } 573 574 return try allocator.realloc(mutable, decoded.len); 575 } 576 577 fn isSubPath(parent: []const u8, child: []const u8) bool { 578 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 579 if (!std.mem.startsWith(u8, child, parent)) return false; 580 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 581 } 582 583 fn normalizeMethod(method: std.http.Method) std.http.Method { 584 return if (method == .HEAD) .GET else method; 585 } 586 587 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 588 const git_protocol = header orelse return .v0; 589 var version: xit.net_server_common.ProtocolVersion = .v0; 590 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 591 while (iter.next()) |entry| { 592 const value = std.mem.trimStart(u8, entry, " "); 593 if (std.mem.startsWith(u8, value, "version=")) { 594 const v = value["version=".len..]; 595 if (std.mem.eql(u8, v, "2")) { 596 version = .v2; 597 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 598 version = .v1; 599 } 600 } 601 } 602 return version; 603 } 604 605 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 606 var it = request.iterateHeaders(); 607 while (it.next()) |header| { 608 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 609 } 610 return null; 611 } 612 613 fn writeSimpleResponse( 614 http_server: *std.http.Server, 615 code: u16, 616 message: []const u8, 617 content_type: []const u8, 618 body: []const u8, 619 ) !void { 620 try http_server.out.print( 621 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 622 .{ code, message, content_type, body.len, body }, 623 ); 624 } 625 626 fn logError(err: *std.Io.Writer, comptime fmt: []const u8, args: anytype) void { 627 err.print(fmt, args) catch return; 628 err.flush() catch {}; 629 } 630