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