gotham
gotham copied to clipboard
assemble_request()
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.
I'm wondering if this is similar enough to #123 to continue this conversation as part of that issue?
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
});
Is there a reason that Gotham hasn't switched to 0.12 and http? Are they not stable Rust yet?
It is not released yet. See also #26
@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>>