Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0bc6628

Browse files
Add more logic to linkWithHref()
This commit is dependent on the commit with same message in r2-shared-kotlin. For more explanation, please see EncoderDecoderUnitTest.kt
1 parent aab1d7c commit 0bc6628

File tree

7 files changed

+88
-39
lines changed

7 files changed

+88
-39
lines changed

r2-streamer/src/main/java/org/readium/r2/streamer/fetcher/Fetcher.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ class Fetcher(var publication: Publication, var container: Container, private va
3030
}
3131

3232
fun data(path: String): ByteArray? {
33-
publication.resource(path) ?: throw Exception("Missing file")
3433
var data: ByteArray? = container.data(path)
3534
if (data != null)
3635
data = contentFilters?.apply(data, publication, container, path)
3736
return data
3837
}
3938

4039
fun dataStream(path: String): InputStream {
41-
publication.resource(path) ?: throw Exception("Missing file")
4240
var inputStream = container.dataInputStream(path)
4341
inputStream = contentFilters?.apply(inputStream, publication, container, path) ?: inputStream
4442
return inputStream

r2-streamer/src/main/java/org/readium/r2/streamer/server/handler/CSSHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.readium.r2.streamer.server.handler
1111

12+
import android.util.Log
1213
import org.nanohttpd.protocols.http.IHTTPSession
1314
import org.nanohttpd.protocols.http.response.IStatus
1415
import org.nanohttpd.protocols.http.response.Response
@@ -37,7 +38,7 @@ class CSSHandler : RouterNanoHTTPD.DefaultHandler() {
3738
val method = session!!.method
3839
var uri = session.uri
3940

40-
println("$TAG Method: $method, Url: $uri")
41+
Log.v(TAG, "Method: $method, Uri: $uri")
4142

4243
return try {
4344
val lastSlashIndex = uri.lastIndexOf('/')
@@ -46,7 +47,7 @@ class CSSHandler : RouterNanoHTTPD.DefaultHandler() {
4647
val x = createResponse(Status.OK, "text/css", resources.get(uri))
4748
x
4849
} catch (e: Exception) {
49-
println(TAG + " Exception " + e.toString())
50+
Log.e(TAG, "Exception in get", e)
5051
newFixedLengthResponse(Status.INTERNAL_ERROR, mimeType, ResponseStatus.FAILURE_RESPONSE)
5152
}
5253

@@ -59,6 +60,6 @@ class CSSHandler : RouterNanoHTTPD.DefaultHandler() {
5960
}
6061

6162
companion object {
62-
private const val TAG = "ResourceHandler"
63+
private val TAG: String = this::class.java.simpleName
6364
}
6465
}

r2-streamer/src/main/java/org/readium/r2/streamer/server/handler/FontHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.readium.r2.streamer.server.handler
1111

1212

13+
import android.util.Log
1314
import android.webkit.MimeTypeMap
1415
import org.nanohttpd.protocols.http.IHTTPSession
1516
import org.nanohttpd.protocols.http.response.IStatus
@@ -40,7 +41,7 @@ class FontHandler : RouterNanoHTTPD.DefaultHandler() {
4041
val method = session!!.method
4142
var uri = session.uri
4243

43-
println("$TAG Method: $method, Url: $uri")
44+
Log.v(TAG, "Method: $method, Uri: $uri")
4445

4546
return try {
4647
val lastSlashIndex = uri.lastIndexOf('/')
@@ -49,7 +50,7 @@ class FontHandler : RouterNanoHTTPD.DefaultHandler() {
4950
val x = createResponse(Status.OK, getMimeType(uri), resources.get(uri).inputStream())
5051
x
5152
} catch (e: Exception) {
52-
println(TAG + " Exception " + e.toString())
53+
Log.e(TAG, "Exception in get", e)
5354
newFixedLengthResponse(Status.INTERNAL_ERROR, mimeType, ResponseStatus.FAILURE_RESPONSE)
5455
}
5556
}
@@ -78,6 +79,6 @@ class FontHandler : RouterNanoHTTPD.DefaultHandler() {
7879
}
7980

8081
companion object {
81-
private const val TAG = "FontHandler"
82+
val TAG: String = this::class.java.simpleName
8283
}
8384
}

r2-streamer/src/main/java/org/readium/r2/streamer/server/handler/JSHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.readium.r2.streamer.server.handler
1111

12+
import android.util.Log
1213
import org.nanohttpd.protocols.http.IHTTPSession
1314
import org.nanohttpd.protocols.http.response.IStatus
1415
import org.nanohttpd.protocols.http.response.Response
@@ -38,7 +39,7 @@ class JSHandler : RouterNanoHTTPD.DefaultHandler() {
3839
val method = session!!.method
3940
var uri = session.uri
4041

41-
println("$TAG Method: $method, Url: $uri")
42+
Log.v(TAG, "Method: $method, Uri: $uri")
4243

4344
return try {
4445
val lastSlashIndex = uri.lastIndexOf('/')
@@ -47,7 +48,7 @@ class JSHandler : RouterNanoHTTPD.DefaultHandler() {
4748
val x = createResponse(Status.OK, "text/javascript", resources.get(uri))
4849
x
4950
} catch (e: Exception) {
50-
println(TAG + " Exception " + e.toString())
51+
Log.e(TAG, "Exception in get", e)
5152
newFixedLengthResponse(Status.INTERNAL_ERROR, mimeType, ResponseStatus.FAILURE_RESPONSE)
5253
}
5354

@@ -60,6 +61,6 @@ class JSHandler : RouterNanoHTTPD.DefaultHandler() {
6061
}
6162

6263
companion object {
63-
private const val TAG = "ResourceHandler"
64+
private val TAG: String = this::class.java.simpleName
6465
}
6566
}

r2-streamer/src/main/java/org/readium/r2/streamer/server/handler/ManifestHandler.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.readium.r2.streamer.server.handler
1111

12+
import android.util.Log
1213
import org.nanohttpd.protocols.http.IHTTPSession
1314
import org.nanohttpd.protocols.http.response.IStatus
1415
import org.nanohttpd.protocols.http.response.Response
@@ -40,13 +41,13 @@ class ManifestHandler : RouterNanoHTTPD.DefaultHandler() {
4041
val fetcher = uriResource!!.initParameter(Fetcher::class.java)
4142
newFixedLengthResponse(status, mimeType, fetcher.publication.manifest())
4243
} catch (e: IOException) {
43-
println(TAG + " IOException " + e.toString())
44+
Log.e(TAG, "Exception in get", e)
4445
newFixedLengthResponse(Status.INTERNAL_ERROR, mimeType, ResponseStatus.FAILURE_RESPONSE)
4546
}
4647

4748
}
4849

4950
companion object {
50-
private const val TAG = "ManifestHandler"
51+
private val TAG: String = this::class.java.simpleName
5152
}
5253
}

r2-streamer/src/main/java/org/readium/r2/streamer/server/handler/ResourceHandler.kt

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import org.nanohttpd.router.RouterNanoHTTPD
2121
import org.readium.r2.streamer.fetcher.Fetcher
2222
import java.io.IOException
2323
import java.io.InputStream
24-
import java.net.URI
25-
import java.net.URISyntaxException
2624

2725

2826
class ResourceHandler : RouterNanoHTTPD.DefaultHandler() {
2927

30-
private val TAG = this::class.java.simpleName
28+
companion object {
29+
val TAG: String = this::class.java.simpleName
30+
}
3131

3232
override fun getMimeType(): String? {
3333
return null
@@ -41,29 +41,13 @@ class ResourceHandler : RouterNanoHTTPD.DefaultHandler() {
4141
return Status.OK
4242
}
4343

44-
override fun get(uriResource: RouterNanoHTTPD.UriResource?, urlParams: Map<String, String>?, session: IHTTPSession?): Response? {
45-
val method = session!!.method
46-
val uri: URI
47-
try {
48-
uri = URI(null, null, session.uri, null)
49-
} catch (e: URISyntaxException) {
50-
e.printStackTrace()
51-
return null
52-
}
53-
54-
var encodedUri = uri.toString()
55-
56-
if (encodedUri.contains("//")) {
57-
encodedUri = session.uri.replace("//", "/")
58-
}
59-
60-
println("$TAG Method: $method, Url: $encodedUri")
61-
44+
override fun get(uriResource: RouterNanoHTTPD.UriResource?, urlParams: Map<String, String>?,
45+
session: IHTTPSession?): Response? {
6246
try {
47+
Log.v(TAG, "Method: ${session!!.method}, Uri: ${session.uri}")
6348
val fetcher = uriResource!!.initParameter(Fetcher::class.java)
64-
val offset = encodedUri.indexOf("/", 0)
65-
val startIndex = encodedUri.indexOf("/", offset + 1)
66-
val filePath = encodedUri.substring(startIndex + 1)
49+
50+
val filePath = getHref(session.uri)
6751
val link = fetcher.publication.linkWithHref(filePath)!!
6852
val mimeType = link.typeLink!!
6953

@@ -77,10 +61,9 @@ class ResourceHandler : RouterNanoHTTPD.DefaultHandler() {
7761
// FONT DEOBFUSCATION
7862
// ********************
7963

80-
8164
return serveResponse(session, fetcher.dataStream(filePath), mimeType)
8265
} catch (e: Exception) {
83-
Log.e(TAG, e.toString(), e)
66+
Log.e(TAG, "Exception in get", e)
8467
return newFixedLengthResponse(Status.INTERNAL_ERROR, mimeType, ResponseStatus.FAILURE_RESPONSE)
8568
}
8669
}
@@ -169,4 +152,9 @@ class ResourceHandler : RouterNanoHTTPD.DefaultHandler() {
169152
return createResponse(Status.OK, "text/plain", message)
170153
}
171154

155+
private fun getHref(path: String): String {
156+
val offset = path.indexOf("/", 0)
157+
val startIndex = path.indexOf("/", offset + 1)
158+
return path.substring(startIndex + 1)
159+
}
172160
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.readium.r2.streamer
2+
3+
import org.junit.Test
4+
import java.net.URI
5+
import java.net.URLDecoder
6+
import java.net.URLEncoder
7+
8+
class EncoderDecoderUnitTest {
9+
10+
@Test
11+
fun test1() {
12+
13+
// Case 1 -
14+
// Sample EPUB - https://drive.google.com/open?id=1fGO53nVsjOuSaZ-nOuzcLE3WhRSI9dkK
15+
// For URL asked to r2-streamer "http://127.0.0.1:8080/hindi1/OEBPS/Text/Chapter%201.html",
16+
// we get "/hindi1/OEBPS/Text/Chapter 1.html" in session.uri of nanohttpd, from which we extract
17+
// "OEBPS/Text/Chapter 1.html" as file path to search in parsed Publication model.
18+
// We have "/OEBPS/Text/Chapter%201.html" in spine link of Publication model.
19+
20+
val case1FilePath = "OEBPS/Text/Chapter 1.html"
21+
val case1Encoding1 = URI(null, null, case1FilePath, null).toString() // OEBPS/Text/Chapter%201.html
22+
val case1Encoding2 = URLEncoder.encode(case1FilePath, "UTF-8") // OEBPS%2FText%2FChapter+1.html
23+
24+
// So in Case 1, we need to pick case1Encoding1
25+
// This logic is implemented in Publication.isLinkWithHrefURIDecoded()
26+
27+
28+
// Case 2 -
29+
// Sample EPUB - https://github.com/FolioReader/FolioReader-Android/files/2403715/sikan.zip
30+
// For URL asked to r2-streamer "http://127.0.0.1:8080/sikan/OEBPS/Text/%E4%BA%B2%E5%AD%902018%E5%B9%B44%E6%9C%88%E5%88%8A_0001.xhtml",
31+
// we get "/sikan/OEBPS/Text/亲子2018年4月刊_0001.xhtml" in session.uri of nanohttpd, from which we extract
32+
// "OEBPS/Text/亲子2018年4月刊_0001.xhtml" as file path to search in parsed Publication model.
33+
// We have "/OEBPS/Text/%E4%BA%B2%E5%AD%902018%E5%B9%B44%E6%9C%88%E5%88%8A_0001.xhtml" in spine link of Publication model.
34+
35+
val case2FilePath = "OEBPS/Text/亲子2018年4月刊_0001.xhtml"
36+
val case2LinkHref = "/OEBPS/Text/%E4%BA%B2%E5%AD%902018%E5%B9%B44%E6%9C%88%E5%88%8A_0001.xhtml"
37+
val case2Decoding1 = URI(null, null, case2LinkHref, null).toString() // /OEBPS/Text/%25E4%25BA%25B2%25E5%25AD%25902018%25E5%25B9%25B44%25E6%259C%2588%25E5%2588%258A_0001.xhtml
38+
val case2Decoding2 = URLDecoder.decode(case2LinkHref, "UTF-8") // /OEBPS/Text/亲子2018年4月刊_0001.xhtml
39+
40+
// So in Case 2, we need to pick case2Decoding2
41+
// This logic is implemented in Publication.isLinkWithLinkHrefURLDecoded()
42+
43+
44+
// Case 3 -
45+
// Sample EPUB - https://github.com/FolioReader/FolioReader-Android/files/2482657/22.zip
46+
// For URL asked to r2-streamer "http://127.0.0.1:8080/22/OEBPS/Text/%D8%A7%D9%84%D9%85%D8%A7%D8%B6%D9%8A%20%D9%88%D8%A7%D9%84%D9%85%D8%B3%D8%AA%D9%82%D8%A8%D9%84.html",
47+
// we get "/22/OEBPS/Text/الماضي والمستقبل.html" in session.uri of nanohttpd, from which we extract
48+
// "OEBPS/Text/الماضي والمستقبل.html" as file path to search in parsed Publication model.
49+
// We have "/OEBPS/Text/%D8%A7%D9%84%D9%85%D8%A7%D8%B6%D9%8A%20%D9%88%D8%A7%D9%84%D9%85%D8%B3%D8%AA%D9%82%D8%A8%D9%84.html" in spine link of Publication model.
50+
51+
val case3FilePath = "OEBPS/Text/الماضي والمستقبل.html"
52+
val case3LinkHref = "/OEBPS/Text/%D8%A7%D9%84%D9%85%D8%A7%D8%B6%D9%8A%20%D9%88%D8%A7%D9%84%D9%85%D8%B3%D8%AA%D9%82%D8%A8%D9%84.html"
53+
val case3Decoding1 = URI(null, null, case3LinkHref, null).toString() // /OEBPS/Text/%25D8%25A7%25D9%2584%25D9%2585%25D8%25A7%25D8%25B6%25D9%258A%2520%25D9%2588%25D8%25A7%25D9%2584%25D9%2585%25D8%25B3%25D8%25AA%25D9%2582%25D8%25A8%25D9%2584.html
54+
val case3Decoding2 = URLDecoder.decode(case3LinkHref, "UTF-8") // /OEBPS/Text/الماضي والمستقبل.html
55+
56+
// So in Case 3, we need to pick case3Decoding2
57+
// This logic is implemented in Publication.isLinkWithLinkHrefURLDecoded()
58+
}
59+
}

0 commit comments

Comments
 (0)