1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 16:32:59 +01:00
actix-web/src/httpmessage.rs

379 lines
10 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
//! Pieces pertaining to the HTTP message protocol.
use std::{io, mem};
use std::str::FromStr;
use std::convert::Into;
use bytes::Bytes;
use http::{Method, StatusCode, Version, Uri};
use hyper::header::{Header, Headers};
use hyper::header::{Connection, ConnectionOption,
Expect, Encoding, ContentLength, TransferEncoding};
use Params;
use error::Error;
2017-10-08 06:48:00 +02:00
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ConnectionType {
Close,
KeepAlive,
Upgrade,
}
2017-10-07 06:48:14 +02:00
pub trait Message {
fn version(&self) -> Version;
fn headers(&self) -> &Headers;
/// Checks if a connection should be kept alive.
fn should_keep_alive(&self) -> bool {
let ret = match (self.version(), self.headers().get::<Connection>()) {
(Version::HTTP_10, None) => false,
(Version::HTTP_10, Some(conn))
if !conn.contains(&ConnectionOption::KeepAlive) => false,
(Version::HTTP_11, Some(conn))
if conn.contains(&ConnectionOption::Close) => false,
_ => true
};
trace!("should_keep_alive(version={:?}, header={:?}) = {:?}",
self.version(), self.headers().get::<Connection>(), ret);
ret
}
/// Checks if a connection is expecting a `100 Continue` before sending its body.
#[inline]
fn expecting_continue(&self) -> bool {
let ret = match (self.version(), self.headers().get::<Expect>()) {
(Version::HTTP_11, Some(&Expect::Continue)) => true,
_ => false
};
trace!("expecting_continue(version={:?}, header={:?}) = {:?}",
self.version(), self.headers().get::<Expect>(), ret);
ret
}
fn is_chunked(&self) -> Result<bool, Error> {
if let Some(&TransferEncoding(ref encodings)) = self.headers().get() {
// https://tools.ietf.org/html/rfc7230#section-3.3.3
// If Transfer-Encoding header is present, and 'chunked' is
// not the final encoding, and this is a Request, then it is
// mal-formed. A server should responsed with 400 Bad Request.
if encodings.last() == Some(&Encoding::Chunked) {
Ok(true)
} else {
debug!("request with transfer-encoding header, but not chunked, bad request");
Err(Error::Header)
}
} else {
Ok(false)
}
}
}
#[derive(Debug)]
/// An HTTP Request
pub struct HttpRequest {
version: Version,
method: Method,
uri: Uri,
headers: Headers,
params: Params,
}
impl Message for HttpRequest {
fn version(&self) -> Version {
self.version
}
fn headers(&self) -> &Headers {
&self.headers
}
}
impl HttpRequest {
/// Construct a new Request.
#[inline]
pub fn new(method: Method, uri: Uri, version: Version, headers: Headers) -> Self {
HttpRequest {
method: method,
uri: uri,
version: version,
headers: headers,
params: Params::new(),
}
}
/// Read the Request Uri.
#[inline]
pub fn uri(&self) -> &Uri { &self.uri }
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version { self.version }
/// Read the Request headers.
#[inline]
pub fn headers(&self) -> &Headers { &self.headers }
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method { &self.method }
// /// The remote socket address of this request
// ///
// /// This is an `Option`, because some underlying transports may not have
// /// a socket address, such as Unix Sockets.
// ///
// /// This field is not used for outgoing requests.
// #[inline]
// pub fn remote_addr(&self) -> Option<SocketAddr> { self.remote_addr }
/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
self.uri.path()
}
/// The query string of this Request.
#[inline]
pub fn query(&self) -> Option<&str> {
self.uri.query()
}
/// Get a mutable reference to the Request headers.
#[inline]
pub fn headers_mut(&mut self) -> &mut Headers {
&mut self.headers
}
2017-10-08 08:59:57 +02:00
/// Get a reference to the Params object.
/// Params is a container for url parameters.
/// Route supports glob patterns: * for a single wildcard segment and :param
/// for matching storing that segment of the request url in the Params object.
2017-10-07 06:48:14 +02:00
#[inline]
pub fn params(&self) -> &Params { &self.params }
2017-10-08 08:59:57 +02:00
/// Create new request with Params object.
2017-10-07 06:48:14 +02:00
pub fn with_params(self, params: Params) -> Self {
HttpRequest {
method: self.method,
uri: self.uri,
version: self.version,
headers: self.headers,
params: params
}
}
2017-10-08 06:48:00 +02:00
2017-10-08 23:56:51 +02:00
/// Is keepalive enabled by client?
pub fn keep_alive(&self) -> bool {
let ret = match (self.version(), self.headers().get::<Connection>()) {
(Version::HTTP_10, None) => false,
(Version::HTTP_10, Some(conn))
if !conn.contains(&ConnectionOption::KeepAlive) => false,
(Version::HTTP_11, Some(conn))
if conn.contains(&ConnectionOption::Close) => false,
_ => true
};
trace!("should_keep_alive(version={:?}, header={:?}) = {:?}",
self.version(), self.headers().get::<Connection>(), ret);
ret
}
2017-10-08 08:59:57 +02:00
pub(crate) fn is_upgrade(&self) -> bool {
2017-10-08 06:48:00 +02:00
if let Some(&Connection(ref conn)) = self.headers().get() {
conn.contains(&ConnectionOption::from_str("upgrade").unwrap())
} else {
false
}
}
2017-10-07 06:48:14 +02:00
}
2017-10-07 08:14:13 +02:00
/// Represents various types of http message body.
2017-10-07 06:48:14 +02:00
#[derive(Debug)]
pub enum Body {
2017-10-07 08:14:13 +02:00
/// Empty response. `Content-Length` header is set to `0`
2017-10-07 06:48:14 +02:00
Empty,
2017-10-07 08:14:13 +02:00
/// Specific response body. `Content-Length` header is set to length of bytes.
2017-10-07 06:48:14 +02:00
Binary(Bytes),
2017-10-07 08:14:13 +02:00
/// Streaming response body with specified length.
2017-10-07 06:48:14 +02:00
Length(u64),
2017-10-07 08:14:13 +02:00
/// Unspecified streaming response. Developer is responsible for setting
/// right `Content-Length` or `Transfer-Encoding` headers.
2017-10-07 06:48:14 +02:00
Streaming,
2017-10-08 06:48:00 +02:00
/// Upgrade connection.
Upgrade,
2017-10-07 06:48:14 +02:00
}
impl Body {
2017-10-07 08:14:13 +02:00
/// Does this body have payload.
2017-10-07 06:48:14 +02:00
pub fn has_body(&self) -> bool {
match *self {
Body::Length(_) | Body::Streaming => true,
_ => false
}
}
}
2017-10-08 23:56:51 +02:00
/// Implements by something that can be converted to `HttpResponse`
2017-10-07 08:14:13 +02:00
pub trait IntoHttpResponse {
/// Convert into response.
2017-10-08 06:48:00 +02:00
fn response(self, req: HttpRequest) -> HttpResponse;
2017-10-07 06:48:14 +02:00
}
#[derive(Debug)]
/// An HTTP Response
2017-10-07 08:14:13 +02:00
pub struct HttpResponse {
2017-10-07 06:48:14 +02:00
request: HttpRequest,
pub version: Version,
pub headers: Headers,
pub status: StatusCode,
2017-10-08 06:48:00 +02:00
reason: Option<&'static str>,
2017-10-07 06:48:14 +02:00
body: Body,
chunked: bool,
compression: Option<Encoding>,
2017-10-08 06:48:00 +02:00
connection_type: Option<ConnectionType>,
2017-10-07 06:48:14 +02:00
}
2017-10-07 08:14:13 +02:00
impl Message for HttpResponse {
2017-10-07 06:48:14 +02:00
fn version(&self) -> Version {
self.version
}
fn headers(&self) -> &Headers {
&self.headers
}
}
2017-10-07 08:14:13 +02:00
impl HttpResponse {
/// Constructs a response
2017-10-07 06:48:14 +02:00
#[inline]
2017-10-07 08:14:13 +02:00
pub fn new(request: HttpRequest, status: StatusCode, body: Body) -> HttpResponse {
2017-10-07 06:48:14 +02:00
let version = request.version;
2017-10-07 08:14:13 +02:00
HttpResponse {
2017-10-07 06:48:14 +02:00
request: request,
version: version,
headers: Default::default(),
status: status,
2017-10-08 06:48:00 +02:00
reason: None,
2017-10-07 06:48:14 +02:00
body: body,
chunked: false,
compression: None,
2017-10-08 06:48:00 +02:00
connection_type: None,
2017-10-07 06:48:14 +02:00
}
}
2017-10-08 06:48:00 +02:00
/// Original prequest
#[inline]
pub fn request(&self) -> &HttpRequest {
&self.request
}
2017-10-07 06:48:14 +02:00
/// Get the HTTP version of this response.
#[inline]
pub fn version(&self) -> Version {
self.version
}
/// Get the headers from the response.
#[inline]
pub fn headers(&self) -> &Headers {
&self.headers
}
/// Get a mutable reference to the headers.
#[inline]
pub fn headers_mut(&mut self) -> &mut Headers {
&mut self.headers
}
/// Get the status from the server.
#[inline]
pub fn status(&self) -> StatusCode {
self.status
}
/// Set the `StatusCode` for this response.
#[inline]
2017-10-08 06:48:00 +02:00
pub fn set_status(mut self, status: StatusCode) -> Self {
2017-10-07 06:48:14 +02:00
self.status = status;
self
}
/// Set a header and move the Response.
#[inline]
2017-10-08 06:48:00 +02:00
pub fn set_header<H: Header>(mut self, header: H) -> Self {
2017-10-07 06:48:14 +02:00
self.headers.set(header);
self
}
2017-10-08 06:48:00 +02:00
/// Set the headers.
2017-10-07 06:48:14 +02:00
#[inline]
2017-10-08 06:48:00 +02:00
pub fn with_headers(mut self, headers: Headers) -> Self {
2017-10-07 06:48:14 +02:00
self.headers = headers;
self
}
2017-10-08 06:48:00 +02:00
/// Set the custom reason for the response.
#[inline]
pub fn set_reason(mut self, reason: &'static str) -> Self {
self.reason = Some(reason);
self
}
/// Set connection type
pub fn set_connection_type(mut self, conn: ConnectionType) -> Self {
self.connection_type = Some(conn);
self
}
/// Connection upgrade status
pub fn upgrade(&self) -> bool {
self.connection_type == Some(ConnectionType::Upgrade)
}
2017-10-07 06:48:14 +02:00
/// Keep-alive status for this connection
pub fn keep_alive(&self) -> bool {
2017-10-08 06:48:00 +02:00
if let Some(ConnectionType::KeepAlive) = self.connection_type {
true
2017-10-07 06:48:14 +02:00
} else {
self.request.should_keep_alive()
}
}
2017-10-08 06:48:00 +02:00
2017-10-07 06:48:14 +02:00
/// Force close connection, even if it is marked as keep-alive
pub fn force_close(&mut self) {
2017-10-08 06:48:00 +02:00
self.connection_type = Some(ConnectionType::Close);
2017-10-07 06:48:14 +02:00
}
/// is chunked encoding enabled
pub fn chunked(&self) -> bool {
self.chunked
}
/// Enables automatic chunked transfer encoding
pub fn enable_chunked_encoding(&mut self) -> Result<(), io::Error> {
if self.headers.has::<ContentLength>() {
Err(io::Error::new(io::ErrorKind::Other,
"You can't enable chunked encoding when a content length is set"))
} else {
self.chunked = true;
Ok(())
}
}
2017-10-07 08:14:13 +02:00
/// Get body os this response
2017-10-07 06:48:14 +02:00
pub fn body(&self) -> &Body {
&self.body
}
2017-10-08 06:48:00 +02:00
/// Set a body
pub fn set_body<B: Into<Body>>(mut self, body: B) -> Self {
self.body = body.into();
self
}
2017-10-07 08:14:13 +02:00
/// Set a body and return previous body value
2017-10-08 06:48:00 +02:00
pub fn replace_body<B: Into<Body>>(&mut self, body: B) -> Body {
2017-10-07 06:48:14 +02:00
mem::replace(&mut self.body, body.into())
}
}