servo / rust-url

URL parser for Rust
https://docs.rs/url/
Apache License 2.0
1.32k stars 330 forks source link

not-a-base on host:port string #588

Open genbattle opened 4 years ago

genbattle commented 4 years ago

I'm using the URL library to parse a Host header, and when my application is running on localhost the value that comes out of the header is localhost:3000 (for example).

This value is parsed into a Url but comes out with cannot_be_a_base() as true, so I can't join it.

With url 2.1.1 I can't call set_scheme on the parsed URL either, but that seems to be related to another regression that multiple people have raised in #577. With 2.1.0 I can call set_scheme on the parsed URL but it still comes out as cannot_be_a_base().

I worked around this by prepending "http://" onto whatever comes out of the Host header. I see this library also has a Host struct, but I don't see any way to convert a Host into a Url. I guess what' I'm looking for is a better way to build a URL from parts.

tmccombs commented 4 years ago

I see this library also has a Host struct, but I don't see any way to convert a Host into a Url

you would need a scheme to convert it to a valid URL. But it does seem like there should be an API to build a url from parts rather than having to create a string and parse it. Maybe an api that allows you to do something like:

let url = Url::builder("http").host(host).port(port)

I'm using the URL library to parse a Host header

The Host header isn't a URL. It is just the host, optionally followed by the port.