Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift nio 2 dependencies #31

Merged
merged 2 commits into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CITests/run
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

swiftLintVersion=0.33.0
swiftLintVersion=0.36.0

USE_SWIFT_LINT=$1

Expand Down
40 changes: 11 additions & 29 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,45 @@
"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
}
},
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "eba9b323b5ba542c119ff17382a4ce737bcdc0b8",
"version": "0.0.0"
"revision": "e8aabbe95db22e064ad42f1a4a9f8982664c70ed",
"version": "1.1.1"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd",
"version": "1.14.1"
"revision": "8066b0f581604e3711979307a4377457e2b0f007",
"version": "2.9.0"
}
},
{
"package": "swift-nio-extras",
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
"state": {
"branch": null,
"revision": "0dbd54199d06c80a35b441c39ac2ef1b78778740",
"version": "0.1.3"
"revision": "ed97628fa310c314c4a5cd8038445054b2991f07",
"version": "1.3.1"
}
},
{
"package": "swift-nio-ssl",
"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"
}
}
]
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
jaechoi2 marked this conversation as resolved.
Show resolved Hide resolved
.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(
Expand Down
40 changes: 20 additions & 20 deletions Sources/SmokeHTTP1/HTTP1ChannelInboundHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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.")
Expand All @@ -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)

Expand All @@ -135,7 +135,7 @@ class HTTP1ChannelInboundHandler: ChannelInboundHandler {
let responseHandler = StandardHTTP1ResponseHandler(
requestHead: requestHead,
keepAliveStatus: keepAliveStatus,
context: ctx,
context: context,
wrapOutboundOut: wrapOutboundOut)

let currentHandler = handler
Expand All @@ -150,49 +150,49 @@ 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:
switch self.state {
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)
}
}
}
10 changes: 5 additions & 5 deletions Sources/SmokeHTTP1/SmokeHTTP1Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand All @@ -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()

Expand Down
22 changes: 12 additions & 10 deletions Sources/SmokeHTTP1/StandardHTTP1ResponseHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ struct StandardHTTP1ResponseHandler: HTTP1ResponseHandler {

private func handleComplete(status: HTTPResponseStatus,
responseComponents: HTTP1ServerResponseComponents) -> Int {
let ctx = context
var headers = HTTPHeaders()

let buffer: ByteBuffer?
Expand All @@ -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
Expand All @@ -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<Void>? = self.keepAliveStatus.state ? nil : ctx.eventLoop.newPromise()
let promise: EventLoopPromise<Void>? = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,90 +152,6 @@ public struct StandardSmokeHTTP1HandlerSelector<ContextType, DefaultOperationDel
}
}

extension HTTPMethod {
/// Returns the value of this HTTP method
var rawValue: String {
switch self {
case .GET:
return "GET"
case .PUT:
return "PUT"
case .ACL:
return "ACL"
case .HEAD:
return "HEAD"
case .POST:
return "POST"
case .COPY:
return "COPY"
case .LOCK:
return "LOCK"
case .MOVE:
return "MOVE"
case .BIND:
return "BIND"
case .LINK:
return "LINK"
case .PATCH:
return "PATCH"
case .TRACE:
return "TRACE"
case .MKCOL:
return "MKCOL"
case .MERGE:
return "MERGE"
case .PURGE:
return "PURGE"
case .NOTIFY:
return "NOTIFY"
case .SEARCH:
return "SEARCH"
case .UNLOCK:
return "UNLOCK"
case .REBIND:
return "REBIND"
case .UNBIND:
return "UNBIND"
case .REPORT:
return "REPORT"
case .DELETE:
return "DELETE"
case .UNLINK:
return "UNLINK"
case .CONNECT:
return "CONNECT"
case .MSEARCH:
return "MSEARCH"
case .OPTIONS:
return "OPTIONS"
case .PROPFIND:
return "PROPFIND"
case .CHECKOUT:
return "CHECKOUT"
case .PROPPATCH:
return "PROPPATCH"
case .SUBSCRIBE:
return "SUBSCRIBE"
case .MKCALENDAR:
return "MKCALENDAR"
case .MKACTIVITY:
return "MKACTIVITY"
case .UNSUBSCRIBE:
return "UNSUBSCRIBE"
case .RAW(let value):
return value
}
}
}

extension HTTPMethod: Hashable {
#if swift(>=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

}