╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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