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