╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
474 options: xit.net_server_http_backend.Options, 475 476 fn serve( 477 self: @This(), 478 comptime repo_kind: rp.RepoKind, 479 comptime repo_opts: rp.RepoOpts(repo_kind), 480 repo: *rp.Repo(repo_kind, repo_opts), 481 io: std.Io, 482 allocator: std.mem.Allocator, 483 ) !void { 484 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 485 } 486 }; 487 488 const SshGitService = struct { 489 reader: *std.Io.Reader, 490 writer: *std.Io.Writer, 491 request: SshRequest, 492 493 fn serve( 494 self: @This(), 495 comptime repo_kind: rp.RepoKind, 496 comptime repo_opts: rp.RepoOpts(repo_kind), 497 repo: *rp.Repo(repo_kind, repo_opts), 498 io: std.Io, 499 allocator: std.mem.Allocator, 500 ) !void { 501 switch (self.request.service) { 502 .upload_pack => try repo.uploadPack(io, allocator, self.reader, self.writer, .{ 503 .protocol_version = self.request.protocol_version, 504 }), 505 .receive_pack => try repo.receivePack(io, allocator, self.reader, self.writer, .{ 506 .protocol_version = self.request.protocol_version, 507 }), 508 } 509 } 510 }; 511 512 fn isReceivePack( 513 handler: xit.net_server_http_backend.HandlerKind, 514 suffix: []const u8, 515 query: ?std.Uri.Component, 516 ) bool { 517 return switch (handler) { 518 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 519 .get_info_refs => if (query) |q| 520 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 521 else 522 false, 523 }; 524 } 525 526 fn readSshRequest(allocator: std.mem.Allocator, reader: *std.Io.Reader) !SshRequest { 527 const magic = try readSshPreludeLine(reader); 528 if (!std.mem.eql(u8, magic, "haxy-ssh-helper-v1")) return error.InvalidSshMagic; 529 530 const service_line = try readSshPreludeLine(reader); 531 const service = parseSshService(stripPrefix(service_line, "service=") orelse return error.InvalidSshServiceLine) orelse return error.InvalidSshService; 532 533 const protocol_line = try readSshPreludeLine(reader); 534 const protocol_version = std.meta.stringToEnum( 535 xit.net_server_common.ProtocolVersion, 536 stripPrefix(protocol_line, "protocol=") orelse return error.InvalidSshProtocolLine, 537 ) orelse return error.InvalidSshProtocol; 538 539 const repo_length_line = try readSshPreludeLine(reader); 540 const repo_len = try std.fmt.parseInt(usize, stripPrefix(repo_length_line, "repo-length=") orelse return error.InvalidSshRepoLengthLine, 10); 541 542 const empty_line = try readSshPreludeLine(reader); 543 if (empty_line.len != 0) return error.InvalidSshPreludeTerminator; 544 545 const repo = try reader.readAlloc(allocator, repo_len); 546 errdefer allocator.free(repo); 547 548 return .{ 549 .service = service, 550 .protocol_version = protocol_version, 551 .repo = repo, 552 }; 553 } 554 555 fn readSshPreludeLine(reader: *std.Io.Reader) ![]const u8 { 556 return (try reader.takeDelimiter('\n')) orelse error.InvalidSshHelperRequest; 557 } 558 559 fn parseSshService(value: []const u8) ?SshService { 560 if (std.mem.eql(u8, value, "upload-pack")) return .upload_pack; 561 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 562 return null; 563 } 564 565 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 566 if (!std.mem.startsWith(u8, value, prefix)) return null; 567 return value[prefix.len..]; 568 } 569 570 fn resolveSshRepoPath( 571 allocator: std.mem.Allocator, 572 repo_root_path: []const u8, 573 repo: []const u8, 574 ) ![]const u8 { 575 if (std.fs.path.isAbsolute(repo)) { 576 return try std.fs.path.resolve(allocator, &.{repo}); 577 } 578 return try std.fs.path.resolve(allocator, &.{ repo_root_path, repo }); 579 } 580 581 fn parseListenAddress(value: []const u8) !ListenAddress { 582 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 583 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 584 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 585 return .{ .host = value[0..colon], .port = port }; 586 } 587 588 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 589 if (encoded.len == 0) return error.InvalidRepoPath; 590 591 const mutable = try allocator.dupe(u8, encoded); 592 errdefer allocator.free(mutable); 593 const decoded = std.Uri.percentDecodeInPlace(mutable); 594 595 var iter = std.mem.splitScalar(u8, decoded, '/'); 596 while (iter.next()) |segment| { 597 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 598 return error.InvalidRepoPath; 599 } 600 } 601 602 return try allocator.realloc(mutable, decoded.len); 603 } 604 605 fn isSubPath(parent: []const u8, child: []const u8) bool { 606 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 607 if (!std.mem.startsWith(u8, child, parent)) return false; 608 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 609 } 610 611 fn normalizeMethod(method: std.http.Method) std.http.Method { 612 return if (method == .HEAD) .GET else method; 613 } 614 615 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 616 const git_protocol = header orelse return .v0; 617 var version: xit.net_server_common.ProtocolVersion = .v0; 618 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 619 while (iter.next()) |entry| { 620 const value = std.mem.trimStart(u8, entry, " "); 621 if (std.mem.startsWith(u8, value, "version=")) { 622 const v = value["version=".len..]; 623 if (std.mem.eql(u8, v, "2")) { 624 version = .v2; 625 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 626 version = .v1; 627 } 628 } 629 } 630 return version; 631 } 632 633 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 634 var it = request.iterateHeaders(); 635 while (it.next()) |header| { 636 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 637 } 638 return null; 639 } 640 641 fn writeSimpleResponse( 642 http_server: *std.http.Server, 643 code: u16, 644 message: []const u8, 645 content_type: []const u8, 646 body: []const u8, 647 ) !void { 648 try http_server.out.print( 649 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 650 .{ code, message, content_type, body.len, body }, 651 ); 652 } 653 654 fn logError(err: *std.Io.Writer, comptime fmt: []const u8, args: anytype) void { 655 err.print(fmt, args) catch return; 656 err.flush() catch {}; 657 } 658