ktor
ktor copied to clipboard
Add Url capability: parsing/validating url strings
Subsystem ktor-http
Is your feature request related to a problem? Please describe.
My app gives me unexpected results on Url(String)
Describe the solution you'd like Url should have a factory method that returns an optional Url, something like:
class Url {
companion object {
fun parse(url: String): Url? { ... }
}
}
This method should return null if the url is not valid. It should not throw exceptions.
Motivation to include to ktor User-submitted content often has urls in it and it would be useful to validate them and extract the various url parts.
Specific urls that are currently broken:
Url("bogus").toString() returns http://localhost/bogus and I think it should return null from this new method.
Url("http://localhost:7000Pragma").toString() throws an exception and I think it should return null from this new method.
For now I'm just working around this on my end by doing:
fun Url.Companion.parse(str: String): Url? {
try {
return Url(str)
} catch (e: Throwable) {
return null
}
}
but obviously this doesn't address the "bogus" case.
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.
Url("bogus").toString()returnshttp://localhost/bogusand I think it should return null from this new method.
Currently the takeFrom(String) function called by Url(urlString) allow parsing relative URLs (see https://github.com/ktorio/ktor/pull/849).
However if no base URL is provided in Url(urlString), protocol and host will default to http and localhost respectively.
I am not sure if the Url(urlString) function should return a default protocol and host. Maybe it should just set protocol and host to null or throw an exception if no protocol is provided?
can i work on this @edenman , @victorcwai ?
@e5l team, please review the PR when you get a chance.
This also got me. Its extremely unexpected to have a relative url get parsed as localhost relative.
Would it make sense to return a kotlin.Result here? The consumer could then fold it to handle success vs failure, or call getOrNull() on the result if they want it to have the functionality described here (null for invalid).
@e5l if this is still needed, I'll give it a try this week.
@ablx, thanks! it would be great
@e5l hi team, I added MR with the solution, could you please take a look?
Sure! Let me check
Merged