Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkar N Kulkarni committed Mar 28, 2019
1 parent 998f1ef commit 05370fe
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 45 deletions.
5 changes: 3 additions & 2 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,11 @@ public class ClientRequest {
/// Initializes a `ClientRequest` instance
///
/// - Parameter options: An array of `Options' describing the request
/// - Parameter unixDomainSocketPath: Specifies a path of a Unix domain socket that the client should connect to.
/// - Parameter callback: The closure of type `Callback` to be used for the callback.
init(options: [Options], socketPath: String? = nil, callback: @escaping Callback) {
init(options: [Options], unixDomainSocketPath: String? = nil, callback: @escaping Callback) {

self.unixDomainSocketPath = socketPath
self.unixDomainSocketPath = unixDomainSocketPath
self.callback = callback

var theSchema = "http://"
Expand Down
12 changes: 7 additions & 5 deletions Sources/KituraNet/HTTP/HTTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,20 @@ public class HTTP {
Create a new `ClientRequest` using a list of options.
- Parameter options: a list of `ClientRequest.Options`.
- Parameter callback: closure to run after the request.
- Parameter unixDomainSocketPath: the path of a Unix domain socket that this client should connect to (defaults to `nil`).
- Parameter callback: The closure to run after the request completes. The `ClientResponse?` parameter allows access to the response from the server.
- Returns: a `ClientRequest` instance
### Usage Example: ###
````swift
let request = HTTP.request([ClientRequest.Options]) {response in
...
let myOptions: [ClientRequest.Options] = [.hostname("localhost"), .port("8080")]
let request = HTTP.request(myOptions) { response in
// Process the ClientResponse
}
````
*/
public static func request(_ options: [ClientRequest.Options], socketPath: String? = nil, callback: @escaping ClientRequest.Callback) -> ClientRequest {
return ClientRequest(options: options, socketPath: socketPath, callback: callback)
public static func request(_ options: [ClientRequest.Options], unixDomainSocketPath: String? = nil, callback: @escaping ClientRequest.Callback) -> ClientRequest {
return ClientRequest(options: options, unixDomainSocketPath: unixDomainSocketPath, callback: callback)
}

/**
Expand Down
65 changes: 46 additions & 19 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,11 @@ public class HTTPServer: Server {
*/
public var delegate: ServerDelegate?

/**
Port number for listening for new connections.
### Usage Example: ###
````swift
httpServer.port = 8080
````
*/
/// The TCP port on which this server listens for new connections. If `nil`, this server does not listen on a TCP socket.
public private(set) var port: Int?

var unixDomainSocketPath: String?
/// The Unix domain socket path on which this server listens for new connections. If `nil`, this server does not listen on a Unix socket.
public private(set) var unixDomainSocketPath: String?

private var _state: ServerState = .unknown

Expand Down Expand Up @@ -227,18 +221,31 @@ public class HTTPServer: Server {
return nil
}

enum SocketType {
case tcpPort(Int)
case unixDomainSocket(String)
// Sockets could either be TCP/IP sockets or Unix domain sockets
private enum SocketType {
// An TCP/IP socket has an associated port number
case tcp(Int)
// A unix domain socket has an associated filename
case unix(String)
}

/**
Listens for connections on a Unix socket.
### Usage Example: ###
````swift
try server.listen(unixDomainSocketPath: "/my/path")
````
- Parameter unixDomainSocketPath: Unix socket path for new connections, eg. "/my/path"
*/
func listen(unixDomainSocketPath: String) throws {
self.unixDomainSocketPath = unixDomainSocketPath
try listen(.unixDomainSocket(unixDomainSocketPath))
try listen(.unix(unixDomainSocketPath))
}

/**
Listens for connections on a socket.
Listens for connections on a TCP socket.
### Usage Example: ###
````swift
Expand All @@ -249,7 +256,7 @@ public class HTTPServer: Server {
*/
public func listen(on port: Int) throws {
self.port = port
try listen(.tcpPort(port))
try listen(.tcp(port))
}

private func listen(_ socket: SocketType) throws {
Expand Down Expand Up @@ -292,9 +299,9 @@ public class HTTPServer: Server {
.childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: 1)

do {
if case let SocketType.tcpPort(port) = socket {
if case let SocketType.tcp(port) = socket {
serverChannel = try bootstrap.bind(host: "0.0.0.0", port: port).wait()
} else if case let SocketType.unixDomainSocket(unixDomainSocketPath) = socket {
} else if case let SocketType.unix(unixDomainSocketPath) = socket {
serverChannel = try bootstrap.bind(unixDomainSocketPath: unixDomainSocketPath).wait()
}
self.port = serverChannel?.localAddress?.port.map { Int($0) }
Expand All @@ -304,9 +311,9 @@ public class HTTPServer: Server {
self.state = .failed
self.lifecycleListener.performFailCallbacks(with: error)
switch socket {
case .tcpPort(let port):
case .tcp(let port):
Log.error("Error trying to bind to \(port): \(error)")
case .unixDomainSocket(let socketPath):
case .unix(let socketPath):
Log.error("Error trying to bind to \(socketPath): \(error)")
}
throw error
Expand Down Expand Up @@ -348,6 +355,26 @@ public class HTTPServer: Server {
return server
}

/**
Static method to create a new HTTP server and have it listen for connections on a Unix domain socket.
### Usage Example: ###
````swift
let server = HTTPServer.listen(unixDomainSocketPath: "/my/path", delegate: self)
````
- Parameter unixDomainSocketPath: The path of the Unix domain socket that this server should listen on.
- Parameter delegate: The delegate handler for HTTP connections.
- Returns: A new instance of a `HTTPServer`.
*/
public static func listen(unixDomainSocketPath: String, delegate: ServerDelegate?) throws -> HTTPServer {
let server = HTTP.createServer()
server.delegate = delegate
try server.listen(unixDomainSocketPath: unixDomainSocketPath)
return server
}

/**
Listen for connections on a socket.
Expand Down
2 changes: 1 addition & 1 deletion Tests/KituraNetTests/KituraNIOTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class KituraNetTest: XCTestCase {
options.append(.disableSSLVerification)
}

let req = HTTP.request(options, socketPath: unixDomainSocketPath, callback: callback)
let req = HTTP.request(options, unixDomainSocketPath: unixDomainSocketPath, callback: callback)
if let requestModifier = requestModifier {
requestModifier(req)
}
Expand Down
19 changes: 2 additions & 17 deletions Tests/KituraNetTests/LargePayloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,10 @@ class LargePayloadTests: KituraNetTest {

private let delegate = TestServerDelegate()

private func uniqueTemporaryFilePath() -> String {
#if os(Linux)
let temporaryDirectory = "/tmp"
#else
var temporaryDirectory: String
if #available(OSX 10.12, *) {
temporaryDirectory = FileManager.default.temporaryDirectory.path
} else {
temporaryDirectory = "/tmp"
}
#endif
return temporaryDirectory + "/" + String(ProcessInfo.processInfo.globallyUniqueString.prefix(20))
}

func testLargePosts() {
let unixDomainSocketPath = uniqueTemporaryFilePath()
performServerTest(delegate, unixDomainSocketPath: unixDomainSocketPath, useSSL: false, asyncTasks: { expectation in
performServerTest(delegate, useSSL: false, asyncTasks: { expectation in
let payload = "[" + contentTypesString + "," + contentTypesString + contentTypesString + "," + contentTypesString + "]"
self.performRequest("post", path: "/largepost", unixDomainSocketPath: unixDomainSocketPath, callback: {response in
self.performRequest("post", path: "/largepost", callback: {response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(String(describing: response?.statusCode))")
do {
let expectedResult = "Read \(payload.count) bytes"
Expand Down
3 changes: 2 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ XCTMain([
testCase(ClientE2ETests.allTests.shuffled()),
testCase(PipeliningTests.allTests.shuffled()),
testCase(RegressionTests.allTests.shuffled()),
testCase(MonitoringTests.allTests.shuffled())
testCase(MonitoringTests.allTests.shuffled()),
testCase(UnixDomainSocketTests.allTests.shuffled())
].shuffled())

0 comments on commit 05370fe

Please sign in to comment.