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