Skip to content

Commit f016eb9

Browse files
committed
Specify host name in PreviewServer.Bind.localhost
`ServerBootstrap.bind` needs to know which host name to bind a socket to, but we were hardcoding it to `localhost` without providing a way for a different host to be provided. By adding a `host:` element to `PreviewServer.Bind.localhost`, we require the caller of `PreviewServer.init` to be specific about which host they want.
1 parent 11c772e commit f016eb9

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Sources/SwiftDocCUtilities/Action/Actions/PreviewAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public final class PreviewAction: Action, RecreatingContext {
173173
print(String(repeating: "=", count: 40), to: &logHandle)
174174
}
175175

176-
let to: PreviewServer.Bind = bindServerToSocketPath.map { .socket(path: $0) } ?? .localhost(port: port)
176+
let to: PreviewServer.Bind = bindServerToSocketPath.map { .socket(path: $0) } ?? .localhost(host: "localhost", port: port)
177177
servers[serverIdentifier] = try PreviewServer(contentURL: convertAction.targetDirectory, bindTo: to, logHandle: &logHandle)
178178

179179
// When the user stops docc - stop the preview server first before exiting.

Sources/SwiftDocCUtilities/PreviewServer/PreviewServer.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ final class PreviewServer {
7272
/// A list of server-bind destinations.
7373
public enum Bind: CustomStringConvertible {
7474
/// A port on the local machine.
75-
case localhost(port: Int)
75+
case localhost(host: String, port: Int)
7676

7777
/// A file socket on disk.
7878
case socket(path: String)
7979

8080
var description: String {
8181
switch self {
82-
case .localhost(port: let port):
83-
return "localhost:\(port)"
82+
case .localhost(host: let host, port: let port):
83+
return "\(host):\(port)"
8484
case .socket(path: let path):
8585
return path
8686
}
@@ -146,21 +146,21 @@ final class PreviewServer {
146146
do {
147147
// Bind to the given destination
148148
switch bindTo {
149-
case .localhost(let port):
150-
channel = try bootstrap.bind(host: "localhost", port: port).wait()
149+
case .localhost(let host, let port):
150+
channel = try bootstrap.bind(host: host, port: port).wait()
151151
case .socket(let path):
152152
channel = try bootstrap.bind(unixDomainSocketPath: path).wait()
153153
}
154154
} catch let error as NIO.IOError where error.errnoCode == EADDRINUSE {
155155
// The given port is not available.
156156
switch bindTo {
157-
case .localhost(let port): throw Error.portNotAvailable(port: port)
157+
case .localhost(_, let port): throw Error.portNotAvailable(port: port)
158158
default: throw error
159159
}
160160
} catch {
161161
// Cannot bind the given address/port.
162162
switch bindTo {
163-
case .localhost(let port): throw Error.cannotStartServer(port: port)
163+
case .localhost(_, let port): throw Error.cannotStartServer(port: port)
164164
default: throw error
165165
}
166166
}

Tests/SwiftDocCUtilitiesTests/PreviewServer/PreviewServerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class PreviewServerTests {
201201
}
202202

203203
func testPreviewServerBindDescription() {
204-
let localhostBind = PreviewServer.Bind.localhost(port: 1234)
204+
let localhostBind = PreviewServer.Bind.localhost(host: "localhost", port: 1234)
205205
XCTAssertEqual("\(localhostBind)", "localhost:1234")
206206
let socketBind = PreviewServer.Bind.socket(path: "/tmp/file.sock")
207207
XCTAssertEqual("\(socketBind)", "/tmp/file.sock")

0 commit comments

Comments
 (0)