Skip to content

Commit

Permalink
KTOR-5591 Fix takeFrom(URL) method with underscore in host (#3417)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov authored Feb 24, 2023
1 parent 5ba3db9 commit c9327e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ktor-http/jvm/src/io/ktor/http/URLUtilsJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ public fun URLBuilder.takeFrom(uri: URI): URLBuilder {
}

/**
* Take URI components from [url]
* Take URL components from [url]
*/
public fun URLBuilder.takeFrom(url: URL): URLBuilder = takeFrom(url.toURI())
public fun URLBuilder.takeFrom(url: URL): URLBuilder = when {
url.host.contains('_') -> takeFrom(url.toString())
else -> takeFrom(url.toURI())
}

/**
* Convert [Url] to [URI]
Expand Down
8 changes: 8 additions & 0 deletions ktor-http/jvm/test/io/ktor/tests/http/URLBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ class URLBuilderTestJvm {
takeFrom(URI("/test"))
}.buildString().let { url -> assertEquals("http://localhost:9093/test", url) }
}

@Test
fun testUnderscoreInHost() {
assertEquals(
"http://my_service:8080",
URLBuilder().takeFrom(URL("http://my_service:8080")).buildString()
)
}
}

0 comments on commit c9327e7

Please sign in to comment.