Skip to content

Commit d63f0b4

Browse files
authored
Fix normalization of some URLs (#546)
1 parent a4aa686 commit d63f0b4

File tree

8 files changed

+20
-12
lines changed

8 files changed

+20
-12
lines changed

Sources/Adapters/GCDWebServer/ResourceResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ enum WebServerResponseError: Error {
1919

2020
/// The object containing the response's ressource data.
2121
/// If the ressource to be served is too big, multiple responses will be created.
22-
class ResourceResponse: ReadiumGCDWebServerFileResponse, Loggable {
22+
class ResourceResponse: ReadiumGCDWebServerResponse, Loggable {
2323
private let bufferSize = 32 * 1024
2424

2525
private var resource: Resource

Sources/LCP/Content Protection/LCPDecryptor.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ final class LCPDecryptor {
2727

2828
init(license: LCPLicense?, encryptionData: [AnyURL: ReadiumShared.Encryption]) {
2929
self.license = license
30-
self.encryptionData = encryptionData
30+
self.encryptionData = encryptionData.reduce(into: [:]) { result, item in
31+
result[item.key.normalized] = item.value
32+
}
3133
}
3234

3335
func decrypt(at href: AnyURL, resource: Resource) -> Resource {
36+
let href = href.normalized
37+
3438
// Checks if the resource is encrypted and whether the encryption
3539
// schemes of the resource and the DRM license are the same.
3640
guard let encryption = encryptionData[href], encryption.scheme == lcpScheme else {

Sources/Navigator/Audiobook/PublicationMediaLoader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ final class PublicationMediaLoader: NSObject, AVAssetResourceLoaderDelegate, Log
5353
private func resource<T: URLConvertible>(forHREF href: T) -> (Link, Resource)? {
5454
dispatchPrecondition(condition: .onQueue(queue))
5555

56-
let href = href.anyURL
56+
let href = href.anyURL.normalized
5757
if let res = resources[equivalent: href] {
5858
return res
5959
}
@@ -79,7 +79,7 @@ final class PublicationMediaLoader: NSObject, AVAssetResourceLoaderDelegate, Log
7979
private func registerRequest<T: URLConvertible>(_ request: AVAssetResourceLoadingRequest, task: Task<Void, Never>, for href: T) {
8080
dispatchPrecondition(condition: .onQueue(queue))
8181

82-
let href = href.anyURL
82+
let href = href.anyURL.normalized
8383
var reqs: [CancellableRequest] = requests[href] ?? []
8484
reqs.append((request, task))
8585
requests[href] = reqs
@@ -196,7 +196,7 @@ extension URL {
196196
// * readium:relative/file.mp3
197197
// * readiumfile:///directory/local-file.mp3
198198
// * readiumhttp(s)://domain.com/external-file.mp3
199-
return AnyURL(string: url.string.removingPrefix(schemePrefix).removingPrefix(":"))
199+
return AnyURL(string: url.string.removingPrefix(schemePrefix).removingPrefix(":"))?.normalized
200200
}
201201
}
202202

Sources/Shared/Toolkit/File/DirectoryContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public struct DirectoryContainer: Container, Loggable {
1818
/// `entries`.
1919
public init(directory: FileURL, entries: Set<RelativeURL>) {
2020
directoryURL = directory
21-
self.entries = Set(entries.map(\.anyURL))
21+
self.entries = Set(entries.map(\.anyURL.normalized))
2222
}
2323

2424
/// Creates a ``DirectoryContainer`` at `directory` serving all its children

Sources/Shared/Toolkit/ZIP/ZIPFoundation.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ final class ZIPFoundationContainer: Container, Loggable {
154154
for try await entry in archive {
155155
guard
156156
entry.type == .file,
157-
let url = RelativeURL(path: entry.path),
157+
let url = RelativeURL(path: entry.path)?.normalized,
158158
!url.path.isEmpty
159159
else {
160160
continue
@@ -179,14 +179,18 @@ final class ZIPFoundationContainer: Container, Loggable {
179179
archiveFactory: ZIPFoundationArchiveFactory,
180180
entries: [RelativeURL: Entry]
181181
) {
182+
let entries = entries.reduce(into: [:]) { result, item in
183+
result[item.key.normalized] = item.value
184+
}
185+
182186
self.archiveFactory = archiveFactory
183187
entriesByPath = entries
184188
self.entries = Set(entries.keys.map(\.anyURL))
185189
}
186190

187191
subscript(url: any URLConvertible) -> (any Resource)? {
188192
guard
189-
let url = url.relativeURL,
193+
let url = url.relativeURL?.normalized,
190194
let entry = entriesByPath[url]
191195
else {
192196
return nil

Sources/Streamer/Parser/Audio/AudioParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public final class AudioParser: PublicationParser {
9191
container.entries
9292
.compactMap { url -> (AnyURL, Format)? in
9393
guard
94-
let format = formats[url],
94+
let format = formats[equivalent: url],
9595
format.conformsToAny(audioSpecifications)
9696
else {
9797
return nil

Sources/Streamer/Parser/Image/ImageParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public final class ImageParser: PublicationParser {
8686
container.entries
8787
.compactMap { url -> (AnyURL, Format)? in
8888
guard
89-
let format = formats[url],
89+
let format = formats[equivalent: url],
9090
format.conformsToAny(bitmapSpecifications)
9191
else {
9292
return nil

Tests/SharedTests/ProxyContainer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ final class ProxyContainer: Container {
1111
private let retrieve: (AnyURL) -> Resource?
1212

1313
init(entries: Set<AnyURL> = [], _ retrieve: @escaping (AnyURL) -> Resource?) {
14-
self.entries = entries
14+
self.entries = Set(entries.map(\.normalized))
1515
self.retrieve = retrieve
1616
}
1717

1818
let sourceURL: AbsoluteURL? = nil
1919
let entries: Set<AnyURL>
2020

2121
subscript(url: any URLConvertible) -> (any Resource)? {
22-
retrieve(url.anyURL)
22+
retrieve(url.anyURL.normalized)
2323
}
2424
}

0 commit comments

Comments
 (0)