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