╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
213 } 214 }.h; 215 216 serve_common.runListener(io, net_server, tasks, err, "web ui", Context{ 217 .io = io, 218 .allocator = allocator, 219 .host = host, 220 .err = err, 221 }, handle); 222 } 223 224 // what a listener does with a port that is already taken 225 const PortPolicy = enum { 226 exact, 227 // leave it to whoever holds it and take an OS-assigned port instead 228 fallback, 229 }; 230 231 fn listen(io: std.Io, listen_address: ListenAddress, policy: PortPolicy) !std.Io.net.Server { 232 const address = try std.Io.net.IpAddress.parseIp4(listen_address.host, listen_address.port); 233 return switch (policy) { 234 .exact => try address.listen(io, .{}), 235 .fallback => address.listen(io, .{}) catch |listen_err| switch (listen_err) { 236 error.AddressInUse => blk: { 237 const any_port = try std.Io.net.IpAddress.parseIp4(listen_address.host, 0); 238 break :blk try any_port.listen(io, .{}); 239 }, 240 else => |e| return e, 241 }, 242 }; 243 } 244 245 fn parseListenAddress(value: []const u8) !ListenAddress { 246 const colon = std.mem.lastIndexOfScalar(u8, value, ':') orelse return error.InvalidListenAddress; 247 if (colon == 0 or colon + 1 >= value.len) return error.InvalidListenAddress; 248 const port = try std.fmt.parseInt(u16, value[colon + 1 ..], 10); 249 return .{ .host = value[0..colon], .port = port }; 250 } 251