╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
45 // create http listener 46 47 const http_listen_address = try parseListenAddress(options.http_listen); 48 const http_address = try std.Io.net.IpAddress.parseIp4(http_listen_address.host, http_listen_address.port); 49 var http_server = try http_address.listen(io, .{ .reuse_address = true }); 50 defer http_server.deinit(io); 51 52 // create ssh listener 53 54 const ssh_listen_address = try parseListenAddress(options.ssh_listen); 55 const ssh_address = try std.Io.net.IpAddress.parseIp4(ssh_listen_address.host, ssh_listen_address.port); 56 var ssh_server = try ssh_address.listen(io, .{ .reuse_address = true }); 57 defer ssh_server.deinit(io); 58 59 // create wui listener 60 61 const wui_listen_address = try parseListenAddress(options.wui_listen); 62 const wui_address = try std.Io.net.IpAddress.parseIp4(wui_listen_address.host, wui_listen_address.port); 63 var wui_server = try wui_address.listen(io, .{ .reuse_address = true }); 64 defer wui_server.deinit(io); 65 66 // start task group 67 68 var tasks: std.Io.Group = .init; 69 defer tasks.cancel(io); 70 71 // run listeners 72 73 try err.print("serving HTTP on {s}, repo root {s}\n", .{ options.http_listen, repo_root_path }); 74 try err.flush(); 75 76 runHttpListener(repo_kind, any_repo_opts, io, allocator, repo_root_path, &http_server, &tasks, err); 77 78 try err.print("serving SSH helper connections on {s}, repo root {s}\n", .{ options.ssh_listen, repo_root_path }); 79 try err.flush(); 80 81 runSshListener(repo_kind, any_repo_opts, io, allocator, repo_root_path, &ssh_server, &tasks, err); 82 83 try err.print("serving web UI on http://{s}/\n", .{options.wui_listen}); 84 try err.flush(); 85 86 web.run(io, &wui_server, &tasks, err); 87 88 if (options.tui) { 89 runTui(io, allocator, &tasks, err); 90 91 while (!xit.xitui.terminal.quit) { 92 try std.Io.sleep(io, .fromMilliseconds(5), .real); 93 } 94 } else { 95 try tasks.await(io); 96 } 97 } 98 99 fn runTui( 100 io: std.Io, 101 allocator: std.mem.Allocator, 102 tasks: *std.Io.Group, 103 err: *std.Io.Writer, 104 ) void { 105 const TuiTask = struct { 106 io: std.Io, 107 allocator: std.mem.Allocator, 108 err: *std.Io.Writer, 109 110 fn run(ctx: @This()) void { 111 ui.run(ctx.io, ctx.allocator) catch |ui_err| { 112 if (ui_err == error.Canceled) return; 113 logError(ctx.err, "ui failed: {s}\n", .{@errorName(ui_err)}); 114 }; 115 } 116 }; 117 118 tasks.async(io, TuiTask.run, .{TuiTask{ 119 .io = io, 120 .allocator = allocator, 121 .err = err, 122 }}); 123 } 124 125 fn runHttpListener( 126 comptime repo_kind: rp.RepoKind, 127 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 128 io: std.Io, 129 allocator: std.mem.Allocator, 130 repo_root_path: []const u8, 131 net_server: *std.Io.net.Server, 132 tasks: *std.Io.Group, 133 err: *std.Io.Writer, 134 ) void { 135 const Listener = struct { 136 io: std.Io, 137 allocator: std.mem.Allocator, 138 repo_root_path: []const u8, 139 net_server: *std.Io.net.Server, 140 tasks: *std.Io.Group, 141 err: *std.Io.Writer, 142 143 fn run(ctx: @This()) void { 144 while (true) { 145 const stream = ctx.net_server.accept(ctx.io) catch |accept_err| { 146 if (accept_err == error.Canceled) return; 147 logError(ctx.err, "accept failed: {s}\n", .{@errorName(accept_err)}); 148 continue; 149 }; 150 151 const Connection = struct { 152 io: std.Io, 153 allocator: std.mem.Allocator, 154 repo_root_path: []const u8, 155 stream: std.Io.net.Stream, 156 err: *std.Io.Writer, 157 158 fn run(conn: @This()) void { 159 defer conn.stream.close(conn.io); 160 handleHttpConnection(repo_kind, any_repo_opts, conn.io, conn.allocator, conn.repo_root_path, conn.stream, conn.err) catch |request_err| { 161 logError(conn.err, "connection failed: {s}\n", .{@errorName(request_err)}); 162 }; 163 } 164 }; 165 166 ctx.tasks.async(ctx.io, Connection.run, .{Connection{ 167 .io = ctx.io, 168 .allocator = ctx.allocator, 169 .repo_root_path = ctx.repo_root_path, 170 .stream = stream, 171 .err = ctx.err, 172 }}); 173 } 174 } 175 }; 176 177 tasks.async(io, Listener.run, .{Listener{ 178 .io = io, 179 .allocator = allocator, 180 .repo_root_path = repo_root_path, 181 .net_server = net_server, 182 .tasks = tasks, 183 .err = err, 184 }}); 185 } 186 187 fn handleHttpConnection( 188 comptime repo_kind: rp.RepoKind, 189 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 190 io: std.Io, 191 allocator: std.mem.Allocator, 192 repo_root_path: []const u8, 193 stream: std.Io.net.Stream, 194 err: *std.Io.Writer, 195 ) !void { 196 var send_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 197 var recv_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 198 var conn_br = stream.reader(io, &recv_buffer); 199 var conn_bw = stream.writer(io, &send_buffer); 200 var http_server = std.http.Server.init(&conn_br.interface, &conn_bw.interface); 201 202 while (http_server.reader.state == .ready) { 203 var request = http_server.receiveHead() catch |receive_err| switch (receive_err) { 204 error.HttpConnectionClosing => break, 205 error.ReadFailed => break, 206 else => |e| return e, 207 }; 208 209 handleHttpGitRequest(repo_kind, any_repo_opts, io, allocator, repo_root_path, &http_server, &request) catch |request_err| { 210 try err.print("request failed: {s}\n", .{@errorName(request_err)}); 211 try err.flush(); 212 if (http_server.reader.state == .received_head) { 213 http_server.reader.state = .ready; 214 } 215 try writeSimpleResponse(&http_server, 500, "Internal Server Error", "text/plain", @errorName(request_err)); 216 }; 217 try http_server.out.flush(); 218 break; 219 } 220 } 221 222 fn handleHttpGitRequest( 223 comptime repo_kind: rp.RepoKind, 224 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 225 io: std.Io, 226 allocator: std.mem.Allocator, 227 repo_root_path: []const u8, 228 http_server: *std.http.Server, 229 request: *std.http.Server.Request, 230 ) !void { 231 const uri = try std.Uri.parseAfterScheme("", request.head.target); 232 const path = uri.path.percent_encoded; 233 if (path.len == 0 or path[0] != '/') { 234 try writeSimpleResponse(http_server, 400, "Bad Request", "text/plain", "bad path"); 235 return; 236 } 237 238 const handler, const suffix = findRoute(path) orelse { 239 if (http_server.reader.state == .received_head) { 240 http_server.reader.state = .ready; 241 } 242 try writeSimpleResponse(http_server, 404, "Not Found", "text/plain", "not found"); 243 return; 244 }; 245 246 const repo_rel_encoded = path[1 .. path.len - suffix.len]; 247 const repo_rel = try decodeAndValidateRepoPath(allocator, repo_rel_encoded); 248 defer allocator.free(repo_rel); 249 250 const repo_path = try std.fs.path.resolve(allocator, &.{ repo_root_path, repo_rel }); 251 defer allocator.free(repo_path); 252 253 if (!isSubPath(repo_root_path, repo_path)) { 254 if (http_server.reader.state == .received_head) { 255 http_server.reader.state = .ready; 256 } 257 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "forbidden"); 258 return; 259 } 260 261 const request_method = normalizeMethod(request.head.method); 262 const content_type = try allocator.dupe(u8, findHeader(request, "content-type") orelse ""); 263 defer allocator.free(content_type); 264 const has_remote_user = findHeader(request, "authorization") != null; 265 const protocol_version = protocolVersionFromHeader(findHeader(request, "git-protocol")); 266 267 const body = if (request.head.method == .POST) blk: { 268 const reader = try request.readerExpectContinue(&.{}); 269 break :blk try reader.allocRemaining(allocator, .unlimited); 270 } else try allocator.dupe(u8, ""); 271 defer allocator.free(body); 272 273 if (http_server.reader.state == .received_head) { 274 http_server.reader.state = .ready; 275 } 276 277 var body_reader = std.Io.Reader.fixed(body); 278 const create_if_missing = isReceivePack(handler, suffix, uri.query); 279 const http_backend_options = xit.net_server_http_backend.Options{ 280 .request_method = request_method, 281 .handler = handler, 282 .suffix = suffix, 283 .query_string = if (uri.query) |q| q.percent_encoded else "", 284 .content_type = content_type, 285 .has_remote_user = has_remote_user, 286 .protocol_version = protocol_version, 287 }; 288 289 try openRepoAndServe(repo_kind, any_repo_opts, io, allocator, repo_path, create_if_missing, HttpGitService{ 290 .body_reader = &body_reader, 291 .writer = http_server.out, 292 .options = http_backend_options, 293 }); 294 } 295 296 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 297 for (&xit.net_server_http_backend.routes) |*route| { 298 if (std.mem.endsWith(u8, path, route.suffix)) { 299 return .{ route.handler, route.suffix }; 300 } 301 } 302 return null; 303 } 304 305 const SshService = enum { 306 upload_pack, 307 receive_pack, 308 }; 309 310 const SshRequest = struct { 311 service: SshService, 312 protocol_version: xit.net_server_common.ProtocolVersion, 313 repo: []const u8, 314 315 fn deinit(self: SshRequest, allocator: std.mem.Allocator) void { 316 allocator.free(self.repo); 317 } 318 }; 319 320 fn runSshListener( 321 comptime repo_kind: rp.RepoKind, 322 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 323 io: std.Io, 324 allocator: std.mem.Allocator, 325 repo_root_path: []const u8, 326 net_server: *std.Io.net.Server, 327 tasks: *std.Io.Group, 328 err: *std.Io.Writer, 329 ) void { 330 const Listener = struct { 331 io: std.Io, 332 allocator: std.mem.Allocator, 333 repo_root_path: []const u8, 334 net_server: *std.Io.net.Server, 335 tasks: *std.Io.Group, 336 err: *std.Io.Writer, 337 338 fn run(ctx: @This()) void { 339 while (true) { 340 const stream = ctx.net_server.accept(ctx.io) catch |accept_err| { 341 if (accept_err == error.Canceled) return; 342 logError(ctx.err, "ssh accept failed: {s}\n", .{@errorName(accept_err)}); 343 continue; 344 }; 345 346 const Connection = struct { 347 io: std.Io, 348 allocator: std.mem.Allocator, 349 repo_root_path: []const u8, 350 stream: std.Io.net.Stream, 351 err: *std.Io.Writer, 352 353 fn run(conn: @This()) void { 354 defer conn.stream.close(conn.io); 355 handleSshGitConnection(repo_kind, any_repo_opts, conn.io, conn.allocator, conn.repo_root_path, conn.stream) catch |request_err| { 356 logError(conn.err, "ssh request failed: {s}\n", .{@errorName(request_err)}); 357 }; 358 } 359 }; 360 361 ctx.tasks.async(ctx.io, Connection.run, .{Connection{ 362 .io = ctx.io, 363 .allocator = ctx.allocator, 364 .repo_root_path = ctx.repo_root_path, 365 .stream = stream, 366 .err = ctx.err, 367 }}); 368 } 369 } 370 }; 371 372 tasks.async(io, Listener.run, .{Listener{ 373 .io = io, 374 .allocator = allocator, 375 .repo_root_path = repo_root_path, 376 .net_server = net_server, 377 .tasks = tasks, 378 .err = err, 379 }}); 380 } 381 382 fn handleSshGitConnection( 383 comptime repo_kind: rp.RepoKind, 384 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 385 io: std.Io, 386 allocator: std.mem.Allocator, 387 repo_root_path: []const u8, 388 stream: std.Io.net.Stream, 389 ) !void { 390 var send_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 391 var recv_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 392 var reader = stream.reader(io, &recv_buffer); 393 var writer = stream.writer(io, &send_buffer); 394 395 const request = try readSshRequest(allocator, &reader.interface); 396 defer request.deinit(allocator); 397 398 const repo_path = try resolveSshRepoPath(allocator, repo_root_path, request.repo); 399 defer allocator.free(repo_path); 400 401 if (!isSubPath(repo_root_path, repo_path)) return error.Forbidden; 402 403 try openRepoAndServe(repo_kind, any_repo_opts, io, allocator, repo_path, request.service == .receive_pack, SshGitService{ 404 .reader = &reader.interface, 405 .writer = &writer.interface, 406 .request = request, 407 }); 408 } 409 410 fn openRepoAndServe( 411 comptime repo_kind: rp.RepoKind, 412 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 413 io: std.Io, 414 allocator: std.mem.Allocator, 415 repo_path: []const u8, 416 create_if_missing: bool, 417 service: anytype, 418 ) !void { 419 if (any_repo_opts.hash) |hash_kind| { 420 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), io, allocator, repo_path, create_if_missing); 421 defer repo.deinit(io, allocator); 422 try service.serve(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator); 423 } else { 424 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 425 error.RepoNotFound => { 426 if (!create_if_missing) return open_err; 427 428 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOpts(), io, allocator, repo_path, true); 429 defer repo.deinit(io, allocator); 430 try service.serve(repo_kind, any_repo_opts.toRepoOpts(), &repo, io, allocator); 431 return; 432 }, 433 else => |e| return e, 434 }; 435 defer any_repo.deinit(io, allocator); 436 437 switch (any_repo) { 438 inline else => |*repo| { 439 try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator); 440 }, 441 } 442 } 443 } 444 445 fn openRepo( 446 comptime repo_kind: rp.RepoKind, 447 comptime repo_opts: rp.RepoOpts(repo_kind), 448 io: std.Io, 449 allocator: std.mem.Allocator, 450 repo_path: []const u8, 451 create_if_missing: bool, 452 ) !rp.Repo(repo_kind, repo_opts) { 453 return rp.Repo(repo_kind, repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 454 error.RepoNotFound => { 455 if (!create_if_missing) return open_err; 456 457 var repo = try rp.Repo(repo_kind, repo_opts).init(io, allocator, .{ .path = repo_path }); 458 errdefer repo.deinit(io, allocator); 459 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 460 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 461 return repo; 462 }, 463 else => |e| return e, 464 }; 465 } 466 467 const HttpGitService = struct { 468 body_reader: *std.Io.Reader, 469 writer: *std.Io.Writer, 470 options: xit.net_server_http_backend.Options, 471 472 fn serve( 473 self: @This(), 474 comptime repo_kind: rp.RepoKind, 475 comptime repo_opts: rp.RepoOpts(repo_kind), 476 repo: *rp.Repo(repo_kind, repo_opts), 477 io: std.Io, 478 allocator: std.mem.Allocator, 479 ) !void { 480 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 481 } 482 }; 483 484 const SshGitService = struct { 485 reader: *std.Io.Reader, 486 writer: *std.Io.Writer, 487 request: SshRequest, 488 489 fn serve( 490 self: @This(), 491 comptime repo_kind: rp.RepoKind, 492 comptime repo_opts: rp.RepoOpts(repo_kind), 493 repo: *rp.Repo(repo_kind, repo_opts), 494 io: std.Io, 495 allocator: std.mem.Allocator, 496 ) !void { 497 switch (self.request.service) { 498 .upload_pack => try repo.uploadPack(io, allocator, self.reader, self.writer, .{ 499 .protocol_version = self.request.protocol_version, 500 }), 501 .receive_pack => try repo.receivePack(io, allocator, self.reader, self.writer, .{ 502 .protocol_version = self.request.protocol_version, 503 }), 504 } 505 } 506 }; 507 508 fn isReceivePack( 509 handler: xit.net_server_http_backend.HandlerKind, 510 suffix: []const u8, 511 query: ?std.Uri.Component, 512 ) bool { 513 return switch (handler) { 514 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 515 .get_info_refs => if (query) |q| 516 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 517 else 518 false, 519 }; 520 } 521 522 fn readSshRequest(allocator: std.mem.Allocator, reader: *std.Io.Reader) !SshRequest { 523 const magic = try readSshPreludeLine(reader); 524 if (!std.mem.eql(u8, magic, "haxy-ssh-helper-v1")) return error.InvalidSshMagic; 525 526 const service_line = try readSshPreludeLine(reader); 527 const service = parseSshService(stripPrefix(service_line, "service=") orelse return error.InvalidSshServiceLine) orelse return error.InvalidSshService; 528 529 const protocol_line = try readSshPreludeLine(reader); 530 const protocol_version = std.meta.stringToEnum( 531 xit.net_server_common.ProtocolVersion, 532 stripPrefix(protocol_line, "protocol=") orelse return error.InvalidSshProtocolLine, 533 ) orelse return error.InvalidSshProtocol; 534 535 const repo_length_line = try readSshPreludeLine(reader); 536 const repo_len = try std.fmt.parseInt(usize, stripPrefix(repo_length_line, "repo-length=") orelse return error.InvalidSshRepoLengthLine, 10); 537 538 const empty_line = try readSshPreludeLine(reader); 539 if (empty_line.len != 0) return error.InvalidSshPreludeTerminator; 540 541 const repo = try reader.readAlloc(allocator, repo_len); 542 errdefer allocator.free(repo); 543 544 return .{ 545 .service = service, 546 .protocol_version = protocol_version, 547 .repo = repo, 548 }; 549 } 550 551 fn readSshPreludeLine(reader: *std.Io.Reader) ![]const u8 { 552 return (try reader.takeDelimiter('\n')) orelse error.InvalidSshHelperRequest; 553 } 554 555 fn parseSshService(value: []const u8) ?SshService { 556 if (std.mem.eql(u8, value, "upload-pack")) return .upload_pack; 557 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 558 return null; 559 } 560 561 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 562 if (!std.mem.startsWith(u8, value, prefix)) return null; 563 return value[prefix.len..]; 564 } 565 566 fn resolveSshRepoPath( 567 allocator: std.mem.Allocator, 568 repo_root_path: []const u8, 569 repo: []const u8, 570 ) ![]const u8 { 571 if (std.fs.path.isAbsolute(repo)) { 572 return try std.fs.path.resolve(allocator, &.{repo}); 573 } 574 return try std.fs.path.resolve(allocator, &.{ repo_root_path, repo }); 575 } 576 577 fn parseListenAddress(value: []const u8) !ListenAddress { 578 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 579 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 580 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 581 return .{ .host = value[0..colon], .port = port }; 582 } 583 584 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 585 if (encoded.len == 0) return error.InvalidRepoPath; 586 587 const mutable = try allocator.dupe(u8, encoded); 588 errdefer allocator.free(mutable); 589 const decoded = std.Uri.percentDecodeInPlace(mutable); 590 591 var iter = std.mem.splitScalar(u8, decoded, '/'); 592 while (iter.next()) |segment| { 593 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 594 return error.InvalidRepoPath; 595 } 596 } 597 598 return try allocator.realloc(mutable, decoded.len); 599 } 600 601 fn isSubPath(parent: []const u8, child: []const u8) bool { 602 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 603 if (!std.mem.startsWith(u8, child, parent)) return false; 604 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 605 } 606 607 fn normalizeMethod(method: std.http.Method) std.http.Method { 608 return if (method == .HEAD) .GET else method; 609 } 610 611 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 612 const git_protocol = header orelse return .v0; 613 var version: xit.net_server_common.ProtocolVersion = .v0; 614 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 615 while (iter.next()) |entry| { 616 const value = std.mem.trimStart(u8, entry, " "); 617 if (std.mem.startsWith(u8, value, "version=")) { 618 const v = value["version=".len..]; 619 if (std.mem.eql(u8, v, "2")) { 620 version = .v2; 621 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 622 version = .v1; 623 } 624 } 625 } 626 return version; 627 } 628 629 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 630 var it = request.iterateHeaders(); 631 while (it.next()) |header| { 632 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 633 } 634 return null; 635 } 636 637 fn writeSimpleResponse( 638 http_server: *std.http.Server, 639 code: u16, 640 message: []const u8, 641 content_type: []const u8, 642 body: []const u8, 643 ) !void { 644 try http_server.out.print( 645 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 646 .{ code, message, content_type, body.len, body }, 647 ); 648 } 649 650 fn logError(err: *std.Io.Writer, comptime fmt: []const u8, args: anytype) void { 651 err.print(fmt, args) catch return; 652 err.flush() catch {}; 653 } 654