╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
196 const handle = struct { 197 fn h(ctx: Context, stream: std.Io.net.Stream) void { 198 defer stream.close(ctx.io); 199 web.handleConnection(ctx.io, ctx.allocator, stream, ctx.host, ctx.err) catch |request_err| { 200 serve_common.logError(ctx.io, ctx.err, "web ui request failed: {s}\n", .{@errorName(request_err)}); 201 }; 202 } 203 }.h; 204 205 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 206 .io = io, 207 .allocator = allocator, 208 .host = host, 209 .err = err, 210 }, handle); 211 } 212 213 // what a listener does with a port that is already taken 214 const PortPolicy = enum { 215 exact, 216 // leave it to whoever holds it and take an OS-assigned port instead 217 fallback, 218 }; 219 220 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 221 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 222 return switch (policy) { 223 .exact => try address.listen(io, .{}), 224 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 225 error.AddressInUse => blk: { 226 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 227 break :blk try any_port.listen(io, .{}); 228 }, 229 else => |e| return e, 230 }, 231 }; 232 } 233 234 fn parseListenAddress(value: []const u8) !ListenAddress { 235 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 236 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 237 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 238 return .{ .host = value[0..colon], .port = port }; 239 } 240