@@ -16,15 +16,25 @@ import NIOCore
16
16
import NIOHTTP1
17
17
import NIOPosix
18
18
19
+ import struct Foundation. URL
20
+
19
21
/// Handles a streaming download to a given file path, allowing headers and progress to be reported.
20
22
public final class FileDownloadDelegate : HTTPClientResponseDelegate {
21
23
/// The response type for this delegate: the total count of bytes as reported by the response
22
- /// "Content-Length" header (if available), the count of bytes downloaded, and the
23
- /// response head.
24
+ /// "Content-Length" header (if available), the count of bytes downloaded, the
25
+ /// response head, and a history of requests and responses .
24
26
public struct Progress : Sendable {
25
27
public var totalBytes : Int ?
26
28
public var receivedBytes : Int
27
29
30
+ /// The history of all requests and responses in redirect order.
31
+ public var history : [ HTTPClient . RequestResponse ] = [ ]
32
+
33
+ /// The target URL (after redirects) of the response.
34
+ public var url : URL ? {
35
+ self . history. last? . request. url
36
+ }
37
+
28
38
public var head : HTTPResponseHead {
29
39
get {
30
40
assert ( self . _head != nil )
@@ -150,6 +160,10 @@ public final class FileDownloadDelegate: HTTPClientResponseDelegate {
150
160
)
151
161
}
152
162
163
+ public func didVisitURL( task: HTTPClient . Task < Progress > , _ request: HTTPClient . Request , _ head: HTTPResponseHead ) {
164
+ self . progress. history. append ( . init( request: request, responseHead: head) )
165
+ }
166
+
153
167
public func didReceiveHead(
154
168
task: HTTPClient . Task < Response > ,
155
169
_ head: HTTPResponseHead
0 commit comments