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