Skip to content

Commit

Permalink
Replace implicitly unwrapped optionals by optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
nethraravindran committed Sep 25, 2018
1 parent c815224 commit 3f0afd0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 26 deletions.
10 changes: 6 additions & 4 deletions Sources/KituraNet/ClientRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)")
Expand All @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/KituraNet/ClientResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 10 additions & 5 deletions Sources/KituraNet/HTTP/HTTPRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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")
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/KituraNet/HTTP/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions Tests/KituraNetTests/RegressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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 {
Expand All @@ -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)))
}

}
Expand Down

0 comments on commit 3f0afd0

Please sign in to comment.