http-rs / http-types

Common types for HTTP operations
https://docs.rs/http-types
Apache License 2.0
200 stars 83 forks source link

Conversion from http::Request to http_types::Request fails #137

Open Nemo157 opened 4 years ago

Nemo157 commented 4 years ago

When converting an http::Request from hyper to an http_types::Request the url fails to convert with an error:

relative URL without a base

small example problem showing the failure:

use std::convert::TryFrom;

fn main() {
    let request = http::Request::builder()
        .method("GET")
        .uri("/")
        .header("Host", "localhost")
        .body(http_types::Body::empty())
        .unwrap();

    http_types::Request::try_from(request).unwrap();
}
yoshuawuyts commented 4 years ago

Hey @Nemo157; thanks for opening this. Just like Node.js http_types uses the WHATWG URL spec and only works with fully qualified URLs. The following code should work:

use std::convert::TryFrom;

fn main() {
    let request = http::Request::builder()
        .method("GET")
        .uri("http://localhost:8080/")
        .header("Host", "localhost")
        .body(http_types::Body::empty())
        .unwrap();

    http_types::Request::try_from(request).unwrap();
}

We realize this is different from what people may be used to, but being able to rely on every part of the URL being populated after it being constructed makes it significantly easier to work with throughout the code.

Nemo157 commented 4 years ago

This was just an example url of the form provided by hyper's server component. On the server side a request does not have a fully resolved url associated with it, it only has the path component. I hacked around it for now by injecting an arbitrary scheme and host, but this seems like something that should be part of the builtin conversion given that hyper::Request is expected to have a relative url.

If I do have to inject this myself, then what is expected from an http-service consumers point-of-view? http-service-h1 appears to inject the bind address, but this is not something I have access to.