╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
240 else 241 false, 242 }; 243 } 244 245 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 246 if (encoded.len == 0) return error.InvalidRepoPath; 247 248 const mutable = try allocator.dupe(u8, encoded); 249 errdefer allocator.free(mutable); 250 const decoded = std.Uri.percentDecodeInPlace(mutable); 251 252 var iter = std.fs.path.componentIterator(decoded); 253 while (iter.next()) |component| { 254 if (component.name.len == 0 or std.mem.eql(u8, component.name, ".") or std.mem.eql(u8, component.name, "..")) { 255 return error.InvalidRepoPath; 256 } 257 } 258 259 return try allocator.realloc(mutable, decoded.len); 260 } 261 262 fn normalizeMethod(method: std.http.Method) std.http.Method { 263 return if (method == .HEAD) .GET else method; 264 } 265 266 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 267 const git_protocol = header orelse return .v0; 268 var version: xit.net_server_common.ProtocolVersion = .v0; 269 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 270 while (iter.next()) |entry| { 271 const value = std.mem.trimStart(u8, entry, " "); 272 if (std.mem.startsWith(u8, value, "version=")) { 273 const v = value["version=".len..]; 274 if (std.mem.eql(u8, v, "2")) { 275 version = .v2; 276 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 277 version = .v1; 278 } 279 } 280 } 281 return version; 282 } 283 284 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 285 var it = request.iterateHeaders(); 286 while (it.next()) |header| { 287 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 288 } 289 return null; 290 } 291 292 fn writeSimpleResponse( 293 http_server: *std.http.Server, 294 code: u16, 295 message: []const u8, 296 content_type: []const u8, 297 body: []const u8, 298 ) !void { 299 try http_server.out.print( 300 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 301 .{ code, message, content_type, body.len, body }, 302 ); 303 } 304