tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Router should support request body types other than hyper::Body

Open sfackler opened this issue 3 years ago • 7 comments

The generated server Service implementations already support arbitrary request body types, but Router is currently hardcoded to work with hyper::Body directly. It'd be great if that were relaxed to work with any suitable body type.

sfackler avatar Feb 03 '22 00:02 sfackler

Agreed we can make this change. I believe we should do this in conjunction with https://github.com/hyperium/tonic/pull/830

LucioFranco avatar Feb 16 '22 18:02 LucioFranco

I am going to remove this from 0.7 because it will be quite challenging to get this change in right now and I would actually like to refactor the transport module in the next release (aka remove it and re do it).

LucioFranco avatar Mar 31 '22 18:03 LucioFranco

Is it actually possible? Hyper requires services accept Request<hyper::Body>. You can change the body in a middleware but the top level body must be hyper::Body.

davidpdrsn avatar Mar 31 '22 19:03 davidpdrsn

You can install layers between hyper and the tonic service that change the body type.

sfackler avatar Mar 31 '22 20:03 sfackler

Yes that's what I meant.

davidpdrsn avatar Mar 31 '22 20:03 davidpdrsn

So then it's totally possible to create a Hyper service that changes the request body type and then feeds that modified body type into the Tonic Router, except that Router won't let you do that.

sfackler avatar Mar 31 '22 20:03 sfackler

This would be extremely helpful for a particular issue I'm working on. As @sfackler points out, the major sticking point seems to be where tonic meets axum. Without this, the only current workaround is doing very goofy stuff like getting a channel-backed hyper::Body, even if you only needed to do something like count the number of bytes per data chunk emitted by the body, etc.

I'd be interested in trying to work on/submit a PR for this. Is this something you'd be receptive to, given the thoughts/plans around breaking tonic apart into more composable chunks?

tobz avatar May 27 '22 20:05 tobz