@@ -16,15 +16,25 @@ import NIOCore
1616import NIOHTTP1
1717import NIOPosix
1818
19+ import struct Foundation. URL
20+
1921/// Handles a streaming download to a given file path, allowing headers and progress to be reported.
2022public final class FileDownloadDelegate : HTTPClientResponseDelegate {
2123 /// 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 .
2426 public struct Progress : Sendable {
2527 public var totalBytes : Int ?
2628 public var receivedBytes : Int
2729
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+
2838 public var head : HTTPResponseHead {
2939 get {
3040 assert ( self . _head != nil )
@@ -150,6 +160,10 @@ public final class FileDownloadDelegate: HTTPClientResponseDelegate {
150160 )
151161 }
152162
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+
153167 public func didReceiveHead(
154168 task: HTTPClient . Task < Response > ,
155169 _ head: HTTPResponseHead
0 commit comments