╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const hash = xit.hash; 5 6 pub const Options = struct { 7 http_listen: []const u8, 8 ssh_listen: ?[]const u8, 9 data_dir: []const u8, 10 }; 11 12 const ListenAddress = struct { 13 host: []const u8, 14 port: u16, 15 }; 16 17 pub fn run( 18 comptime repo_kind: rp.RepoKind, 19 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 20 io: std.Io, 21 allocator: std.mem.Allocator, 22 cwd_path: []const u8, 23 options: Options, 24 err: *std.Io.Writer, 25 ) !void { 26 const data_dir = try std.fs.path.resolve(allocator, &.{ cwd_path, options.data_dir }); 27 defer allocator.free(data_dir); 28 const repo_root = try std.fs.path.resolve(allocator, &.{ data_dir, "repos" }); 29 defer allocator.free(repo_root); 30 31 try std.Io.Dir.cwd().createDirPath(io, repo_root); 32 33 if (options.ssh_listen) |ssh_listen| { 34 const HttpContext = struct { 35 io: std.Io, 36 allocator: std.mem.Allocator, 37 repo_root: []const u8, 38 http_listen: []const u8, 39 err: *std.Io.Writer, 40 41 fn run(ctx: @This()) !void { 42 try runHttpListener(repo_kind, any_repo_opts, ctx.io, ctx.allocator, ctx.repo_root, ctx.http_listen, ctx.err); 43 } 44 }; 45 46 const http_thread = try std.Thread.spawn(.{}, HttpContext.run, .{HttpContext{ 47 .io = io, 48 .allocator = allocator, 49 .repo_root = repo_root, 50 .http_listen = options.http_listen, 51 .err = err, 52 }}); 53 defer http_thread.join(); 54 55 try runSshListener(repo_kind, any_repo_opts, io, allocator, repo_root, ssh_listen, err); 56 } else { 57 try runHttpListener(repo_kind, any_repo_opts, io, allocator, repo_root, options.http_listen, err); 58 } 59 } 60 61 fn runHttpListener( 62 comptime repo_kind: rp.RepoKind, 63 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 64 io: std.Io, 65 allocator: std.mem.Allocator, 66 repo_root: []const u8, 67 http_listen: []const u8, 68 err: *std.Io.Writer, 69 ) !void { 70 const listen_address = try parseListenAddress(http_listen); 71 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 72 var net_server = try address.listen(io, .{ .reuse_address = true }); 73 defer net_server.deinit(io); 74 75 try err.print("serving HTTP on {s}, repo root {s}\n", .{ http_listen, repo_root }); 76 try err.flush(); 77 78 var send_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 79 var recv_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 80 81 while (true) { 82 const stream = net_server.accept(io) catch |accept_err| { 83 try err.print("accept failed: {s}\n", .{@errorName(accept_err)}); 84 continue; 85 }; 86 defer stream.close(io); 87 88 var conn_br = stream.reader(io, &recv_buffer); 89 var conn_bw = stream.writer(io, &send_buffer); 90 var http_server = std.http.Server.init(&conn_br.interface, &conn_bw.interface); 91 92 while (http_server.reader.state == .ready) { 93 var request = http_server.receiveHead() catch |receive_err| switch (receive_err) { 94 error.HttpConnectionClosing => break, 95 error.ReadFailed => break, 96 else => |e| return e, 97 }; 98 99 handleRequest(repo_kind, any_repo_opts, io, allocator, repo_root, &http_server, &request) catch |request_err| { 100 try err.print("request failed: {s}\n", .{@errorName(request_err)}); 101 try err.flush(); 102 if (http_server.reader.state == .received_head) { 103 http_server.reader.state = .ready; 104 } 105 try writeSimpleResponse(&http_server, 500, "Internal Server Error", "text/plain", @errorName(request_err)); 106 }; 107 try http_server.out.flush(); 108 break; 109 } 110 } 111 } 112 113 fn handleRequest( 114 comptime repo_kind: rp.RepoKind, 115 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 116 io: std.Io, 117 allocator: std.mem.Allocator, 118 repo_root: []const u8, 119 http_server: *std.http.Server, 120 request: *std.http.Server.Request, 121 ) !void { 122 const uri = try std.Uri.parseAfterScheme("", request.head.target); 123 const path = uri.path.percent_encoded; 124 if (path.len == 0 or path[0] != '/') { 125 try writeSimpleResponse(http_server, 400, "Bad Request", "text/plain", "bad path"); 126 return; 127 } 128 129 const handler, const suffix = findRoute(path) orelse { 130 if (http_server.reader.state == .received_head) { 131 http_server.reader.state = .ready; 132 } 133 try writeSimpleResponse(http_server, 404, "Not Found", "text/plain", "not found"); 134 return; 135 }; 136 137 const repo_rel_encoded = path[1 .. path.len - suffix.len]; 138 const repo_rel = try decodeAndValidateRepoPath(allocator, repo_rel_encoded); 139 defer allocator.free(repo_rel); 140 141 const repo_path = try std.fs.path.resolve(allocator, &.{ repo_root, repo_rel }); 142 defer allocator.free(repo_path); 143 144 if (!isSubPath(repo_root, repo_path)) { 145 if (http_server.reader.state == .received_head) { 146 http_server.reader.state = .ready; 147 } 148 try writeSimpleResponse(http_server, 403, "Forbidden", "text/plain", "forbidden"); 149 return; 150 } 151 152 const request_method = normalizeMethod(request.head.method); 153 const content_type = try allocator.dupe(u8, findHeader(request, "content-type") orelse ""); 154 defer allocator.free(content_type); 155 const has_remote_user = findHeader(request, "authorization") != null; 156 const protocol_version = protocolVersionFromHeader(findHeader(request, "git-protocol")); 157 158 const body = if (request.head.method == .POST) blk: { 159 const reader = try request.readerExpectContinue(&.{}); 160 break :blk try reader.allocRemaining(allocator, .unlimited); 161 } else try allocator.dupe(u8, ""); 162 defer allocator.free(body); 163 164 if (http_server.reader.state == .received_head) { 165 http_server.reader.state = .ready; 166 } 167 168 var body_reader = std.Io.Reader.fixed(body); 169 const create_if_missing = isReceivePack(handler, suffix, uri.query); 170 const http_backend_options = xit.net_server_http_backend.Options{ 171 .request_method = request_method, 172 .handler = handler, 173 .suffix = suffix, 174 .query_string = if (uri.query) |q| q.percent_encoded else "", 175 .content_type = content_type, 176 .has_remote_user = has_remote_user, 177 .protocol_version = protocol_version, 178 }; 179 180 if (any_repo_opts.hash) |hash_kind| { 181 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), io, allocator, repo_path, create_if_missing); 182 defer repo.deinit(io, allocator); 183 try repo.httpBackend(io, allocator, &body_reader, http_server.out, .http, http_backend_options); 184 } else { 185 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |err| switch (err) { 186 error.RepoNotFound => { 187 if (!create_if_missing) return err; 188 189 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOpts(), io, allocator, repo_path, true); 190 defer repo.deinit(io, allocator); 191 192 try repo.httpBackend(io, allocator, &body_reader, http_server.out, .http, http_backend_options); 193 return; 194 }, 195 else => |e| return e, 196 }; 197 defer any_repo.deinit(io, allocator); 198 199 switch (any_repo) { 200 inline else => |*repo| { 201 try repo.httpBackend(io, allocator, &body_reader, http_server.out, .http, http_backend_options); 202 }, 203 } 204 } 205 } 206 207 fn findRoute(path: []const u8) ?struct { xit.net_server_http_backend.HandlerKind, []const u8 } { 208 for (&xit.net_server_http_backend.routes) |*route| { 209 if (std.mem.endsWith(u8, path, route.suffix)) { 210 return .{ route.handler, route.suffix }; 211 } 212 } 213 return null; 214 } 215 216 const SshService = enum { 217 upload_pack, 218 receive_pack, 219 }; 220 221 const SshRequest = struct { 222 service: SshService, 223 protocol_version: xit.net_server_common.ProtocolVersion, 224 repo: []const u8, 225 226 fn deinit(self: SshRequest, allocator: std.mem.Allocator) void { 227 allocator.free(self.repo); 228 } 229 }; 230 231 fn runSshListener( 232 comptime repo_kind: rp.RepoKind, 233 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 234 io: std.Io, 235 allocator: std.mem.Allocator, 236 repo_root: []const u8, 237 ssh_listen: []const u8, 238 err: *std.Io.Writer, 239 ) !void { 240 const listen_address = try parseListenAddress(ssh_listen); 241 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 242 var net_server = try address.listen(io, .{ .reuse_address = true }); 243 defer net_server.deinit(io); 244 245 try err.print("serving SSH helper connections on {s}, repo root {s}\n", .{ ssh_listen, repo_root }); 246 try err.flush(); 247 248 while (true) { 249 const stream = net_server.accept(io) catch |accept_err| { 250 try err.print("ssh accept failed: {s}\n", .{@errorName(accept_err)}); 251 continue; 252 }; 253 defer stream.close(io); 254 255 handleSshConnection(repo_kind, any_repo_opts, io, allocator, repo_root, stream) catch |request_err| { 256 try err.print("ssh request failed: {s}\n", .{@errorName(request_err)}); 257 try err.flush(); 258 }; 259 } 260 } 261 262 fn handleSshConnection( 263 comptime repo_kind: rp.RepoKind, 264 comptime any_repo_opts: rp.AnyRepoOpts(repo_kind), 265 io: std.Io, 266 allocator: std.mem.Allocator, 267 repo_root: []const u8, 268 stream: std.Io.net.Stream, 269 ) !void { 270 var send_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 271 var recv_buffer = [_]u8{0} ** any_repo_opts.net_buffer_size; 272 var reader = stream.reader(io, &recv_buffer); 273 var writer = stream.writer(io, &send_buffer); 274 275 const request = try readSshRequest(allocator, &reader.interface); 276 defer request.deinit(allocator); 277 278 const repo_path = try resolveSshRepoPath(allocator, repo_root, request.repo); 279 defer allocator.free(repo_path); 280 281 if (!isSubPath(repo_root, repo_path)) return error.Forbidden; 282 283 if (any_repo_opts.hash) |hash_kind| { 284 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), io, allocator, repo_path, request.service == .receive_pack); 285 defer repo.deinit(io, allocator); 286 try runSshService(repo_kind, any_repo_opts.toRepoOptsWithHash(hash_kind), &repo, io, allocator, &reader.interface, &writer.interface, request); 287 } else { 288 var any_repo = rp.AnyRepo(repo_kind, any_repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 289 error.RepoNotFound => { 290 if (request.service != .receive_pack) return open_err; 291 292 var repo = try openRepo(repo_kind, any_repo_opts.toRepoOpts(), io, allocator, repo_path, true); 293 defer repo.deinit(io, allocator); 294 try runSshService(repo_kind, any_repo_opts.toRepoOpts(), &repo, io, allocator, &reader.interface, &writer.interface, request); 295 return; 296 }, 297 else => |e| return e, 298 }; 299 defer any_repo.deinit(io, allocator); 300 switch (any_repo) { 301 inline else => |*repo| { 302 try runSshService(repo.self_repo_kind, repo.self_repo_opts, repo, io, allocator, &reader.interface, &writer.interface, request); 303 }, 304 } 305 } 306 } 307 308 fn runSshService( 309 comptime repo_kind: rp.RepoKind, 310 comptime repo_opts: rp.RepoOpts(repo_kind), 311 repo: *rp.Repo(repo_kind, repo_opts), 312 io: std.Io, 313 allocator: std.mem.Allocator, 314 reader: *std.Io.Reader, 315 writer: *std.Io.Writer, 316 request: SshRequest, 317 ) !void { 318 switch (request.service) { 319 .upload_pack => try repo.uploadPack(io, allocator, reader, writer, .{ 320 .protocol_version = request.protocol_version, 321 }), 322 .receive_pack => try repo.receivePack(io, allocator, reader, writer, .{ 323 .protocol_version = request.protocol_version, 324 }), 325 } 326 } 327 328 fn openRepo( 329 comptime repo_kind: rp.RepoKind, 330 comptime repo_opts: rp.RepoOpts(repo_kind), 331 io: std.Io, 332 allocator: std.mem.Allocator, 333 repo_path: []const u8, 334 create_if_missing: bool, 335 ) !rp.Repo(repo_kind, repo_opts) { 336 return rp.Repo(repo_kind, repo_opts).open(io, allocator, .{ .path = repo_path }) catch |open_err| switch (open_err) { 337 error.RepoNotFound => { 338 if (!create_if_missing) return open_err; 339 340 var repo = try rp.Repo(repo_kind, repo_opts).init(io, allocator, .{ .path = repo_path }); 341 errdefer repo.deinit(io, allocator); 342 try repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 343 try repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 344 return repo; 345 }, 346 else => |e| return e, 347 }; 348 } 349 350 fn isReceivePack( 351 handler: xit.net_server_http_backend.HandlerKind, 352 suffix: []const u8, 353 query: ?std.Uri.Component, 354 ) bool { 355 return switch (handler) { 356 .run_service => std.mem.eql(u8, suffix, "/git-receive-pack"), 357 .get_info_refs => if (query) |q| 358 std.mem.startsWith(u8, q.percent_encoded, "service=git-receive-pack") 359 else 360 false, 361 }; 362 } 363 364 fn readSshRequest(allocator: std.mem.Allocator, reader: *std.Io.Reader) !SshRequest { 365 const magic = try readSshPreludeLine(reader); 366 if (!std.mem.eql(u8, magic, "haxy-ssh-helper-v1")) return error.InvalidSshMagic; 367 368 const service_line = try readSshPreludeLine(reader); 369 const service = parseSshService(stripPrefix(service_line, "service=") orelse return error.InvalidSshServiceLine) orelse return error.InvalidSshService; 370 371 const protocol_line = try readSshPreludeLine(reader); 372 const protocol_version = std.meta.stringToEnum( 373 xit.net_server_common.ProtocolVersion, 374 stripPrefix(protocol_line, "protocol=") orelse return error.InvalidSshProtocolLine, 375 ) orelse return error.InvalidSshProtocol; 376 377 const repo_length_line = try readSshPreludeLine(reader); 378 const repo_len = try std.fmt.parseInt(usize, stripPrefix(repo_length_line, "repo-length=") orelse return error.InvalidSshRepoLengthLine, 10); 379 380 const empty_line = try readSshPreludeLine(reader); 381 if (empty_line.len != 0) return error.InvalidSshPreludeTerminator; 382 383 const repo = try reader.readAlloc(allocator, repo_len); 384 errdefer allocator.free(repo); 385 386 return .{ 387 .service = service, 388 .protocol_version = protocol_version, 389 .repo = repo, 390 }; 391 } 392 393 fn readSshPreludeLine(reader: *std.Io.Reader) ![]const u8 { 394 return (try reader.takeDelimiter('\n')) orelse error.InvalidSshHelperRequest; 395 } 396 397 fn parseSshService(value: []const u8) ?SshService { 398 if (std.mem.eql(u8, value, "upload-pack")) return .upload_pack; 399 if (std.mem.eql(u8, value, "receive-pack")) return .receive_pack; 400 return null; 401 } 402 403 fn stripPrefix(value: []const u8, prefix: []const u8) ?[]const u8 { 404 if (!std.mem.startsWith(u8, value, prefix)) return null; 405 return value[prefix.len..]; 406 } 407 408 fn resolveSshRepoPath( 409 allocator: std.mem.Allocator, 410 repo_root: []const u8, 411 repo: []const u8, 412 ) ![]const u8 { 413 if (std.fs.path.isAbsolute(repo)) { 414 return try std.fs.path.resolve(allocator, &.{repo}); 415 } 416 return try std.fs.path.resolve(allocator, &.{ repo_root, repo }); 417 } 418 419 fn parseListenAddress(value: []const u8) !ListenAddress { 420 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 421 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 422 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 423 return .{ .host = value[0..colon], .port = port }; 424 } 425 426 fn decodeAndValidateRepoPath(allocator: std.mem.Allocator, encoded: []const u8) ![]const u8 { 427 if (encoded.len == 0) return error.InvalidRepoPath; 428 429 const mutable = try allocator.dupe(u8, encoded); 430 errdefer allocator.free(mutable); 431 const decoded = std.Uri.percentDecodeInPlace(mutable); 432 433 var iter = std.mem.splitScalar(u8, decoded, '/'); 434 while (iter.next()) |segment| { 435 if (segment.len == 0 or std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) { 436 return error.InvalidRepoPath; 437 } 438 } 439 440 return try allocator.realloc(mutable, decoded.len); 441 } 442 443 fn isSubPath(parent: []const u8, child: []const u8) bool { 444 if (std.mem.eql(u8, parent, std.fs.path.sep_str)) return std.fs.path.isAbsolute(child); 445 if (!std.mem.startsWith(u8, child, parent)) return false; 446 return child.len == parent.len or child[parent.len] == std.fs.path.sep; 447 } 448 449 fn normalizeMethod(method: std.http.Method) std.http.Method { 450 return if (method == .HEAD) .GET else method; 451 } 452 453 fn protocolVersionFromHeader(header: ?[]const u8) xit.net_server_common.ProtocolVersion { 454 const git_protocol = header orelse return .v0; 455 var version: xit.net_server_common.ProtocolVersion = .v0; 456 var iter = std.mem.splitScalar(u8, git_protocol, ':'); 457 while (iter.next()) |entry| { 458 const value = std.mem.trimStart(u8, entry, " "); 459 if (std.mem.startsWith(u8, value, "version=")) { 460 const v = value["version=".len..]; 461 if (std.mem.eql(u8, v, "2")) { 462 version = .v2; 463 } else if (std.mem.eql(u8, v, "1") and version != .v2) { 464 version = .v1; 465 } 466 } 467 } 468 return version; 469 } 470 471 fn findHeader(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 472 var it = request.iterateHeaders(); 473 while (it.next()) |header| { 474 if (std.ascii.eqlIgnoreCase(header.name, name)) return header.value; 475 } 476 return null; 477 } 478 479 fn writeSimpleResponse( 480 http_server: *std.http.Server, 481 code: u16, 482 message: []const u8, 483 content_type: []const u8, 484 body: []const u8, 485 ) !void { 486 try http_server.out.print( 487 "HTTP/1.1 {d} {s}\r\nContent-Type: {s}\r\nContent-Length: {d}\r\n\r\n{s}", 488 .{ code, message, content_type, body.len, body }, 489 ); 490 } 491