dedupe h1 send_response

This commit is contained in:
Rob Ede 2021-05-08 23:55:20 +01:00
parent 5e77ef66b0
commit a1e6f10a8d
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 23 additions and 29 deletions

View File

@ -296,11 +296,11 @@ where
io.poll_flush(cx) io.poll_flush(cx)
} }
fn send_response( fn send_response_inner(
self: Pin<&mut Self>, self: Pin<&mut Self>,
message: Response<()>, message: Response<()>,
body: B, body: &impl MessageBody,
) -> Result<(), DispatchError> { ) -> Result<BodySize, DispatchError> {
let size = body.size(); let size = body.size();
let mut this = self.project(); let mut this = self.project();
this.codec this.codec
@ -313,36 +313,35 @@ where
})?; })?;
this.flags.set(Flags::KEEPALIVE, this.codec.keepalive()); this.flags.set(Flags::KEEPALIVE, this.codec.keepalive());
match size {
BodySize::None | BodySize::Empty => this.state.set(State::None), Ok(size)
_ => this.state.set(State::SendPayload(body)), }
fn send_response(
mut self: Pin<&mut Self>,
message: Response<()>,
body: B,
) -> Result<(), DispatchError> {
let size = self.as_mut().send_response_inner(message, &body)?;
let state = match size {
BodySize::None | BodySize::Empty => State::None,
_ => State::SendPayload(body),
}; };
self.project().state.set(state);
Ok(()) Ok(())
} }
fn send_error_response( fn send_error_response(
self: Pin<&mut Self>, mut self: Pin<&mut Self>,
message: Response<()>, message: Response<()>,
body: Body, body: Body,
) -> Result<(), DispatchError> { ) -> Result<(), DispatchError> {
// TODO: de-dupe impl with send_response let size = self.as_mut().send_response_inner(message, &body)?;
let state = match size {
let size = body.size(); BodySize::None | BodySize::Empty => State::None,
let mut this = self.project(); _ => State::SendErrorPayload(body),
this.codec
.encode(Message::Item((message, size)), &mut this.write_buf)
.map_err(|err| {
if let Some(mut payload) = this.payload.take() {
payload.set_error(PayloadError::Incomplete(None));
}
DispatchError::Io(err)
})?;
this.flags.set(Flags::KEEPALIVE, this.codec.keepalive());
match size {
BodySize::None | BodySize::Empty => this.state.set(State::None),
_ => this.state.set(State::SendErrorPayload(body)),
}; };
self.project().state.set(state);
Ok(()) Ok(())
} }

View File

@ -413,11 +413,6 @@ impl<B> ServiceResponse<B> {
} }
} }
// /// Extract response body
// pub fn take_body(&mut self) -> ResponseBody<B> {
// self.response.take_body()
// }
/// Extract response body /// Extract response body
pub fn into_body(self) -> B { pub fn into_body(self) -> B {
self.response.into_body() self.response.into_body()