From a1e6f10a8d69ee834604032e446a9169097df34e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 8 May 2021 23:55:20 +0100 Subject: [PATCH] dedupe h1 send_response --- actix-http/src/h1/dispatcher.rs | 47 ++++++++++++++++----------------- src/service.rs | 5 ---- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/actix-http/src/h1/dispatcher.rs b/actix-http/src/h1/dispatcher.rs index d287d9d5e..62e57da14 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -296,11 +296,11 @@ where io.poll_flush(cx) } - fn send_response( + fn send_response_inner( self: Pin<&mut Self>, message: Response<()>, - body: B, - ) -> Result<(), DispatchError> { + body: &impl MessageBody, + ) -> Result { let size = body.size(); let mut this = self.project(); this.codec @@ -313,36 +313,35 @@ where })?; this.flags.set(Flags::KEEPALIVE, this.codec.keepalive()); - match size { - BodySize::None | BodySize::Empty => this.state.set(State::None), - _ => this.state.set(State::SendPayload(body)), + + Ok(size) + } + + 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(()) } fn send_error_response( - self: Pin<&mut Self>, + mut self: Pin<&mut Self>, message: Response<()>, body: Body, ) -> Result<(), DispatchError> { - // TODO: de-dupe impl with send_response - - let size = body.size(); - let mut this = self.project(); - 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)), + let size = self.as_mut().send_response_inner(message, &body)?; + let state = match size { + BodySize::None | BodySize::Empty => State::None, + _ => State::SendErrorPayload(body), }; + self.project().state.set(state); Ok(()) } diff --git a/src/service.rs b/src/service.rs index 23b772574..475822395 100644 --- a/src/service.rs +++ b/src/service.rs @@ -413,11 +413,6 @@ impl ServiceResponse { } } - // /// Extract response body - // pub fn take_body(&mut self) -> ResponseBody { - // self.response.take_body() - // } - /// Extract response body pub fn into_body(self) -> B { self.response.into_body()