╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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