Skip to content

Commit bea610a

Browse files
committed
Fix HTTP headers not passed on caching requests
1 parent 79a2e9b commit bea610a

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Source/CachingPlayerItem.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public final class CachingPlayerItem: AVPlayerItem {
4242
private let initialScheme: String?
4343
private let saveFilePath: String
4444
private var customFileExtension: String?
45+
/// HTTPHeaderFields set in avUrlAssetOptions using AVURLAssetHTTPHeaderFieldsKey
46+
internal var urlRequestHeaders: [String: String]?
4547

4648
/// Useful for keeping relevant model associated with CachingPlayerItem instance. This is a **strong** reference, be mindful not to create a **retain cycle**.
4749
public var passOnObject: Any?
@@ -110,6 +112,10 @@ public final class CachingPlayerItem: AVPlayerItem {
110112
assert(url.pathExtension.isEmpty == false, "CachingPlayerItem error: url pathExtension empty, pass the extension in `customFileExtension` parameter")
111113
}
112114

115+
if let headers = avUrlAssetOptions?["AVURLAssetHTTPHeaderFieldsKey"] as? [String: String] {
116+
self.urlRequestHeaders = headers
117+
}
118+
113119
let asset = AVURLAsset(url: urlWithCustomScheme, options: avUrlAssetOptions)
114120
super.init(asset: asset, automaticallyLoadedAssetKeys: nil)
115121

Source/ResourceLoaderDelegate.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ final class ResourceLoaderDelegate: NSObject, AVAssetResourceLoaderDelegate, URL
105105

106106
let configuration = URLSessionConfiguration.default
107107
configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
108+
var urlRequest = URLRequest(url: url)
109+
owner?.urlRequestHeaders?.forEach { urlRequest.setValue($0.value, forHTTPHeaderField: $0.key) }
108110
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
109-
session?.dataTask(with: url).resume()
111+
session?.dataTask(with: urlRequest).resume()
110112
}
111113

112114
func invalidateAndCancelSession() {

0 commit comments

Comments
 (0)