Skip to content

Propagate HTTPClient.Task<Response> failures to subsequent redirect tasks #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Sources/AsyncHTTPClient/HTTPHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ internal struct RedirectHandler<ResponseType> {
status: HTTPResponseStatus,
to redirectURL: URL,
promise: EventLoopPromise<ResponseType>
) {
) -> HTTPClient.Task<ResponseType>? {
do {
var redirectState = self.redirectState
try redirectState.redirect(to: redirectURL.absoluteString)
Expand All @@ -976,13 +976,19 @@ internal struct RedirectHandler<ResponseType> {
headers: headers,
body: body
)
self.execute(newRequest, redirectState).futureResult.whenComplete { result in

let newTask = self.execute(newRequest, redirectState)

newTask.futureResult.whenComplete { result in
promise.futureResult.eventLoop.execute {
promise.completeWith(result)
}
}

return newTask
} catch {
promise.fail(error)
return nil
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions Sources/AsyncHTTPClient/RequestBag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate> {
// the consume body part stack depth is synchronized on the task event loop.
private var consumeBodyPartStackDepth: Int

// if a redirect occurs, we store the task for it so we can propagate cancellation
private var redirectTask: HTTPClient.Task<Delegate.Response>? = nil

// MARK: HTTPClientTask properties

var logger: Logger {
Expand Down Expand Up @@ -234,7 +237,7 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate> {
executor.demandResponseBodyStream(self)

case .redirect(let executor, let handler, let head, let newURL):
handler.redirect(status: head.status, to: newURL, promise: self.task.promise)
self.redirectTask = handler.redirect(status: head.status, to: newURL, promise: self.task.promise)
executor.cancelRequest(self)

case .forwardResponseHead(let head):
Expand All @@ -258,7 +261,7 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate> {
executor.demandResponseBodyStream(self)

case .redirect(let executor, let handler, let head, let newURL):
handler.redirect(status: head.status, to: newURL, promise: self.task.promise)
self.redirectTask = handler.redirect(status: head.status, to: newURL, promise: self.task.promise)
executor.cancelRequest(self)

case .forwardResponsePart(let part):
Expand Down Expand Up @@ -294,7 +297,7 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate> {
}

case .redirect(let handler, let head, let newURL):
handler.redirect(status: head.status, to: newURL, promise: self.task.promise)
self.redirectTask = handler.redirect(status: head.status, to: newURL, promise: self.task.promise)
}
}

Expand Down Expand Up @@ -358,6 +361,8 @@ final class RequestBag<Delegate: HTTPClientResponseDelegate> {
let action = self.state.fail(error)

self.executeFailAction0(action)

self.redirectTask?.fail(reason: error)
}

private func executeFailAction0(_ action: RequestBag<Delegate>.StateMachine.FailAction) {
Expand Down
64 changes: 64 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4132,6 +4132,70 @@ final class HTTPClientTests: XCTestCaseHTTPClientTestsBaseClass {
XCTAssertNoThrow(try client.execute(request: request).wait())
}

func testCancelingRequestAfterRedirect() throws {
let request = try Request(
url: self.defaultHTTPBinURLPrefix + "redirect/target",
method: .GET,
headers: ["X-Target-Redirect-URL": self.defaultHTTPBinURLPrefix + "wait"],
body: nil
)

class CancelAfterRedirect: HTTPClientResponseDelegate {
init() {}
func didFinishRequest(task: AsyncHTTPClient.HTTPClient.Task<Void>) throws {}
}

let task = defaultClient.execute(
request: request,
delegate: CancelAfterRedirect(),
deadline: .now() + .seconds(1)
)

// there is currently no HTTPClientResponseDelegate method to ensure the redirect occurs before we cancel, so we just sleep for 500ms
Thread.sleep(forTimeInterval: 0.5)

task.cancel()

XCTAssertThrowsError(try task.wait()) { error in
guard case let error = error as? HTTPClientError, error == .cancelled else {
return XCTFail("Should fail with cancelled")
}
}
}

func testFailingRequestAfterRedirect() throws {
let request = try Request(
url: self.defaultHTTPBinURLPrefix + "redirect/target",
method: .GET,
headers: ["X-Target-Redirect-URL": self.defaultHTTPBinURLPrefix + "wait"],
body: nil
)

class FailAfterRedirect: HTTPClientResponseDelegate {
init() {}
func didFinishRequest(task: AsyncHTTPClient.HTTPClient.Task<Void>) throws {}
}

let task = defaultClient.execute(
request: request,
delegate: FailAfterRedirect(),
deadline: .now() + .seconds(1)
)

// there is currently no HTTPClientResponseDelegate method to ensure the redirect occurs before we fail, so we just sleep for 500ms
Thread.sleep(forTimeInterval: 0.5)

struct TestError: Error {}

task.fail(reason: TestError())

XCTAssertThrowsError(try task.wait()) { error in
guard error is TestError else {
return XCTFail("Should fail with TestError")
}
}
}

func testCancelingHTTP1RequestAfterHeaderSend() throws {
var request = try HTTPClient.Request(url: self.defaultHTTPBin.baseURL + "/wait", method: .POST)
// non-empty body is important
Expand Down