Skip to content

FileDownloadDelegate: pass response header through #680

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

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
Next Next commit
FileDownloadDelegate: pass response header through
Currently `FileDownloadDelegate` doesn't pass the response header through in its `Response` associated type, which is satisfied by the `Progress` inner type declaration. This makes it hard to use in the Swift Concurrency context, since it's no trivial to pass response header around from `reportHead` closure. Let's fix that by adding a new field that passes the response header in the delegate's response.

This change is additive and is not source-breaking.
  • Loading branch information
MaxDesiatov authored Apr 12, 2023
commit 08cf95f9b53e9adf28447550f2c7830de4a10f7a
6 changes: 4 additions & 2 deletions Sources/AsyncHTTPClient/FileDownloadDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import NIOPosix

/// Handles a streaming download to a given file path, allowing headers and progress to be reported.
public final class FileDownloadDelegate: HTTPClientResponseDelegate {
/// The response type for this delegate: the total count of bytes as reported by the response
/// "Content-Length" header (if available) and the count of bytes downloaded.
/// The response type for this delegate: the response header, the total count of bytes as reported
/// by the response "Content-Length" header (if available) and the count of bytes downloaded.
public struct Progress: Sendable {
public var reponseHead: HTTPResponseHead?
public var totalBytes: Int?
public var receivedBytes: Int
}
Expand Down Expand Up @@ -97,6 +98,7 @@ public final class FileDownloadDelegate: HTTPClientResponseDelegate {
task: HTTPClient.Task<Response>,
_ head: HTTPResponseHead
) -> EventLoopFuture<Void> {
self.progress.responseHead = head
self.reportHead?(head)

if let totalBytesString = head.headers.first(name: "Content-Length"),
Expand Down