1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-09-01 01:16:59 +02:00

simplify Frame::Message; impl Try for Reply

This commit is contained in:
Nikolay Kim
2017-10-13 14:43:17 -07:00
parent c0e73c7275
commit 0447c66de1
13 changed files with 226 additions and 141 deletions

View File

@@ -1,8 +1,8 @@
//! Pieces pertaining to the HTTP message protocol.
use std::{io, mem};
use std::error::Error as StdError;
use std::{io, mem, str};
use std::convert::Into;
use cookie;
use bytes::Bytes;
use http::{Method, StatusCode, Version, Uri, HeaderMap, HttpTryFrom, Error};
use http::header::{self, HeaderName, HeaderValue};
@@ -78,6 +78,17 @@ impl HttpRequest {
self.uri.query()
}
/// Return request cookie.
pub fn cookie(&self) -> Result<Option<cookie::Cookie>, cookie::ParseError> {
if let Some(val) = self.headers.get(header::COOKIE) {
let s = str::from_utf8(val.as_bytes())
.map_err(|e| cookie::ParseError::from(e))?;
cookie::Cookie::parse(s).map(|c| Some(c))
} else {
Ok(None)
}
}
/// Get a mutable reference to the Request headers.
#[inline]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
@@ -300,13 +311,7 @@ impl HttpResponse {
}
}
impl From<Error> for HttpResponse {
fn from(err: Error) -> Self {
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR,
Body::Binary(err.description().into()))
}
}
/// Helper conversion implementation
impl<I: Into<HttpResponse>, E: Into<HttpResponse>> From<Result<I, E>> for HttpResponse {
fn from(res: Result<I, E>) -> Self {
match res {