Skip to content

Instantly share code, notes, and snippets.

@calebkleveter
Created April 14, 2020 13:48
Show Gist options
  • Save calebkleveter/c635b6f80371726b6039ac3137354467 to your computer and use it in GitHub Desktop.
Save calebkleveter/c635b6f80371726b6039ac3137354467 to your computer and use it in GitHub Desktop.
public final class HTTPRequestBodySizeVerifyer: ChannelDuplexHandler, RemovableChannelHandler {
public let maxSize: Int
private var currentSize: Int
public init(maxSize: Int) {
self.maxSize = maxSize
self.currentSize = 0
}
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
let request = self.unwrapInboundIn(data)
switch request {
case .head, .end:
self.currentSize = 0
context.fireChannelRead(data)
case .body(var part):
self.currentSize += part.readableBytes
guard self.currentSize > self.maxSize else {
context.fireErrorCaught(Abort(.payloadTooLarge))
}
context.fireChannelRead(data)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment