╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
248 fn normalizeMethod(method: std.http.Method) std.http.Method { 249 return if (method == .HEAD) .GET else method; 250 } 251 252 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 253 const git_protocol = header orelse return .v0; 254 var version: xit.net_server_common.ProtocolVersion = .v0; 255 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 256 while (iter.next()) |entry| { 257 const value = std.mem.trimStart(u8, entry, " "); 258 if (std.mem.startsWith(u8, value, "version=")) { 259 const v = value["version=".len..]; 260 if (std.mem.eql(u8, v, "2")) { 261 version = .v2; 262 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 263 version = .v1; 264 } 265 } 266 } 267 return version; 268 } 269 270 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 271 var it = request.iterateHeaders(); 272 while (it.next()) |header| { 273 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 274 } 275 return null; 276 } 277 278 fn writeSimpleResponse( 279 http_server: *std.http.Server, 280 code: u16, 281 message: []const u8, 282 content_type: []const u8, 283 body: []const u8, 284 ) !void { 285 try http_server.out.print( 286 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 287 .{ code, message, content_type, body.len, body }, 288 ); 289 } 290