╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
85 const protocol_version = xit.net_server_common.parseProtocolVersion(findHeader(request, "git-protocol")); 86 87 // pushing over HTTP is not supported; pushes go over SSH 88 if (isReceivePack(handler, suffix, uri.query)) { 89 if (http_server.reader.state == .received_head) { 90 http_server.reader.state = .ready; 91 } 92 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "push over HTTP is not supported"); 93 return; 94 } 95 96 const owner_repo = evt.parseOwnerRepoPath(repo_rel) orelse { 97 if (http_server.reader.state == .received_head) http_server.reader.state = .ready; 98 try writeSimpleResponse(http_server, 400, "Bad Request", "text/plain", "repo path must be <owner>:<repo>"); 99 return; 100 }; 101 const repo_path = blk: { 102 var admin = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 103 error.RepoNotFound => return error.RepoNotFound, 104 else => |e| return e, 105 }; 106 defer admin.deinit(io, allocator); 107 var arena = std.heap.ArenaAllocator.init(allocator); 108 defer arena.deinit(); 109 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 110 const repo = (try evt.readRepoByOwnerAndName(io, allocator, &arena, moment, users_dir, owner_repo.owner, owner_repo.name, null)) orelse 111 return error.RepoNotFound; 112 if (repo.repo.event.read_access == .private) { 113 if (http_server.reader.state == .received_head) http_server.reader.state = .ready; 114 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "private repos cannot be fetched over HTTP"); 115 return; 116 } 117 break :blk try evt.repoPath(allocator, users_dir, &try evt.parseEventId(&repo.repo.event.user_id), &repo.event_id); 118 }; 119 defer allocator.free(repo_path); 120 121 const body = if (request.head.method == .POST) blk: { 122 const reader = try request.readerExpectContinue(&.{}); 123 break :blk try reader.allocRemaining(allocator, .unlimited); 124 } else try allocator.dupe(u8, ""); 125 defer allocator.free(body); 126 127 if (http_server.reader.state == .received_head) { 128 http_server.reader.state = .ready; 129 } 130 131 var body_reader = std.Io.Reader.fixed(body); 132 const http_backend_options = xit.net_server_http_backend.Options{ 133 .request_method = request_method, 134 .handler = handler, 135 .suffix = suffix, 136 .query_string = if (uri.query) |q| q.percent_encoded else "", 137 .content_type = content_type, 138 .has_remote_user = has_remote_user, 139 .protocol_version = protocol_version, 140 }; 141 142 // serve the existing on-disk repo at its event-id directory; HTTP only 143 // handles fetch/clone, so a missing repo is simply not found 144 if (!try serveIfExists(repo_kind, any_repo_opts, io, allocator, repo_path, GitService{ 145 .body_reader = &body_reader, 146 .writer = http_server.out, 147 .options = http_backend_options, 148 })) return error.RepoNotFound; 149 } 150 151 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 152 for (&xit.net_server_http_backend.routes) |*route| { 153 if (std.mem.endsWith(u8, path, route.suffix)) { 154 return .{ route.handler, route.suffix }; 155 } 156 } 157 return null; 158 } 159 160 // serve an existing repo at `repo_path`, or return false if none is there 161 fn serveIfExists( 162 comptime repo_kind: rp.RepoKind, 163 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 164 io: std.Io, 165 allocator: std.mem.Allocator, 166 repo_path: []const u8, 167 service: anytype, 168 ) !bool { 169 // a bare open() creates the directory while probing for the repo, so only 170 // attempt it when the path already exists 171 std.Io.Dir.accessAbsolute(io, repo_path, .{}) catch return false; 172 173 if (any_repo_opts.hash) |hash_kind| { 174 var repo = rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 175 error.RepoNotFound => return false, 176 else => |e| return e, 177 }; 178 defer repo.deinit(io, allocator); 179 try service.serve(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator); 180 } else { 181 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).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 any_repo.deinit(io, allocator); 186 switch (any_repo) { 187 inline else => |*repo| try service.serve(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator), 188 } 189 } 190 return true; 191 } 192 193 const GitService = struct { 194 body_reader: *std.Io.Reader, 195 writer: *std.Io.Writer, 196 options: xit.net_server_http_backend.Options, 197 198 fn serve( 199 self: @This(), 200 comptime repo_kind: rp.RepoKind, 201 comptime repo_opts: rp.RepoOpts(repo_kind), 202 repo: *rp.Repo(repo_kind, repo_opts), 203 io: std.Io, 204 allocator: std.mem.Allocator, 205 ) !void { 206 try repo.httpBackend(io, allocator, self.body_reader, self.writer, .http, self.options); 207 } 208 }; 209 210 fn isReceivePack( 211 handler: xit.net_server_http_backend.HandlerKind, 212 suffix: []const u8, 213 query: ?std.Uri.Component, 214 ) bool { 215 return switch (handler) { 216 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 217 .get_info_refs => if (query) |q| 218 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 219 else 220 false, 221 }; 222 } 223 224 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 225 if (encoded.len == 0) return error.InvalidRepoPath; 226 227 const mutable = try allocator.dupe(u8, encoded); 228 errdefer allocator.free(mutable); 229 const decoded = std.Uri.percentDecodeInPlace(mutable); 230 231 var iter = std.Io.Dir.path.componentIterator(decoded); 232 while (iter.next()) |component| { 233 if (component.name.len == 0 or std.mem.eql(u8, component.name, ".") or std.mem.eql(u8, component.name, "..")) { 234 return error.InvalidRepoPath; 235 } 236 } 237 238 return try allocator.realloc(mutable, decoded.len); 239 } 240 241 fn normalizeMethod(method: std.http.Method) std.http.Method { 242 return if (method == .HEAD) .GET else method; 243 } 244 245 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 246 var it = request.iterateHeaders(); 247 while (it.next()) |header| { 248 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 249 } 250 return null; 251 } 252 253 fn writeSimpleResponse( 254 http_server: *std.http.Server, 255 code: u16, 256 message: []const u8, 257 content_type: []const u8, 258 body: []const u8, 259 ) !void { 260 try http_server.out.print( 261 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 262 .{ code, message, content_type, body.len, body }, 263 ); 264 } 265