2017-10-16 19:31:31 +02:00
|
|
|
//! HTTP Request message related code.
|
2017-11-27 04:00:57 +01:00
|
|
|
use std::{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;
|
2017-10-19 08:43:50 +02:00
|
|
|
use bytes::BytesMut;
|
2017-10-18 01:46:57 +02:00
|
|
|
use futures::{Async, Future, Stream, Poll};
|
2017-12-07 01:26:27 +01:00
|
|
|
use url::{Url, form_urlencoded};
|
2017-12-08 01:40:29 +01:00
|
|
|
use cookie::Cookie;
|
|
|
|
use http_range::HttpRange;
|
2017-12-01 04:01:25 +01:00
|
|
|
use http::{header, Uri, Method, Version, HeaderMap, Extensions};
|
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;
|
2017-11-16 07:06:28 +01:00
|
|
|
use payload::Payload;
|
|
|
|
use multipart::Multipart;
|
2017-12-07 01:26:27 +01:00
|
|
|
use error::{ParseError, PayloadError, UrlGenerationError,
|
2017-11-27 07:00:25 +01:00
|
|
|
MultipartError, CookieParseError, HttpRangeError, UrlencodedError};
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-12-06 02:09:15 +01:00
|
|
|
|
2017-11-27 05:32:12 +01:00
|
|
|
struct HttpMessage {
|
2017-10-15 07:52:38 +02:00
|
|
|
version: Version,
|
|
|
|
method: Method,
|
2017-12-01 04:01:25 +01:00
|
|
|
uri: Uri,
|
2017-10-15 07:52:38 +02:00
|
|
|
headers: HeaderMap,
|
2017-11-27 05:32:12 +01:00
|
|
|
extensions: Extensions,
|
2017-12-08 01:22:26 +01:00
|
|
|
params: Params<'static>,
|
2017-12-08 03:00:20 +01:00
|
|
|
cookies: Option<Vec<Cookie<'static>>>,
|
2017-11-10 22:08:15 +01:00
|
|
|
addr: Option<SocketAddr>,
|
2017-11-27 04:00:57 +01:00
|
|
|
payload: Payload,
|
2017-12-06 02:09:15 +01:00
|
|
|
info: Option<ConnectionInfo<'static>>,
|
2017-12-07 01:26:27 +01:00
|
|
|
|
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,
|
|
|
|
headers: HeaderMap::new(),
|
2017-12-05 22:31:06 +01:00
|
|
|
params: Params::default(),
|
2017-12-08 03:00:20 +01:00
|
|
|
cookies: None,
|
2017-11-10 22:08:15 +01:00
|
|
|
addr: None,
|
2017-11-27 04:00:57 +01:00
|
|
|
payload: Payload::empty(),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// An HTTP Request
|
2017-12-07 01:26:27 +01:00
|
|
|
pub struct HttpRequest<S=()>(Rc<HttpMessage>, Rc<S>, Option<Router<S>>);
|
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,
|
|
|
|
version: Version, headers: HeaderMap, payload: Payload) -> HttpRequest
|
2017-11-27 05:32:12 +01:00
|
|
|
{
|
|
|
|
HttpRequest(
|
|
|
|
Rc::new(HttpMessage {
|
|
|
|
method: method,
|
2017-12-01 04:01:25 +01:00
|
|
|
uri: uri,
|
2017-11-27 05:32:12 +01:00
|
|
|
version: version,
|
|
|
|
headers: headers,
|
2017-12-05 22:31:06 +01:00
|
|
|
params: Params::default(),
|
2017-12-08 03:00:20 +01:00
|
|
|
cookies: None,
|
2017-11-27 05:32:12 +01:00
|
|
|
addr: None,
|
|
|
|
payload: payload,
|
|
|
|
extensions: Extensions::new(),
|
2017-12-06 02:09:15 +01:00
|
|
|
info: None,
|
2017-11-27 06:18:38 +01:00
|
|
|
}),
|
2017-12-07 01:26:27 +01:00
|
|
|
Rc::new(()),
|
|
|
|
None,
|
2017-11-27 05:32:12 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-27 06:18:38 +01:00
|
|
|
/// Construct new http request with state.
|
2017-12-07 01:26:27 +01:00
|
|
|
pub fn with_state<S>(self, state: Rc<S>, router: Router<S>) -> HttpRequest<S> {
|
|
|
|
HttpRequest(self.0, state, Some(router))
|
2017-11-27 06:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> HttpRequest<S> {
|
|
|
|
|
2017-12-03 01:37:21 +01:00
|
|
|
/// Construct new http request without state.
|
|
|
|
pub fn clone_without_state(&self) -> HttpRequest {
|
2017-12-07 01:26:27 +01:00
|
|
|
HttpRequest(Rc::clone(&self.0), Rc::new(()), None)
|
2017-12-03 01:37:21 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 02:38:18 +01:00
|
|
|
// get mutable reference for inner message
|
|
|
|
// mutable reference should not be returned as result for request's method
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-12-08 02:38:18 +01:00
|
|
|
#[allow(mutable_transmutes)]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref))]
|
|
|
|
fn as_mut(&self) -> &mut HttpMessage {
|
2017-11-27 05:32:12 +01:00
|
|
|
let r: &HttpMessage = self.0.as_ref();
|
|
|
|
unsafe{mem::transmute(r)}
|
|
|
|
}
|
2017-10-22 07:59:09 +02:00
|
|
|
|
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 {
|
|
|
|
&self.1
|
|
|
|
}
|
|
|
|
|
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-08 03:00:20 +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]
|
|
|
|
pub fn uri(&self) -> &Uri { &self.0.uri }
|
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Read the Request method.
|
|
|
|
#[inline]
|
2017-11-27 05:32:12 +01:00
|
|
|
pub fn method(&self) -> &Method { &self.0.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-11-27 05:32:12 +01:00
|
|
|
self.0.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-11-27 05:32:12 +01:00
|
|
|
&self.0.headers
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 06:38:52 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
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-01 04:01:25 +01:00
|
|
|
self.0.uri.path()
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 02:38:18 +01:00
|
|
|
/// Get *ConnectionInfo* for currect request.
|
|
|
|
pub fn connection_info(&self) -> &ConnectionInfo {
|
2017-12-06 02:09:15 +01:00
|
|
|
if self.0.info.is_none() {
|
|
|
|
let info: ConnectionInfo<'static> = unsafe{
|
|
|
|
mem::transmute(ConnectionInfo::new(self))};
|
|
|
|
self.as_mut().info = Some(info);
|
|
|
|
}
|
|
|
|
self.0.info.as_ref().unwrap()
|
2017-11-30 00:07:49 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn router(&self) -> Option<&Router<S>> {
|
|
|
|
self.2.as_ref()
|
|
|
|
}
|
|
|
|
|
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-11-27 05:32:12 +01:00
|
|
|
self.0.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-10-16 18:43:10 +02:00
|
|
|
/// Return a new iterator that yields pairs of `Cow<str>` for query parameters
|
2017-10-29 14:03:21 +01:00
|
|
|
pub fn query(&self) -> HashMap<String, String> {
|
|
|
|
let mut q: HashMap<String, String> = HashMap::new();
|
2017-12-01 04:01:25 +01:00
|
|
|
if let Some(query) = self.0.uri.query().as_ref() {
|
|
|
|
for (key, val) in form_urlencoded::parse(query.as_ref()) {
|
|
|
|
q.insert(key.to_string(), val.to_string());
|
|
|
|
}
|
2017-10-29 14:03:21 +01:00
|
|
|
}
|
|
|
|
q
|
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-01 04:01:25 +01:00
|
|
|
if let Some(query) = self.0.uri.query().as_ref() {
|
|
|
|
query
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 03:00:20 +01:00
|
|
|
/// Load request cookies.
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-12-08 03:00:20 +01:00
|
|
|
pub fn cookies(&self) -> Result<&Vec<Cookie<'static>>, CookieParseError> {
|
|
|
|
if self.0.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)
|
|
|
|
}
|
|
|
|
Ok(self.0.cookies.as_ref().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
unsafe{ mem::transmute(&self.0.params) }
|
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
/// Set request 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-11-27 05:32:12 +01:00
|
|
|
if let Some(conn) = self.0.headers.get(header::CONNECTION) {
|
2017-10-15 07:52:38 +02:00
|
|
|
if let Ok(conn) = conn.to_str() {
|
2017-11-27 05:32:12 +01:00
|
|
|
if self.0.version == Version::HTTP_10 && conn.contains("keep-alive") {
|
2017-10-15 07:52:38 +02:00
|
|
|
true
|
|
|
|
} else {
|
2017-11-27 05:32:12 +01:00
|
|
|
self.0.version == Version::HTTP_11 &&
|
2017-10-15 07:52:38 +02:00
|
|
|
!(conn.contains("close") || conn.contains("upgrade"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-27 05:32:12 +01:00
|
|
|
self.0.version != Version::HTTP_10
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-29 14:03:21 +01:00
|
|
|
/// Read the request content type
|
|
|
|
pub fn content_type(&self) -> &str {
|
2017-11-27 05:32:12 +01:00
|
|
|
if let Some(content_type) = self.0.headers.get(header::CONTENT_TYPE) {
|
2017-10-29 14:03:21 +01:00
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
|
|
|
return content_type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2017-10-15 07:52:38 +02:00
|
|
|
/// Check if request requires connection upgrade
|
|
|
|
pub(crate) fn upgrade(&self) -> bool {
|
2017-11-27 05:32:12 +01:00
|
|
|
if let Some(conn) = self.0.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-11-27 05:32:12 +01:00
|
|
|
self.0.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-11-27 05:32:12 +01:00
|
|
|
if let Some(encodings) = self.0.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-11-27 05:32:12 +01:00
|
|
|
if let Some(range) = self.0.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
|
|
|
|
2017-11-27 04:00:57 +01:00
|
|
|
/// Returns reference to the associated http payload.
|
|
|
|
#[inline]
|
|
|
|
pub fn payload(&self) -> &Payload {
|
2017-11-27 05:32:12 +01:00
|
|
|
&self.0.payload
|
2017-11-27 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns mutable reference to the associated http payload.
|
|
|
|
#[inline]
|
|
|
|
pub fn payload_mut(&mut self) -> &mut Payload {
|
2017-11-27 05:32:12 +01:00
|
|
|
&mut self.as_mut().payload
|
2017-11-27 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return payload
|
2017-12-07 01:26:27 +01:00
|
|
|
#[inline]
|
2017-11-27 04:00:57 +01:00
|
|
|
pub fn take_payload(&mut self) -> Payload {
|
2017-11-27 05:32:12 +01:00
|
|
|
mem::replace(&mut self.as_mut().payload, Payload::empty())
|
2017-11-27 04:00:57 +01:00
|
|
|
}
|
|
|
|
|
2017-10-18 01:46:57 +02:00
|
|
|
/// Return stream to process BODY as multipart.
|
|
|
|
///
|
|
|
|
/// Content-type: multipart/form-data;
|
2017-11-27 06:47:33 +01:00
|
|
|
pub fn multipart(&mut self) -> Result<Multipart, MultipartError> {
|
|
|
|
let boundary = Multipart::boundary(&self.0.headers)?;
|
|
|
|
Ok(Multipart::new(boundary, self.take_payload()))
|
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-11-27 07:00:25 +01:00
|
|
|
pub fn urlencoded(&mut self) -> Result<UrlEncoded, UrlencodedError> {
|
|
|
|
if let Ok(true) = self.chunked() {
|
|
|
|
return Err(UrlencodedError::Chunked)
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(len) = self.headers().get(header::CONTENT_LENGTH) {
|
|
|
|
if let Ok(s) = len.to_str() {
|
|
|
|
if let Ok(len) = s.parse::<u64>() {
|
|
|
|
if len > 262_144 {
|
2017-11-27 07:00:25 +01:00
|
|
|
return Err(UrlencodedError::Overflow)
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-27 07:00:25 +01:00
|
|
|
return Err(UrlencodedError::UnknownLength)
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-11-27 07:00:25 +01:00
|
|
|
return Err(UrlencodedError::UnknownLength)
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
// check content type
|
|
|
|
let t = if let Some(content_type) = self.0.headers.get(header::CONTENT_TYPE) {
|
2017-10-18 01:46:57 +02:00
|
|
|
if let Ok(content_type) = content_type.to_str() {
|
2017-11-27 07:00:25 +01:00
|
|
|
content_type.to_lowercase() == "application/x-www-form-urlencoded"
|
|
|
|
} else {
|
|
|
|
false
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
2017-11-27 07:00:25 +01:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2017-10-18 01:46:57 +02:00
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
if t {
|
|
|
|
Ok(UrlEncoded{pl: self.take_payload(), body: BytesMut::new()})
|
|
|
|
} else {
|
|
|
|
Err(UrlencodedError::ContentType)
|
|
|
|
}
|
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-07 01:26:27 +01:00
|
|
|
HttpRequest(Rc::new(HttpMessage::default()), Rc::new(()), 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> {
|
2017-12-07 01:26:27 +01:00
|
|
|
HttpRequest(Rc::clone(&self.0), Rc::clone(&self.1), None)
|
2017-11-27 05:32:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01 04:01:25 +01:00
|
|
|
self.0.version, self.0.method, self.0.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-11-27 05:32:12 +01:00
|
|
|
let _ = write!(f, " params: {:?}\n", self.0.params);
|
2017-10-19 08:43:50 +02:00
|
|
|
}
|
|
|
|
let _ = write!(f, " headers:\n");
|
2017-11-27 05:32:12 +01:00
|
|
|
for key in self.0.headers.keys() {
|
|
|
|
let vals: Vec<_> = self.0.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 {
|
|
|
|
pl: Payload,
|
|
|
|
body: BytesMut,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for UrlEncoded {
|
|
|
|
type Item = HashMap<String, String>;
|
|
|
|
type Error = PayloadError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
loop {
|
|
|
|
return match self.pl.poll() {
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
let mut m = HashMap::new();
|
|
|
|
for (k, v) in form_urlencoded::parse(&self.body) {
|
|
|
|
m.insert(k.into(), v.into());
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(m))
|
|
|
|
},
|
2017-10-27 08:14:33 +02:00
|
|
|
Ok(Async::Ready(Some(item))) => {
|
|
|
|
self.body.extend(item.0);
|
|
|
|
continue
|
|
|
|
},
|
|
|
|
Err(err) => Err(err),
|
2017-10-18 01:46:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-15 07:52:38 +02:00
|
|
|
}
|
2017-10-23 23:26:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2017-12-07 01:26:27 +01:00
|
|
|
use http::Uri;
|
2017-12-01 04:01:25 +01:00
|
|
|
use std::str::FromStr;
|
2017-12-08 01:22:26 +01:00
|
|
|
use router::Pattern;
|
2017-10-23 23:26:01 +02:00
|
|
|
use payload::Payload;
|
2017-12-07 01:26:27 +01:00
|
|
|
use resource::Resource;
|
2017-10-23 23:26:01 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_urlencoded_error() {
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::TRANSFER_ENCODING,
|
|
|
|
header::HeaderValue::from_static("chunked"));
|
2017-11-27 07:00:25 +01:00
|
|
|
let mut req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::Chunked);
|
2017-10-23 23:26:01 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static("application/x-www-form-urlencoded"));
|
|
|
|
headers.insert(header::CONTENT_LENGTH,
|
|
|
|
header::HeaderValue::from_static("xxxx"));
|
2017-11-27 07:00:25 +01:00
|
|
|
let mut req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(), Version::HTTP_11,
|
|
|
|
headers, Payload::empty());
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::UnknownLength);
|
2017-10-23 23:26:01 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static("application/x-www-form-urlencoded"));
|
|
|
|
headers.insert(header::CONTENT_LENGTH,
|
|
|
|
header::HeaderValue::from_static("1000000"));
|
2017-11-27 07:00:25 +01:00
|
|
|
let mut req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::Overflow);
|
2017-10-23 23:26:01 +02:00
|
|
|
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static("text/plain"));
|
|
|
|
headers.insert(header::CONTENT_LENGTH,
|
|
|
|
header::HeaderValue::from_static("10"));
|
2017-11-27 07:00:25 +01:00
|
|
|
let mut req = HttpRequest::new(
|
2017-12-01 04:01:25 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
2017-10-23 23:26:01 +02:00
|
|
|
|
2017-11-27 07:00:25 +01:00
|
|
|
assert_eq!(req.urlencoded().err().unwrap(), UrlencodedError::ContentType);
|
2017-10-23 23:26:01 +02:00
|
|
|
}
|
2017-12-07 01:26:27 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_url_for() {
|
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.insert(header::HOST,
|
|
|
|
header::HeaderValue::from_static("www.rust-lang.org"));
|
2017-12-08 03:00:20 +01:00
|
|
|
let req = HttpRequest::new(
|
2017-12-07 01:26:27 +01:00
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, headers, Payload::empty());
|
|
|
|
|
|
|
|
let mut resource = Resource::default();
|
|
|
|
resource.name("index");
|
|
|
|
let mut map = HashMap::new();
|
2017-12-08 01:22:26 +01:00
|
|
|
map.insert(Pattern::new("index", "/user/{name}.{ext}"), Some(resource));
|
2017-12-07 01:26:27 +01:00
|
|
|
let router = Router::new("", map);
|
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
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_url_for_external() {
|
|
|
|
let req = HttpRequest::new(
|
|
|
|
Method::GET, Uri::from_str("/").unwrap(),
|
|
|
|
Version::HTTP_11, HeaderMap::new(), Payload::empty());
|
|
|
|
|
|
|
|
let mut resource = Resource::<()>::default();
|
|
|
|
resource.name("index");
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert(Pattern::new("youtube", "https://youtube.com/watch/{video_id}"), None);
|
|
|
|
let router = Router::new("", map);
|
|
|
|
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
|
|
|
}
|