╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
131 }; 132 defer admin.deinit(io, allocator); 133 var arena = std.heap.ArenaAllocator.init(allocator); 134 defer arena.deinit(); 135 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 136 const repo = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, owner_repo.owner, owner_repo.name)) orelse 137 return error.RepoNotFound; 138 if (repo.repo.event.read_access == .private) { 139 if (http_server.reader.state == .received_head) http_server.reader.state = .ready; 140 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "private repos cannot be fetched over HTTP"); 141 return; 142 } 143 const repo_id = std.fmt.bytesToHex(repo.event_id, .lower); 144 break :blk try std.fs.path.join(allocator, &.{ repo_root_path, &repo_id }); 145 }; 146 defer allocator.free(repo_path); 147 148 const body = if (request.head.method == .POST) blk: { 149 const reader = try request.readerExpectContinue(&.{}); 150 break :blk try reader.allocRemaining(allocator, .unlimited); 151 } else try allocator.dupe(u8, ""); 152 defer allocator.free(body); 153 154 if (http_server.reader.state == .received_head) { 155 http_server.reader.state = .ready; 156 } 157 158 var body_reader = std.Io.Reader.fixed(body); 159 const http_backend_options = xit.net_server_http_backend.Options{ 160 .request_method = request_method, 161 .handler = handler, 162 .suffix = suffix, 163 .query_string = if (uri.query) |q| q.percent_encoded else "", 164 .content_type = content_type, 165 .has_remote_user = has_remote_user, 166 .protocol_version = protocol_version, 167 }; 168 169 // serve the existing on-disk repo at its event-id directory; HTTP only 170 // handles fetch/clone, so a missing repo is simply not found 171 if (!try serveIfExists(repo_kind, any_repo_opts, io, allocator, repo_path, GitService{ 172 .body_reader = &body_reader, 173 .writer = http_server.out, 174 .options = http_backend_options, 175 })) return error.RepoNotFound; 176 } 177 178 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 179 for (&xit.net_server_http_backend.routes) |*route| { 180 if (std.mem.endsWith(u8, path, route.suffix)) { 181 return .{ route.handler, route.suffix }; 182 } 183 } 184 return null; 185 } 186 187 // serve an existing repo at `repo_path`, or return false if none is there 188 fn serveIfExists( 189 comptime repo_kind: rp.RepoKind, 190 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 191 io: std.Io, 192 allocator: std.mem.Allocator, 193 repo_path: []const u8, 194 service: anytype, 195 ) !bool { 196 // a bare open() creates the directory while probing for the repo, so only 197 // attempt it when the path already exists 198 std.Io.Dir.accessAbsolute(io, repo_path, .{}) catch return false; 199 200 if (any_repo_opts.hash) |hash_kind| { 201 var repo = rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 202 error.RepoNotFound => return false, 203 else => |e| return e, 204 }; 205 defer repo.deinit(io, allocator); 206 try service.serve(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator); 207 } else { 208 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 209 error.RepoNotFound => return false, 210 else => |e| return e, 211 }; 212 defer any_repo.deinit(io, allocator); 213 switch (any_repo) { 214 inline else => |*repo| try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator), 215 } 216 } 217 return true; 218 } 219 220 const GitService = struct { 221 body_reader: *std.Io.Reader, 222 writer: *std.Io.Writer, 223 options: xit.net_server_http_backend.Options, 224 225 fn serve( 226 self: @This(), 227 comptime repo_kind: rp.RepoKind, 228 comptime repo_opts: rp.RepoOpts(repo_kind), 229 repo: *rp.Repo(repo_kind, repo_opts), 230 io: std.Io, 231 allocator: std.mem.Allocator, 232 ) !void { 233 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 234 } 235 }; 236 237 fn isReceivePack( 238 handler: xit.net_server_http_backend.HandlerKind, 239 suffix: []const u8, 240 query: ?std.Uri.Component, 241 ) bool { 242 return switch (handler) { 243 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 244 .get_info_refs => if (query) |q| 245 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 246 else 247 false, 248 }; 249 } 250 251 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 252 if (encoded.len == 0) return error.InvalidRepoPath; 253 254 const mutable = try allocator.dupe(u8, encoded); 255 errdefer allocator.free(mutable); 256 const decoded = std.Uri.percentDecodeInPlace(mutable); 257 258 var iter = std.fs.path.componentIterator(decoded); 259 while (iter.next()) |component| { 260 if (component.name.len == 0 or std.mem.eql(u8, component.name, ".") or std.mem.eql(u8, component.name, "..")) { 261 return error.InvalidRepoPath; 262 } 263 } 264 265 return try allocator.realloc(mutable, decoded.len); 266 } 267 268 fn normalizeMethod(method: std.http.Method) std.http.Method { 269 return if (method == .HEAD) .GET else method; 270 } 271 272 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 273 const git_protocol = header orelse return .v0; 274 var version: xit.net_server_common.ProtocolVersion = .v0; 275 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 276 while (iter.next()) |entry| { 277 const value = std.mem.trimStart(u8, entry, " "); 278 if (std.mem.startsWith(u8, value, "version=")) { 279 const v = value["version=".len..]; 280 if (std.mem.eql(u8, v, "2")) { 281 version = .v2; 282 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 283 version = .v1; 284 } 285 } 286 } 287 return version; 288 } 289 290 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 291 var it = request.iterateHeaders(); 292 while (it.next()) |header| { 293 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 294 } 295 return null; 296 } 297 298 fn writeSimpleResponse( 299 http_server: *std.http.Server, 300 code: u16, 301 message: []const u8, 302 content_type: []const u8, 303 body: []const u8, 304 ) !void { 305 try http_server.out.print( 306 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 307 .{ code, message, content_type, body.len, body }, 308 ); 309 } 310