Created
April 14, 2020 13:48
-
-
Save calebkleveter/c635b6f80371726b6039ac3137354467 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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