╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
447 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 448 else 449 false, 450 }; 451 } 452 453 fn parseListenAddress(value: []const u8) !ListenAddress { 454 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 455 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 456 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 457 return .{ .host = value[0..colon], .port = port }; 458 } 459 460 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 461 if (encoded.len == 0) return error.InvalidRepoPath; 462 463 const mutable = try allocator.dupe(u8, encoded); 464 errdefer allocator.free(mutable); 465 const decoded = std.Uri.percentDecodeInPlace(mutable); 466 467 var iter = std.mem.splitScalar(u8, decoded, '/'); 468 while (iter.next()) |segment| { 469 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 470 return error.InvalidRepoPath; 471 } 472 } 473 474 return try allocator.realloc(mutable, decoded.len); 475 } 476 477 fn isSubPath(parent: []const u8, child: []const u8) bool { 478 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 479 if (!std.mem.startsWith(u8, child, parent)) return false; 480 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 481 } 482 483 fn normalizeMethod(method: std.http.Method) std.http.Method { 484 return if (method == .HEAD) .GET else method; 485 } 486 487 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 488 const git_protocol = header orelse return .v0; 489 var version: xit.net_server_common.ProtocolVersion = .v0; 490 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 491 while (iter.next()) |entry| { 492 const value = std.mem.trimStart(u8, entry, " "); 493 if (std.mem.startsWith(u8, value, "version=")) { 494 const v = value["version=".len..]; 495 if (std.mem.eql(u8, v, "2")) { 496 version = .v2; 497 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 498 version = .v1; 499 } 500 } 501 } 502 return version; 503 } 504 505 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 506 var it = request.iterateHeaders(); 507 while (it.next()) |header| { 508 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 509 } 510 return null; 511 } 512 513 fn writeSimpleResponse( 514 http_server: *std.http.Server, 515 code: u16, 516 message: []const u8, 517 content_type: []const u8, 518 body: []const u8, 519 ) !void { 520 try http_server.out.print( 521 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 522 .{ code, message, content_type, body.len, body }, 523 ); 524 } 525 526 fn logError(err: *std.Io.Writer, comptime fmt: []const u8, args: anytype) void { 527 err.print(fmt, args) catch return; 528 err.flush() catch {}; 529 } 530