gotham icon indicating copy to clipboard operation
gotham copied to clipboard

assemble_request()

Open nyarly opened this issue 6 years ago • 5 comments

I was trying to incorporate https://github.com/stephank/hyper-staticfile into a Gotham app, and realized that since 0.2.0 decomposes the Request into State, it's kind of tricky to wrap hyper services in Gotham.

As a first cut, it seems like it should be possible to have a function that constructs a duplicate Request for the purpose. Even moreso, it seems like gotham (or an adjacent crate?) could provide a wrapper Handler that would do the reconstruction. You'd need a NewHandler, though, to set up the inner hyper service, though.

I think I could probably put together a PR for this.

nyarly avatar Mar 19 '18 18:03 nyarly

I'm wondering if this is similar enough to #123 to continue this conversation as part of that issue?

bradleybeddoes avatar Mar 20 '18 10:03 bradleybeddoes

This will get to be a bit easier when gotham eventually switches to hyper 0.12 and the http crate, where deconstruct() is replaced by ::into_parts() and ::from_parts().

Right now you have to do something like this to reconstruct the request;

state.take::<Body>().concat2().then(move |chunk| {
    let method = state.borrow::<Method>().clone();
    let uri = state.borrow::<Uri>().clone();
    let headers = state.borrow::<Headers>().clone();

    let mut request = Request::new(method, uri);
    for header in headers.iter() {
        request.headers_mut().set_raw(
            header.name().to_owned(),
            header.raw().clone()
        );
    }
    let body = chunk.unwrap().to_vec();
    request.set_body(body);
    // Request is now reconstructed
});

ParisLiakos avatar Mar 22 '18 23:03 ParisLiakos

Is there a reason that Gotham hasn't switched to 0.12 and http? Are they not stable Rust yet?

nyarly avatar Mar 25 '18 02:03 nyarly

It is not released yet. See also #26

ParisLiakos avatar Mar 25 '18 11:03 ParisLiakos

@smangelsdorf pointed out Extensions struct in the http crate the other day too.

This is interesting as it is essentially what we already refer to as state. You can see how it is accessed here.

Naturally this would require investigation (especially wrt Middleware and pipelines) as we work with the http crate / hyper 0.12 but it could lead to handlers becoming something like fn(Request) -> Box<Future<Item = Response>>

bradleybeddoes avatar Mar 25 '18 22:03 bradleybeddoes