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