2017-10-16 19:31:31 +02:00
|
|
|
//! HTTP Request message related code.
|
2018-02-25 19:26:58 +01:00
|
|
|
use std::{io, cmp, str, fmt, mem};
|
2017-11-27 05:32:12 +01:00
|
|
|
use std::rc::Rc;
|
2017-11-10 22:08:15 +01:00
|
|
|
use std::net::SocketAddr;
|
2017-10-18 01:46:57 +02:00
|
|
|
use std::collections::HashMap;
|
2018-01-03 18:23:58 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2017-12-08 01:40:29 +01:00
|
|
|
use cookie::Cookie;
|
2018-02-25 19:26:58 +01:00
|
|
|
use futures::{Async, Future, Stream, Poll};
|
2017-12-08 01:40:29 +01:00
|
|
|
use http_range::HttpRange;
|
2017-12-21 05:30:54 +01:00
|
|
|
use serde::de::DeserializeOwned;
|
2018-01-26 06:50:28 +01:00
|
|
|
use mime::Mime;
|
2018-02-25 19:26:58 +01:00
|
|
|
use failure;
|
2017-12-09 22:25:06 +01:00
|
|
|
use url::{Url, form_urlencoded};
|
2017-12-01 04:01:25 +01:00
|
|
|
use http::{header, Uri, Method, Version, HeaderMap, Extensions};
|
2018-02-25 19:26:58 +01:00
|
|
|
use tokio_io::AsyncRead;
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-12-06 02:09:15 +01:00
|
|
|
use info::ConnectionInfo;
|
2017-12-08 01:22:26 +01:00
|
|
|
use param::Params;
|
2017-12-07 01:26:27 +01:00
|
|
|
use router::Router;
|
2018-02-25 09:21:45 +01:00
|
|
|
use payload::Payload;
|
2017-12-21 05:30:54 +01:00
|
|
|
use json::JsonBody;
|
2017-11-16 07:06:28 +01:00
|
|
|
use multipart::Multipart;
|
2017-12-15 04:34:31 +01:00
|
|
|
use helpers::SharedHttpMessage;
|
2018-01-03 18:23:58 +01:00
|
|
|
use error::{ParseError, UrlGenerationError,
|
|
|
|
CookieParseError, HttpRangeError, PayloadError, UrlencodedError};
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-12-06 02:09:15 +01:00
|
|
|
|
2017-12-09 13:33:40 +01:00
|
|
|
pub struct HttpMessage {
|
|
|
|
pub version: Version,
|
|
|
|
pub method: Method,
|
|
|
|
pub uri: Uri,
|
|
|
|
pub headers: HeaderMap,
|
|
|
|
pub extensions: Extensions,
|
|
|
|
pub params: Params<'static>,
|
|
|
|
pub cookies: Option<Vec<Cookie<'static>>>,
|
2017-12-28 04:02:29 +01:00
|
|
|
pub query: Params<'static>,
|
|
|
|
pub query_loaded: bool,
|
2017-12-09 13:33:40 +01:00
|
|
|
pub addr: Option<SocketAddr>,
|
2017-12-13 06:32:58 +01:00
|
|
|
pub payload: Option<Payload>,
|
2017-12-09 13:33:40 +01:00
|
|
|
pub info: Option<ConnectionInfo<'static>>,
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 05:32:12 +01:00
|
|
|
impl Default for HttpMessage {
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-11-27 05:32:12 +01:00
|
|
|
fn default() -> HttpMessage {
|
|
|
|
HttpMessage {
|
2017-10-22 07:59:09 +02:00
|
|
|
method: Method::GET,
|
2017-12-01 04:01:25 +01:00
|
|
|
uri: Uri::default(),
|
2017-10-22 07:59:09 +02:00
|
|
|
version: Version::HTTP_11,
|
2017-12-16 07:49:48 +01:00
|
|
|
headers: HeaderMap::with_capacity(16),
|
2017-12-28 04:19:28 +01:00
|
|
|
params: Params::new(),
|
|
|
|
query: Params::new(),
|
2017-12-28 04:02:29 +01:00
|
|
|
query_loaded: false,
|
2017-12-08 03:00:20 +01:00
|
|
|
cookies: None,
|
2017-11-10 22:08:15 +01:00
|
|
|
addr: None,
|
2017-12-13 06:32:58 +01:00
|
|
|
payload: None,
|
2017-11-27 05:32:12 +01:00
|
|
|
extensions: Extensions::new(),
|
2017-12-06 02:09:15 +01:00
|
|
|
info: None,
|
2017-10-22 07:59:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 05:32:12 +01:00
|
|
|
}
|
|
|
|
|
2017-12-09 13:33:40 +01:00
|
|
|
impl HttpMessage {
|
|
|
|
|
|
|
|
/// Checks if a connection should be kept alive.
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2017-12-09 13:33:40 +01:00
|
|
|
pub fn keep_alive(&self) -> bool {
|
|
|
|
if let Some(conn) = self.headers.get(header::CONNECTION) {
|
|
|
|
if let Ok(conn) = conn.to_str() {
|
|
|
|
if self.version == Version::HTTP_10 && conn.contains("keep-alive") {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.version == Version::HTTP_11 &&
|
|
|
|
!(conn.contains("close") || conn.contains("upgrade"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.version != Version::HTTP_10
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 04:34:31 +01:00
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2017-12-15 04:34:31 +01:00
|
|
|
pub(crate) fn reset(&mut self) {
|
|
|
|
self.headers.clear();
|
|
|
|
self.extensions.clear();
|
|
|
|
self.params.clear();
|
2017-12-28 04:02:29 +01:00
|
|
|
self.query.clear();
|
|
|
|
self.query_loaded = false;
|
2017-12-16 07:49:48 +01:00
|
|
|
self.cookies = None;
|
|
|
|
self.addr = None;
|
|
|
|
self.info = None;
|
2017-12-19 09:18:57 +01:00
|
|
|
self.payload = None;
|
2017-12-15 04:34:31 +01:00
|
|
|
}
|
2017-12-09 13:33:40 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 05:32:12 +01:00
|
|
|
/// An HTTP Request
|
2017-12-26 18:00:45 +01:00
|
|
|
pub struct HttpRequest<S=()>(SharedHttpMessage, Option<Rc<S>>, Option<Router>);
|
2017-11-27 05:32:12 +01:00
|
|
|
|
2017-11-27 06:18:38 +01:00
|
|
|
impl HttpRequest<()> {
|
2017-11-27 05:32:12 +01:00
|
|
|
/// Construct a new Request.
|
|
|
|
#[inline]
|
2017-12-01 04:01:25 +01:00
|
|
|
pub fn new(method: Method, uri: Uri,
|
2017-12-13 06:32:58 +01:00
|
|
|
version: Version, headers: HeaderMap, payload: Option<Payload>) -> HttpRequest
|
2017-11-27 05:32:12 +01:00
|
|
|
{
|
|
|
|
HttpRequest(
|
2017-12-15 04:34:31 +01:00
|
|
|
SharedHttpMessage::from_message(HttpMessage {
|
2018-02-26 23:33:56 +01:00
|
|
|
method,
|
|
|
|
uri,
|
|
|
|
version,
|
|
|
|
headers,
|
|
|
|
payload,
|
2017-12-28 04:19:28 +01:00
|
|
|
params: Params::new(),
|
|
|
|
query: Params::new(),
|
2017-12-28 04:02:29 +01:00
|
|
|
query_loaded: false,
|
2017-12-08 03:00:20 +01:00
|
|
|
cookies: None,
|
2017-11-27 05:32:12 +01:00
|
|
|
addr: None,
|
|
|
|
extensions: Extensions::new(),
|
2017-12-06 02:09:15 +01:00
|
|
|
info: None,
|
2017-12-09 22:25:06 +01:00
|
|
|
}),
|
2017-12-13 06:32:58 +01:00
|
|
|
None,
|
2017-12-09 22:25:06 +01:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline(always)]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
2017-12-15 04:34:31 +01:00
|
|
|
pub(crate) fn from_message(msg: SharedHttpMessage) -> HttpRequest {
|
|
|
|
HttpRequest(msg, None, None)
|
|
|
|
}
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2017-11-27 06:18:38 +01:00
|
|
|
/// Construct new http request with state.
|
2017-12-26 18:00:45 +01:00
|
|
|
pub fn with_state<S>(self, state: Rc<S>, router: Router) -> HttpRequest<S> {
|
2017-12-13 06:32:58 +01:00
|
|
|
HttpRequest(self.0, Some(state), Some(router))
|
2017-11-27 06:18:38 +01:00
|
|
|
}
|
2018-01-03 19:57:57 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
/// Construct new http request with state.
|
|
|
|
pub(crate) fn with_state_no_router<S>(self, state: Rc<S>) -> HttpRequest<S> {
|
|
|
|
HttpRequest(self.0, Some(state), None)
|
|
|
|
}
|
2017-11-27 06:18:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> HttpRequest<S> {
|
|
|
|
|
2018-01-01 02:26:32 +01:00
|
|
|
#[inline]
|
|
|
|
/// Construct new http request with state.
|
2018-01-03 08:43:17 +01:00
|
|
|
pub fn change_state<NS>(&self, state: Rc<NS>) -> HttpRequest<NS> {
|
|
|
|
HttpRequest(self.0.clone(), Some(state), self.2.clone())
|
2018-01-01 02:26:32 +01:00
|
|
|
}
|
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline]
|
2017-12-03 01:37:21 +01:00
|
|
|
/// Construct new http request without state.
|
2018-02-26 23:33:56 +01:00
|
|
|
pub(crate) fn without_state(&self) -> HttpRequest {
|
2017-12-15 04:34:31 +01:00
|
|
|
HttpRequest(self.0.clone(), None, None)
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
|
2018-02-25 18:34:26 +01:00
|
|
|
/// get mutable reference for inner message
|
|
|
|
/// mutable reference should not be returned as result for request's method
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline(always)]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
|
2017-12-27 04:48:02 +01:00
|
|
|
pub(crate) fn as_mut(&self) -> &mut HttpMessage {
|
2017-12-15 04:34:31 +01:00
|
|
|
self.0.get_mut()
|
2017-11-27 05:32:12 +01:00
|
|
|
}
|
2017-10-22 07:59:09 +02:00
|
|
|
|
2017-12-16 07:49:48 +01:00
|
|
|
#[inline(always)]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref, inline_always))]
|
2017-12-15 04:34:31 +01:00
|
|
|
fn as_ref(&self) -> &HttpMessage {
|
|
|
|
self.0.get_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-12-09 13:33:40 +01:00
|
|
|
pub(crate) fn get_inner(&mut self) -> &mut HttpMessage {
|
|
|
|
self.as_mut()
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:18:38 +01:00
|
|
|
/// Shared application state
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-11-27 06:18:38 +01:00
|
|
|
pub fn state(&self) -> &S {
|
2017-12-13 06:32:58 +01:00
|
|
|
self.1.as_ref().unwrap()
|
2017-11-27 06:18:38 +01:00
|
|
|
}
|
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
/// Protocol extensions.
|
|
|
|
#[inline]
|
|
|
|
pub fn extensions(&mut self) -> &mut Extensions {
|
2017-11-27 05:32:12 +01:00
|
|
|
&mut self.as_mut().extensions
|
2017-10-22 07:59:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 02:09:15 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn prefix_len(&self) -> usize {
|
2017-12-16 07:49:48 +01:00
|
|
|
if let Some(router) = self.router() { router.prefix().len() } else { 0 }
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 04:01:25 +01:00
|
|
|
/// Read the Request Uri.
|
|
|
|
#[inline]
|
2017-12-15 04:34:31 +01:00
|
|
|
pub fn uri(&self) -> &Uri { &self.as_ref().uri }
|
2017-12-01 04:01:25 +01:00
|
|
|
|
2018-02-03 17:31:32 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
|
|
|
/// Modify the Request Uri.
|
|
|
|
///
|
|
|
|
/// This might be useful for middlewares, i.e. path normalization
|
|
|
|
pub fn uri_mut(&mut self) -> &mut Uri {
|
|
|
|
&mut self.as_mut().uri
|
|
|
|
}
|
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Read the Request method.
|
|
|
|
#[inline]
|
2017-12-15 04:34:31 +01:00
|
|
|
pub fn method(&self) -> &Method { &self.as_ref().method }
|
2017-10-15 07:52:38 +02:00
|
|
|
|
|
|
|
/// Read the Request Version.
|
2017-11-10 22:26:12 +01:00
|
|
|
#[inline]
|
2017-10-15 07:52:38 +02:00
|
|
|
pub fn version(&self) -> Version {
|
2017-12-15 04:34:31 +01:00
|
|
|
self.as_ref().version
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the Request Headers.
|
2017-11-10 22:26:12 +01:00
|
|
|
#[inline]
|
2017-10-15 07:52:38 +02:00
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2017-12-15 04:34:31 +01:00
|
|
|
&self.as_ref().headers
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:24:05 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline]
|
2017-12-06 06:38:52 +01:00
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
|
|
|
&mut self.as_mut().headers
|
|
|
|
}
|
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
/// The target path of this Request.
|
|
|
|
#[inline]
|
|
|
|
pub fn path(&self) -> &str {
|
2017-12-19 20:46:11 +01:00
|
|
|
self.uri().path()
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Get *ConnectionInfo* for correct request.
|
2017-12-08 02:38:18 +01:00
|
|
|
pub fn connection_info(&self) -> &ConnectionInfo {
|
2017-12-15 04:34:31 +01:00
|
|
|
if self.as_ref().info.is_none() {
|
2017-12-06 02:09:15 +01:00
|
|
|
let info: ConnectionInfo<'static> = unsafe{
|
|
|
|
mem::transmute(ConnectionInfo::new(self))};
|
|
|
|
self.as_mut().info = Some(info);
|
|
|
|
}
|
2017-12-15 04:34:31 +01:00
|
|
|
self.as_ref().info.as_ref().unwrap()
|
2017-11-30 00:07:49 +01:00
|
|
|
}
|
|
|
|
|
2017-12-19 20:34:51 +01:00
|
|
|
/// Generate url for named resource
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use actix_web::httpcodes::*;
|
|
|
|
/// #
|
|
|
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
|
|
|
/// let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
|
|
|
|
/// HTTPOk.into()
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let app = Application::new()
|
|
|
|
/// .resource("/test/{one}/{two}/{three}", |r| {
|
|
|
|
/// r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
|
|
|
/// r.method(Method::GET).f(|_| httpcodes::HTTPOk);
|
|
|
|
/// })
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-12-08 02:38:18 +01:00
|
|
|
pub fn url_for<U, I>(&self, name: &str, elements: U) -> Result<Url, UrlGenerationError>
|
2017-12-07 01:26:27 +01:00
|
|
|
where U: IntoIterator<Item=I>,
|
|
|
|
I: AsRef<str>,
|
|
|
|
{
|
|
|
|
if self.router().is_none() {
|
|
|
|
Err(UrlGenerationError::RouterNotAvailable)
|
|
|
|
} else {
|
|
|
|
let path = self.router().unwrap().resource_path(name, elements)?;
|
2017-12-08 01:22:26 +01:00
|
|
|
if path.starts_with('/') {
|
2017-12-08 02:38:18 +01:00
|
|
|
let conn = self.connection_info();
|
2017-12-08 01:22:26 +01:00
|
|
|
Ok(Url::parse(&format!("{}://{}{}", conn.scheme(), conn.host(), path))?)
|
|
|
|
} else {
|
|
|
|
Ok(Url::parse(&path)?)
|
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:34:51 +01:00
|
|
|
/// This method returns reference to current `Router` object.
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-12-26 18:00:45 +01:00
|
|
|
pub fn router(&self) -> Option<&Router> {
|
2017-12-07 01:26:27 +01:00
|
|
|
self.2.as_ref()
|
|
|
|
}
|
|
|
|
|
2017-12-19 20:34:51 +01:00
|
|
|
/// Peer socket address
|
|
|
|
///
|
2018-01-15 22:47:25 +01:00
|
|
|
/// Peer address is actual socket address, if proxy is used in front of
|
2017-12-19 20:34:51 +01:00
|
|
|
/// actix http server, then peer address would be address of this proxy.
|
|
|
|
///
|
|
|
|
/// To get client connection information `connection_info()` method should be used.
|
2017-11-10 22:26:12 +01:00
|
|
|
#[inline]
|
2017-12-06 06:38:52 +01:00
|
|
|
pub fn peer_addr(&self) -> Option<&SocketAddr> {
|
2017-12-15 04:34:31 +01:00
|
|
|
self.as_ref().addr.as_ref()
|
2017-11-10 22:08:15 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-12-06 06:38:52 +01:00
|
|
|
pub(crate) fn set_peer_addr(&mut self, addr: Option<SocketAddr>) {
|
2017-11-27 05:32:12 +01:00
|
|
|
self.as_mut().addr = addr
|
2017-11-10 22:08:15 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 04:02:29 +01:00
|
|
|
/// Get a reference to the Params object.
|
|
|
|
/// Params is a container for url query parameters.
|
|
|
|
pub fn query(&self) -> &Params {
|
|
|
|
if !self.as_ref().query_loaded {
|
|
|
|
let params: &mut Params = unsafe{ mem::transmute(&mut self.as_mut().query) };
|
|
|
|
self.as_mut().query_loaded = true;
|
|
|
|
for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) {
|
|
|
|
params.add(key, val);
|
|
|
|
}
|
2017-10-29 14:03:21 +01:00
|
|
|
}
|
2017-12-28 04:02:29 +01:00
|
|
|
unsafe{ mem::transmute(&self.as_ref().query) }
|
2017-10-16 18:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The query string in the URL.
|
|
|
|
///
|
|
|
|
/// E.g., id=10
|
|
|
|
#[inline]
|
|
|
|
pub fn query_string(&self) -> &str {
|
2017-12-19 20:46:11 +01:00
|
|
|
if let Some(query) = self.uri().query().as_ref() {
|
2017-12-01 04:01:25 +01:00
|
|
|
query
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 03:00:20 +01:00
|
|
|
/// Load request cookies.
|
|
|
|
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
|
2017-12-15 04:34:31 +01:00
|
|
|
if self.as_ref().cookies.is_none() {
|
2017-11-27 05:32:12 +01:00
|
|
|
let msg = self.as_mut();
|
2017-12-08 03:00:20 +01:00
|
|
|
let mut cookies = Vec::new();
|
2017-11-27 05:32:12 +01:00
|
|
|
if let Some(val) = msg.headers.get(header::COOKIE) {
|
2017-11-09 06:01:56 +01:00
|
|
|
let s = str::from_utf8(val.as_bytes())
|
|
|
|
.map_err(CookieParseError::from)?;
|
|
|
|
for cookie in s.split("; ") {
|
2017-12-08 03:00:20 +01:00
|
|
|
cookies.push(Cookie::parse_encoded(cookie)?.into_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg.cookies = Some(cookies)
|
|
|
|
}
|
2017-12-15 04:34:31 +01:00
|
|
|
Ok(self.as_ref().cookies.as_ref().unwrap())
|
2017-12-08 03:00:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return request cookie.
|
|
|
|
pub fn cookie(&self, name: &str) -> Option<&Cookie> {
|
|
|
|
if let Ok(cookies) = self.cookies() {
|
|
|
|
for cookie in cookies {
|
|
|
|
if cookie.name() == name {
|
|
|
|
return Some(cookie)
|
2017-11-09 06:01:56 +01:00
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 03:00:20 +01:00
|
|
|
None
|
2017-10-15 07:52:38 +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.
|
|
|
|
#[inline]
|
2017-12-08 01:22:26 +01:00
|
|
|
pub fn match_info(&self) -> &Params {
|
2017-12-15 04:34:31 +01:00
|
|
|
unsafe{ mem::transmute(&self.as_ref().params) }
|
2017-12-08 01:22:26 +01:00
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-12-19 20:46:11 +01:00
|
|
|
/// Get mutable reference to request's Params.
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-12-08 01:22:26 +01:00
|
|
|
pub(crate) fn match_info_mut(&mut self) -> &mut Params {
|
|
|
|
unsafe{ mem::transmute(&mut self.as_mut().params) }
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if a connection should be kept alive.
|
|
|
|
pub fn keep_alive(&self) -> bool {
|
2017-12-16 07:49:48 +01:00
|
|
|
self.as_ref().keep_alive()
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-19 20:46:11 +01:00
|
|
|
/// Read the request content type. If request does not contain
|
|
|
|
/// *Content-Type* header, empty str get returned.
|
2017-10-29 14:03:21 +01:00
|
|
|
pub fn content_type(&self) -> &str {
|
2017-12-15 04:34:31 +01:00
|
|
|
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) {
|
2017-10-29 14:03:21 +01:00
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
2018-01-26 06:50:28 +01:00
|
|
|
return content_type.split(';').next().unwrap().trim()
|
2017-10-29 14:03:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:50:28 +01:00
|
|
|
/// Convert the request content type to a known mime type.
|
|
|
|
pub fn mime_type(&self) -> Option<Mime> {
|
|
|
|
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) {
|
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
|
|
|
return match content_type.parse() {
|
|
|
|
Ok(mt) => Some(mt),
|
|
|
|
Err(_) => None
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Check if request requires connection upgrade
|
|
|
|
pub(crate) fn upgrade(&self) -> bool {
|
2017-12-15 04:34:31 +01:00
|
|
|
if let Some(conn) = self.as_ref().headers.get(header::CONNECTION) {
|
2017-10-15 07:52:38 +02:00
|
|
|
if let Ok(s) = conn.to_str() {
|
|
|
|
return s.to_lowercase().contains("upgrade")
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 04:34:31 +01:00
|
|
|
self.as_ref().method == Method::CONNECT
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if request has chunked transfer encoding
|
2017-10-16 19:31:31 +02:00
|
|
|
pub fn chunked(&self) -> Result<bool, ParseError> {
|
2017-12-15 04:34:31 +01:00
|
|
|
if let Some(encodings) = self.headers().get(header::TRANSFER_ENCODING) {
|
2017-10-15 07:52:38 +02:00
|
|
|
if let Ok(s) = encodings.to_str() {
|
|
|
|
Ok(s.to_lowercase().contains("chunked"))
|
|
|
|
} else {
|
2017-10-16 19:31:31 +02:00
|
|
|
Err(ParseError::Header)
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses Range HTTP header string as per RFC 2616.
|
|
|
|
/// `size` is full size of response (file).
|
2017-11-16 07:06:28 +01:00
|
|
|
pub fn range(&self, size: u64) -> Result<Vec<HttpRange>, HttpRangeError> {
|
2017-12-15 04:34:31 +01:00
|
|
|
if let Some(range) = self.headers().get(header::RANGE) {
|
2017-10-15 07:52:38 +02:00
|
|
|
HttpRange::parse(unsafe{str::from_utf8_unchecked(range.as_bytes())}, size)
|
2017-11-16 07:06:28 +01:00
|
|
|
.map_err(|e| e.into())
|
2017-10-15 07:52:38 +02:00
|
|
|
} else {
|
|
|
|
Ok(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 01:46:57 +02:00
|
|
|
|
2018-01-03 18:23:58 +01:00
|
|
|
/// Load request body.
|
|
|
|
///
|
|
|
|
/// By default only 256Kb payload reads to a memory, then `BAD REQUEST`
|
|
|
|
/// http response get returns to a peer. Use `RequestBody::limit()`
|
|
|
|
/// method to change upper limit.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate bytes;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// # #[macro_use] extern crate serde_derive;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// use bytes::Bytes;
|
|
|
|
/// use futures::future::Future;
|
|
|
|
///
|
|
|
|
/// fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
/// req.body() // <- get Body future
|
|
|
|
/// .limit(1024) // <- change max size of the body to a 1kb
|
|
|
|
/// .from_err()
|
|
|
|
/// .and_then(|bytes: Bytes| { // <- complete body
|
|
|
|
/// println!("==== BODY ==== {:?}", bytes);
|
|
|
|
/// Ok(httpcodes::HTTPOk.into())
|
|
|
|
/// }).responder()
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-02-25 18:34:26 +01:00
|
|
|
pub fn body(self) -> RequestBody {
|
2018-02-26 23:33:56 +01:00
|
|
|
RequestBody::new(self.without_state())
|
2018-01-03 18:23:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 08:19:21 +01:00
|
|
|
/// Return stream to http payload processes as multipart.
|
2017-10-18 01:46:57 +02:00
|
|
|
///
|
|
|
|
/// Content-type: multipart/form-data;
|
2017-12-21 08:19:21 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix;
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate env_logger;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// # use std::str;
|
|
|
|
/// # use actix::*;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use futures::{Future, Stream};
|
|
|
|
/// # use futures::future::{ok, result, Either};
|
|
|
|
/// fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
/// req.multipart().from_err() // <- get multipart stream for current request
|
|
|
|
/// .and_then(|item| match item { // <- iterate over multipart items
|
|
|
|
/// multipart::MultipartItem::Field(field) => {
|
|
|
|
/// // Field in turn is stream of *Bytes* object
|
|
|
|
/// Either::A(field.from_err()
|
|
|
|
/// .map(|c| println!("-- CHUNK: \n{:?}", str::from_utf8(&c)))
|
|
|
|
/// .finish())
|
|
|
|
/// },
|
|
|
|
/// multipart::MultipartItem::Nested(mp) => {
|
|
|
|
/// // Or item could be nested Multipart stream
|
|
|
|
/// Either::B(ok(()))
|
|
|
|
/// }
|
|
|
|
/// })
|
|
|
|
/// .finish() // <- Stream::finish() combinator from actix
|
2018-01-01 02:26:32 +01:00
|
|
|
/// .map(|_| httpcodes::HTTPOk.into())
|
2017-12-21 08:19:21 +01:00
|
|
|
/// .responder()
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-02-26 03:55:07 +01:00
|
|
|
pub fn multipart(self) -> Multipart<HttpRequest<S>> {
|
|
|
|
let boundary = Multipart::boundary(self.headers());
|
|
|
|
Multipart::new(boundary, self)
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse `application/x-www-form-urlencoded` encoded body.
|
2017-10-19 08:43:50 +02:00
|
|
|
/// Return `UrlEncoded` future. It resolves to a `HashMap<String, String>` which
|
|
|
|
/// contains decoded parameters.
|
2017-10-18 01:46:57 +02:00
|
|
|
///
|
|
|
|
/// Returns error:
|
|
|
|
///
|
|
|
|
/// * content type is not `application/x-www-form-urlencoded`
|
|
|
|
/// * transfer encoding is `chunked`.
|
|
|
|
/// * content-length is greater than 256k
|
2017-12-19 23:03:01 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// use futures::future::{Future, ok};
|
|
|
|
///
|
|
|
|
/// fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
2017-12-21 06:06:04 +01:00
|
|
|
/// req.urlencoded() // <- get UrlEncoded future
|
|
|
|
/// .from_err()
|
|
|
|
/// .and_then(|params| { // <- url encoded parameters
|
|
|
|
/// println!("==== BODY ==== {:?}", params);
|
2018-01-01 02:26:32 +01:00
|
|
|
/// ok(httpcodes::HTTPOk.into())
|
2017-12-21 06:06:04 +01:00
|
|
|
/// })
|
|
|
|
/// .responder()
|
2017-12-19 23:03:01 +01:00
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-02-25 18:34:26 +01:00
|
|
|
pub fn urlencoded(self) -> UrlEncoded {
|
2018-02-26 23:33:56 +01:00
|
|
|
UrlEncoded::new(self.without_state())
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
2017-12-21 05:30:54 +01:00
|
|
|
|
|
|
|
/// Parse `application/json` encoded body.
|
|
|
|
/// Return `JsonBody<T>` future. It resolves to a `T` value.
|
|
|
|
///
|
|
|
|
/// Returns error:
|
|
|
|
///
|
|
|
|
/// * content type is not `application/json`
|
|
|
|
/// * content length is greater than 256k
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
|
|
|
/// # #[macro_use] extern crate serde_derive;
|
|
|
|
/// use actix_web::*;
|
|
|
|
/// use futures::future::{Future, ok};
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize, Debug)]
|
|
|
|
/// struct MyObj {
|
|
|
|
/// name: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|
|
|
/// req.json() // <- get JsonBody future
|
|
|
|
/// .from_err()
|
|
|
|
/// .and_then(|val: MyObj| { // <- deserialized value
|
|
|
|
/// println!("==== BODY ==== {:?}", val);
|
2018-01-01 02:26:32 +01:00
|
|
|
/// Ok(httpcodes::HTTPOk.into())
|
2017-12-21 05:30:54 +01:00
|
|
|
/// }).responder()
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
2018-02-25 18:34:26 +01:00
|
|
|
pub fn json<T: DeserializeOwned>(self) -> JsonBody<S, T> {
|
2017-12-21 05:30:54 +01:00
|
|
|
JsonBody::from_request(self)
|
|
|
|
}
|
2018-02-27 00:26:27 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn payload(&self) -> &Payload {
|
|
|
|
let msg = self.as_mut();
|
|
|
|
if msg.payload.is_none() {
|
|
|
|
msg.payload = Some(Payload::empty());
|
|
|
|
}
|
|
|
|
msg.payload.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn payload_mut(&mut self) -> &mut Payload {
|
|
|
|
let msg = self.as_mut();
|
|
|
|
if msg.payload.is_none() {
|
|
|
|
msg.payload = Some(Payload::empty());
|
|
|
|
}
|
|
|
|
msg.payload.as_mut().unwrap()
|
|
|
|
}
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 07:20:28 +01:00
|
|
|
impl Default for HttpRequest<()> {
|
|
|
|
|
|
|
|
/// Construct default request
|
|
|
|
fn default() -> HttpRequest {
|
2017-12-15 04:34:31 +01:00
|
|
|
HttpRequest(SharedHttpMessage::default(), None, None)
|
2017-11-27 07:20:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:18:38 +01:00
|
|
|
impl<S> Clone for HttpRequest<S> {
|
|
|
|
fn clone(&self) -> HttpRequest<S> {
|
2018-02-19 23:26:51 +01:00
|
|
|
HttpRequest(self.0.clone(), self.1.clone(), self.2.clone())
|
2017-11-27 05:32:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 09:21:45 +01:00
|
|
|
impl<S> Stream for HttpRequest<S> {
|
|
|
|
type Item = Bytes;
|
|
|
|
type Error = PayloadError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Bytes>, PayloadError> {
|
2018-02-27 00:26:27 +01:00
|
|
|
let msg = self.as_mut();
|
|
|
|
if msg.payload.is_none() {
|
|
|
|
Ok(Async::Ready(None))
|
|
|
|
} else {
|
|
|
|
msg.payload.as_mut().unwrap().poll()
|
|
|
|
}
|
2018-02-25 09:21:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 19:26:58 +01:00
|
|
|
impl<S> io::Read for HttpRequest<S> {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2018-02-27 00:26:27 +01:00
|
|
|
if self.as_mut().payload.is_some() {
|
|
|
|
match self.as_mut().payload.as_mut().unwrap().poll() {
|
|
|
|
Ok(Async::Ready(Some(mut b))) => {
|
|
|
|
let i = cmp::min(b.len(), buf.len());
|
|
|
|
buf.copy_from_slice(&b.split_to(i)[..i]);
|
|
|
|
|
|
|
|
if !b.is_empty() {
|
|
|
|
self.as_mut().payload.as_mut().unwrap().unread_data(b);
|
|
|
|
}
|
2018-02-25 19:26:58 +01:00
|
|
|
|
2018-02-27 00:26:27 +01:00
|
|
|
if i < buf.len() {
|
|
|
|
match self.read(&mut buf[i..]) {
|
|
|
|
Ok(n) => Ok(i + n),
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => Ok(i),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(i)
|
2018-02-25 19:26:58 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-27 00:26:27 +01:00
|
|
|
Ok(Async::Ready(None)) => Ok(0),
|
|
|
|
Ok(Async::NotReady) =>
|
|
|
|
Err(io::Error::new(io::ErrorKind::WouldBlock, "Not ready")),
|
|
|
|
Err(e) =>
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, failure::Error::from(e).compat())),
|
2018-02-25 19:26:58 +01:00
|
|
|
}
|
2018-02-27 00:26:27 +01:00
|
|
|
} else {
|
|
|
|
Ok(0)
|
2018-02-25 19:26:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> AsyncRead for HttpRequest<S> {}
|
|
|
|
|
2017-11-27 06:18:38 +01:00
|
|
|
impl<S> fmt::Debug for HttpRequest<S> {
|
2017-10-19 08:43:50 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-10-28 07:19:00 +02:00
|
|
|
let res = write!(f, "\nHttpRequest {:?} {}:{}\n",
|
2017-12-15 04:34:31 +01:00
|
|
|
self.as_ref().version, self.as_ref().method, self.as_ref().uri);
|
2017-10-29 14:03:21 +01:00
|
|
|
if !self.query_string().is_empty() {
|
|
|
|
let _ = write!(f, " query: ?{:?}\n", self.query_string());
|
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
if !self.match_info().is_empty() {
|
2017-12-15 04:34:31 +01:00
|
|
|
let _ = write!(f, " params: {:?}\n", self.as_ref().params);
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
let _ = write!(f, " headers:\n");
|
2017-12-15 04:34:31 +01:00
|
|
|
for key in self.as_ref().headers.keys() {
|
|
|
|
let vals: Vec<_> = self.as_ref().headers.get_all(key).iter().collect();
|
2017-10-19 08:43:50 +02:00
|
|
|
if vals.len() > 1 {
|
|
|
|
let _ = write!(f, " {:?}: {:?}\n", key, vals);
|
|
|
|
} else {
|
|
|
|
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-19 08:43:50 +02:00
|
|
|
res
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Future that resolves to a parsed urlencoded values.
|
|
|
|
pub struct UrlEncoded {
|
2018-02-25 18:34:26 +01:00
|
|
|
req: Option<HttpRequest<()>>,
|
|
|
|
limit: usize,
|
|
|
|
fut: Option<Box<Future<Item=HashMap<String, String>, Error=UrlencodedError>>>,
|
2017-12-19 23:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UrlEncoded {
|
2018-02-26 23:33:56 +01:00
|
|
|
pub fn new(req: HttpRequest) -> UrlEncoded {
|
2018-02-25 18:34:26 +01:00
|
|
|
UrlEncoded {
|
2018-02-26 23:33:56 +01:00
|
|
|
req: Some(req),
|
2018-02-25 18:34:26 +01:00
|
|
|
limit: 262_144,
|
|
|
|
fut: None,
|
2017-12-19 23:03:01 +01:00
|
|
|
}
|
2018-02-25 18:34:26 +01:00
|
|
|
}
|
2017-12-19 23:03:01 +01:00
|
|
|
|
2018-02-25 18:34:26 +01:00
|
|
|
/// Change max size of payload. By default max size is 256Kb
|
|
|
|
pub fn limit(mut self, limit: usize) -> Self {
|
|
|
|
self.limit = limit;
|
|
|
|
self
|
2017-12-19 23:03:01 +01:00
|
|
|
}
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for UrlEncoded {
|
|
|
|
type Item = HashMap<String, String>;
|
2017-12-19 23:03:01 +01:00
|
|
|
type Error = UrlencodedError;
|
2017-10-18 01:46:57 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-02-25 18:34:26 +01:00
|
|
|
if let Some(req) = self.req.take() {
|
|
|
|
if req.chunked().unwrap_or(false) {
|
|
|
|
return Err(UrlencodedError::Chunked)
|
|
|
|
} else if let Some(len) = req.headers().get(header::CONTENT_LENGTH) {
|
|
|
|
if let Ok(s) = len.to_str() {
|
|
|
|
if let Ok(len) = s.parse::<u64>() {
|
|
|
|
if len > 262_144 {
|
|
|
|
return Err(UrlencodedError::Overflow);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(UrlencodedError::UnknownLength)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(UrlencodedError::UnknownLength)
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 23:03:01 +01:00
|
|
|
|
2018-02-25 18:34:26 +01:00
|
|
|
// check content type
|
|
|
|
let mut err = true;
|
|
|
|
if let Some(content_type) = req.headers().get(header::CONTENT_TYPE) {
|
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
|
|
|
if content_type.to_lowercase() == "application/x-www-form-urlencoded" {
|
|
|
|
err = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err {
|
|
|
|
return Err(UrlencodedError::ContentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// future
|
|
|
|
let limit = self.limit;
|
|
|
|
let fut = req.from_err()
|
|
|
|
.fold(BytesMut::new(), move |mut body, chunk| {
|
|
|
|
if (body.len() + chunk.len()) > limit {
|
|
|
|
Err(UrlencodedError::Overflow)
|
|
|
|
} else {
|
|
|
|
body.extend_from_slice(&chunk);
|
|
|
|
Ok(body)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|body| {
|
2017-10-18 01:46:57 +02:00
|
|
|
let mut m = HashMap::new();
|
2018-02-25 18:34:26 +01:00
|
|
|
for (k, v) in form_urlencoded::parse(&body) {
|
2017-10-18 01:46:57 +02:00
|
|
|
m.insert(k.into(), v.into());
|
|
|
|
}
|
2018-02-25 18:34:26 +01:00
|
|
|
m
|
|
|
|
});
|
|
|
|
self.fut = Some(Box::new(fut));
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
2018-02-25 18:34:26 +01:00
|
|
|
|
|
|
|
self.fut.as_mut().expect("UrlEncoded could not be used second time").poll()
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2018-01-03 18:23:58 +01:00
|
|
|
/// Future that resolves to a complete request body.
|
|
|
|
pub struct RequestBody {
|
|
|
|
limit: usize,
|
2018-01-30 21:44:14 +01:00
|
|
|
req: Option<HttpRequest<()>>,
|
2018-02-25 18:34:26 +01:00
|
|
|
fut: Option<Box<Future<Item=Bytes, Error=PayloadError>>>,
|
2018-01-03 18:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestBody {
|
|
|
|
|
|
|
|
/// Create `RequestBody` for request.
|
2018-02-26 23:33:56 +01:00
|
|
|
pub fn new(req: HttpRequest) -> RequestBody {
|
2018-01-30 21:44:14 +01:00
|
|
|
RequestBody {
|
2018-01-03 18:23:58 +01:00
|
|
|
limit: 262_144,
|
2018-02-26 23:33:56 +01:00
|
|
|
req: Some(req),
|
2018-02-25 18:34:26 +01:00
|
|
|
fut: None,
|
2018-01-03 18:23:58 +01:00
|
|
|
}
|
2018-01-30 21:44:14 +01:00
|
|
|
}
|
2018-01-03 18:23:58 +01:00
|
|
|
|
|
|
|
/// Change max size of payload. By default max size is 256Kb
|
|
|
|
pub fn limit(mut self, limit: usize) -> Self {
|
|
|
|
self.limit = limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for RequestBody {
|
|
|
|
type Item = Bytes;
|
|
|
|
type Error = PayloadError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-01-30 21:44:14 +01:00
|
|
|
if let Some(req) = self.req.take() {
|
|
|
|
if let Some(len) = req.headers().get(header::CONTENT_LENGTH) {
|
|
|
|
if let Ok(s) = len.to_str() {
|
|
|
|
if let Ok(len) = s.parse::<usize>() {
|
|
|
|
if len > self.limit {
|
|
|
|
return Err(PayloadError::Overflow);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(PayloadError::UnknownLength);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(PayloadError::UnknownLength);
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 18:23:58 +01:00
|
|
|
|
2018-02-25 18:34:26 +01:00
|
|
|
// future
|
|
|
|
let limit = self.limit;
|
|
|
|
self.fut = Some(Box::new(
|
|
|
|
req.from_err()
|
|
|
|
.fold(BytesMut::new(), move |mut body, chunk| {
|
|
|
|
if (body.len() + chunk.len()) > limit {
|
|
|
|
Err(PayloadError::Overflow)
|
|
|
|
} else {
|
|
|
|
body.extend_from_slice(&chunk);
|
|
|
|
Ok(body)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|body| body.freeze())
|
|
|
|
));
|
2018-01-03 18:23:58 +01:00
|
|
|
}
|
2018-02-25 18:34:26 +01:00
|
|
|
|
|
|
|
self.fut.as_mut().expect("UrlEncoded could not be used second time").poll()
|
2018-01-03 18:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:26:01 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-01-26 06:50:28 +01:00
|
|
|
use mime;
|
2018-02-03 17:31:32 +01:00
|
|
|
use http::{Uri, HttpTryFrom};
|
2017-12-01 04:01:25 +01:00
|
|
|
use std::str::FromStr;
|
2017-12-08 01:22:26 +01:00
|
|
|
use router::Pattern;
|
2017-12-07 01:26:27 +01:00
|
|
|
use resource::Resource;
|
2017-12-27 04:48:02 +01:00
|
|
|
use test::TestRequest;
|
2017-12-29 20:49:36 +01:00
|
|
|
use server::ServerSettings;
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2017-12-13 20:16:26 +01:00
|
|
|
#[test]
|
|
|
|
fn test_debug() {
|
2017-12-27 04:48:02 +01:00
|
|
|
let req = TestRequest::with_header("content-type", "text/plain").finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
let dbg = format!("{:?}", req);
|
|
|
|
assert!(dbg.contains("HttpRequest"));
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:50:28 +01:00
|
|
|
#[test]
|
|
|
|
fn test_content_type() {
|
|
|
|
let req = TestRequest::with_header("content-type", "text/plain").finish();
|
|
|
|
assert_eq!(req.content_type(), "text/plain");
|
|
|
|
let req = TestRequest::with_header(
|
|
|
|
"content-type", "application/json; charset=utf=8").finish();
|
|
|
|
assert_eq!(req.content_type(), "application/json");
|
|
|
|
let req = HttpRequest::default();
|
|
|
|
assert_eq!(req.content_type(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mime_type() {
|
|
|
|
let req = TestRequest::with_header("content-type", "application/json").finish();
|
|
|
|
assert_eq!(req.mime_type(), Some(mime::APPLICATION_JSON));
|
|
|
|
let req = HttpRequest::default();
|
|
|
|
assert_eq!(req.mime_type(), None);
|
|
|
|
let req = TestRequest::with_header(
|
|
|
|
"content-type", "application/json; charset=utf-8").finish();
|
|
|
|
let mt = req.mime_type().unwrap();
|
|
|
|
assert_eq!(mt.get_param(mime::CHARSET), Some(mime::UTF_8));
|
|
|
|
assert_eq!(mt.type_(), mime::APPLICATION);
|
|
|
|
assert_eq!(mt.subtype(), mime::JSON);
|
|
|
|
}
|
|
|
|
|
2018-02-03 17:31:32 +01:00
|
|
|
#[test]
|
|
|
|
fn test_uri_mut() {
|
|
|
|
let mut req = HttpRequest::default();
|
|
|
|
assert_eq!(req.path(), "/");
|
|
|
|
*req.uri_mut() = Uri::try_from("/test").unwrap();
|
|
|
|
assert_eq!(req.path(), "/test");
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:16:26 +01:00
|
|
|
#[test]
|
|
|
|
fn test_no_request_cookies() {
|
2017-12-27 04:48:02 +01:00
|
|
|
let req = HttpRequest::default();
|
2017-12-13 20:16:26 +01:00
|
|
|
assert!(req.cookies().unwrap().is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_request_cookies() {
|
2017-12-27 04:48:02 +01:00
|
|
|
let req = TestRequest::with_header(
|
|
|
|
header::COOKIE, "cookie1=value1; cookie2=value2").finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
{
|
|
|
|
let cookies = req.cookies().unwrap();
|
|
|
|
assert_eq!(cookies.len(), 2);
|
|
|
|
assert_eq!(cookies[0].name(), "cookie1");
|
|
|
|
assert_eq!(cookies[0].value(), "value1");
|
|
|
|
assert_eq!(cookies[1].name(), "cookie2");
|
|
|
|
assert_eq!(cookies[1].value(), "value2");
|
|
|
|
}
|
|
|
|
|
|
|
|
let cookie = req.cookie("cookie1");
|
|
|
|
assert!(cookie.is_some());
|
|
|
|
let cookie = cookie.unwrap();
|
|
|
|
assert_eq!(cookie.name(), "cookie1");
|
|
|
|
assert_eq!(cookie.value(), "value1");
|
|
|
|
|
|
|
|
let cookie = req.cookie("cookie-unknown");
|
|
|
|
assert!(cookie.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_no_request_range_header() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = HttpRequest::default();
|
2017-12-13 20:16:26 +01:00
|
|
|
let ranges = req.range(100).unwrap();
|
|
|
|
assert!(ranges.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_request_range_header() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(header::RANGE, "bytes=0-4").finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
let ranges = req.range(100).unwrap();
|
|
|
|
assert_eq!(ranges.len(), 1);
|
|
|
|
assert_eq!(ranges[0].start, 0);
|
|
|
|
assert_eq!(ranges[0].length, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_request_query() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_uri("/?id=test").finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
assert_eq!(req.query_string(), "id=test");
|
|
|
|
let query = req.query();
|
|
|
|
assert_eq!(&query["id"], "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_request_match_info() {
|
2017-12-27 04:48:02 +01:00
|
|
|
let mut req = TestRequest::with_uri("/value/?id=test").finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
let mut resource = Resource::<()>::default();
|
2017-12-13 20:16:26 +01:00
|
|
|
resource.name("index");
|
2018-02-22 14:48:18 +01:00
|
|
|
let mut routes = Vec::new();
|
|
|
|
routes.push((Pattern::new("index", "/{key}/"), Some(resource)));
|
|
|
|
let (router, _) = Router::new("", ServerSettings::default(), routes);
|
2017-12-13 20:16:26 +01:00
|
|
|
assert!(router.recognize(&mut req).is_some());
|
|
|
|
|
|
|
|
assert_eq!(req.match_info().get("key"), Some("value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunked() {
|
2017-12-27 04:48:02 +01:00
|
|
|
let req = HttpRequest::default();
|
2017-12-13 20:16:26 +01:00
|
|
|
assert!(!req.chunked().unwrap());
|
|
|
|
|
2017-12-27 04:48:02 +01:00
|
|
|
let req = TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
2017-12-13 20:16:26 +01:00
|
|
|
assert!(req.chunked().unwrap());
|
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
let s = unsafe{str::from_utf8_unchecked(b"some va\xadscc\xacas0xsdasdlue".as_ref())};
|
|
|
|
|
|
|
|
headers.insert(header::TRANSFER_ENCODING,
|
|
|
|
header::HeaderValue::from_str(s).unwrap());
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, None);
|
|
|
|
assert!(req.chunked().is_err());
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:03:01 +01:00
|
|
|
impl PartialEq for UrlencodedError {
|
|
|
|
fn eq(&self, other: &UrlencodedError) -> bool {
|
|
|
|
match *self {
|
|
|
|
UrlencodedError::Chunked => match *other {
|
|
|
|
UrlencodedError::Chunked => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
UrlencodedError::Overflow => match *other {
|
|
|
|
UrlencodedError::Overflow => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
UrlencodedError::UnknownLength => match *other {
|
|
|
|
UrlencodedError::UnknownLength => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
UrlencodedError::ContentType => match *other {
|
|
|
|
UrlencodedError::ContentType => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 23:26:01 +02:00
|
|
|
#[test]
|
|
|
|
fn test_urlencoded_error() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(header::TRANSFER_ENCODING, "chunked").finish();
|
2017-12-19 23:03:01 +01:00
|
|
|
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::Chunked);
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(
|
|
|
|
header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.header(header::CONTENT_LENGTH, "xxxx")
|
|
|
|
.finish();
|
2017-12-19 23:03:01 +01:00
|
|
|
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::UnknownLength);
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(
|
|
|
|
header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
|
|
.header(header::CONTENT_LENGTH, "1000000")
|
|
|
|
.finish();
|
2017-12-19 23:03:01 +01:00
|
|
|
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::Overflow);
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(
|
|
|
|
header::CONTENT_TYPE, "text/plain")
|
|
|
|
.header(header::CONTENT_LENGTH, "10")
|
|
|
|
.finish();
|
2017-12-19 23:03:01 +01:00
|
|
|
assert_eq!(req.urlencoded().poll().err().unwrap(), UrlencodedError::ContentType);
|
2017-10-23 23:26:01 +02:00
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
|
2018-01-03 19:57:57 +01:00
|
|
|
#[test]
|
|
|
|
fn test_request_body() {
|
|
|
|
let req = TestRequest::with_header(header::CONTENT_LENGTH, "xxxx").finish();
|
|
|
|
match req.body().poll().err().unwrap() {
|
|
|
|
PayloadError::UnknownLength => (),
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let req = TestRequest::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
|
|
|
match req.body().poll().err().unwrap() {
|
|
|
|
PayloadError::Overflow => (),
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut req = HttpRequest::default();
|
|
|
|
req.payload_mut().unread_data(Bytes::from_static(b"test"));
|
|
|
|
match req.body().poll().ok().unwrap() {
|
|
|
|
Async::Ready(bytes) => assert_eq!(bytes, Bytes::from_static(b"test")),
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut req = HttpRequest::default();
|
|
|
|
req.payload_mut().unread_data(Bytes::from_static(b"11111111111111"));
|
|
|
|
match req.body().limit(5).poll().err().unwrap() {
|
|
|
|
PayloadError::Overflow => (),
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 01:26:27 +01:00
|
|
|
#[test]
|
|
|
|
fn test_url_for() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(header::HOST, "www.rust-lang.org")
|
|
|
|
.finish_no_router();
|
2017-12-07 01:26:27 +01:00
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
let mut resource = Resource::<()>::default();
|
2017-12-07 01:26:27 +01:00
|
|
|
resource.name("index");
|
2018-02-22 14:48:18 +01:00
|
|
|
let routes = vec!((Pattern::new("index", "/user/{name}.{ext}"), Some(resource)));
|
|
|
|
let (router, _) = Router::new("/", ServerSettings::default(), routes);
|
2017-12-07 01:58:49 +01:00
|
|
|
assert!(router.has_route("/user/test.html"));
|
|
|
|
assert!(!router.has_route("/test/unknown"));
|
2017-12-07 01:26:27 +01:00
|
|
|
|
|
|
|
assert_eq!(req.url_for("unknown", &["test"]),
|
|
|
|
Err(UrlGenerationError::RouterNotAvailable));
|
|
|
|
|
2017-12-08 03:00:20 +01:00
|
|
|
let req = req.with_state(Rc::new(()), router);
|
2017-12-07 01:26:27 +01:00
|
|
|
|
|
|
|
assert_eq!(req.url_for("unknown", &["test"]),
|
|
|
|
Err(UrlGenerationError::ResourceNotFound));
|
|
|
|
assert_eq!(req.url_for("index", &["test"]),
|
|
|
|
Err(UrlGenerationError::NotEnoughElements));
|
|
|
|
let url = req.url_for("index", &["test", "html"]);
|
|
|
|
assert_eq!(url.ok().unwrap().as_str(), "http://www.rust-lang.org/user/test.html");
|
|
|
|
}
|
2017-12-08 01:22:26 +01:00
|
|
|
|
2017-12-29 23:04:13 +01:00
|
|
|
#[test]
|
|
|
|
fn test_url_for_with_prefix() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = TestRequest::with_header(header::HOST, "www.rust-lang.org").finish();
|
2017-12-29 23:04:13 +01:00
|
|
|
|
|
|
|
let mut resource = Resource::<()>::default();
|
|
|
|
resource.name("index");
|
2018-02-22 14:48:18 +01:00
|
|
|
let routes = vec![(Pattern::new("index", "/user/{name}.{ext}"), Some(resource))];
|
|
|
|
let (router, _) = Router::new("/prefix/", ServerSettings::default(), routes);
|
2017-12-29 23:04:13 +01:00
|
|
|
assert!(router.has_route("/user/test.html"));
|
|
|
|
assert!(!router.has_route("/prefix/user/test.html"));
|
|
|
|
|
|
|
|
let req = req.with_state(Rc::new(()), router);
|
|
|
|
let url = req.url_for("index", &["test", "html"]);
|
|
|
|
assert_eq!(url.ok().unwrap().as_str(), "http://www.rust-lang.org/prefix/user/test.html");
|
|
|
|
}
|
|
|
|
|
2017-12-08 01:22:26 +01:00
|
|
|
#[test]
|
|
|
|
fn test_url_for_external() {
|
2018-01-03 19:57:57 +01:00
|
|
|
let req = HttpRequest::default();
|
2017-12-08 01:22:26 +01:00
|
|
|
|
|
|
|
let mut resource = Resource::<()>::default();
|
|
|
|
resource.name("index");
|
2018-02-22 14:48:18 +01:00
|
|
|
let routes = vec![
|
|
|
|
(Pattern::new("youtube", "https://youtube.com/watch/{video_id}"), None)];
|
|
|
|
let (router, _) = Router::new::<()>("", ServerSettings::default(), routes);
|
2017-12-08 01:22:26 +01:00
|
|
|
assert!(!router.has_route("https://youtube.com/watch/unknown"));
|
|
|
|
|
2017-12-08 03:00:20 +01:00
|
|
|
let req = req.with_state(Rc::new(()), router);
|
2017-12-08 01:22:26 +01:00
|
|
|
let url = req.url_for("youtube", &["oHg5SJYRHA0"]);
|
|
|
|
assert_eq!(url.ok().unwrap().as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
|
|
|
|
}
|
2017-10-23 23:26:01 +02:00
|
|
|
}
|