mirror of
https://github.com/fafhrd91/actix-web
synced 2025-09-01 01:16:59 +02:00
split http request; add HttpRequest::range()
This commit is contained in:
@@ -4,177 +4,23 @@ use std::convert::Into;
|
||||
|
||||
use cookie::CookieJar;
|
||||
use bytes::Bytes;
|
||||
use http::{Method, StatusCode, Version, Uri, HeaderMap, HttpTryFrom, Error};
|
||||
use http::{StatusCode, Version, HeaderMap, HttpTryFrom, Error};
|
||||
use http::header::{self, HeaderName, HeaderValue};
|
||||
|
||||
use Params;
|
||||
use {Cookie, CookieParseError};
|
||||
use Cookie;
|
||||
|
||||
|
||||
/// Represents various types of connection
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub enum ConnectionType {
|
||||
/// Close connection after response
|
||||
Close,
|
||||
/// Keep connection alive after response
|
||||
KeepAlive,
|
||||
/// Connection is upgraded to different type
|
||||
Upgrade,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// An HTTP Request
|
||||
pub struct HttpRequest {
|
||||
version: Version,
|
||||
method: Method,
|
||||
uri: Uri,
|
||||
headers: HeaderMap,
|
||||
params: Params,
|
||||
cookies: Vec<Cookie<'static>>,
|
||||
}
|
||||
|
||||
impl HttpRequest {
|
||||
/// Construct a new Request.
|
||||
#[inline]
|
||||
pub fn new(method: Method, uri: Uri, version: Version, headers: HeaderMap) -> Self {
|
||||
HttpRequest {
|
||||
method: method,
|
||||
uri: uri,
|
||||
version: version,
|
||||
headers: headers,
|
||||
params: Params::new(),
|
||||
cookies: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the Request Uri.
|
||||
#[inline]
|
||||
pub fn uri(&self) -> &Uri { &self.uri }
|
||||
|
||||
/// Read the Request method.
|
||||
#[inline]
|
||||
pub fn method(&self) -> &Method { &self.method }
|
||||
|
||||
/// Read the Request Version.
|
||||
pub fn version(&self) -> Version {
|
||||
self.version
|
||||
}
|
||||
|
||||
/// Read the Request Headers.
|
||||
pub fn headers(&self) -> &HeaderMap {
|
||||
&self.headers
|
||||
}
|
||||
|
||||
// /// 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()
|
||||
}
|
||||
|
||||
/// Return request cookies.
|
||||
pub fn cookies(&self) -> &Vec<Cookie<'static>> {
|
||||
&self.cookies
|
||||
}
|
||||
|
||||
/// Return request cookie.
|
||||
pub fn cookie(&self, name: &str) -> Option<&Cookie> {
|
||||
for cookie in &self.cookies {
|
||||
if cookie.name() == name {
|
||||
return Some(&cookie)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Load cookies
|
||||
pub fn load_cookies(&mut self) -> Result<&Vec<Cookie>, CookieParseError>
|
||||
{
|
||||
if let Some(val) = self.headers.get(header::COOKIE) {
|
||||
let s = str::from_utf8(val.as_bytes())
|
||||
.map_err(CookieParseError::from)?;
|
||||
for cookie in s.split("; ") {
|
||||
self.cookies.push(Cookie::parse_encoded(cookie)?.into_owned());
|
||||
}
|
||||
}
|
||||
Ok(&self.cookies)
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the Request headers.
|
||||
#[inline]
|
||||
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
||||
&mut self.headers
|
||||
}
|
||||
|
||||
/// 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]
|
||||
pub fn params(&self) -> &Params { &self.params }
|
||||
|
||||
/// Create new request with Params object.
|
||||
pub fn with_params(self, params: Params) -> Self {
|
||||
HttpRequest {
|
||||
method: self.method,
|
||||
uri: self.uri,
|
||||
version: self.version,
|
||||
headers: self.headers,
|
||||
params: params,
|
||||
cookies: self.cookies,
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if a connection should be kept alive.
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn upgrade(&self) -> bool {
|
||||
if let Some(conn) = self.headers().get(header::CONNECTION) {
|
||||
if let Ok(s) = conn.to_str() {
|
||||
return s.to_lowercase().contains("upgrade")
|
||||
}
|
||||
}
|
||||
self.method == Method::CONNECT
|
||||
}
|
||||
|
||||
pub fn chunked(&self) -> Result<bool, io::Error> {
|
||||
if let Some(encodings) = self.headers().get(header::TRANSFER_ENCODING) {
|
||||
if let Ok(s) = encodings.to_str() {
|
||||
Ok(s.to_lowercase().contains("chunked"))
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other, "Can not read transfer-encoding header"))
|
||||
}
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents various types of http message body.
|
||||
#[derive(Debug)]
|
||||
pub enum Body {
|
||||
|
Reference in New Issue
Block a user