-
Notifications
You must be signed in to change notification settings - Fork 133
fix: styles and scripts not injected into epub navigator #749
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
base: develop
Are you sure you want to change the base?
Changes from 5 commits
8815d6c
a207963
4156dae
94a913a
278d7f8
c317268
8d4e30b
80d8810
a902f67
d233a4a
4c6e796
e2b74bf
d7fbb71
723f56a
772d7ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ package org.readium.r2.navigator.epub | |
|
|
||
| import android.app.Application | ||
| import android.os.PatternMatcher | ||
| import android.webkit.MimeTypeMap | ||
| import android.webkit.WebResourceRequest | ||
| import android.webkit.WebResourceResponse | ||
| import androidx.webkit.WebViewAssetLoader | ||
|
|
@@ -28,9 +29,11 @@ import org.readium.r2.shared.util.data.ReadError | |
| import org.readium.r2.shared.util.data.asInputStream | ||
| import org.readium.r2.shared.util.http.HttpHeaders | ||
| import org.readium.r2.shared.util.http.HttpRange | ||
| import org.readium.r2.shared.util.mediatype.MediaType | ||
| import org.readium.r2.shared.util.resource.Resource | ||
| import org.readium.r2.shared.util.resource.StringResource | ||
| import org.readium.r2.shared.util.resource.fallback | ||
| import org.readium.r2.shared.util.toUrl | ||
|
|
||
| /** | ||
| * Serves the publication resources and application assets in the EPUB navigator web views. | ||
|
|
@@ -44,8 +47,11 @@ internal class WebViewServer( | |
| private val onResourceLoadFailed: (Url, ReadError) -> Unit, | ||
| ) { | ||
| companion object { | ||
| val publicationBaseHref = AbsoluteUrl("https://readium/publication/")!! | ||
| val assetsBaseHref = AbsoluteUrl("https://readium/assets/")!! | ||
| const val READIUM_PACKAGE_HOSTNAME = "readium_package" | ||
| const val ASSETS_HOSTNAME = "readium_assets" | ||
|
|
||
| val publicationBaseHref = AbsoluteUrl("https://$READIUM_PACKAGE_HOSTNAME/")!! | ||
| val assetsBaseHref = AbsoluteUrl("https://$ASSETS_HOSTNAME/")!! | ||
|
|
||
| fun assetUrl(path: String): Url? = | ||
| Url.fromDecodedPath(path)?.let { assetsBaseHref.resolve(it) } | ||
|
|
@@ -54,16 +60,18 @@ internal class WebViewServer( | |
| /** | ||
| * Serves the requests of the navigator web views. | ||
| * | ||
| * https://readium/publication/ serves the publication resources through its fetcher. | ||
| * https://readium/assets/ serves the application assets. | ||
| * https://readium_package/ serves the publication resources through its fetcher. | ||
| * https://readium_assets/ serves the application assets. | ||
| */ | ||
| fun shouldInterceptRequest(request: WebResourceRequest, css: ReadiumCss): WebResourceResponse? { | ||
| if (request.url.host != "readium") return null | ||
| val path = request.url.path ?: return null | ||
| val hostname = request.url.host ?: return null | ||
| val requestUrl = request.url.toUrl() ?: return null | ||
|
|
||
| return when { | ||
| path.startsWith("/publication/") -> { | ||
| val href = Url.fromDecodedPath(path.removePrefix("/publication/")) | ||
| return when (hostname) { | ||
| READIUM_PACKAGE_HOSTNAME -> { | ||
| // Request is for a packaged resource. | ||
| val href = Url.fromDecodedPath(path.removePrefix("/")) | ||
| ?: return null | ||
|
|
||
| servePublicationResource( | ||
|
|
@@ -72,10 +80,30 @@ internal class WebViewServer( | |
| css = css | ||
| ) | ||
| } | ||
| path.startsWith("/assets/") && isServedAsset(path.removePrefix("/assets/")) -> { | ||
|
|
||
| ASSETS_HOSTNAME if isServedAsset(path.removePrefix("/")) -> { | ||
| // Request is for a known asset. | ||
| assetsLoader.shouldInterceptRequest(request.url) | ||
| } | ||
| else -> null | ||
|
|
||
| ASSETS_HOSTNAME -> return null // Request is for an unknown asset. | ||
|
|
||
| else -> { | ||
| // Request is for streaming a resource, if the baseUrl an AbsoluteUrl and hostname | ||
| // is not ASSETS_HOSTNAME or READIUM_PACKAGE_HOSTNAME | ||
| val baseUrl = publication.baseUrl as? AbsoluteUrl ?: return null | ||
|
||
|
|
||
| // Look up the link in the publication to make sure we have the right resource. | ||
| val link = publicationLinkFromHref(baseUrl.resolve(requestUrl)) | ||
| ?: publicationLinkFromHref(baseUrl.relativize(requestUrl)) | ||
| ?: return null // Link not found in publication, we can't serve this resource. | ||
|
|
||
| servePublicationResource( | ||
| href = link.href.resolve(), | ||
| range = HttpHeaders(request.requestHeaders).range, | ||
| css = css | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -84,15 +112,17 @@ internal class WebViewServer( | |
| * | ||
| * If the [Resource] is an HTML document, injects the required JavaScript and CSS files. | ||
| */ | ||
| private fun servePublicationResource(href: Url, range: HttpRange?, css: ReadiumCss): WebResourceResponse { | ||
| val link = publication.linkWithHref(href) | ||
| // Query parameters must be kept as they might be relevant for the fetcher. | ||
| ?.copy(href = Href(href)) | ||
| ?: Link(href = href) | ||
| private fun servePublicationResource( | ||
| href: Url, | ||
| range: HttpRange?, | ||
| css: ReadiumCss | ||
| ): WebResourceResponse { | ||
| val link = publicationLinkFromHref(href) | ||
| // Link not found, create a Link from the href and guess the MediaType | ||
| ?: Link(href = href, mediaType = mediaTypeFromUrl(href)) | ||
|
|
||
| // Drop anchor because it is meant to be interpreted by the client. | ||
| val urlWithoutAnchor = href.removeFragment() | ||
|
|
||
| var resource = publication | ||
| .get(urlWithoutAnchor) | ||
| ?.fallback { | ||
|
|
@@ -122,17 +152,17 @@ internal class WebViewServer( | |
| "Accept-Ranges" to "bytes" | ||
| ) | ||
|
|
||
| val stream = resource.asInputStream() | ||
| if (range == null) { | ||
| return WebResourceResponse( | ||
| link.mediaType?.toString(), | ||
| null, | ||
| 200, | ||
| "OK", | ||
| headers, | ||
| resource.asInputStream() | ||
| stream | ||
| ) | ||
| } else { // Byte range request | ||
| val stream = resource.asInputStream() | ||
| val length = stream.available() | ||
| val longRange = range.toLongRange(length.toLong()) | ||
| headers["Content-Range"] = "bytes ${longRange.first}-${longRange.last}/$length" | ||
|
|
@@ -149,6 +179,23 @@ internal class WebViewServer( | |
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the [MediaType] from a [Url]. | ||
| */ | ||
| private fun mediaTypeFromUrl(href: Url): MediaType? { | ||
| val ext = MimeTypeMap.getFileExtensionFromUrl(href.normalize().toString()) ?: return null | ||
| val mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext) ?: return null | ||
|
|
||
| return MediaType.invoke(mimetype) | ||
| } | ||
|
|
||
| private fun publicationLinkFromHref(href: Url): Link? { | ||
| return publication.linkWithHref(href) | ||
| // Query parameters must be kept as they might be relevant for the fetcher. | ||
| ?.copy(href = Href(href)) | ||
| } | ||
|
|
||
| private fun errorResource(): Resource = | ||
| StringResource { | ||
| withContext(Dispatchers.IO) { | ||
|
|
@@ -169,7 +216,7 @@ internal class WebViewServer( | |
|
|
||
| private val assetsLoader = | ||
| WebViewAssetLoader.Builder() | ||
| .setDomain("readium") | ||
| .addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(application)) | ||
| .setDomain(assetsBaseHref.host!!) | ||
| .addPathHandler("/", WebViewAssetLoader.AssetsPathHandler(application)) | ||
| .build() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.