mirror of
https://github.com/apple/swift-nio-extras
synced 2026-11-22 04:30:42 +00:00
Fix `HTTPResumableUploadHandler` silently dropping all responses after the first request on a connection. ### Motivation: `HTTPResumableUploadHandler` forwards all outbound operations through its stored `ChannelHandlerContext`, which is set only in `handlerAdded` and cleared by `detach()` whenever an upload releases the handler. When a second request arrives on the same channel — the normal case for HTTP/1.1 with keep-alive — `channelRead` calls `resetUpload`, which ends the previous upload. That synchronously detaches the handler and clears its context, and nothing restores it before the new upload is attached. Every response write for the second and subsequent requests then hits `self.context?.write(...)` as a no-op: the response is silently dropped, write promises never complete, and `read()` forwarding to the parent channel stops, so the client hangs until it times out. The existing tests never caught this because each test sends one request per `EmbeddedChannel`; resumption is only exercised across separate channels, and per-stream channels (HTTP/2) recreate the handler per request, which also masks the bug. ### Modifications: - Restore `self.context` in `resetUpload(context:)` after ending the previous upload. - Add `testNonUploadKeepAlive` and `testResumableUploadKeepAlive`, which send a second request on the same `EmbeddedChannel` for pass-through traffic and after a completed upload creation respectively. Both fail without the fix. ### Result: Connections handled by `HTTPResumableUploadHandler` can serve more than one request: responses to the second and subsequent requests on a keep-alive connection are delivered instead of being silently dropped. Fixes #318 Co-authored-by: Cory Benfield <lukasa@apple.com>
219 lines
6.8 KiB
Swift
219 lines
6.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2023-2024 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIOCore
|
|
import NIOHTTPTypes
|
|
|
|
/// A channel handler that translates resumable uploads into regular uploads, and passes through
|
|
/// other HTTP traffic.
|
|
public final class HTTPResumableUploadHandler: ChannelDuplexHandler {
|
|
public typealias InboundIn = HTTPRequestPart
|
|
public typealias InboundOut = Never
|
|
|
|
public typealias OutboundIn = Never
|
|
public typealias OutboundOut = HTTPResponsePart
|
|
|
|
var upload: HTTPResumableUpload.SendableView? = nil
|
|
let createUpload: () -> HTTPResumableUpload
|
|
var shouldReset: Bool = false
|
|
|
|
private var context: ChannelHandlerContext?
|
|
private var eventLoop: EventLoop!
|
|
|
|
/// Create an `HTTPResumableUploadHandler` within a given `HTTPResumableUploadContext`.
|
|
/// - Parameters:
|
|
/// - context: The context for this upload handler.
|
|
/// Use the same context across upload handlers, as uploads can't resume across different contexts.
|
|
/// - channelConfigurator: A closure for configuring the child HTTP server channel.
|
|
public init(
|
|
context: HTTPResumableUploadContext,
|
|
channelConfigurator: @escaping (Channel) -> Void
|
|
) {
|
|
self.createUpload = {
|
|
HTTPResumableUpload(
|
|
context: context,
|
|
channelConfigurator: channelConfigurator
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Create an `HTTPResumableUploadHandler` within a given `HTTPResumableUploadContext`.
|
|
/// - Parameters:
|
|
/// - context: The context for this upload handler.
|
|
/// Use the same context across upload handlers, as uploads can't resume across different contexts.
|
|
/// - handlers: Handlers to add to the child HTTP server channel.
|
|
public init(
|
|
context: HTTPResumableUploadContext,
|
|
handlers: [ChannelHandler] = []
|
|
) {
|
|
self.createUpload = {
|
|
HTTPResumableUpload(context: context) { channel in
|
|
if !handlers.isEmpty {
|
|
try? channel.pipeline.syncOperations.addHandlers(handlers)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func resetUpload(context: ChannelHandlerContext) {
|
|
if let existingUpload = self.upload {
|
|
existingUpload.end(handler: self, error: nil)
|
|
}
|
|
self.context = context
|
|
let upload = self.createUpload()
|
|
upload.scheduleOnEventLoop(self.eventLoop)
|
|
upload.attachUploadHandler(self.sendableView, channel: context.channel)
|
|
self.upload = upload.sendableView
|
|
self.shouldReset = false
|
|
}
|
|
|
|
public func handlerAdded(context: ChannelHandlerContext) {
|
|
self.context = context
|
|
self.eventLoop = context.eventLoop
|
|
|
|
self.resetUpload(context: context)
|
|
}
|
|
|
|
public func channelActive(context: ChannelHandlerContext) {
|
|
context.read()
|
|
}
|
|
|
|
public func channelInactive(context: ChannelHandlerContext) {
|
|
self.upload?.end(handler: self, error: nil)
|
|
}
|
|
|
|
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
if self.shouldReset {
|
|
self.resetUpload(context: context)
|
|
}
|
|
let part = self.unwrapInboundIn(data)
|
|
if case .end = part {
|
|
self.shouldReset = true
|
|
}
|
|
self.upload?.receive(handler: self, channel: context.channel, part: part)
|
|
}
|
|
|
|
public func channelReadComplete(context: ChannelHandlerContext) {
|
|
self.upload?.receiveComplete(handler: self)
|
|
}
|
|
|
|
public func channelWritabilityChanged(context: ChannelHandlerContext) {
|
|
self.upload?.writabilityChanged(handler: self)
|
|
}
|
|
|
|
public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {}
|
|
|
|
public func errorCaught(context: ChannelHandlerContext, error: Error) {
|
|
self.upload?.end(handler: self, error: error)
|
|
}
|
|
|
|
public func read(context: ChannelHandlerContext) {
|
|
if self.shouldReset {
|
|
context.read()
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension HTTPResumableUploadHandler: Sendable {}
|
|
|
|
// For `HTTPResumableUpload`.
|
|
extension HTTPResumableUploadHandler {
|
|
func write(_ part: HTTPResponsePart, promise: EventLoopPromise<Void>?) {
|
|
self.context?.write(self.wrapOutboundOut(part), promise: promise)
|
|
}
|
|
|
|
func flush() {
|
|
self.context?.flush()
|
|
}
|
|
|
|
func writeAndFlush(_ part: HTTPResponsePart, promise: EventLoopPromise<Void>?) {
|
|
self.context?.writeAndFlush(self.wrapOutboundOut(part), promise: promise)
|
|
}
|
|
|
|
func read() {
|
|
self.context?.read()
|
|
}
|
|
|
|
func close(mode: CloseMode, promise: EventLoopPromise<Void>?) {
|
|
self.context?.close(mode: mode, promise: promise)
|
|
}
|
|
|
|
func detach() {
|
|
self.context = nil
|
|
}
|
|
}
|
|
|
|
extension HTTPResumableUploadHandler {
|
|
var sendableView: SendableView {
|
|
SendableView(self)
|
|
}
|
|
|
|
struct SendableView: Sendable {
|
|
private let handler: NIOLoopBound<HTTPResumableUploadHandler>
|
|
let id: ObjectIdentifier
|
|
|
|
fileprivate init(_ handler: HTTPResumableUploadHandler) {
|
|
self.handler = NIOLoopBound(handler, eventLoop: handler.eventLoop)
|
|
self.id = ObjectIdentifier(handler)
|
|
}
|
|
|
|
func withHandler(_ body: @Sendable @escaping (HTTPResumableUploadHandler) -> Void) {
|
|
if self.handler.eventLoop.inEventLoop {
|
|
body(self.handler.value)
|
|
} else {
|
|
self.handler.eventLoop.execute {
|
|
body(self.handler.value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func write(_ part: HTTPResponsePart, promise: EventLoopPromise<Void>?) {
|
|
self.withHandler {
|
|
$0.context?.write($0.wrapOutboundOut(part), promise: promise)
|
|
}
|
|
}
|
|
|
|
func flush() {
|
|
self.withHandler {
|
|
$0.context?.flush()
|
|
}
|
|
}
|
|
|
|
func writeAndFlush(_ part: HTTPResponsePart, promise: EventLoopPromise<Void>?) {
|
|
self.withHandler {
|
|
$0.context?.writeAndFlush($0.wrapOutboundOut(part), promise: promise)
|
|
}
|
|
}
|
|
|
|
func read() {
|
|
self.withHandler {
|
|
$0.context?.read()
|
|
}
|
|
}
|
|
|
|
func close(mode: CloseMode, promise: EventLoopPromise<Void>?) {
|
|
self.withHandler {
|
|
$0.context?.close(mode: mode, promise: promise)
|
|
}
|
|
}
|
|
|
|
func detach() {
|
|
self.withHandler {
|
|
$0.context = nil
|
|
}
|
|
}
|
|
}
|
|
}
|