Skip to content

Commit

Permalink
Fixed HttpURL decoding problem (the parts are saved as original value…
Browse files Browse the repository at this point in the history
…, and not percentDecoded)
  • Loading branch information
oxgl committed Dec 1, 2019
1 parent 3cf9f2e commit 5e6f5de
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 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.9"
version = "1.0.10"


repositories {
Expand Down
22 changes: 13 additions & 9 deletions src/main/kotlin/com/oxyggen/net/CommonURL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ open class CommonURL(uriString: String, val context: ContextURI? = null) : URL(u
""
}


val uriPort: Int
val port: Int get() = if (uriPort > 0) uriPort else getDefaultPort()
val port: Int
get() = if (uriPort > 0) uriPort else getDefaultPort()

val path: Path

val query: String
get() = if (queryRange != null) schemeSpecificPart.substring(queryRange) else ""
val fragment: String
get() = if (fragmentRange != null) schemeSpecificPart.substring(fragmentRange) else ""


/**
* The default port for current scheme (http = 80, https = 443,...)
Expand Down Expand Up @@ -72,7 +79,7 @@ open class CommonURL(uriString: String, val context: ContextURI? = null) : URL(u
fragmentRange = match.groups["fragment"]?.range

// We have to resolve the full path here in constructor, to check it's validness
val foundPath = Path.parse(percentDecode(if (pathRange != null) schemeSpecificPart.substring(pathRange) else ""))
val foundPath = Path.parse(if (pathRange != null) schemeSpecificPart.substring(pathRange) else "")
path = if (foundPath.isAbsolute) {
foundPath
} else if (context is CommonURL) {
Expand All @@ -82,9 +89,6 @@ open class CommonURL(uriString: String, val context: ContextURI? = null) : URL(u
}
}

val query: String by lazy { if (queryRange != null) percentDecode(schemeSpecificPart.substring(queryRange)) else "" }
val fragment: String by lazy { if (fragmentRange != null) percentDecode(schemeSpecificPart.substring(fragmentRange)) else "" }


private fun toUriStringInternal(normalized: Boolean = false): String {
// Scheme
Expand All @@ -102,17 +106,17 @@ open class CommonURL(uriString: String, val context: ContextURI? = null) : URL(u

// Normalized path or the complete path
if (normalized)
result += percentEncode(path.normalized.complete)
result += path.normalized.complete
else
result += percentEncode(path.complete)
result += path.complete

// Query
if (query.isNotEmpty())
result += "?" + percentEncode(query)
result += "?$query"

// Fragment
if (fragment.isNotEmpty())
result += "#" + percentEncode(fragment)
result += "#$fragment"

return result
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/oxyggen/net/HttpURL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,23 @@ open class HttpURL(uriString: String, context: ContextURI? = null) : CommonURL(u
else -> 80
}

open val queryParam: Map<String, List<String>> by lazy {
val result = mutableMapOf<String, MutableList<String>>()

if (query.isNotBlank()) {
val paramValue = query.split('&')
paramValue.forEach {
val parts = it.split('=')
val name = percentDecode(parts[0])
val value = percentDecode(parts.getOrElse(1) { "" })

if (result.containsKey(name))
result[name]!!.add(value)
else
result.put(name, mutableListOf(value))
}
}
result
}

}
15 changes: 13 additions & 2 deletions src/test/kotlin/com/oxyggen/net/URITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class URITest {
Assertions.assertEquals("subdomain.domain.com", u3.host, "host")
Assertions.assertEquals(81, u3.port, "port")
Assertions.assertEquals("/", u3.path.complete, "path")
Assertions.assertEquals("test=a b", u3.query, "query")
Assertions.assertEquals("test=a%20b", u3.query, "query")
Assertions.assertEquals("", u3.fragment, "fragment")
}

Expand All @@ -57,7 +57,7 @@ internal class URITest {
Assertions.assertEquals("subdomain.domain.com", u4.host, "host")
Assertions.assertEquals(80, u4.port, "port")
Assertions.assertEquals("/", u4.path.complete, "path")
Assertions.assertEquals("test=c d", u4.query, "query")
Assertions.assertEquals("test=c+d", u4.query, "query")
Assertions.assertEquals("", u4.fragment, "fragment")
}

Expand Down Expand Up @@ -154,4 +154,15 @@ internal class URITest {

}

@Test
fun `HTTP URL parameters test`() {
val u = URI.parse("https://[email protected]:8080/my/path/to/file.htm?name=first&name=last&other=z#hash_value")

if (u is HttpURL) {
Assertions.assertEquals(u.queryParam["name"]?.first(), "first")
Assertions.assertEquals(u.queryParam["name"]?.get(1), "last")
}

}

}

0 comments on commit 5e6f5de

Please sign in to comment.