╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
302 try runSshService(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator, &reader.interface, &writer.interface, request); 303 }, 304 } 305 } 306 } 307 308 fn runSshService( 309 comptime repo_kind: rp.RepoKind, 310 comptime repo_opts: rp.RepoOpts(repo_kind), 311 repo: *rp.Repo(repo_kind, repo_opts), 312 io: std.Io, 313 allocator: std.mem.Allocator, 314 reader: *std.Io.Reader, 315 writer: *std.Io.Writer, 316 request: SshRequest, 317 ) !void { 318 switch (request.service) { 319 .upload_pack => try repo.uploadPack(io, allocator, reader, writer, .{ 320 .protocol_version = request.protocol_version, 321 }), 322 .receive_pack => try repo.receivePack(io, allocator, reader, writer, .{ 323 .protocol_version = request.protocol_version, 324 }), 325 } 326 } 327 328 fn openRepo( 329 comptime repo_kind: rp.RepoKind, 330 comptime repo_opts: rp.RepoOpts(repo_kind), 331 io: std.Io, 332 allocator: std.mem.Allocator, 333 repo_path: []const u8, 334 create_if_missing: bool, 335 ) !rp.Repo(repo_kind, repo_opts) { 336 return rp.Repo(repo_kind, repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 337 error.RepoNotFound => { 338 if (!create_if_missing) return open_err; 339 340 var repo = try rp.Repo(repo_kind, repo_opts).init(io, allocator, .{ .path = repo_path }); 341 errdefer repo.deinit(io, allocator); 342 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 343 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 344 return repo; 345 }, 346 else => |e| return e, 347 }; 348 } 349 350 fn isReceivePack( 351 handler: xit.net_server_http_backend.HandlerKind, 352 suffix: []const u8, 353 query: ?std.Uri.Component, 354 ) bool { 355 return switch (handler) { 356 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 357 .get_info_refs => if (query) |q| 358 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 359 else 360 false, 361 }; 362 } 363 364 fn readSshRequest(allocator: std.mem.Allocator, reader: *std.Io.Reader) !SshRequest { 365 const magic = try readSshPreludeLine(reader); 366 if (!std.mem.eql(u8, magic, "haxy-ssh-helper-v1")) return error.InvalidSshMagic; 367 368 const service_line = try readSshPreludeLine(reader); 369 const service = parseSshService(stripPrefix(service_line, "service=") orelse return error.InvalidSshServiceLine) orelse return error.InvalidSshService; 370 371 const protocol_line = try readSshPreludeLine(reader); 372 const protocol_version = std.meta.stringToEnum( 373 xit.net_server_common.ProtocolVersion, 374 stripPrefix(protocol_line, "protocol=") orelse return error.InvalidSshProtocolLine, 375 ) orelse return error.InvalidSshProtocol; 376 377 const repo_length_line = try readSshPreludeLine(reader); 378 const repo_len = try std.fmt.parseInt(usize, stripPrefix(repo_length_line, "repo-length=") orelse return error.InvalidSshRepoLengthLine, 10); 379 380 const empty_line = try readSshPreludeLine(reader); 381 if (empty_line.len != 0) return error.InvalidSshPreludeTerminator; 382 383 const repo = try reader.readAlloc(allocator, repo_len); 384 errdefer allocator.free(repo); 385 386 return .{ 387 .service = service, 388 .protocol_version = protocol_version, 389 .repo = repo, 390 }; 391 } 392 393 fn readSshPreludeLine(reader: *std.Io.Reader) ![]const u8 { 394 return (try reader.takeDelimiter('\n')) orelse error.InvalidSshHelperRequest; 395 } 396 397 fn parseSshService(value: []const u8) ?SshService { 398 if (std.mem.eql(u8, value, "upload-pack")) return .upload_pack; 399 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 400 return null; 401 } 402 403 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 404 if (!std.mem.startsWith(u8, value, prefix)) return null; 405 return value[prefix.len..]; 406 } 407 408 fn resolveSshRepoPath( 409 allocator: std.mem.Allocator, 410 repo_root: []const u8, 411 repo: []const u8, 412 ) ![]const u8 { 413 if (std.fs.path.isAbsolute(repo)) { 414 return try std.fs.path.resolve(allocator, &.{repo}); 415 } 416 return try std.fs.path.resolve(allocator, &.{ repo_root, repo }); 417 } 418 419 fn parseListenAddress(value: []const u8) !ListenAddress { 420 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 421 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 422 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 423 return .{ .host = value[0..colon], .port = port }; 424 } 425 426 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 427 if (encoded.len == 0) return error.InvalidRepoPath; 428 429 const mutable = try allocator.dupe(u8, encoded); 430 errdefer allocator.free(mutable); 431 const decoded = std.Uri.percentDecodeInPlace(mutable); 432 433 var iter = std.mem.splitScalar(u8, decoded, '/'); 434 while (iter.next()) |segment| { 435 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 436 return error.InvalidRepoPath; 437 } 438 } 439 440 return try allocator.realloc(mutable, decoded.len); 441 } 442 443 fn isSubPath(parent: []const u8, child: []const u8) bool { 444 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 445 if (!std.mem.startsWith(u8, child, parent)) return false; 446 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 447 } 448 449 fn normalizeMethod(method: std.http.Method) std.http.Method { 450 return if (method == .HEAD) .GET else method; 451 } 452 453 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 454 const git_protocol = header orelse return .v0; 455 var version: xit.net_server_common.ProtocolVersion = .v0; 456 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 457 while (iter.next()) |entry| { 458 const value = std.mem.trimStart(u8, entry, " "); 459 if (std.mem.startsWith(u8, value, "version=")) { 460 const v = value["version=".len..]; 461 if (std.mem.eql(u8, v, "2")) { 462 version = .v2; 463 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 464 version = .v1; 465 } 466 } 467 } 468 return version; 469 } 470 471 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 472 var it = request.iterateHeaders(); 473 while (it.next()) |header| { 474 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 475 } 476 return null; 477 } 478 479 fn writeSimpleResponse( 480 http_server: *std.http.Server, 481 code: u16, 482 message: []const u8, 483 content_type: []const u8, 484 body: []const u8, 485 ) !void { 486 try http_server.out.print( 487 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 488 .{ code, message, content_type, body.len, body }, 489 ); 490 } 491