Skip to content

Commit

Permalink
KTOR-4816: UrlBuilder: support tel scheme (#4518)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-erofeev authored Dec 23, 2024
1 parent eec170d commit 6cf97bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ktor-http/common/src/io/ktor/http/URLBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ private fun <A : Appendable> URLBuilder.appendTo(out: A): A {
out.appendAbout(host)
return out
}

"tel" -> {
out.appendTel(host)
return out
}
}

out.append("://")
Expand Down Expand Up @@ -190,6 +195,11 @@ private fun Appendable.appendAbout(host: String) {
append(host)
}

private fun Appendable.appendTel(host: String) {
append(":")
append(host)
}

/**
* Hostname of current origin.
*
Expand Down
6 changes: 6 additions & 0 deletions ktor-http/common/src/io/ktor/http/URLParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ internal fun URLBuilder.takeFromUnsafe(urlString: String): URLBuilder {
return this
}

if (protocol.name == "tel") {
require(slashCount == 0)
host = urlString.substring(startIndex, endIndex)
return this
}

if (slashCount >= 2) {
loop@ while (true) {
val delimiter = urlString.indexOfAny("@/\\?#".toCharArray(), startIndex).takeIf { it > 0 } ?: endIndex
Expand Down
18 changes: 18 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 @@ -365,4 +365,22 @@ class UrlTest {
assertEquals(URLProtocol.HTTP, urlHttp.protocol)
assertTrue(urlHttp.rawSegments.contains("about"))
}

@Test
fun testTelUrl() {
val globalTelUrl = Url("tel:+14085555555")
assertEquals("tel:+14085555555", globalTelUrl.toString())
assertEquals("tel", globalTelUrl.protocol.name)
assertEquals("+14085555555", globalTelUrl.host)

val localTelUrlWithContext = Url("tel:863-1234;phone-context=+1-914-555")
assertEquals("tel:863-1234;phone-context=+1-914-555", localTelUrlWithContext.toString())
assertEquals("tel", localTelUrlWithContext.protocol.name)
assertEquals("863-1234;phone-context=+1-914-555", localTelUrlWithContext.host)

val telUrlWithParams = Url("tel:+1-408-555-5555;extension=ext;phone-context=context")
assertEquals("tel:+1-408-555-5555;extension=ext;phone-context=context", telUrlWithParams.toString())
assertEquals("tel", telUrlWithParams.protocol.name)
assertEquals("+1-408-555-5555;extension=ext;phone-context=context", telUrlWithParams.host)
}
}

0 comments on commit 6cf97bb

Please sign in to comment.