Skip to content
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

Fix normalization of some URLs #546

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Sources/Adapters/GCDWebServer/ResourceResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum WebServerResponseError: Error {

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

private var resource: Resource
Expand Down
6 changes: 5 additions & 1 deletion Sources/LCP/Content Protection/LCPDecryptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ final class LCPDecryptor {

init(license: LCPLicense?, encryptionData: [AnyURL: ReadiumShared.Encryption]) {
self.license = license
self.encryptionData = encryptionData
self.encryptionData = encryptionData.reduce(into: [:]) { result, item in
result[item.key.normalized] = item.value
}
}

func decrypt(at href: AnyURL, resource: Resource) -> Resource {
let href = href.normalized

// Checks if the resource is encrypted and whether the encryption
// schemes of the resource and the DRM license are the same.
guard let encryption = encryptionData[href], encryption.scheme == lcpScheme else {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Navigator/Audiobook/PublicationMediaLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class PublicationMediaLoader: NSObject, AVAssetResourceLoaderDelegate, Log
private func resource<T: URLConvertible>(forHREF href: T) -> (Link, Resource)? {
dispatchPrecondition(condition: .onQueue(queue))

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

let href = href.anyURL
let href = href.anyURL.normalized
var reqs: [CancellableRequest] = requests[href] ?? []
reqs.append((request, task))
requests[href] = reqs
Expand Down Expand Up @@ -196,7 +196,7 @@ extension URL {
// * readium:relative/file.mp3
// * readiumfile:///directory/local-file.mp3
// * readiumhttp(s)://domain.com/external-file.mp3
return AnyURL(string: url.string.removingPrefix(schemePrefix).removingPrefix(":"))
return AnyURL(string: url.string.removingPrefix(schemePrefix).removingPrefix(":"))?.normalized
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/Toolkit/File/DirectoryContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct DirectoryContainer: Container, Loggable {
/// `entries`.
public init(directory: FileURL, entries: Set<RelativeURL>) {
directoryURL = directory
self.entries = Set(entries.map(\.anyURL))
self.entries = Set(entries.map(\.anyURL.normalized))
}

/// Creates a ``DirectoryContainer`` at `directory` serving all its children
Expand Down
8 changes: 6 additions & 2 deletions Sources/Shared/Toolkit/ZIP/ZIPFoundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ final class ZIPFoundationContainer: Container, Loggable {
for try await entry in archive {
guard
entry.type == .file,
let url = RelativeURL(path: entry.path),
let url = RelativeURL(path: entry.path)?.normalized,
!url.path.isEmpty
else {
continue
Expand All @@ -179,14 +179,18 @@ final class ZIPFoundationContainer: Container, Loggable {
archiveFactory: ZIPFoundationArchiveFactory,
entries: [RelativeURL: Entry]
) {
let entries = entries.reduce(into: [:]) { result, item in
result[item.key.normalized] = item.value
}

self.archiveFactory = archiveFactory
entriesByPath = entries
self.entries = Set(entries.keys.map(\.anyURL))
}

subscript(url: any URLConvertible) -> (any Resource)? {
guard
let url = url.relativeURL,
let url = url.relativeURL?.normalized,
let entry = entriesByPath[url]
else {
return nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/Streamer/Parser/Audio/AudioParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public final class AudioParser: PublicationParser {
container.entries
.compactMap { url -> (AnyURL, Format)? in
guard
let format = formats[url],
let format = formats[equivalent: url],
format.conformsToAny(audioSpecifications)
else {
return nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/Streamer/Parser/Image/ImageParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public final class ImageParser: PublicationParser {
container.entries
.compactMap { url -> (AnyURL, Format)? in
guard
let format = formats[url],
let format = formats[equivalent: url],
format.conformsToAny(bitmapSpecifications)
else {
return nil
Expand Down
4 changes: 2 additions & 2 deletions Tests/SharedTests/ProxyContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ final class ProxyContainer: Container {
private let retrieve: (AnyURL) -> Resource?

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

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

subscript(url: any URLConvertible) -> (any Resource)? {
retrieve(url.anyURL)
retrieve(url.anyURL.normalized)
}
}