╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
63 else => |e| return e, 64 }; 65 66 handleGitRequest(repo_kind, any_repo_opts, io, allocator, repo_root_path, admin_repo_path, &http_server, &request) catch |request_err| { 67 serve_common.logError(io, err, "request failed: {s}\n", .{@errorName(request_err)}); 68 if (http_server.reader.state == .received_head) { 69 http_server.reader.state = .ready; 70 } 71 try writeSimpleResponse(&http_server, 500, "Internal Server Error", "text/plain", @errorName(request_err)); 72 }; 73 try http_server.out.flush(); 74 } 75 } 76 77 fn handleGitRequest( 78 comptime repo_kind: rp.RepoKind, 79 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 80 io: std.Io, 81 allocator: std.mem.Allocator, 82 repo_root_path: []const u8, 83 admin_repo_path: []const u8, 84 http_server: *std.http.Server, 85 request: *std.http.Server.Request, 86 ) !void { 87 const uri = try std.Uri.parseAfterScheme("", request.head.target); 88 const path = uri.path.percent_encoded; 89 if (path.len == 0 or path[0] != '/') { 90 try writeSimpleResponse(http_server, 400, "Bad Request", "text/plain", "bad path"); 91 return; 92 } 93 94 const handler, const suffix = findRoute(path) orelse { 95 if (http_server.reader.state == .received_head) { 96 http_server.reader.state = .ready; 97 } 98 try writeSimpleResponse(http_server, 404, "Not Found", "text/plain", "not found"); 99 return; 100 }; 101 102 const repo_rel_encoded = path[1 .. path.len - suffix.len]; 103 const repo_rel = try decodeAndValidateRepoPath(allocator, repo_rel_encoded); 104 defer allocator.free(repo_rel); 105 106 const request_method = normalizeMethod(request.head.method); 107 const content_type = try allocator.dupe(u8, findHeader(request, "content-type") orelse ""); 108 defer allocator.free(content_type); 109 const has_remote_user = findHeader(request, "authorization") != null; 110 const protocol_version = protocolVersionFromHeader(findHeader(request, "git-protocol")); 111 112 // pushing over HTTP is not supported; pushes go over SSH 113 if (isReceivePack(handler, suffix, uri.query)) { 114 if (http_server.reader.state == .received_head) { 115 http_server.reader.state = .ready; 116 } 117 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "push over HTTP is not supported"); 118 return; 119 } 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 const repo_path = switch (try serve_common.resolveRepoPath(io, allocator, repo_root_path, admin_repo_path, repo_rel, false)) { 132 .ok => |p| p, 133 .invalid => return writeSimpleResponse(http_server, 400, "Bad Request", "text/plain", "repo path must be <owner>/<repo>"), 134 .not_found => return error.RepoNotFound, 135 }; 136 defer allocator.free(repo_path); 137 138 var body_reader = std.Io.Reader.fixed(body); 139 const http_backend_options = xit.net_server_http_backend.Options{ 140 .request_method = request_method, 141 .handler = handler, 142 .suffix = suffix, 143 .query_string = if (uri.query) |q| q.percent_encoded else "", 144 .content_type = content_type, 145 .has_remote_user = has_remote_user, 146 .protocol_version = protocol_version, 147 }; 148 149 // serve the existing on-disk repo at its event-id directory; HTTP only 150 // handles fetch/clone, so a missing repo is simply not found 151 if (!try serveIfExists(repo_kind, any_repo_opts, io, allocator, repo_path, GitService{ 152 .body_reader = &body_reader, 153 .writer = http_server.out, 154 .options = http_backend_options, 155 })) return error.RepoNotFound; 156 } 157 158 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 159 for (&xit.net_server_http_backend.routes) |*route| { 160 if (std.mem.endsWith(u8, path, route.suffix)) { 161 return .{ route.handler, route.suffix }; 162 } 163 } 164 return null; 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