rename ctx to context (#842)

Motivation:

`ctx` was always an abbreviation was 'context` and in Swift we don't
really use abbreviations, so let's fix it.

Modifications:

- rename all instances of `ctx` to `context`

Result:

- fixes #483
This commit is contained in:
Johannes Weiss authored and GitHub committed 2019-02-25 18:20:22 +00:00
1 parent 862533d089
commit a41280919e
55 files changed
+1287 -1285

No files matched your search

+24 -24
View File
@@ -14,13 +14,13 @@
import NIO
private func writeChunk(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHandlerContext, isChunked: Bool, chunk: IOData, promise: EventLoopPromise<Void>?) {
private func writeChunk(wrapOutboundOut: (IOData) -> NIOAny, context: ChannelHandlerContext, isChunked: Bool, chunk: IOData, promise: EventLoopPromise<Void>?) {
let (mW1, mW2, mW3): (EventLoopPromise<Void>?, EventLoopPromise<Void>?, EventLoopPromise<Void>?)
switch (isChunked, promise) {
case (true, .some(let p)):
/* chunked encoding and the user's interested: we need three promises and need to cascade into the users promise */
let (w1, w2, w3) = (ctx.eventLoop.makePromise() as EventLoopPromise<Void>, ctx.eventLoop.makePromise() as EventLoopPromise<Void>, ctx.eventLoop.makePromise() as EventLoopPromise<Void>)
let (w1, w2, w3) = (context.eventLoop.makePromise() as EventLoopPromise<Void>, context.eventLoop.makePromise() as EventLoopPromise<Void>, context.eventLoop.makePromise() as EventLoopPromise<Void>)
w1.futureResult.and(w2.futureResult).and(w3.futureResult).map { (_: ((((), ()), ()))) in }.cascade(to: p)
(mW1, mW2, mW3) = (w1, w2, w3)
case (false, .some(let p)):
@@ -35,40 +35,40 @@ private func writeChunk(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHandler
/* we don't want to copy the chunk unnecessarily and therefore call write an annoyingly large number of times */
if isChunked {
var buffer = ctx.channel.allocator.buffer(capacity: 32)
var buffer = context.channel.allocator.buffer(capacity: 32)
let len = String(readableBytes, radix: 16)
buffer.writeString(len)
buffer.writeStaticString("\r\n")
ctx.write(wrapOutboundOut(.byteBuffer(buffer)), promise: mW1)
context.write(wrapOutboundOut(.byteBuffer(buffer)), promise: mW1)
ctx.write(wrapOutboundOut(chunk), promise: mW2)
context.write(wrapOutboundOut(chunk), promise: mW2)
// Just move the buffers readerIndex to only make the \r\n readable and depend on COW semantics.
buffer.moveReaderIndex(forwardBy: buffer.readableBytes - 2)
ctx.write(wrapOutboundOut(.byteBuffer(buffer)), promise: mW3)
context.write(wrapOutboundOut(.byteBuffer(buffer)), promise: mW3)
} else {
ctx.write(wrapOutboundOut(chunk), promise: mW2)
context.write(wrapOutboundOut(chunk), promise: mW2)
}
}
private func writeTrailers(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHandlerContext, isChunked: Bool, trailers: HTTPHeaders?, promise: EventLoopPromise<Void>?) {
private func writeTrailers(wrapOutboundOut: (IOData) -> NIOAny, context: ChannelHandlerContext, isChunked: Bool, trailers: HTTPHeaders?, promise: EventLoopPromise<Void>?) {
switch (isChunked, promise) {
case (true, let p):
var buffer: ByteBuffer
if let trailers = trailers {
buffer = ctx.channel.allocator.buffer(capacity: 256)
buffer = context.channel.allocator.buffer(capacity: 256)
buffer.writeStaticString("0\r\n")
buffer.write(headers: trailers) // Includes trailing CRLF.
} else {
buffer = ctx.channel.allocator.buffer(capacity: 8)
buffer = context.channel.allocator.buffer(capacity: 8)
buffer.writeStaticString("0\r\n\r\n")
}
ctx.write(wrapOutboundOut(.byteBuffer(buffer)), promise: p)
context.write(wrapOutboundOut(.byteBuffer(buffer)), promise: p)
case (false, .some(let p)):
// Not chunked so we have nothing to write. However, we don't want to satisfy this promise out-of-order
// so we issue a zero-length write down the chain.
let buf = ctx.channel.allocator.buffer(capacity: 0)
ctx.write(wrapOutboundOut(.byteBuffer(buf)), promise: p)
let buf = context.channel.allocator.buffer(capacity: 0)
context.write(wrapOutboundOut(.byteBuffer(buf)), promise: p)
case (false, .none):
break
}
@@ -77,12 +77,12 @@ private func writeTrailers(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHand
// starting about swift-5.0-DEVELOPMENT-SNAPSHOT-2019-01-20-a, this doesn't get automatically inlined, which costs
// 2 extra allocations so we need to help the optimiser out.
@inline(__always)
private func writeHead(wrapOutboundOut: (IOData) -> NIOAny, writeStartLine: (inout ByteBuffer) -> Void, ctx: ChannelHandlerContext, headers: HTTPHeaders, promise: EventLoopPromise<Void>?) {
private func writeHead(wrapOutboundOut: (IOData) -> NIOAny, writeStartLine: (inout ByteBuffer) -> Void, context: ChannelHandlerContext, headers: HTTPHeaders, promise: EventLoopPromise<Void>?) {
var buffer = ctx.channel.allocator.buffer(capacity: 256)
var buffer = context.channel.allocator.buffer(capacity: 256)
writeStartLine(&buffer)
buffer.write(headers: headers)
ctx.write(wrapOutboundOut(.byteBuffer(buffer)), promise: promise)
context.write(wrapOutboundOut(.byteBuffer(buffer)), promise: promise)
}
/// The type of framing that is used to mark the end of the body.
@@ -140,7 +140,7 @@ public final class HTTPRequestEncoder: ChannelOutboundHandler {
public init () { }
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
switch self.unwrapOutboundIn(data) {
case .head(var request):
@@ -148,11 +148,11 @@ public final class HTTPRequestEncoder: ChannelOutboundHandler {
writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in
buffer.write(request: request)
}, ctx: ctx, headers: request.headers, promise: promise)
}, context: context, headers: request.headers, promise: promise)
case .body(let bodyPart):
writeChunk(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
writeChunk(wrapOutboundOut: self.wrapOutboundOut, context: context, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
case .end(let trailers):
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, trailers: trailers, promise: promise)
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, context: context, isChunked: self.isChunked, trailers: trailers, promise: promise)
}
}
}
@@ -169,7 +169,7 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler, RemovableChannel
public init () { }
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
switch self.unwrapOutboundIn(data) {
case .head(var response):
@@ -177,11 +177,11 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler, RemovableChannel
writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in
buffer.write(response: response)
}, ctx: ctx, headers: response.headers, promise: promise)
}, context: context, headers: response.headers, promise: promise)
case .body(let bodyPart):
writeChunk(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
writeChunk(wrapOutboundOut: self.wrapOutboundOut, context: context, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
case .end(let trailers):
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, trailers: trailers, promise: promise)
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, context: context, isChunked: self.isChunked, trailers: trailers, promise: promise)
}
}
}