Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Fix broken server if a publication file contains special characters #99

Merged
merged 2 commits into from
May 7, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ package org.readium.r2.streamer.server
/**
* Created by aferditamuriqi on 10/3/17.
*/
@Deprecated("Use Publication::localBaseUrlOf() instead")
const val BASE_URL = "http://127.0.0.1"
20 changes: 13 additions & 7 deletions r2-streamer/src/main/java/org/readium/r2/streamer/server/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import timber.log.Timber
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.net.URI
import java.net.URL
import java.net.URLDecoder
import java.net.URLEncoder
import java.util.*

class Server(port: Int) : AbstractServer(port)
Expand Down Expand Up @@ -171,20 +174,23 @@ abstract class AbstractServer(private var port: Int) : RouterNanoHTTPD("127.0.0.
}
}


fun addEpub(publication: Publication, container: Container, fileName: String, userPropertiesPath: String?) {
val baseUrl = URL(Publication.localBaseUrlOf(filename = fileName, port = port))
val fetcher = Fetcher(publication, container, userPropertiesPath, customResources)

// addLinks(publication, fileName)
publication.setSelfLink(URL(baseUrl, "manifest.json").toString())

publication.setSelfLink("$BASE_URL:$port/$fileName/manifest.json")
// NanoHTTPD expects percent-decoded routes.
val basePath =
try { URLDecoder.decode(baseUrl.path, "UTF-8") }
catch (e: Exception) { baseUrl.path }

if (containsMediaOverlay) {
addRoute(fileName + MEDIA_OVERLAY_HANDLE, MediaOverlayHandler::class.java, fetcher)
addRoute(basePath + MEDIA_OVERLAY_HANDLE, MediaOverlayHandler::class.java, fetcher)
}
addRoute(fileName + JSON_MANIFEST_HANDLE, ManifestHandler::class.java, fetcher)
addRoute(fileName + MANIFEST_HANDLE, ManifestHandler::class.java, fetcher)
addRoute(fileName + MANIFEST_ITEM_HANDLE, ResourceHandler::class.java, fetcher)
addRoute(basePath + JSON_MANIFEST_HANDLE, ManifestHandler::class.java, fetcher)
addRoute(basePath + MANIFEST_HANDLE, ManifestHandler::class.java, fetcher)
addRoute(basePath + MANIFEST_ITEM_HANDLE, ResourceHandler::class.java, fetcher)
addRoute(JS_HANDLE, JSHandler::class.java, resources)
addRoute(CSS_HANDLE, CSSHandler::class.java, resources)
addRoute(FONT_HANDLE, FontHandler::class.java, fonts)
Expand Down