Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ All notable changes to this project will be documented in this file. Take a look

* Fixed parsing the table of contents of an EPUB 3 using NCX instead of a Navigation Document.

#### Navigator

* [swift-toolkit#61](https://github.com/readium/swift-toolkit/issues/61) Fixed serving EPUB resources when the HREF contains an anchor or query parameters.


## [2.2.0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.readium.r2.shared.extensions.addPrefix
import org.readium.r2.shared.extensions.tryOr
import org.readium.r2.shared.extensions.tryOrLog
import org.readium.r2.shared.extensions.tryOrNull
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Properties
Expand Down Expand Up @@ -57,8 +58,7 @@ class ArchiveFetcher private constructor(private val archive: Archive) : Fetcher
suspend fun entry(): ResourceTry<Archive.Entry> {
if (!::_entry.isInitialized) {
_entry = try {
val entry = archive.entry(originalLink.href.removePrefix("/"))
Try.success(entry)
Try.success(findEntry(originalLink))
} catch (e: Exception) {
Try.failure(Resource.Exception.NotFound(e))
}
Expand All @@ -67,6 +67,16 @@ class ArchiveFetcher private constructor(private val archive: Archive) : Fetcher
return _entry
}

suspend fun findEntry(link: Link): Archive.Entry {
val href = link.href.removePrefix("/")
return try {
archive.entry(href)
} catch (e: Exception) {
// Try again after removing query parameters and anchors from the href.
archive.entry(href.takeWhile { it !in "#?" })
}
}

override suspend fun link(): Link {
val entry = entry().getOrNull() ?: return originalLink
return originalLink.addProperties(entry.toLinkProperties())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/*
* Module: r2-shared-kotlin
* Developers: Quentin Gliosca
*
* Copyright (c) 2020. Readium Foundation. All rights reserved.
* Use of this source code is governed by a BSD-style license which is detailed in the
* LICENSE file present in the project repository where this source code is maintained.
* Copyright 2022 Readium Foundation. All rights reserved.
* Use of this source code is governed by the BSD-style license
* available in the top-level LICENSE file of the project.
*/

package org.readium.r2.shared.fetcher
Expand Down Expand Up @@ -172,4 +169,25 @@ class ArchiveFetcherTest {
)
}

/**
* When the HREF contains query parameters, the fetcher should first be able to remove them as
* a fallback.
*/
@Test
fun `Get resource from HREF with query parameters`() = runBlocking {
val resource = fetcher.get(Link(href = "/mimetype?query=param"))
val result = resource.readAsString().getOrNull()
assertEquals("application/epub+zip", result)
}

/**
* When the HREF contains an anchor, the fetcher should first be able to remove them as
* a fallback.
*/
@Test
fun `Get resource from HREF with anchors`() = runBlocking {
val resource = fetcher.get(Link(href = "/mimetype#anchor"))
val result = resource.readAsString().getOrNull()
assertEquals("application/epub+zip", result)
}
}