http icon indicating copy to clipboard operation
http copied to clipboard

Some Uri round-tripping through Parts fails unexpectantly

Open Nokel81 opened this issue 4 years ago • 3 comments

This might be because I am not well versed in the HTTP URI standards but I would expect following two pieces of code to produce the same (or basically the same) Parts instances.

1:

let mut parts = Uri::from_static("localhost:8080").into_parts();
parts.scheme = Some(Scheme::HTTPS);

2:

let parts = Uri::from_static("https://localhost:8080").into_parts();

However, for 2, the path_and_query is set to effectively Some("/") where as in 1 it is set to None. Which means that putting them back into a Uri, the second one passes where as the first one fails.

I think that the first one should work.

Nokel81 avatar Feb 04 '20 15:02 Nokel81

I've run into this as well, it's unfortunate this is unintuitive. The reason is that No. 1 is parsing in authority-form, which means it doesn't have a scheme or path. But once you've added a scheme, it no longer matches authority-form, and only fits the absolute-form. That format requires a path to be set...

seanmonstar avatar Feb 06 '20 22:02 seanmonstar

Can authority-form have a path with no scheme?

Since the second parses with no explicit path but with an explicit scheme. Would using a default path be valid or merely more surprising but in a different way

Nokel81 avatar Feb 06 '20 22:02 Nokel81

But either way I think that the API could be designed to better represent that distinction. Like having Parts be a enum of the possible parsing variants or something

Nokel81 avatar Feb 06 '20 22:02 Nokel81