Skip to content

Commit

Permalink
KTOR-6709 Fix URLParserException on minimal file url (#4622)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriandieter authored Jan 28, 2025
1 parent 5ff1a3c commit 15f0921
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ktor-http/common/src/io/ktor/http/URLParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ internal fun URLBuilder.takeFromUnsafe(urlString: String): URLBuilder {

private fun URLBuilder.parseFile(urlString: String, startIndex: Int, endIndex: Int, slashCount: Int) {
when (slashCount) {
1 -> {
host = ""
encodedPath = urlString.substring(startIndex, endIndex)
}
2 -> {
val nextSlash = urlString.indexOf('/', startIndex)
if (nextSlash == -1 || nextSlash == endIndex) {
Expand Down
9 changes: 9 additions & 0 deletions ktor-http/common/test/io/ktor/tests/http/UrlTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ class UrlTest {
assertEquals(expectedUrl, result.toString())
}

@Test
fun testForFileProtocolMinimalRepresentation() {
val result = Url("file:/var/www")
assertEquals("file", result.protocol.name)
assertEquals("", result.host)
assertEquals(listOf("var", "www"), result.rawSegments)
assertEquals("file:///var/www", result.toString())
}

@Test
fun testForMailProtocol() {
val expectedUrl = "mailto:[email protected]"
Expand Down

0 comments on commit 15f0921

Please sign in to comment.