Hyper: Provide Conversion from url::Url to hyper::Uri

Created on 17 Jun 2017  路  3Comments  路  Source: hyperium/hyper

When using the url crate https://github.com/servo/rust-url together with hyper I always have to do something like this:

let uri: hyper::Uri = url.into_string().parse()?;

Either provide an impl From<url::Url> for hyper::Uri or switch to using the servo Url implementation internally. In my opinion having 2 different Url types which basically do the same thing is not only annoying to work with, it is also confusing for newcomers.

Most helpful comment

Similar issue I raised before: https://github.com/hyperium/hyper/issues/1102

Although I understand the motivation for removing Url, it seems users are feeling some pain in not having impl From<url::Url> for hyper::Uri.

All 3 comments

The code example you pasted is probably the way to do this, for now.

The reason for having a separate type is simple: all valid URIs are not valid URLs. A client may only care about using a Url in 99% of cases, but a server needs to handle more cases.

Consider these cases, which are all valid Uris, but not Urls:

  • * (like OPTIONS * HTTP/1.1)
  • / (like GET / HTTP/1.1)
  • www.example.com (like CONNECT www.example.com HTTP/1.1)

Additionally, the layout of a URI is different in HTTP2, so having the Uri type in hyper allows adjusting for that.

As for a conversion, perhaps. I'm not 100% against it, but the reason so far is that url is a big dependency just to provide a single little From implementation. It so far seems like hyper shouldn't. The conversion you pasted should be fine. It shouldn't ever error (if it does, likely a bug in Uri or Url), and it's really not that expensive. Parsing a Uri is much cheaper than parsing Url.

See #1089 for more.

Similar issue I raised before: https://github.com/hyperium/hyper/issues/1102

Although I understand the motivation for removing Url, it seems users are feeling some pain in not having impl From<url::Url> for hyper::Uri.

hyper is changing to just using http::Uri, so this isn't actionable in hyper proper. Closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcseemk picture mcseemk  路  3Comments

crackcomm picture crackcomm  路  5Comments

lslima91 picture lslima91  路  4Comments

Firstyear picture Firstyear  路  4Comments

toplinuxsir picture toplinuxsir  路  3Comments