mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
Adopt swift-format (#2794)
* Apply formatting
* Apply no block comments rule
* Apply OmitExplicitReturns
* Apple OnlyOneTrailingClosureArgument
* Apply NoAssignmentInExpressions
* Fix up DontRepeatTypeInStaticProperties lint errors
* Apply `OrderedImports`
* Apply `ReplaceForEachWithForLoop`
* format file
* Enable the formatting pipeline
* Adopt `AmbiguousTrailingClosureOverload`
* Fix license header
* Fix format check
* Fix `EndOfLineComment`
* Fix CI
* Adapt CI script to check if changes when running formatting
* Separate lint and format into to steps
* Fix format
* Adopt `UseEarlyExits`
* Revert "Adopt `UseEarlyExits`"
This reverts commit d1ac5bbe12.
This commit is contained in:
1 parent
980bd3e630
commit
c9756e1083
374 files changed
+23992
-14890
No files matched your search
@@ -13,7 +13,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
import NIOCore
|
||||
|
||||
|
||||
/// `NIOWebSocketFrameAggregator` buffers inbound fragmented `WebSocketFrame`'s and aggregates them into a single `WebSocketFrame`.
|
||||
/// It guarantees that a `WebSocketFrame` with an `opcode` of `.continuation` is never forwarded.
|
||||
/// Frames which are not fragmented are just forwarded without any processing.
|
||||
@@ -30,15 +29,14 @@ public final class NIOWebSocketFrameAggregator: ChannelInboundHandler {
|
||||
}
|
||||
public typealias InboundIn = WebSocketFrame
|
||||
public typealias InboundOut = WebSocketFrame
|
||||
|
||||
|
||||
private let minNonFinalFragmentSize: Int
|
||||
private let maxAccumulatedFrameCount: Int
|
||||
private let maxAccumulatedFrameSize: Int
|
||||
|
||||
|
||||
private var bufferedFrames: [WebSocketFrame] = []
|
||||
private var accumulatedFrameSize: Int = 0
|
||||
|
||||
|
||||
|
||||
/// Configures a `NIOWebSocketFrameAggregator`.
|
||||
/// - Parameters:
|
||||
/// - minNonFinalFragmentSize: Minimum size in bytes of a fragment which is not the last fragment of a complete frame. Used to defend against many really small payloads.
|
||||
@@ -54,7 +52,6 @@ public final class NIOWebSocketFrameAggregator: ChannelInboundHandler {
|
||||
self.maxAccumulatedFrameSize = maxAccumulatedFrameSize
|
||||
}
|
||||
|
||||
|
||||
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
||||
let frame = Self.unwrapInboundIn(data)
|
||||
do {
|
||||
@@ -64,16 +61,16 @@ public final class NIOWebSocketFrameAggregator: ChannelInboundHandler {
|
||||
throw Error.didReceiveFragmentBeforeReceivingTextOrBinaryFrame
|
||||
}
|
||||
try self.bufferFrame(frame)
|
||||
|
||||
|
||||
guard frame.fin else { break }
|
||||
// final frame received
|
||||
|
||||
|
||||
let aggregatedFrame = self.aggregateFrames(
|
||||
opcode: firstFrameOpcode,
|
||||
allocator: context.channel.allocator
|
||||
)
|
||||
self.clearBuffer()
|
||||
|
||||
|
||||
context.fireChannelRead(wrapInboundOut(aggregatedFrame))
|
||||
case .binary, .text:
|
||||
if frame.fin {
|
||||
@@ -95,7 +92,7 @@ public final class NIOWebSocketFrameAggregator: ChannelInboundHandler {
|
||||
context.fireErrorCaught(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func bufferFrame(_ frame: WebSocketFrame) throws {
|
||||
guard self.bufferedFrames.isEmpty || frame.opcode == .continuation else {
|
||||
throw Error.receivedNewFrameWithoutFinishingPrevious
|
||||
@@ -106,31 +103,31 @@ public final class NIOWebSocketFrameAggregator: ChannelInboundHandler {
|
||||
guard self.bufferedFrames.count < self.maxAccumulatedFrameCount else {
|
||||
throw Error.tooManyFragments
|
||||
}
|
||||
|
||||
|
||||
// if this is not a final frame, we will at least receive one more frame
|
||||
guard frame.fin || (self.bufferedFrames.count + 1) < self.maxAccumulatedFrameCount else {
|
||||
throw Error.tooManyFragments
|
||||
}
|
||||
|
||||
|
||||
self.bufferedFrames.append(frame)
|
||||
self.accumulatedFrameSize += frame.length
|
||||
|
||||
|
||||
guard self.accumulatedFrameSize <= self.maxAccumulatedFrameSize else {
|
||||
throw Error.accumulatedFrameSizeIsTooLarge
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func aggregateFrames(opcode: WebSocketOpcode, allocator: ByteBufferAllocator) -> WebSocketFrame {
|
||||
var dataBuffer = allocator.buffer(capacity: self.accumulatedFrameSize)
|
||||
|
||||
|
||||
for frame in self.bufferedFrames {
|
||||
var unmaskedData = frame.unmaskedData
|
||||
dataBuffer.writeBuffer(&unmaskedData)
|
||||
}
|
||||
|
||||
|
||||
return WebSocketFrame(fin: true, opcode: opcode, data: dataBuffer)
|
||||
}
|
||||
|
||||
|
||||
private func clearBuffer() {
|
||||
self.bufferedFrames.removeAll(keepingCapacity: true)
|
||||
self.accumulatedFrameSize = 0
|
||||
|
||||
Reference in new issue
Block a user