mirror of https://github.com/fafhrd91/actix-web
return Option<State> according to review
This commit is contained in:
parent
6c5b0c67f3
commit
50aa2e6cd3
|
@ -141,10 +141,6 @@ where
|
||||||
matches!(self, State::None)
|
matches!(self, State::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_noop(&self) -> bool {
|
|
||||||
matches!(self, State::NoOp)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_call(&self) -> bool {
|
fn is_call(&self) -> bool {
|
||||||
matches!(self, State::ServiceCall(_))
|
matches!(self, State::ServiceCall(_))
|
||||||
}
|
}
|
||||||
|
@ -344,7 +340,7 @@ where
|
||||||
self: Pin<&mut Self>,
|
self: Pin<&mut Self>,
|
||||||
message: Response<()>,
|
message: Response<()>,
|
||||||
body: ResponseBody<B>,
|
body: ResponseBody<B>,
|
||||||
) -> Result<State<S, B, X>, DispatchError> {
|
) -> Result<Option<State<S, B, X>>, DispatchError> {
|
||||||
let mut this = self.project();
|
let mut this = self.project();
|
||||||
this.codec
|
this.codec
|
||||||
.encode(Message::Item((message, body.size())), &mut this.write_buf)
|
.encode(Message::Item((message, body.size())), &mut this.write_buf)
|
||||||
|
@ -357,8 +353,8 @@ where
|
||||||
|
|
||||||
this.flags.set(Flags::KEEPALIVE, this.codec.keepalive());
|
this.flags.set(Flags::KEEPALIVE, this.codec.keepalive());
|
||||||
match body.size() {
|
match body.size() {
|
||||||
BodySize::None | BodySize::Empty => Ok(State::None),
|
BodySize::None | BodySize::Empty => Ok(Some(State::None)),
|
||||||
_ => Ok(State::SendPayload(body)),
|
_ => Ok(Some(State::SendPayload(body))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,12 +373,11 @@ where
|
||||||
let state = match this.state.project() {
|
let state = match this.state.project() {
|
||||||
StateProj::None => match this.messages.pop_front() {
|
StateProj::None => match this.messages.pop_front() {
|
||||||
Some(DispatcherMessage::Item(req)) => {
|
Some(DispatcherMessage::Item(req)) => {
|
||||||
Some(self.as_mut().handle_request(req, cx)?)
|
self.as_mut().handle_request(req, cx)?
|
||||||
}
|
}
|
||||||
Some(DispatcherMessage::Error(res)) => Some(
|
Some(DispatcherMessage::Error(res)) => self
|
||||||
self.as_mut()
|
.as_mut()
|
||||||
.send_response(res, ResponseBody::Other(Body::Empty))?,
|
.send_response(res, ResponseBody::Other(Body::Empty))?,
|
||||||
),
|
|
||||||
Some(DispatcherMessage::Upgrade(req)) => {
|
Some(DispatcherMessage::Upgrade(req)) => {
|
||||||
return Ok(PollResponse::Upgrade(req));
|
return Ok(PollResponse::Upgrade(req));
|
||||||
}
|
}
|
||||||
|
@ -398,14 +393,15 @@ where
|
||||||
Poll::Ready(Err(e)) => {
|
Poll::Ready(Err(e)) => {
|
||||||
let res: Response = e.into().into();
|
let res: Response = e.into().into();
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
Some(self.as_mut().send_response(res, body.into_body())?)
|
self.as_mut().send_response(res, body.into_body())?
|
||||||
}
|
}
|
||||||
Poll::Pending => None,
|
Poll::Pending => None,
|
||||||
},
|
},
|
||||||
StateProj::ServiceCall(fut) => match fut.poll(cx) {
|
StateProj::ServiceCall(fut) => match fut.poll(cx) {
|
||||||
Poll::Ready(Ok(res)) => {
|
Poll::Ready(Ok(res)) => {
|
||||||
let (res, body) = res.into().replace_body(());
|
let (res, body) = res.into().replace_body(());
|
||||||
let state = self.as_mut().send_response(res, body)?;
|
// send_response does not return a None variant of State.
|
||||||
|
let state = self.as_mut().send_response(res, body)?.unwrap();
|
||||||
this = self.as_mut().project();
|
this = self.as_mut().project();
|
||||||
this.state.set(state);
|
this.state.set(state);
|
||||||
continue;
|
continue;
|
||||||
|
@ -413,7 +409,7 @@ where
|
||||||
Poll::Ready(Err(e)) => {
|
Poll::Ready(Err(e)) => {
|
||||||
let res: Response = e.into().into();
|
let res: Response = e.into().into();
|
||||||
let (res, body) = res.replace_body(());
|
let (res, body) = res.replace_body(());
|
||||||
Some(self.as_mut().send_response(res, body.into_body())?)
|
self.as_mut().send_response(res, body.into_body())?
|
||||||
}
|
}
|
||||||
Poll::Pending => None,
|
Poll::Pending => None,
|
||||||
},
|
},
|
||||||
|
@ -457,10 +453,7 @@ where
|
||||||
|
|
||||||
// set new state
|
// set new state
|
||||||
if let Some(state) = state {
|
if let Some(state) = state {
|
||||||
// only set state when it's not noop
|
this.state.set(state);
|
||||||
if !state.is_noop() {
|
|
||||||
this.state.set(state);
|
|
||||||
}
|
|
||||||
if !self.state.is_empty() {
|
if !self.state.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -485,7 +478,7 @@ where
|
||||||
mut self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
req: Request,
|
req: Request,
|
||||||
cx: &mut Context<'_>,
|
cx: &mut Context<'_>,
|
||||||
) -> Result<State<S, B, X>, DispatchError> {
|
) -> Result<Option<State<S, B, X>>, DispatchError> {
|
||||||
// Handle `EXPECT: 100-Continue` header
|
// Handle `EXPECT: 100-Continue` header
|
||||||
if req.head().expect() {
|
if req.head().expect() {
|
||||||
// set dispatcher state so the future is pinned.
|
// set dispatcher state so the future is pinned.
|
||||||
|
@ -511,7 +504,7 @@ where
|
||||||
}
|
}
|
||||||
// future is pending. return NoOp state to notify that we already set
|
// future is pending. return NoOp state to notify that we already set
|
||||||
// the state and it should not be updated again.
|
// the state and it should not be updated again.
|
||||||
Poll::Pending => return Ok(State::NoOp),
|
Poll::Pending => return Ok(None),
|
||||||
// future is error. send response and return a state on success to notify
|
// future is error. send response and return a state on success to notify
|
||||||
// the dispatcher state should be updated.
|
// the dispatcher state should be updated.
|
||||||
Poll::Ready(Err(e)) => {
|
Poll::Ready(Err(e)) => {
|
||||||
|
@ -532,7 +525,7 @@ where
|
||||||
self.send_response(res, body)
|
self.send_response(res, body)
|
||||||
}
|
}
|
||||||
// see the comment on ExpectCall state branch's Pending.
|
// see the comment on ExpectCall state branch's Pending.
|
||||||
Poll::Pending => Ok(State::NoOp),
|
Poll::Pending => Ok(None),
|
||||||
// see the comment on ExpectCall state branch's Ready(Err(e)).
|
// see the comment on ExpectCall state branch's Ready(Err(e)).
|
||||||
Poll::Ready(Err(e)) => {
|
Poll::Ready(Err(e)) => {
|
||||||
let res: Response = e.into().into();
|
let res: Response = e.into().into();
|
||||||
|
@ -594,9 +587,10 @@ where
|
||||||
|
|
||||||
// handle request early
|
// handle request early
|
||||||
if this.state.is_empty() {
|
if this.state.is_empty() {
|
||||||
|
// State can be set here
|
||||||
let state = self.as_mut().handle_request(req, cx)?;
|
let state = self.as_mut().handle_request(req, cx)?;
|
||||||
this = self.as_mut().project();
|
this = self.as_mut().project();
|
||||||
if !state.is_noop() {
|
if let Some(state) = state {
|
||||||
this.state.set(state);
|
this.state.set(state);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue