pub use hyper::Method;
pub use hyper::Uri;
pub use hyper::Version;
pub use hyper::Body;
pub use hyper::body::Payload;
pub use hyper::StatusCode;
pub use hyper::Request;
pub use hyper::Response;
use crate::http_types::response::Builder as ResponseBuilder;
use crate::http_types::request::Parts as ReqParts;
use crate::utils::RequestAddonCollection;
pub use crate::http_types::Extensions;
pub mod header {
pub use crate::http_types::header::*;
pub use hyperx::mime;
pub use hyperx::header::*;
}
use futures::Future;
use futures::Stream;
use crate::http_types::HttpTryFrom;
use std::any::Any;
static EMPTY_BODY: &[u8] = b"";
#[derive(Debug)]
pub struct SyncRequest {
head: ReqParts,
body: Vec<u8>,
addons: RequestAddonCollection,
current_path: String,
captures: Vec<String>,
}
impl SyncRequest {
#[inline]
pub fn new(head: ReqParts,
body: Vec<u8>,
) -> SyncRequest {
let cp = head.uri.path().to_owned();
SyncRequest {
head,
body,
addons: RequestAddonCollection::new(),
current_path: cp,
captures: Vec::new(),
}
}
#[inline]
pub fn method(&self) -> &Method {
&self.head.method
}
#[inline]
pub fn method_mut(&mut self) -> &mut Method {
&mut self.head.method
}
#[inline]
pub fn uri(&self) -> &Uri {
&self.head.uri
}
#[inline]
pub fn uri_mut(&mut self) -> &mut Uri {
&mut self.head.uri
}
pub(crate) fn current_path_match(&mut self, re: &::regex::Regex) -> bool {
let current = self.current_path.clone();
re.find(¤t).map_or_else(|| false, |ma| {
let mut path = self.current_path.split_off(ma.end());
if path.len() < 1 {
path.push('/');
}
self.current_path = path;
true
})
}
pub(crate) fn current_path_match_and_capture(&mut self, re: &::regex::Regex) -> bool {
let current = self.current_path.clone();
re.captures(¤t).map_or_else(|| false, |cap| {
if let Some(ma) = cap.get(0) {
let mut path = self.current_path.split_off(ma.end());
if path.len() < 1 {
path.push('/');
}
self.current_path = path;
}
for i in 1..cap.len() {
if let Some(ma) = cap.get(i) {
self.captures.push(ma.as_str().to_owned())
}
}
true
})
}
pub fn captures(&self) -> &Vec<String> {
&self.captures
}
#[inline]
pub fn version(&self) -> Version {
self.head.version
}
#[inline]
pub fn version_mut(&mut self) -> &mut Version {
&mut self.head.version
}
#[inline]
pub fn headers_map(&self) -> &header::HeaderMap<header::HeaderValue> {
&self.head.headers
}
#[inline]
pub fn headers_map_mut(&mut self) -> &mut header::HeaderMap<header::HeaderValue> {
&mut self.head.headers
}
pub fn parsed_header(&self) -> header::Headers {
self.head.headers.clone().into()
}
pub fn insert_parsed_headers(&mut self, headers: header::Headers) {
let map: header::HeaderMap = headers.into();
for header in map {
if let (Some(name), value) = header {
self.head.headers.insert(name, value);
}
}
}
#[inline]
pub fn extensions(&self) -> &Extensions {
&self.head.extensions
}
#[inline]
pub fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.head.extensions
}
#[inline]
pub fn body(&self) -> &Vec<u8> {
&self.body
}
#[inline]
pub fn body_mut(&mut self) -> &mut Vec<u8> {
&mut self.body
}
#[inline]
pub fn addons(&self) -> &RequestAddonCollection {
&self.addons
}
#[inline]
pub fn addons_mut(&mut self) -> &mut RequestAddonCollection {
&mut self.addons
}
}
pub trait LoadBody {
fn load_body(self) -> Box<Future<Item=SyncRequest, Error=::hyper::Error> + Send>;
}
impl LoadBody for Request<Body> {
fn load_body(self) -> Box<Future<Item=SyncRequest, Error=::hyper::Error> + Send> {
let (parts, body) = self.into_parts();
Box::new(body.concat2().map(move |b| {
let body_vec: Vec<u8> = b.to_vec();
SyncRequest::new(parts, body_vec)
}))
}
}
pub struct SyncResponse {
builder: ResponseBuilder,
body: Box<ToBody>,
}
impl SyncResponse {
pub fn new() -> Self{
SyncResponse {
builder: ResponseBuilder::new(),
body: Box::new(EMPTY_BODY),
}
}
pub fn status<T>(&mut self, status: T) -> &mut SyncResponse
where StatusCode: HttpTryFrom<T>,
{
self.builder.status(status);
self
}
pub fn version(&mut self, version: Version) -> &mut SyncResponse {
self.builder.version(version);
self
}
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut SyncResponse
where header::HeaderName: HttpTryFrom<K>,
header::HeaderValue: HttpTryFrom<V>
{
self.builder.header(key, value);
self
}
pub fn parsed_header(&mut self, headers: header::Headers) -> &mut SyncResponse {
let map: header::HeaderMap = headers.into();
for header in map {
if let (Some(name), value) = header {
self.builder.header(name, value);
}
}
self
}
pub fn extension<T>(&mut self, extension: T) -> &mut SyncResponse
where T: Any + Send + Sync + 'static,
{
self.builder.extension(extension);
self
}
pub fn body<B: 'static + ToBody>(&mut self, body: B) -> &mut SyncResponse {
self.body = Box::new(body);
self
}
pub fn build_response(self) -> Result<Response<Body>, crate::http_types::Error> {
let SyncResponse { mut builder, body } = self;
let b: Body = body.to_body();
builder.body(b)
}
}
pub trait ToBody {
fn to_body(&self) -> Body;
}
impl<I> ToBody for I where I: Into<Body> + Clone {
fn to_body(&self) -> Body {
self.clone().into()
}
}