diff --git a/CITests/run b/CITests/run index 7abd797..945640a 100755 --- a/CITests/run +++ b/CITests/run @@ -1,7 +1,7 @@ #!/bin/bash set -e -swiftLintVersion=0.33.0 +swiftLintVersion=0.36.0 USE_SWIFT_LINT=$1 diff --git a/Package.resolved b/Package.resolved index 1340294..5d35d01 100644 --- a/Package.resolved +++ b/Package.resolved @@ -14,9 +14,9 @@ "package": "SmokeHTTP", "repositoryURL": "https://github.com/amzn/smoke-http.git", "state": { - "branch": null, - "revision": "705553c6f59d15f8b9fe2fd458a0ad3755ef9bc4", - "version": "1.0.1" + "branch": "master", + "revision": "f4b5adf4e108d94763d8db364b3a18a322f0659e", + "version": null } }, { @@ -24,8 +24,8 @@ "repositoryURL": "https://github.com/apple/swift-log.git", "state": { "branch": null, - "revision": "eba9b323b5ba542c119ff17382a4ce737bcdc0b8", - "version": "0.0.0" + "revision": "e8aabbe95db22e064ad42f1a4a9f8982664c70ed", + "version": "1.1.1" } }, { @@ -33,8 +33,8 @@ "repositoryURL": "https://github.com/apple/swift-nio.git", "state": { "branch": null, - "revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd", - "version": "1.14.1" + "revision": "8066b0f581604e3711979307a4377457e2b0f007", + "version": "2.9.0" } }, { @@ -42,8 +42,8 @@ "repositoryURL": "https://github.com/apple/swift-nio-extras.git", "state": { "branch": null, - "revision": "0dbd54199d06c80a35b441c39ac2ef1b78778740", - "version": "0.1.3" + "revision": "ed97628fa310c314c4a5cd8038445054b2991f07", + "version": "1.3.1" } }, { @@ -51,26 +51,8 @@ "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", "state": { "branch": null, - "revision": "0f3999f3e3c359cc74480c292644c3419e44a12f", - "version": "1.4.0" - } - }, - { - "package": "swift-nio-ssl-support", - "repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git", - "state": { - "branch": null, - "revision": "c02eec4e0e6d351cd092938cf44195a8e669f555", - "version": "1.0.0" - } - }, - { - "package": "swift-nio-zlib-support", - "repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git", - "state": { - "branch": null, - "revision": "37760e9a52030bb9011972c5213c3350fa9d41fd", - "version": "1.0.0" + "revision": "e5c1af45ac934ac0a6117b2927a51d845cf4f705", + "version": "2.4.3" } } ] diff --git a/Package.swift b/Package.swift index 8591d7e..3558d31 100644 --- a/Package.swift +++ b/Package.swift @@ -30,9 +30,9 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/IBM-Swift/LoggerAPI.git", .upToNextMajor(from: "1.0.0")), - .package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"), - .package(url: "https://github.com/apple/swift-nio-extras.git", from: "0.1.3"), - .package(url: "https://github.com/amzn/smoke-http.git", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), + .package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.0.0"), + .package(url: "https://github.com/amzn/smoke-http.git", .branch("master")), ], targets: [ .target( diff --git a/Sources/SmokeHTTP1/HTTP1ChannelInboundHandler.swift b/Sources/SmokeHTTP1/HTTP1ChannelInboundHandler.swift index 53d2ffe..83187ad 100644 --- a/Sources/SmokeHTTP1/HTTP1ChannelInboundHandler.swift +++ b/Sources/SmokeHTTP1/HTTP1ChannelInboundHandler.swift @@ -78,7 +78,7 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { /** Function called when the inbound channel receives data. */ - func channelRead(ctx: ChannelHandlerContext, data: NIOAny) { + func channelRead(context: ChannelHandlerContext, data: NIOAny) { let requestPart = self.unwrapInboundIn(data) switch requestPart { @@ -106,7 +106,7 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { Log.verbose("Request end received.") // this signals that the head and all possible body parts have been received self.state.requestComplete() - handleCompleteRequest(context: ctx, bodyData: partialBody) + handleCompleteRequest(context: context, bodyData: partialBody) reset() } } @@ -115,7 +115,7 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { Is called when the request has been completed received and can be passed to the request hander. */ - func handleCompleteRequest(context ctx: ChannelHandlerContext, bodyData: Data?) { + func handleCompleteRequest(context: ChannelHandlerContext, bodyData: Data?) { self.state.responseComplete() Log.verbose("Handling request body with \(bodyData?.count ?? 0) size.") @@ -124,7 +124,7 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { guard let requestHead = requestHead else { Log.error("Unable to complete Http request as the head was not received") - handleResponseAsError(ctx: ctx, + handleResponseAsError(context: context, responseString: "Missing request head.", status: .badRequest) @@ -135,7 +135,7 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { let responseHandler = StandardHTTP1ResponseHandler( requestHead: requestHead, keepAliveStatus: keepAliveStatus, - context: ctx, + context: context, wrapOutboundOut: wrapOutboundOut) let currentHandler = handler @@ -150,34 +150,34 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { /** Called when reading from the channel is completed. */ - func channelReadComplete(ctx: ChannelHandlerContext) { - ctx.flush() + func channelReadComplete(context: ChannelHandlerContext) { + context.flush() } /** Writes a error to the response and closes the channel. */ - func handleResponseAsError(ctx: ChannelHandlerContext, + func handleResponseAsError(context: ChannelHandlerContext, responseString: String, status: HTTPResponseStatus) { var headers = HTTPHeaders() - var buffer = ctx.channel.allocator.buffer(capacity: responseString.utf8.count) - buffer.set(string: responseString, at: 0) + var buffer = context.channel.allocator.buffer(capacity: responseString.utf8.count) + buffer.setString(responseString, at: 0) headers.add(name: HTTP1Headers.contentLength, value: "\(responseString.utf8.count)") - ctx.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: requestHead!.version, - status: status, - headers: headers))), promise: nil) - ctx.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) - ctx.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil)), - promise: nil) - ctx.close(promise: nil) + context.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: requestHead!.version, + status: status, + headers: headers))), promise: nil) + context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) + context.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil)), + promise: nil) + context.close(promise: nil) } /** Called when an inbound event occurs. */ - func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) { switch event { // if the remote peer half-closed the channel. case let evt as ChannelEvent where evt == ChannelEvent.inputClosed: @@ -185,14 +185,14 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler { case .idle, .waitingForRequestBody: // not waiting on anything else, channel can be closed // immediately - ctx.close(promise: nil) + context.close(promise: nil) case .sendingResponse: // waiting on sending the response, signal that the // channel should be closed after sending the response. self.keepAliveStatus.state = false } default: - ctx.fireUserInboundEventTriggered(event) + context.fireUserInboundEventTriggered(event) } } } diff --git a/Sources/SmokeHTTP1/SmokeHTTP1Server.swift b/Sources/SmokeHTTP1/SmokeHTTP1Server.swift index b249ecc..3565a8e 100644 --- a/Sources/SmokeHTTP1/SmokeHTTP1Server.swift +++ b/Sources/SmokeHTTP1/SmokeHTTP1Server.swift @@ -138,7 +138,7 @@ public class SmokeHTTP1Server { } self.quiesce = ServerQuiescingHelper(group: eventLoopGroup) - self.fullyShutdownPromise = eventLoopGroup.next().newPromise() + self.fullyShutdownPromise = eventLoopGroup.next().makePromise() self.signalSource = newSignalSource?.0 self.shutdownDispatchGroup = DispatchGroup() // enter the DispatchGroup during initialization so waiting for the @@ -183,11 +183,11 @@ public class SmokeHTTP1Server { .serverChannelOption(ChannelOptions.backlog, value: 256) .serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) .serverChannelInitializer { [unowned self] channel in - channel.pipeline.add(handler: self.quiesce.makeServerChannelHandler(channel: channel)) + channel.pipeline.addHandler(self.quiesce.makeServerChannelHandler(channel: channel)) } .childChannelInitializer { channel in - channel.pipeline.configureHTTPServerPipeline().then { - channel.pipeline.add(handler: HTTP1ChannelInboundHandler( + channel.pipeline.configureHTTPServerPipeline().flatMap { + channel.pipeline.addHandler(HTTP1ChannelInboundHandler( handler: currentHandler, invocationStrategy: currentInvocationStrategy)) } @@ -199,7 +199,7 @@ public class SmokeHTTP1Server { channel = try bootstrap.bind(host: ServerDefaults.defaultHost, port: port).wait() - fullyShutdownPromise.futureResult.whenComplete { [unowned self] in + fullyShutdownPromise.futureResult.whenComplete { [unowned self] _ in do { let shutdownCompletionHandlers = self.updateStateOnShutdownComplete() diff --git a/Sources/SmokeHTTP1/StandardHTTP1ResponseHandler.swift b/Sources/SmokeHTTP1/StandardHTTP1ResponseHandler.swift index 9947af4..5acf4f0 100644 --- a/Sources/SmokeHTTP1/StandardHTTP1ResponseHandler.swift +++ b/Sources/SmokeHTTP1/StandardHTTP1ResponseHandler.swift @@ -72,7 +72,6 @@ struct StandardHTTP1ResponseHandler: HTTP1ResponseHandler { private func handleComplete(status: HTTPResponseStatus, responseComponents: HTTP1ServerResponseComponents) -> Int { - let ctx = context var headers = HTTPHeaders() let buffer: ByteBuffer? @@ -82,8 +81,8 @@ struct StandardHTTP1ResponseHandler: HTTP1ResponseHandler { if let body = responseComponents.body { let data = body.data // create a buffer for the body and copy the body into it - var newBuffer = ctx.channel.allocator.buffer(capacity: data.count) - newBuffer.write(bytes: data) + var newBuffer = context.channel.allocator.buffer(capacity: data.count) + newBuffer.writeBytes(data) buffer = newBuffer bodySize = data.count @@ -102,24 +101,27 @@ struct StandardHTTP1ResponseHandler: HTTP1ResponseHandler { responseComponents.additionalHeaders.forEach { header in headers.add(name: header.0, value: header.1) } - ctx.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: requestHead.version, - status: status, - headers: headers))), promise: nil) + context.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: requestHead.version, + status: status, + headers: headers))), promise: nil) // if there is a body, write it to the response if let buffer = buffer { - ctx.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) + context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil) } - let promise: EventLoopPromise? = self.keepAliveStatus.state ? nil : ctx.eventLoop.newPromise() + let promise: EventLoopPromise? = self.keepAliveStatus.state ? nil : context.eventLoop.makePromise() if let promise = promise { + let currentContext = context // if keep alive is false, close the channel when the response end // has been written - promise.futureResult.whenComplete { ctx.close(promise: nil) } + promise.futureResult.whenComplete { _ in + currentContext.close(promise: nil) + } } // write the response end and flush - ctx.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil)), + context.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil)), promise: promise) return bodySize diff --git a/Sources/SmokeOperationsHTTP1/StandardSmokeHTTP1HandlerSelector.swift b/Sources/SmokeOperationsHTTP1/StandardSmokeHTTP1HandlerSelector.swift index 8ccf512..c84f898 100644 --- a/Sources/SmokeOperationsHTTP1/StandardSmokeHTTP1HandlerSelector.swift +++ b/Sources/SmokeOperationsHTTP1/StandardSmokeHTTP1HandlerSelector.swift @@ -152,90 +152,6 @@ public struct StandardSmokeHTTP1HandlerSelector=5.0) || (swift(>=4.1.50) && !swift(>=4.2)) || (swift(>=3.5) && !swift(>=4.0)) - public func hash(into hasher: inout Hasher) { - hasher.combine(rawValue) - } - #else - public var hashValue: Int { - return rawValue.hashValue - } - #endif + }