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