╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_http.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 273 var it = request.iterateHeaders(); 274 while (it.next()) |header| { 275 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 276 } 277 return null; 278 } 279 280 fn writeSimpleResponse( 281 http_server: *std.http.Server, 282 code: u16, 283 message: []const u8, 284 content_type: []const u8, 285 body: []const u8, 286 ) !void { 287 try http_server.out.print( 288 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 289 .{ code, message, content_type, body.len, body }, 290 ); 291 } 292