╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
40 const stream = net_server.accept(io) catch |accept_err| { 41 try err.print("accept failed: {s}\n", .{@errorName(accept_err)}); 42 continue; 43 }; 44 defer stream.close(io); 45 46 var conn_br = stream.reader(io, &recv_buffer); 47 var conn_bw = stream.writer(io, &send_buffer); 48 var http_server = std.http.Server.init(&conn_br.interface, &conn_bw.interface); 49 50 while (http_server.reader.state == .ready) { 51 var request = http_server.receiveHead() catch |receive_err| switch (receive_err) { 52 error.HttpConnectionClosing => break, 53 error.ReadFailed => break, 54 else => |e| return e, 55 }; 56 57 handleRequest(repo_kind, any_repo_opts, io, allocator, project_root, &http_server, &request) catch |request_err| { 58 try err.print("request failed: {s}\n", .{@errorName(request_err)}); 59 try err.flush(); 60 if (http_server.reader.state == .received_head) { 61 http_server.reader.state = .ready; 62 } 63 try writeSimpleResponse(&http_server, 500, "Internal Server Error", "text/plain", @errorName(request_err)); 64 }; 65 try http_server.out.flush(); 66 break; 67 } 68 } 69 } 70 71 fn handleRequest( 72 comptime repo_kind: rp.RepoKind, 73 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 74 io: std.Io, 75 allocator: std.mem.Allocator, 76 project_root: []const u8, 77 http_server: *std.http.Server, 78 request: *std.http.Server.Request, 79 ) !void { 80 const uri = try std.Uri.parseAfterScheme("", request.head.target); 81 const path = uri.path.percent_encoded; 82 if (path.len == 0 or path[0] != '/') { 83 try writeSimpleResponse(http_server, 400, "Bad Request", "text/plain", "bad path"); 84 return; 85 } 86 87 const handler, const suffix = findRoute(path) orelse { 88 if (http_server.reader.state == .received_head) { 89 http_server.reader.state = .ready; 90 } 91 try writeSimpleResponse(http_server, 404, "Not Found", "text/plain", "not found"); 92 return; 93 }; 94 95 const repo_rel_encoded = path[1 .. path.len - suffix.len]; 96 const repo_rel = try decodeAndValidateRepoPath(allocator, repo_rel_encoded); 97 defer allocator.free(repo_rel); 98 99 const repo_path = try std.fs.path.resolve(allocator, &.{ project_root, repo_rel }); 100 defer allocator.free(repo_path); 101 102 if (!isSubPath(project_root, repo_path)) { 103 if (http_server.reader.state == .received_head) { 104 http_server.reader.state = .ready; 105 } 106 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "forbidden"); 107 return; 108 } 109 110 const request_method = normalizeMethod(request.head.method); 111 const content_type = try allocator.dupe(u8, findHeader(request, "content-type") orelse ""); 112 defer allocator.free(content_type); 113 const has_remote_user = findHeader(request, "authorization") != null; 114 const protocol_version = protocolVersionFromHeader(findHeader(request, "git-protocol")); 115 116 const body = if (request.head.method == .POST) blk: { 117 const reader = try request.readerExpectContinue(&.{}); 118 break :blk try reader.allocRemaining(allocator, .unlimited); 119 } else try allocator.dupe(u8, ""); 120 defer allocator.free(body); 121 122 if (http_server.reader.state == .received_head) { 123 http_server.reader.state = .ready; 124 } 125 126 var body_reader = std.Io.Reader.fixed(body); 127 if (any_repo_opts.hash) |hash_kind| { 128 var repo = try rp.Repo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind)).open(io, allocator, .{ .path = repo_path }); 129 defer repo.deinit(io, allocator); 130 try runHttpBackend(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator, &body_reader, http_server.out, request_method, handler, suffix, uri.query, content_type, has_remote_user, protocol_version); 131 } else { 132 var any_repo = try rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }); 133 defer any_repo.deinit(io, allocator); 134 switch (any_repo) { 135 inline else => |*repo| { 136 try runHttpBackend(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator, &body_reader, http_server.out, request_method, handler, suffix, uri.query, content_type, has_remote_user, protocol_version); 137 }, 138 } 139 } 140 } 141 142 fn runHttpBackend( 143 comptime repo_kind: rp.RepoKind, 144 comptime repo_opts: rp.RepoOpts(repo_kind), 145 repo: *rp.Repo(repo_kind, repo_opts), 146 io: std.Io, 147 allocator: std.mem.Allocator, 148 reader: *std.Io.Reader, 149 writer: *std.Io.Writer, 150 request_method: std.http.Method, 151 handler: xit.net_server_http_backend.HandlerKind, 152 suffix: []const u8, 153 query: ?std.Uri.Component, 154 content_type: []const u8, 155 has_remote_user: bool, 156 protocol_version: xit.net_server_common.ProtocolVersion, 157 ) !void { 158 try repo.httpBackend(io, allocator, reader, writer, .http, .{ 159 .request_method = request_method, 160 .handler = handler, 161 .suffix = suffix, 162 .query_string = if (query) |q| q.percent_encoded else "", 163 .content_type = content_type, 164 .has_remote_user = has_remote_user, 165 .protocol_version = protocol_version, 166 }); 167 } 168 169 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 170 for (&xit.net_server_http_backend.routes) |*route| { 171 if (std.mem.endsWith(u8, path, route.suffix)) { 172 return .{ route.handler, route.suffix }; 173 } 174 } 175 return null; 176 } 177 178 fn parseListenAddress(value: []const u8) !ListenAddress { 179 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 180 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 181 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 182 return .{ .host = value[0..colon], .port = port }; 183 } 184 185 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 186 if (encoded.len == 0) return error.InvalidRepoPath; 187 188 const mutable = try allocator.dupe(u8, encoded); 189 errdefer allocator.free(mutable); 190 const decoded = std.Uri.percentDecodeInPlace(mutable); 191 192 var iter = std.mem.splitScalar(u8, decoded, '/'); 193 while (iter.next()) |segment| { 194 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 195 return error.InvalidRepoPath; 196 } 197 } 198 199 return try allocator.realloc(mutable, decoded.len); 200 } 201 202 fn isSubPath(parent: []const u8, child: []const u8) bool { 203 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 204 if (!std.mem.startsWith(u8, child, parent)) return false; 205 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 206 } 207 208 fn normalizeMethod(method: std.http.Method) std.http.Method { 209 return if (method == .HEAD) .GET else method; 210 } 211 212 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 213 const git_protocol = header orelse return .v0; 214 var version: xit.net_server_common.ProtocolVersion = .v0; 215 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 216 while (iter.next()) |entry| { 217 const value = std.mem.trimStart(u8, entry, " "); 218 if (std.mem.startsWith(u8, value, "version=")) { 219 const v = value["version=".len..]; 220 if (std.mem.eql(u8, v, "2")) { 221 version = .v2; 222 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 223 version = .v1; 224 } 225 } 226 } 227 return version; 228 } 229 230 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 231 var it = request.iterateHeaders(); 232 while (it.next()) |header| { 233 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 234 } 235 return null; 236 } 237 238 fn writeSimpleResponse( 239 http_server: *std.http.Server, 240 code: u16, 241 message: []const u8, 242 content_type: []const u8, 243 body: []const u8, 244 ) !void { 245 try http_server.out.print( 246 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 247 .{ code, message, content_type, body.len, body }, 248 ); 249 } 250