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