╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
353 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 354 return null; 355 } 356 357 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 358 if (!std.mem.startsWith(u8, value, prefix)) return null; 359 return value[prefix.len..]; 360 } 361 362 fn resolveSshRepoPath( 363 allocator: std.mem.Allocator, 364 project_root: []const u8, 365 repo: []const u8, 366 ) ![]const u8 { 367 if (std.fs.path.isAbsolute(repo)) { 368 return try std.fs.path.resolve(allocator, &.{repo}); 369 } 370 return try std.fs.path.resolve(allocator, &.{ project_root, repo }); 371 } 372 373 fn parseListenAddress(value: []const u8) !ListenAddress { 374 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 375 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 376 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 377 return .{ .host = value[0..colon], .port = port }; 378 } 379 380 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 381 if (encoded.len == 0) return error.InvalidRepoPath; 382 383 const mutable = try allocator.dupe(u8, encoded); 384 errdefer allocator.free(mutable); 385 const decoded = std.Uri.percentDecodeInPlace(mutable); 386 387 var iter = std.mem.splitScalar(u8, decoded, '/'); 388 while (iter.next()) |segment| { 389 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 390 return error.InvalidRepoPath; 391 } 392 } 393 394 return try allocator.realloc(mutable, decoded.len); 395 } 396 397 fn isSubPath(parent: []const u8, child: []const u8) bool { 398 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 399 if (!std.mem.startsWith(u8, child, parent)) return false; 400 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 401 } 402 403 fn normalizeMethod(method: std.http.Method) std.http.Method { 404 return if (method == .HEAD) .GET else method; 405 } 406 407 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 408 const git_protocol = header orelse return .v0; 409 var version: xit.net_server_common.ProtocolVersion = .v0; 410 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 411 while (iter.next()) |entry| { 412 const value = std.mem.trimStart(u8, entry, " "); 413 if (std.mem.startsWith(u8, value, "version=")) { 414 const v = value["version=".len..]; 415 if (std.mem.eql(u8, v, "2")) { 416 version = .v2; 417 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 418 version = .v1; 419 } 420 } 421 } 422 return version; 423 } 424 425 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 426 var it = request.iterateHeaders(); 427 while (it.next()) |header| { 428 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 429 } 430 return null; 431 } 432 433 fn writeSimpleResponse( 434 http_server: *std.http.Server, 435 code: u16, 436 message: []const u8, 437 content_type: []const u8, 438 body: []const u8, 439 ) !void { 440 try http_server.out.print( 441 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 442 .{ code, message, content_type, body.len, body }, 443 ); 444 } 445