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