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