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