1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-09-09 05:08:32 +00:00

fix failure Send+Sync compatibility

This commit is contained in:
Nikolay Kim 2018-06-21 10:45:24 +06:00
parent dbd093075d
commit c2c4a5ba3f
2 changed files with 61 additions and 23 deletions

View file

@ -24,7 +24,7 @@ pub use cookie::ParseError as CookieParseError;
use handler::Responder;
use httprequest::HttpRequest;
use httpresponse::{HttpResponse, InnerHttpResponse};
use httpresponse::{HttpResponse, HttpResponseParts};
/// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html)
/// for actix web operations
@ -654,7 +654,7 @@ pub struct InternalError<T> {
enum InternalErrorType {
Status(StatusCode),
Response(Mutex<Option<Box<InnerHttpResponse>>>),
Response(Mutex<Option<HttpResponseParts>>),
}
impl<T> InternalError<T> {
@ -669,9 +669,7 @@ impl<T> InternalError<T> {
/// Create `InternalError` with predefined `HttpResponse`.
pub fn from_response(cause: T, response: HttpResponse) -> Self {
let mut resp = response.into_inner();
resp.drop_unsupported_body();
let resp = response.into_parts();
InternalError {
cause,
status: InternalErrorType::Response(Mutex::new(Some(resp))),
@ -716,7 +714,7 @@ where
InternalErrorType::Status(st) => HttpResponse::new(st),
InternalErrorType::Response(ref resp) => {
if let Some(resp) = resp.lock().unwrap().take() {
HttpResponse::from_inner(resp)
HttpResponse::from_parts(resp)
} else {
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR)
}

View file

@ -299,12 +299,15 @@ impl HttpResponse {
self.get_mut().write_capacity = cap;
}
pub(crate) fn into_inner(mut self) -> Box<InnerHttpResponse> {
self.0.take().unwrap()
pub(crate) fn into_parts(mut self) -> HttpResponseParts {
self.0.take().unwrap().into_parts()
}
pub(crate) fn from_inner(inner: Box<InnerHttpResponse>) -> HttpResponse {
HttpResponse(Some(inner), HttpResponsePool::pool())
pub(crate) fn from_parts(parts: HttpResponseParts) -> HttpResponse {
HttpResponse(
Some(Box::new(InnerHttpResponse::from_parts(parts))),
HttpResponsePool::pool(),
)
}
}
@ -880,12 +883,12 @@ impl<'a, S> From<&'a HttpRequest<S>> for HttpResponseBuilder {
}
#[derive(Debug)]
pub(crate) struct InnerHttpResponse {
struct InnerHttpResponse {
version: Option<Version>,
headers: HeaderMap,
status: StatusCode,
reason: Option<&'static str>,
pub(crate) body: Body,
body: Body,
chunked: Option<bool>,
encoding: Option<ContentEncoding>,
connection_type: Option<ConnectionType>,
@ -894,10 +897,16 @@ pub(crate) struct InnerHttpResponse {
error: Option<Error>,
}
/// This is here only because `failure::Fail: Send + Sync` which looks insane to me
unsafe impl Sync for InnerHttpResponse {}
/// This is here only because `failure::Fail: Send + Sync` which looks insane to me
unsafe impl Send for InnerHttpResponse {}
pub(crate) struct HttpResponseParts {
version: Option<Version>,
headers: HeaderMap,
status: StatusCode,
reason: Option<&'static str>,
body: Option<Bytes>,
encoding: Option<ContentEncoding>,
connection_type: Option<ConnectionType>,
error: Option<Error>,
}
impl InnerHttpResponse {
#[inline]
@ -918,16 +927,47 @@ impl InnerHttpResponse {
}
/// This is for failure, we can not have Send + Sync on Streaming and Actor response
pub(crate) fn drop_unsupported_body(&mut self) {
let body = mem::replace(&mut self.body, Body::Empty);
match body {
Body::Empty => (),
Body::Binary(mut bin) => {
self.body = Body::Binary(bin.take().into());
}
fn into_parts(mut self) -> HttpResponseParts {
let body = match mem::replace(&mut self.body, Body::Empty) {
Body::Empty => None,
Body::Binary(mut bin) => Some(bin.take()),
Body::Streaming(_) | Body::Actor(_) => {
error!("Streaming or Actor body is not support by error response");
None
}
};
HttpResponseParts {
body,
version: self.version,
headers: self.headers,
status: self.status,
reason: self.reason,
encoding: self.encoding,
connection_type: self.connection_type,
error: self.error,
}
}
fn from_parts(parts: HttpResponseParts) -> InnerHttpResponse {
let body = if let Some(ref body) = parts.body {
Body::Binary(body.clone().into())
} else {
Body::Empty
};
InnerHttpResponse {
body,
status: parts.status,
version: parts.version,
headers: parts.headers,
reason: parts.reason,
chunked: None,
encoding: parts.encoding,
connection_type: parts.connection_type,
response_size: 0,
write_capacity: MAX_WRITE_BUFFER_SIZE,
error: parts.error,
}
}
}