Follow up: Setting a hyper::Body from a Stream


#1

The question is related to this question. However, I’ll better start a new topic, since the question is slightly different.

Like @thejefflarson I also would have needed to create a Body from a Stream of Chunks. The context is the following:

  • receive requst as server
  • do something to the body
  • forward the request as client to another destination

(i.e., some kind of proxy behaviour)

It would be pretty easy to simply use Requst<Box<Stream<Item=Chunk, Error=hyper::Error> as not having to deal with the problem that Body does not offer a corresponding From trait implementation.
But: Client is only implemented for Client<HttpConnector, Body>, hence I’m forced to rely on Body as body type.
This feels unsatisfactory. So there are two fundamental questions here (which are both related to resolving the issue):

  • Why can’t a Body be build out of a Stream, especially a Stream of Chunks? It seems natural to have such an implementation, since a Body is a Stream of Chunks. Is there some reason why it can’t be provided? This is probably tokio-proto related, since the Body implementation in hyper is just a thin wrapper around the tokio-proto Body. The workaround (suggested here) with using a channel internally for (re-)creating the body works, but is somehow ugly…
  • Why is a Client forced to use Body as Request Body? I suppose it’s using the same encoding/decoding infrastructur as sending and receiving in the Server? Maybe I’m missing something… can someone give me some pointers?
    EDIT: type can be adjusted via the client’s configure() method

#2

Ok, I just noticed, that I can adjust the Client Body via the configure method (https://docs.rs/hyper/0.11.6/hyper/client/struct.Client.html#method.configure). Pretty hidden, but nevertheless there :smile: So, question number 2 is obsolete now