Skip to content

Commit

Permalink
Relative path handling in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
oxgl committed Nov 13, 2019
1 parent eb77753 commit 3cf84de
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.oxyggen.net"
version = "1.0.6"
version = "1.0.7"


repositories {
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/com/oxyggen/net/CommonURL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ open class CommonURL(uriString: String, context: ContextURI? = null) : URL(uriSt
}
}

path = Path.parse(percentDecode(match?.groups?.get("path")?.value ?: ""))
val foundPath = Path.parse(percentDecode(match?.groups?.get("path")?.value ?: ""))
path = if (foundPath.isAbsolute) {
foundPath
} else if (context is CommonURL) {
context.path.resolve(foundPath)
} else {
throw Exception("Can't handle relative path ${foundPath.complete} without context!")
}
query = percentDecode(match?.groups?.get("query")?.value ?: "")
fragment = percentDecode(match?.groups?.get("fragment")?.value ?: "")
}
Expand Down
31 changes: 19 additions & 12 deletions src/test/kotlin/com/oxyggen/net/URITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ internal class URITest {
Assertions.assertEquals("", u4.fragment, "fragment")
}

val u5 = URI.parse("./", u4 as ContextURI)

Assertions.assertTrue(u5 is HttpURL)
if (u5 is CommonURL) {
Assertions.assertEquals("http", u5.scheme, "scheme")
Assertions.assertEquals("", u5.userinfo, "userinfo")
Assertions.assertEquals("subdomain.domain.com", u5.host, "host")
Assertions.assertEquals(80, u5.port, "port")
Assertions.assertEquals("./", u5.path.complete, "path")
Assertions.assertEquals("", u5.query, "query")
Assertions.assertEquals("", u5.fragment, "fragment")
}

}

Expand Down Expand Up @@ -118,6 +106,25 @@ internal class URITest {
}
}

@Test
fun `Relative URL path parsing`() {
val u1 = URI.parse("http://subdomain.domain.com/abc/?test=c+d")

val u2 = URI.parse("test.html", u1 as ContextURI)

Assertions.assertTrue(u2 is HttpURL)
if (u2 is CommonURL) {
println(u2.toResolvedUriString())
Assertions.assertEquals("http", u2.scheme, "scheme")
Assertions.assertEquals("", u2.userinfo, "userinfo")
Assertions.assertEquals("subdomain.domain.com", u2.host, "host")
Assertions.assertEquals(80, u2.port, "port")
Assertions.assertEquals("/abc/test.html", u2.path.complete, "path")
Assertions.assertEquals("", u2.query, "query")
Assertions.assertEquals("", u2.fragment, "fragment")
}
}

@Test
fun `URL encoding test`() {
val original = "hd_ÁÖüÜűé_€_ह_¢_!%abc!a"
Expand Down

0 comments on commit 3cf84de

Please sign in to comment.