╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
210 const handle = struct { 211 fn h(ctx: Context, stream: std.Io.net.Stream) void { 212 defer stream.close(ctx.io); 213 web.handleConnection(ctx.io, ctx.allocator, stream, ctx.host, ctx.err) catch |request_err| { 214 serve_common.logError(ctx.io, ctx.err, "web ui request failed: {s}\n", .{@errorName(request_err)}); 215 }; 216 } 217 }.h; 218 219 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 220 .io = io, 221 .allocator = allocator, 222 .host = host, 223 .err = err, 224 }, handle); 225 } 226 227 // what a listener does with a port that is already taken 228 const PortPolicy = enum { 229 exact, 230 // leave it to whoever holds it and take an OS-assigned port instead 231 fallback, 232 }; 233 234 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 235 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 236 return switch (policy) { 237 // so a restart can bind past the old process's connections in TIME_WAIT. 238 // not for fallback, where it would let a second instance share the port. 239 .exact => try address.listen(io, .{ .reuse_address = true }), 240 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 241 error.AddressInUse => blk: { 242 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 243 break :blk try any_port.listen(io, .{}); 244 }, 245 else => |e| return e, 246 }, 247 }; 248 } 249 250 fn parseListenAddress(value: []const u8) !ListenAddress { 251 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 252 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 253 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 254 return .{ .host = value[0..colon], .port = port }; 255 } 256