* removed trailing whitespace (#52)

This commit is contained in:
adamnemecek authored and Norman Maurer committed 2018-03-02 15:40:52 +09:00
1 parent 85131d6235
commit 0bbed6d72f
61 files changed
+821 -821

No files matched your search

+19 -19
View File
@@ -25,7 +25,7 @@ private struct HTTPParserState {
var readerIndexAdjustment = 0
// This is set before http_parser_execute(...) is called and set to nil again after it finish
var baseAddress: UnsafePointer<UInt8>?
enum DataAwaitingState {
case messageBegin
case status
@@ -52,7 +52,7 @@ private struct HTTPParserState {
self.slice = nil
return string
}
mutating func complete(state: DataAwaitingState) {
switch state {
case .messageBegin:
@@ -232,13 +232,13 @@ public final class HTTPResponseDecoder: HTTPDecoder<HTTPClientResponsePart>, Cha
public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
public typealias InboundIn = ByteBuffer
public typealias InboundOut = HTTPMessageT
private var parser = http_parser()
private var settings = http_parser_settings()
fileprivate var pendingCallouts: [() -> Void] = []
fileprivate var state = HTTPParserState()
fileprivate init(type: HTTPMessageT.Type) {
/* this is a private init, the public versions only allow HTTPClientResponsePart and HTTPServerRequestPart */
assert(HTTPMessageT.self == HTTPClientResponsePart.self || HTTPMessageT.self == HTTPServerRequestPart.self)
@@ -248,7 +248,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
///
/// Naturally, in the base case this returns nil, as servers never issue requests!
fileprivate func popRequestMethod() -> HTTPMethod? { return nil }
private func newRequestHead(_ parser: UnsafeMutablePointer<http_parser>!) -> HTTPRequestHead {
let method = HTTPMethod.from(httpParserMethod: http_method(rawValue: parser.pointee.method))
let version = HTTPVersion(major: parser.pointee.http_major, minor: parser.pointee.http_minor)
@@ -256,7 +256,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
state.currentHeaders = nil
return request
}
private func newResponseHead(_ parser: UnsafeMutablePointer<http_parser>!) -> HTTPResponseHead {
let status = HTTPResponseStatus.from(parser.pointee.status_code, state.currentStatus!)
let version = HTTPVersion(major: parser.pointee.http_major, minor: parser.pointee.http_minor)
@@ -264,7 +264,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
state.currentHeaders = nil
return response
}
public func decoderAdded(ctx: ChannelHandlerContext) {
if HTTPMessageT.self == HTTPServerRequestPart.self {
c_nio_http_parser_init(&parser, HTTP_REQUEST)
@@ -326,10 +326,10 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
let ctx = evacuateChannelHandlerContext(parser)
let handler = evacuateHTTPDecoder(parser)
assert(handler.state.dataAwaitingState == .body)
// Calculate the index of the data in the cumulationBuffer so we can slice out the ByteBuffer without doing any memory copy
let index = handler.state.calculateIndex(data: data!, length: len)
let slice = handler.state.cumulationBuffer!.getSlice(at: index, length: len)!
handler.pendingCallouts.append {
switch handler {
@@ -341,7 +341,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
fatalError("the impossible happened: handler neither a HTTPRequestDecoder nor a HTTPResponseDecoder which should be impossible")
}
}
return 0
}
@@ -373,7 +373,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
}
return 0
}
settings.on_url = { parser, data, len in
let handler = evacuateHTTPDecoder(parser)
assert(handler is HTTPRequestDecoder)
@@ -410,7 +410,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
public func decoderRemoved(ctx: ChannelHandlerContext) {
// Remove the stored reference to ChannelHandlerContext
parser.data = UnsafeMutableRawPointer(bitPattern: 0x0000deadbeef0000)
// Set the callbacks to nil as we dont need these anymore
settings.on_body = nil
settings.on_chunk_complete = nil
@@ -422,7 +422,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
settings.on_header_value = nil
settings.on_message_begin = nil
}
public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
if let slice = state.slice {
// If we stored a slice before we need to ensure we move the readerIndex so we don't try to parse the data again. We
@@ -430,16 +430,16 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
state.slice = (buffer.readerIndex, slice.length)
buffer.moveReaderIndex(forwardBy: state.readerIndexAdjustment)
}
let result = try buffer.withVeryUnsafeBytes { (pointer) -> size_t in
state.baseAddress = pointer.baseAddress!.assumingMemoryBound(to: UInt8.self)
let result = state.baseAddress!.withMemoryRebound(to: Int8.self, capacity: pointer.count, { p in
c_nio_http_parser_execute(&parser, &settings, p.advanced(by: buffer.readerIndex), buffer.readableBytes)
})
state.baseAddress = nil
let errno = parser.http_errno
if errno != 0 {
throw HTTPParserError.httpError(fromCHTTPParserErrno: http_errno(rawValue: errno))!
@@ -462,7 +462,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
return .continue
}
}
public func channelReadComplete(ctx: ChannelHandlerContext) {
/* call all the callbacks generated while parsing */
let pending = self.pendingCallouts
+11 -11
View File
@@ -16,7 +16,7 @@ import NIO
private func writeChunk(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHandlerContext, isChunked: Bool, chunk: IOData, promise: EventLoopPromise<Void>?) {
let (mW1, mW2, mW3): (EventLoopPromise<()>?, EventLoopPromise<()>?, EventLoopPromise<()>?)
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 */
@@ -30,9 +30,9 @@ private func writeChunk(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHandler
/* user isn't interested, let's not bother even allocating promises */
(mW1, mW2, mW3) = (nil, nil, nil)
}
let readableBytes = chunk.readableBytes
/* 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)
@@ -40,9 +40,9 @@ private func writeChunk(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHandler
buffer.write(string: len)
buffer.write(staticString: "\r\n")
ctx.write(wrapOutboundOut(.byteBuffer(buffer)), promise: mW1)
ctx.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)
@@ -75,7 +75,7 @@ private func writeTrailers(wrapOutboundOut: (IOData) -> NIOAny, ctx: ChannelHand
}
private func writeHead(wrapOutboundOut: (IOData) -> NIOAny, writeStartLine: (inout ByteBuffer) -> Void, ctx: ChannelHandlerContext, headers: HTTPHeaders, promise: EventLoopPromise<Void>?) {
var buffer = ctx.channel.allocator.buffer(capacity: 256)
writeStartLine(&buffer)
headers.write(buffer: &buffer)
@@ -115,18 +115,18 @@ private func sanitizeTransportHeaders(hasBody: HTTPMethod.HasBody, headers: inou
public final class HTTPRequestEncoder: ChannelOutboundHandler {
public typealias OutboundIn = HTTPClientRequestPart
public typealias OutboundOut = IOData
private var isChunked = false
public init () { }
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
switch self.unwrapOutboundIn(data) {
case .head(var request):
sanitizeTransportHeaders(hasBody: request.method.hasRequestBody, headers: &request.headers, version: request.version)
self.isChunked = isChunkedPart(request.headers)
writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in
request.method.write(buffer: &buffer)
buffer.write(staticString: " ")
@@ -159,7 +159,7 @@ public final class HTTPResponseEncoder: ChannelOutboundHandler {
switch self.unwrapOutboundIn(data) {
case .head(var response):
sanitizeTransportHeaders(hasBody: response.status.mayHaveResponseBody ? .yes : .no, headers: &response.headers, version: response.version)
self.isChunked = isChunkedPart(response.headers)
writeHead(wrapOutboundOut: self.wrapOutboundOut, writeStartLine: { buffer in
response.version.write(buffer: &buffer)
@@ -184,7 +184,7 @@ public final class HTTPResponseCompressor: ChannnelDuplexHandler {
// the client has expressed a preference.
return .gzip
}
return nil
}
+2 -2
View File
@@ -139,7 +139,7 @@ public struct HTTPResponseHead: Equatable {
self.status = status
self.headers = headers
}
public static func ==(lhs: HTTPResponseHead, rhs: HTTPResponseHead) -> Bool {
return lhs.status == rhs.status && lhs.version == rhs.version && lhs.headers == rhs.headers
}
@@ -388,7 +388,7 @@ public enum HTTPMethod: Equatable {
case no
case unlikely
}
public static func ==(lhs: HTTPMethod, rhs: HTTPMethod) -> Bool {
switch (lhs, rhs){
case (.GET, .GET):