mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-27 05:04:23 +00:00
ContentLengthVerifier Update (#369)
Motivation: Currently http2 ignores content length when handling a response to a HEAD request and handling a 304 response. Modifications: Changing ContentLengthVerifier and any functions called --------- Co-authored-by: George Barnett <gbarnett@apple.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
10 files changed
+294
-103
No files matched your search
@@ -25,11 +25,23 @@ final class HTTP1TestServer: ChannelInboundHandler {
|
||||
public typealias InboundIn = HTTPServerRequestPart
|
||||
public typealias OutboundOut = HTTPServerResponsePart
|
||||
|
||||
private var head: HTTPRequestHead? = nil
|
||||
|
||||
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
||||
guard case .end = self.unwrapInboundIn(data) else {
|
||||
switch self.unwrapInboundIn(data) {
|
||||
case .head(let head):
|
||||
self.head = head
|
||||
return
|
||||
case .body:
|
||||
return
|
||||
case .end:
|
||||
// Deliberate fallthrough
|
||||
()
|
||||
}
|
||||
|
||||
let requestHead = self.head!
|
||||
self.head = nil
|
||||
|
||||
// Insert an event loop tick here. This more accurately represents real workloads in SwiftNIO, which will not
|
||||
// re-entrantly write their response frames.
|
||||
context.eventLoop.execute {
|
||||
@@ -39,9 +51,12 @@ final class HTTP1TestServer: ChannelInboundHandler {
|
||||
headers.add(name: "x-stream-id", value: String(Int(streamID)))
|
||||
context.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.head(HTTPResponseHead(version: .init(major: 2, minor: 0), status: .ok, headers: headers))), promise: nil)
|
||||
|
||||
var buffer = context.channel.allocator.buffer(capacity: 12)
|
||||
buffer.writeStaticString("hello")
|
||||
context.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil)
|
||||
if requestHead.method != .HEAD {
|
||||
var buffer = context.channel.allocator.buffer(capacity: 12)
|
||||
buffer.writeStaticString("hello")
|
||||
context.channel.write(self.wrapOutboundOut(HTTPServerResponsePart.body(.byteBuffer(buffer))), promise: nil)
|
||||
}
|
||||
|
||||
return context.channel.writeAndFlush(self.wrapOutboundOut(HTTPServerResponsePart.end(nil)))
|
||||
}.whenComplete { _ in
|
||||
context.close(promise: nil)
|
||||
|
||||
Reference in new issue
Block a user