From 3f0afd019d842e649c2e8c1b368ef8d9b505473c Mon Sep 17 00:00:00 2001 From: Nethra Ravindran Date: Tue, 25 Sep 2018 01:31:34 -0700 Subject: [PATCH] Replace implicitly unwrapped optionals by optionals --- Sources/KituraNet/ClientRequest.swift | 10 ++++---- Sources/KituraNet/ClientResponse.swift | 2 +- .../KituraNet/HTTP/HTTPRequestHandler.swift | 15 ++++++++---- Sources/KituraNet/HTTP/HTTPServer.swift | 9 +++---- Tests/KituraNetTests/RegressionTests.swift | 24 +++++++++---------- 5 files changed, 34 insertions(+), 26 deletions(-) diff --git a/Sources/KituraNet/ClientRequest.swift b/Sources/KituraNet/ClientRequest.swift index e4fabc4d..c8271013 100644 --- a/Sources/KituraNet/ClientRequest.swift +++ b/Sources/KituraNet/ClientRequest.swift @@ -334,10 +334,10 @@ public class ClientRequest { } /// The channel connecting to the remote server - var channel: Channel! + var channel: Channel? /// The client bootstrap used to connect to the remote server - var bootstrap: ClientBootstrap! + var bootstrap: ClientBootstrap? /// Send the request to the remote server @@ -392,6 +392,7 @@ public class ClientRequest { } do { + guard let bootstrap = bootstrap else { return } channel = try bootstrap.connect(host: hostName, port: Int(self.port!)).wait() } catch let error { Log.error("Connection to \(hostName):\(self.port ?? 80) failed with error: \(error)") @@ -404,15 +405,16 @@ public class ClientRequest { // Make the HTTP request, the response callbacks will be received on the HTTPClientHandler. // We are mostly not running on the event loop. Let's make sure we send the request over the event loop. + guard let channel = channel else { return } execute(on: channel.eventLoop) { - self.sendRequest(request: request, on: self.channel) + self.sendRequest(request: request, on: channel) } waitSemaphore.wait() // We are now free to close the connection if asked for. if closeConnection { execute(on: channel.eventLoop) { - self.channel.close(promise: nil) + channel.close(promise: nil) } } } diff --git a/Sources/KituraNet/ClientResponse.swift b/Sources/KituraNet/ClientResponse.swift index 559b9858..c1258402 100644 --- a/Sources/KituraNet/ClientResponse.swift +++ b/Sources/KituraNet/ClientResponse.swift @@ -37,7 +37,7 @@ public class ClientResponse { /// Minor version of HTTP of the response public var httpVersionMinor: UInt16? - internal var _headers: HTTPHeaders! + internal var _headers: HTTPHeaders? /// Set of HTTP headers of the response. public var headers: HeadersContainer { diff --git a/Sources/KituraNet/HTTP/HTTPRequestHandler.swift b/Sources/KituraNet/HTTP/HTTPRequestHandler.swift index 42587632..4b9d981e 100644 --- a/Sources/KituraNet/HTTP/HTTPRequestHandler.swift +++ b/Sources/KituraNet/HTTP/HTTPRequestHandler.swift @@ -27,10 +27,10 @@ internal class HTTPRequestHandler: ChannelInboundHandler { var server: HTTPServer /// The serverRequest related to this handler instance - var serverRequest: HTTPServerRequest! + var serverRequest: HTTPServerRequest? /// The serverResponse related to this handler instance - var serverResponse: HTTPServerResponse! + var serverResponse: HTTPServerResponse? /// We'd want to send an error response only once var errorResponseSent = false @@ -67,6 +67,10 @@ internal class HTTPRequestHandler: ChannelInboundHandler { serverRequest = HTTPServerRequest(ctx: ctx, requestHead: header, enableSSL: enableSSLVerfication) self.clientRequestedKeepAlive = header.isKeepAlive case .body(var buffer): + guard let serverRequest = serverRequest else { + Log.error("No ServerRequest available") + return + } if serverRequest.buffer == nil { serverRequest.buffer = BufferList(with: buffer) } else { @@ -76,9 +80,10 @@ internal class HTTPRequestHandler: ChannelInboundHandler { serverResponse = HTTPServerResponse(channel: ctx.channel, handler: self) //Make sure we use the latest delegate registered with the server DispatchQueue.global().async { + guard let serverRequest = self.serverRequest, let serverResponse = self.serverResponse else { return } let delegate = self.server.delegate ?? HTTPDummyServerDelegate() - Monitor.delegate?.started(request: self.serverRequest, response: self.serverResponse) - delegate.handle(request: self.serverRequest, response: self.serverResponse) + Monitor.delegate?.started(request: serverRequest, response: serverResponse) + delegate.handle(request: serverRequest, response: serverResponse) } } } @@ -119,7 +124,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler { do { serverResponse = HTTPServerResponse(channel: ctx.channel, handler: self) errorResponseSent = true - try serverResponse.end(with: .badRequest, message: message) + try serverResponse?.end(with: .badRequest, message: message) } catch { Log.error("Failed to send error response") } diff --git a/Sources/KituraNet/HTTP/HTTPServer.swift b/Sources/KituraNet/HTTP/HTTPServer.swift index f1ab0dd7..402d679a 100644 --- a/Sources/KituraNet/HTTP/HTTPServer.swift +++ b/Sources/KituraNet/HTTP/HTTPServer.swift @@ -57,7 +57,7 @@ public class HTTPServer : Server { public var keepAliveState: KeepAliveState = .unlimited /// The channel used to listen for new connections - var serverChannel: Channel! + var serverChannel: Channel? /// Whether or not this server allows port reuse (default: disallowed) public var allowPortReuse = false @@ -204,8 +204,9 @@ public class HTTPServer : Server { Log.info("Listening on port \(self.port!)") Log.verbose("Options for port \(self.port!): maxPendingConnections: \(maxPendingConnections), allowPortReuse: \(self.allowPortReuse)") - let queuedBlock = DispatchWorkItem(block: { - try! self.serverChannel.closeFuture.wait() + let queuedBlock = DispatchWorkItem(block: { + guard let serverChannel = self.serverChannel else { return } + try! serverChannel.closeFuture.wait() self.state = .stopped self.lifecycleListener.performStopCallbacks() }) @@ -270,7 +271,7 @@ public class HTTPServer : Server { /// Stop listening for new connections. public func stop() { - guard serverChannel != nil else { return } + guard let serverChannel = serverChannel else { return } do { try serverChannel.close().wait() } catch let error { diff --git a/Tests/KituraNetTests/RegressionTests.swift b/Tests/KituraNetTests/RegressionTests.swift index 7117d50f..f044b2e2 100644 --- a/Tests/KituraNetTests/RegressionTests.swift +++ b/Tests/KituraNetTests/RegressionTests.swift @@ -200,10 +200,10 @@ class RegressionTests: KituraNetTest { struct BadClient { let clientBootstrap: ClientBootstrap - var channel: Channel! + var channel: Channel? var connectedPort: Int { - return Int(channel.remoteAddress?.port ?? 0) + return Int(channel?.remoteAddress?.port ?? 0) } init() throws { @@ -219,7 +219,7 @@ class RegressionTests: KituraNetTest { } catch { Log.error("Failed to connect to port \(port)") } - if channel.remoteAddress != nil { + if channel?.remoteAddress != nil { expectation.fulfill() } } @@ -231,10 +231,10 @@ class RegressionTests: KituraNetTest { struct GoodClient { let clientBootstrap: ClientBootstrap - var channel: Channel! + var channel: Channel? var connectedPort: Int { - return Int(channel.remoteAddress?.port ?? 0) + return Int(channel?.remoteAddress?.port ?? 0) } init() throws { @@ -275,7 +275,7 @@ class RegressionTests: KituraNetTest { } catch { Log.error("Failed to connect to port \(port)") } - if channel.remoteAddress != nil { + if channel?.remoteAddress != nil { expectation.fulfill() } } @@ -287,8 +287,8 @@ class RegressionTests: KituraNetTest { Log.error("Failed to connect to port \(port)") } let request = HTTPRequestHead(version: HTTPVersion(major: 1, minor:1), method: .GET, uri: "#/") - _ = channel.write(NIOAny(HTTPClientRequestPart.head(request))) - _ = channel.writeAndFlush(NIOAny(HTTPClientRequestPart.end(nil))) + _ = channel?.write(NIOAny(HTTPClientRequestPart.head(request))) + _ = channel?.writeAndFlush(NIOAny(HTTPClientRequestPart.end(nil))) } mutating func makeGoodRequestFollowedByBadRequest(_ port: Int) throws { @@ -300,12 +300,12 @@ class RegressionTests: KituraNetTest { let request = HTTPRequestHead(version: HTTPVersion(major: 1, minor:1), method: .GET, uri: "/") var httpHeaders = HTTPHeaders() httpHeaders.add(name: "Connection", value: "Keep-Alive") - _ = channel.write(NIOAny(HTTPClientRequestPart.head(request))) - _ = channel.writeAndFlush(NIOAny(HTTPClientRequestPart.end(nil))) + _ = channel?.write(NIOAny(HTTPClientRequestPart.head(request))) + _ = channel?.writeAndFlush(NIOAny(HTTPClientRequestPart.end(nil))) sleep(1) //workaround for an apparent swift-nio issue let request0 = HTTPRequestHead(version: HTTPVersion(major: 1, minor:1), method: .GET, uri: "#/") - _ = channel.write(NIOAny(HTTPClientRequestPart.head(request0))) - _ = channel.writeAndFlush(NIOAny(HTTPClientRequestPart.end(nil))) + _ = channel?.write(NIOAny(HTTPClientRequestPart.head(request0))) + _ = channel?.writeAndFlush(NIOAny(HTTPClientRequestPart.end(nil))) } }