ktor icon indicating copy to clipboard operation
ktor copied to clipboard

Add Url capability: parsing/validating url strings

Open edenman opened this issue 5 years ago • 9 comments

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.

edenman avatar May 28 '20 18:05 edenman

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.

edenman avatar May 28 '20 19:05 edenman

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

oleg-larshin avatar Aug 10 '20 15:08 oleg-larshin

Url("bogus").toString() returns http://localhost/bogus and 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?

vcwai avatar Oct 30 '20 00:10 vcwai

can i work on this @edenman , @victorcwai ?

chetankokil avatar Aug 27 '22 23:08 chetankokil

@e5l team, please review the PR when you get a chance.

chetankokil avatar Aug 28 '22 00:08 chetankokil

This also got me. Its extremely unexpected to have a relative url get parsed as localhost relative.

J-Swift avatar Nov 02 '22 01:11 J-Swift

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).

jsoberg avatar Jan 15 '23 15:01 jsoberg

@e5l if this is still needed, I'll give it a try this week.

ablx avatar Oct 17 '23 09:10 ablx

@ablx, thanks! it would be great

e5l avatar Oct 25 '23 08:10 e5l

@e5l hi team, I added MR with the solution, could you please take a look?

anton-erofeev avatar Jun 13 '24 18:06 anton-erofeev

Sure! Let me check

e5l avatar Jun 14 '24 05:06 e5l

Merged

e5l avatar Jun 15 '24 07:06 e5l