╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
179 } 180 181 // serve an existing repo at `repo_path`, or return false if none is there 182 fn serveIfExists( 183 comptime repo_kind: rp.RepoKind, 184 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 185 io: std.Io, 186 allocator: std.mem.Allocator, 187 repo_path: []const u8, 188 service: anytype, 189 ) !bool { 190 // a bare open() creates the directory while probing for the repo, so only 191 // attempt it when the path already exists 192 std.Io.Dir.accessAbsolute(io, repo_path, .{}) catch return false; 193 194 if (any_repo_opts.hash) |hash_kind| { 195 var repo = rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 196 error.RepoNotFound => return false, 197 else => |e| return e, 198 }; 199 defer repo.deinit(io, allocator); 200 try service.serve(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator); 201 } else { 202 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 203 error.RepoNotFound => return false, 204 else => |e| return e, 205 }; 206 defer any_repo.deinit(io, allocator); 207 switch (any_repo) { 208 inline else => |*repo| try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator), 209 } 210 } 211 return true; 212 } 213 214 const GitService = struct { 215 body_reader: *std.Io.Reader, 216 writer: *std.Io.Writer, 217 options: xit.net_server_http_backend.Options, 218 219 fn serve( 220 self: @This(), 221 comptime repo_kind: rp.RepoKind, 222 comptime repo_opts: rp.RepoOpts(repo_kind), 223 repo: *rp.Repo(repo_kind, repo_opts), 224 io: std.Io, 225 allocator: std.mem.Allocator, 226 ) !void { 227 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 228 } 229 }; 230 231 fn isReceivePack( 232 handler: xit.net_server_http_backend.HandlerKind, 233 suffix: []const u8, 234 query: ?std.Uri.Component, 235 ) bool { 236 return switch (handler) { 237 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 238 .get_info_refs => if (query) |q| 239 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 240 else 241 false, 242 }; 243 } 244 245 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 246 if (encoded.len == 0) return error.InvalidRepoPath; 247 248 const mutable = try allocator.dupe(u8, encoded); 249 errdefer allocator.free(mutable); 250 const decoded = std.Uri.percentDecodeInPlace(mutable); 251 252 var iter = std.fs.path.componentIterator(decoded); 253 while (iter.next()) |component| { 254 if (component.name.len == 0 or std.mem.eql(u8, component.name, ".") or std.mem.eql(u8, component.name, "..")) { 255 return error.InvalidRepoPath; 256 } 257 } 258 259 return try allocator.realloc(mutable, decoded.len); 260 } 261 262 fn normalizeMethod(method: std.http.Method) std.http.Method { 263 return if (method == .HEAD) .GET else method; 264 } 265 266 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 267 const git_protocol = header orelse return .v0; 268 var version: xit.net_server_common.ProtocolVersion = .v0; 269 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 270 while (iter.next()) |entry| { 271 const value = std.mem.trimStart(u8, entry, " "); 272 if (std.mem.startsWith(u8, value, "version=")) { 273 const v = value["version=".len..]; 274 if (std.mem.eql(u8, v, "2")) { 275 version = .v2; 276 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 277 version = .v1; 278 } 279 } 280 } 281 return version; 282 } 283 284 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 285 var it = request.iterateHeaders(); 286 while (it.next()) |header| { 287 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 288 } 289 return null; 290 } 291 292 fn writeSimpleResponse( 293 http_server: *std.http.Server, 294 code: u16, 295 message: []const u8, 296 content_type: []const u8, 297 body: []const u8, 298 ) !void { 299 try http_server.out.print( 300 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 301 .{ code, message, content_type, body.len, body }, 302 ); 303 } 304