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