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