1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-26 03:21:08 +00:00

pass request by value

This commit is contained in:
Nikolay Kim 2017-11-26 20:32:12 -08:00
parent eb7f48a1c6
commit 8e0a7f44d4
12 changed files with 132 additions and 109 deletions

View file

@ -83,14 +83,14 @@ impl Actor for MyWebSocket {
impl Route for MyWebSocket { impl Route for MyWebSocket {
type State = (); type State = ();
fn request(req: &mut HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self> fn request(mut req: HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
{ {
// websocket handshake // websocket handshake
let resp = ws::handshake(req)?; let resp = ws::handshake(&req)?;
// send HttpResponse back to peer // send HttpResponse back to peer
ctx.start(resp); ctx.start(resp);
// convert bytes stream to a stream of `ws::Message` and handle stream // convert bytes stream to a stream of `ws::Message` and handle stream
ctx.add_stream(ws::WsStream::new(req)); ctx.add_stream(ws::WsStream::new(&mut req));
Reply::async(MyWebSocket) Reply::async(MyWebSocket)
} }
} }

View file

@ -12,7 +12,7 @@ use actix_web::middlewares::RequestSession;
use futures::stream::{once, Once}; use futures::stream::{once, Once};
/// somple handle /// somple handle
fn index(req: &mut HttpRequest, state: &()) -> Result<HttpResponse> { fn index(mut req: HttpRequest, state: &()) -> Result<HttpResponse> {
println!("{:?}", req); println!("{:?}", req);
if let Ok(ch) = req.payload_mut().readany() { if let Ok(ch) = req.payload_mut().readany() {
if let futures::Async::Ready(Some(d)) = ch { if let futures::Async::Ready(Some(d)) = ch {
@ -32,7 +32,7 @@ fn index(req: &mut HttpRequest, state: &()) -> Result<HttpResponse> {
} }
/// somple handle /// somple handle
fn index_async(req: &mut HttpRequest, state: &()) -> Once<actix_web::Frame, Error> fn index_async(req: HttpRequest, state: &()) -> Once<actix_web::Frame, Error>
{ {
println!("{:?}", req); println!("{:?}", req);
@ -44,7 +44,7 @@ fn index_async(req: &mut HttpRequest, state: &()) -> Once<actix_web::Frame, Erro
} }
/// handle with path parameters like `/user/{name}/` /// handle with path parameters like `/user/{name}/`
fn with_param(req: &mut HttpRequest, state: &()) -> Result<HttpResponse> fn with_param(req: HttpRequest, state: &()) -> Result<HttpResponse>
{ {
println!("{:?}", req); println!("{:?}", req);

View file

@ -15,7 +15,7 @@ struct AppState {
} }
/// somple handle /// somple handle
fn index(req: &mut HttpRequest, state: &AppState) -> HttpResponse { fn index(req: HttpRequest, state: &AppState) -> HttpResponse {
println!("{:?}", req); println!("{:?}", req);
state.counter.set(state.counter.get() + 1); state.counter.set(state.counter.get() + 1);
httpcodes::HTTPOk.with_body( httpcodes::HTTPOk.with_body(
@ -36,11 +36,11 @@ impl Route for MyWebSocket {
/// Shared application state /// Shared application state
type State = AppState; type State = AppState;
fn request(req: &mut HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self> fn request(mut req: HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
{ {
let resp = ws::handshake(req)?; let resp = ws::handshake(&req)?;
ctx.start(resp); ctx.start(resp);
ctx.add_stream(ws::WsStream::new(req)); ctx.add_stream(ws::WsStream::new(&mut req));
Reply::async(MyWebSocket{counter: 0}) Reply::async(MyWebSocket{counter: 0})
} }
} }

View file

@ -22,14 +22,14 @@ impl Actor for MyWebSocket {
impl Route for MyWebSocket { impl Route for MyWebSocket {
type State = (); type State = ();
fn request(req: &mut HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self> fn request(mut req: HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
{ {
// websocket handshake // websocket handshake
let resp = ws::handshake(req)?; let resp = ws::handshake(&req)?;
// send HttpResponse back to peer // send HttpResponse back to peer
ctx.start(resp); ctx.start(resp);
// convert bytes stream to a stream of `ws::Message` and register it // convert bytes stream to a stream of `ws::Message` and register it
ctx.add_stream(ws::WsStream::new(req)); ctx.add_stream(ws::WsStream::new(&mut req));
Reply::async(MyWebSocket) Reply::async(MyWebSocket)
} }
} }

View file

@ -24,7 +24,7 @@ pub struct Application<S> {
impl<S: 'static> Application<S> { impl<S: 'static> Application<S> {
fn run(&self, req: &mut HttpRequest) -> Task { fn run(&self, mut req: HttpRequest) -> Task {
if let Some((params, h)) = self.router.recognize(req.path()) { if let Some((params, h)) = self.router.recognize(req.path()) {
if let Some(params) = params { if let Some(params) = params {
req.set_match_info(params); req.set_match_info(params);
@ -48,8 +48,7 @@ impl<S: 'static> HttpHandler for Application<S> {
} }
fn handle(&self, req: HttpRequest) -> Pipeline { fn handle(&self, req: HttpRequest) -> Pipeline {
Pipeline::new(req, Rc::clone(&self.middlewares), Pipeline::new(req, Rc::clone(&self.middlewares), &|req: HttpRequest| {self.run(req)})
&|req: &mut HttpRequest| {self.run(req)})
} }
} }
@ -139,7 +138,7 @@ impl<S> ApplicationBuilder<S> where S: 'static {
/// impl Route for MyRoute { /// impl Route for MyRoute {
/// type State = (); /// type State = ();
/// ///
/// fn request(req: &mut HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self> { /// fn request(req: HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self> {
/// Reply::reply(httpcodes::HTTPOk) /// Reply::reply(httpcodes::HTTPOk)
/// } /// }
/// } /// }
@ -202,7 +201,7 @@ impl<S> ApplicationBuilder<S> where S: 'static {
/// } /// }
/// ``` /// ```
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Into<HttpResponse> + 'static, R: Into<HttpResponse> + 'static,
P: Into<String>, P: Into<String>,
{ {

View file

@ -70,7 +70,7 @@ impl StaticResponse {
} }
impl<S> RouteHandler<S> for StaticResponse { impl<S> RouteHandler<S> for StaticResponse {
fn handle(&self, _: &mut HttpRequest, _: Rc<S>) -> Task { fn handle(&self, _: HttpRequest, _: Rc<S>) -> Task {
Task::reply(HttpResponse::new(self.0, Body::Empty)) Task::reply(HttpResponse::new(self.0, Body::Empty))
} }
} }

View file

@ -1,5 +1,6 @@
//! HTTP Request message related code. //! HTTP Request message related code.
use std::{str, fmt, mem}; use std::{str, fmt, mem};
use std::rc::Rc;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::collections::HashMap; use std::collections::HashMap;
use bytes::BytesMut; use bytes::BytesMut;
@ -13,45 +14,24 @@ use payload::Payload;
use multipart::Multipart; use multipart::Multipart;
use error::{ParseError, PayloadError, MultipartError, CookieParseError, HttpRangeError}; use error::{ParseError, PayloadError, MultipartError, CookieParseError, HttpRangeError};
struct HttpMessage {
/// An HTTP Request
pub struct HttpRequest {
version: Version, version: Version,
method: Method, method: Method,
path: String, path: String,
query: String, query: String,
headers: HeaderMap, headers: HeaderMap,
extensions: Extensions,
params: Params, params: Params,
cookies: Vec<Cookie<'static>>, cookies: Vec<Cookie<'static>>,
cookies_loaded: bool, cookies_loaded: bool,
extensions: Extensions,
addr: Option<SocketAddr>, addr: Option<SocketAddr>,
payload: Payload, payload: Payload,
} }
impl HttpRequest { impl Default for HttpMessage {
/// Construct a new Request.
#[inline]
pub fn new(method: Method, path: String,
version: Version, headers: HeaderMap, query: String, payload: Payload) -> Self
{
HttpRequest {
method: method,
path: path,
query: query,
version: version,
headers: headers,
params: Params::empty(),
cookies: Vec::new(),
cookies_loaded: false,
extensions: Extensions::new(),
addr: None,
payload: payload,
}
}
pub(crate) fn for_error() -> HttpRequest { fn default() -> HttpMessage {
HttpRequest { HttpMessage {
method: Method::GET, method: Method::GET,
path: String::new(), path: String::new(),
query: String::new(), query: String::new(),
@ -60,38 +40,75 @@ impl HttpRequest {
params: Params::empty(), params: Params::empty(),
cookies: Vec::new(), cookies: Vec::new(),
cookies_loaded: false, cookies_loaded: false,
extensions: Extensions::new(),
addr: None, addr: None,
payload: Payload::empty(), payload: Payload::empty(),
extensions: Extensions::new(),
} }
} }
}
/// An HTTP Request
pub struct HttpRequest(Rc<HttpMessage>);
impl HttpRequest {
/// Construct a new Request.
#[inline]
pub fn new(method: Method, path: String, version: Version,
headers: HeaderMap, query: String, payload: Payload) -> HttpRequest
{
HttpRequest(
Rc::new(HttpMessage {
method: method,
path: path,
query: query,
version: version,
headers: headers,
params: Params::empty(),
cookies: Vec::new(),
cookies_loaded: false,
addr: None,
payload: payload,
extensions: Extensions::new(),
})
)
}
pub(crate) fn for_error() -> HttpRequest {
HttpRequest(Rc::new(HttpMessage::default()))
}
fn as_mut(&mut self) -> &mut HttpMessage {
let r: &HttpMessage = self.0.as_ref();
#[allow(mutable_transmutes)]
unsafe{mem::transmute(r)}
}
/// Protocol extensions. /// Protocol extensions.
#[inline] #[inline]
pub fn extensions(&mut self) -> &mut Extensions { pub fn extensions(&mut self) -> &mut Extensions {
&mut self.extensions &mut self.as_mut().extensions
} }
/// Read the Request method. /// Read the Request method.
#[inline] #[inline]
pub fn method(&self) -> &Method { &self.method } pub fn method(&self) -> &Method { &self.0.method }
/// Read the Request Version. /// Read the Request Version.
#[inline] #[inline]
pub fn version(&self) -> Version { pub fn version(&self) -> Version {
self.version self.0.version
} }
/// Read the Request Headers. /// Read the Request Headers.
#[inline] #[inline]
pub fn headers(&self) -> &HeaderMap { pub fn headers(&self) -> &HeaderMap {
&self.headers &self.0.headers
} }
/// The target path of this Request. /// The target path of this Request.
#[inline] #[inline]
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
&self.path &self.0.path
} }
/// Remote IP of client initiated HTTP request. /// Remote IP of client initiated HTTP request.
@ -103,18 +120,18 @@ impl HttpRequest {
/// - peername of opened socket /// - peername of opened socket
#[inline] #[inline]
pub fn remote(&self) -> Option<&SocketAddr> { pub fn remote(&self) -> Option<&SocketAddr> {
self.addr.as_ref() self.0.addr.as_ref()
} }
pub(crate) fn set_remove_addr(&mut self, addr: Option<SocketAddr>) { pub(crate) fn set_remove_addr(&mut self, addr: Option<SocketAddr>) {
self.addr = addr self.as_mut().addr = addr
} }
/// Return a new iterator that yields pairs of `Cow<str>` for query parameters /// Return a new iterator that yields pairs of `Cow<str>` for query parameters
#[inline] #[inline]
pub fn query(&self) -> HashMap<String, String> { pub fn query(&self) -> HashMap<String, String> {
let mut q: HashMap<String, String> = HashMap::new(); let mut q: HashMap<String, String> = HashMap::new();
for (key, val) in form_urlencoded::parse(self.query.as_ref()) { for (key, val) in form_urlencoded::parse(self.0.query.as_ref()) {
q.insert(key.to_string(), val.to_string()); q.insert(key.to_string(), val.to_string());
} }
q q
@ -125,17 +142,17 @@ impl HttpRequest {
/// E.g., id=10 /// E.g., id=10
#[inline] #[inline]
pub fn query_string(&self) -> &str { pub fn query_string(&self) -> &str {
&self.query &self.0.query
} }
/// Return request cookies. /// Return request cookies.
pub fn cookies(&self) -> &Vec<Cookie<'static>> { pub fn cookies(&self) -> &Vec<Cookie<'static>> {
&self.cookies &self.0.cookies
} }
/// Return request cookie. /// Return request cookie.
pub fn cookie(&self, name: &str) -> Option<&Cookie> { pub fn cookie(&self, name: &str) -> Option<&Cookie> {
for cookie in &self.cookies { for cookie in &self.0.cookies {
if cookie.name() == name { if cookie.name() == name {
return Some(cookie) return Some(cookie)
} }
@ -146,17 +163,18 @@ impl HttpRequest {
/// Load cookies /// Load cookies
pub fn load_cookies(&mut self) -> Result<&Vec<Cookie<'static>>, CookieParseError> pub fn load_cookies(&mut self) -> Result<&Vec<Cookie<'static>>, CookieParseError>
{ {
if !self.cookies_loaded { if !self.0.cookies_loaded {
self.cookies_loaded = true; let msg = self.as_mut();
if let Some(val) = self.headers.get(header::COOKIE) { msg.cookies_loaded = true;
if let Some(val) = msg.headers.get(header::COOKIE) {
let s = str::from_utf8(val.as_bytes()) let s = str::from_utf8(val.as_bytes())
.map_err(CookieParseError::from)?; .map_err(CookieParseError::from)?;
for cookie in s.split("; ") { for cookie in s.split("; ") {
self.cookies.push(Cookie::parse_encoded(cookie)?.into_owned()); msg.cookies.push(Cookie::parse_encoded(cookie)?.into_owned());
} }
} }
} }
Ok(&self.cookies) Ok(&self.0.cookies)
} }
/// Get a reference to the Params object. /// Get a reference to the Params object.
@ -164,34 +182,34 @@ impl HttpRequest {
/// Route supports glob patterns: * for a single wildcard segment and :param /// Route supports glob patterns: * for a single wildcard segment and :param
/// for matching storing that segment of the request url in the Params object. /// for matching storing that segment of the request url in the Params object.
#[inline] #[inline]
pub fn match_info(&self) -> &Params { &self.params } pub fn match_info(&self) -> &Params { &self.0.params }
/// Set request Params. /// Set request Params.
pub fn set_match_info(&mut self, params: Params) { pub fn set_match_info(&mut self, params: Params) {
self.params = params; self.as_mut().params = params;
} }
/// Checks if a connection should be kept alive. /// Checks if a connection should be kept alive.
pub fn keep_alive(&self) -> bool { pub fn keep_alive(&self) -> bool {
if let Some(conn) = self.headers.get(header::CONNECTION) { if let Some(conn) = self.0.headers.get(header::CONNECTION) {
if let Ok(conn) = conn.to_str() { if let Ok(conn) = conn.to_str() {
if self.version == Version::HTTP_10 && conn.contains("keep-alive") { if self.0.version == Version::HTTP_10 && conn.contains("keep-alive") {
true true
} else { } else {
self.version == Version::HTTP_11 && self.0.version == Version::HTTP_11 &&
!(conn.contains("close") || conn.contains("upgrade")) !(conn.contains("close") || conn.contains("upgrade"))
} }
} else { } else {
false false
} }
} else { } else {
self.version != Version::HTTP_10 self.0.version != Version::HTTP_10
} }
} }
/// Read the request content type /// Read the request content type
pub fn content_type(&self) -> &str { pub fn content_type(&self) -> &str {
if let Some(content_type) = self.headers.get(header::CONTENT_TYPE) { if let Some(content_type) = self.0.headers.get(header::CONTENT_TYPE) {
if let Ok(content_type) = content_type.to_str() { if let Ok(content_type) = content_type.to_str() {
return content_type return content_type
} }
@ -201,17 +219,17 @@ impl HttpRequest {
/// Check if request requires connection upgrade /// Check if request requires connection upgrade
pub(crate) fn upgrade(&self) -> bool { pub(crate) fn upgrade(&self) -> bool {
if let Some(conn) = self.headers.get(header::CONNECTION) { if let Some(conn) = self.0.headers.get(header::CONNECTION) {
if let Ok(s) = conn.to_str() { if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade") return s.to_lowercase().contains("upgrade")
} }
} }
self.method == Method::CONNECT self.0.method == Method::CONNECT
} }
/// Check if request has chunked transfer encoding /// Check if request has chunked transfer encoding
pub fn chunked(&self) -> Result<bool, ParseError> { pub fn chunked(&self) -> Result<bool, ParseError> {
if let Some(encodings) = self.headers.get(header::TRANSFER_ENCODING) { if let Some(encodings) = self.0.headers.get(header::TRANSFER_ENCODING) {
if let Ok(s) = encodings.to_str() { if let Ok(s) = encodings.to_str() {
Ok(s.to_lowercase().contains("chunked")) Ok(s.to_lowercase().contains("chunked"))
} else { } else {
@ -225,7 +243,7 @@ impl HttpRequest {
/// Parses Range HTTP header string as per RFC 2616. /// Parses Range HTTP header string as per RFC 2616.
/// `size` is full size of response (file). /// `size` is full size of response (file).
pub fn range(&self, size: u64) -> Result<Vec<HttpRange>, HttpRangeError> { pub fn range(&self, size: u64) -> Result<Vec<HttpRange>, HttpRangeError> {
if let Some(range) = self.headers().get(header::RANGE) { if let Some(range) = self.0.headers.get(header::RANGE) {
HttpRange::parse(unsafe{str::from_utf8_unchecked(range.as_bytes())}, size) HttpRange::parse(unsafe{str::from_utf8_unchecked(range.as_bytes())}, size)
.map_err(|e| e.into()) .map_err(|e| e.into())
} else { } else {
@ -236,25 +254,25 @@ impl HttpRequest {
/// Returns reference to the associated http payload. /// Returns reference to the associated http payload.
#[inline] #[inline]
pub fn payload(&self) -> &Payload { pub fn payload(&self) -> &Payload {
&self.payload &self.0.payload
} }
/// Returns mutable reference to the associated http payload. /// Returns mutable reference to the associated http payload.
#[inline] #[inline]
pub fn payload_mut(&mut self) -> &mut Payload { pub fn payload_mut(&mut self) -> &mut Payload {
&mut self.payload &mut self.as_mut().payload
} }
/// Return payload /// Return payload
pub fn take_payload(&mut self) -> Payload { pub fn take_payload(&mut self) -> Payload {
mem::replace(&mut self.payload, Payload::empty()) mem::replace(&mut self.as_mut().payload, Payload::empty())
} }
/// Return stream to process BODY as multipart. /// Return stream to process BODY as multipart.
/// ///
/// Content-type: multipart/form-data; /// Content-type: multipart/form-data;
pub fn multipart(&self, payload: Payload) -> Result<Multipart, MultipartError> { pub fn multipart(&self, payload: Payload) -> Result<Multipart, MultipartError> {
Ok(Multipart::new(Multipart::boundary(&self.headers)?, payload)) Ok(Multipart::new(Multipart::boundary(&self.0.headers)?, payload))
} }
/// Parse `application/x-www-form-urlencoded` encoded body. /// Parse `application/x-www-form-urlencoded` encoded body.
@ -287,7 +305,7 @@ impl HttpRequest {
} }
} }
if let Some(content_type) = self.headers().get(header::CONTENT_TYPE) { if let Some(content_type) = self.0.headers.get(header::CONTENT_TYPE) {
if let Ok(content_type) = content_type.to_str() { if let Ok(content_type) = content_type.to_str() {
if content_type.to_lowercase() == "application/x-www-form-urlencoded" { if content_type.to_lowercase() == "application/x-www-form-urlencoded" {
return Ok(UrlEncoded{pl: payload, body: BytesMut::new()}) return Ok(UrlEncoded{pl: payload, body: BytesMut::new()})
@ -299,19 +317,25 @@ impl HttpRequest {
} }
} }
impl Clone for HttpRequest {
fn clone(&self) -> HttpRequest {
HttpRequest(Rc::clone(&self.0))
}
}
impl fmt::Debug for HttpRequest { impl fmt::Debug for HttpRequest {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = write!(f, "\nHttpRequest {:?} {}:{}\n", let res = write!(f, "\nHttpRequest {:?} {}:{}\n",
self.version, self.method, self.path); self.0.version, self.0.method, self.0.path);
if !self.query_string().is_empty() { if !self.query_string().is_empty() {
let _ = write!(f, " query: ?{:?}\n", self.query_string()); let _ = write!(f, " query: ?{:?}\n", self.query_string());
} }
if !self.params.is_empty() { if !self.0.params.is_empty() {
let _ = write!(f, " params: {:?}\n", self.params); let _ = write!(f, " params: {:?}\n", self.0.params);
} }
let _ = write!(f, " headers:\n"); let _ = write!(f, " headers:\n");
for key in self.headers.keys() { for key in self.0.headers.keys() {
let vals: Vec<_> = self.headers.get_all(key).iter().collect(); let vals: Vec<_> = self.0.headers.get_all(key).iter().collect();
if vals.len() > 1 { if vals.len() > 1 {
let _ = write!(f, " {:?}: {:?}\n", key, vals); let _ = write!(f, " {:?}: {:?}\n", key, vals);
} else { } else {

View file

@ -10,8 +10,8 @@ use h1writer::Writer;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use httpresponse::HttpResponse; use httpresponse::HttpResponse;
type Handler = Fn(&mut HttpRequest) -> Task; type Handler = Fn(HttpRequest) -> Task;
pub(crate) type PipelineHandler<'a> = &'a Fn(&mut HttpRequest) -> Task; pub(crate) type PipelineHandler<'a> = &'a Fn(HttpRequest) -> Task;
pub struct Pipeline(PipelineState); pub struct Pipeline(PipelineState);
@ -26,10 +26,10 @@ enum PipelineState {
impl Pipeline { impl Pipeline {
pub fn new(mut req: HttpRequest, pub fn new(req: HttpRequest, mw: Rc<Vec<Box<Middleware>>>, handler: PipelineHandler) -> Pipeline
mw: Rc<Vec<Box<Middleware>>>, handler: PipelineHandler) -> Pipeline { {
if mw.is_empty() { if mw.is_empty() {
let task = (handler)(&mut req); let task = (handler)(req.clone());
Pipeline(PipelineState::Task(Box::new((task, req)))) Pipeline(PipelineState::Task(Box::new((task, req))))
} else { } else {
match Start::init(mw, req, handler) { match Start::init(mw, req, handler) {
@ -191,7 +191,7 @@ impl Start {
let len = self.middlewares.len(); let len = self.middlewares.len();
loop { loop {
if self.idx == len { if self.idx == len {
let task = (unsafe{&*self.hnd})(&mut req); let task = (unsafe{&*self.hnd})(req.clone());
return Ok(StartResult::Ready( return Ok(StartResult::Ready(
Box::new(Handle::new(self.idx-1, req, self.prepare(task), self.middlewares)))) Box::new(Handle::new(self.idx-1, req, self.prepare(task), self.middlewares))))
} else { } else {
@ -243,7 +243,7 @@ impl Start {
self.prepare(Task::reply(resp)), Rc::clone(&self.middlewares))))) self.prepare(Task::reply(resp)), Rc::clone(&self.middlewares)))))
} }
if self.idx == len { if self.idx == len {
let task = (unsafe{&*self.hnd})(&mut req); let task = (unsafe{&*self.hnd})(req.clone());
return Ok(Async::Ready(Box::new(Handle::new( return Ok(Async::Ready(Box::new(Handle::new(
self.idx-1, req, self.prepare(task), Rc::clone(&self.middlewares))))) self.idx-1, req, self.prepare(task), Rc::clone(&self.middlewares)))))
} else { } else {

View file

@ -64,7 +64,7 @@ impl<S> Resource<S> where S: 'static {
/// Register handler for specified method. /// Register handler for specified method.
pub fn handler<F, R>(&mut self, method: Method, handler: F) pub fn handler<F, R>(&mut self, method: Method, handler: F)
where F: Fn(&mut HttpRequest, &S) -> Result<R> + 'static, where F: Fn(HttpRequest, &S) -> Result<R> + 'static,
R: Into<HttpResponse> + 'static, R: Into<HttpResponse> + 'static,
{ {
self.routes.insert(method, Box::new(FnHandler::new(handler))); self.routes.insert(method, Box::new(FnHandler::new(handler)));
@ -72,7 +72,7 @@ impl<S> Resource<S> where S: 'static {
/// Register async handler for specified method. /// Register async handler for specified method.
pub fn async<F, R>(&mut self, method: Method, handler: F) pub fn async<F, R>(&mut self, method: Method, handler: F)
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static, R: Stream<Item=Frame, Error=Error> + 'static,
{ {
self.routes.insert(method, Box::new(StreamHandler::new(handler))); self.routes.insert(method, Box::new(StreamHandler::new(handler)));
@ -125,7 +125,7 @@ impl<S> Resource<S> where S: 'static {
impl<S: 'static> RouteHandler<S> for Resource<S> { impl<S: 'static> RouteHandler<S> for Resource<S> {
fn handle(&self, req: &mut HttpRequest, state: Rc<S>) -> Task { fn handle(&self, req: HttpRequest, state: Rc<S>) -> Task {
if let Some(handler) = self.routes.get(req.method()) { if let Some(handler) = self.routes.get(req.method()) {
handler.handle(req, state) handler.handle(req, state)
} else { } else {

View file

@ -33,7 +33,7 @@ impl Frame {
#[allow(unused_variables)] #[allow(unused_variables)]
pub trait RouteHandler<S>: 'static { pub trait RouteHandler<S>: 'static {
/// Handle request /// Handle request
fn handle(&self, req: &mut HttpRequest, state: Rc<S>) -> Task; fn handle(&self, req: HttpRequest, state: Rc<S>) -> Task;
/// Set route prefix /// Set route prefix
fn set_prefix(&mut self, prefix: String) {} fn set_prefix(&mut self, prefix: String) {}
@ -50,7 +50,7 @@ pub trait Route: Actor {
type State; type State;
/// Handle `EXPECT` header. By default respones with `HTTP/1.1 100 Continue` /// Handle `EXPECT` header. By default respones with `HTTP/1.1 100 Continue`
fn expect(req: &HttpRequest, ctx: &mut Self::Context) -> Result<(), Error> fn expect(req: &mut HttpRequest, ctx: &mut Self::Context) -> Result<(), Error>
where Self: Actor<Context=HttpContext<Self>> where Self: Actor<Context=HttpContext<Self>>
{ {
// handle expect header only for HTTP/1.1 // handle expect header only for HTTP/1.1
@ -80,7 +80,7 @@ pub trait Route: Actor {
/// request/response or websocket connection. /// request/response or websocket connection.
/// In that case `HttpContext::start` and `HttpContext::write` has to be used /// In that case `HttpContext::start` and `HttpContext::write` has to be used
/// for writing response. /// for writing response.
fn request(req: &mut HttpRequest, ctx: &mut Self::Context) -> RouteResult<Self>; fn request(req: HttpRequest, ctx: &mut Self::Context) -> RouteResult<Self>;
/// This method creates `RouteFactory` for this actor. /// This method creates `RouteFactory` for this actor.
fn factory() -> RouteFactory<Self, Self::State> { fn factory() -> RouteFactory<Self, Self::State> {
@ -95,12 +95,12 @@ impl<A, S> RouteHandler<S> for RouteFactory<A, S>
where A: Actor<Context=HttpContext<A>> + Route<State=S>, where A: Actor<Context=HttpContext<A>> + Route<State=S>,
S: 'static S: 'static
{ {
fn handle(&self, req: &mut HttpRequest, state: Rc<A::State>) -> Task { fn handle(&self, mut req: HttpRequest, state: Rc<A::State>) -> Task {
let mut ctx = HttpContext::new(state); let mut ctx = HttpContext::new(state);
// handle EXPECT header // handle EXPECT header
if req.headers().contains_key(header::EXPECT) { if req.headers().contains_key(header::EXPECT) {
if let Err(resp) = A::expect(req, &mut ctx) { if let Err(resp) = A::expect(&mut req, &mut ctx) {
return Task::reply(resp) return Task::reply(resp)
} }
} }
@ -114,7 +114,7 @@ impl<A, S> RouteHandler<S> for RouteFactory<A, S>
/// Fn() route handler /// Fn() route handler
pub(crate) pub(crate)
struct FnHandler<S, R, F> struct FnHandler<S, R, F>
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Into<HttpResponse>, R: Into<HttpResponse>,
S: 'static, S: 'static,
{ {
@ -123,7 +123,7 @@ struct FnHandler<S, R, F>
} }
impl<S, R, F> FnHandler<S, R, F> impl<S, R, F> FnHandler<S, R, F>
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Into<HttpResponse> + 'static, R: Into<HttpResponse> + 'static,
S: 'static, S: 'static,
{ {
@ -133,11 +133,11 @@ impl<S, R, F> FnHandler<S, R, F>
} }
impl<S, R, F> RouteHandler<S> for FnHandler<S, R, F> impl<S, R, F> RouteHandler<S> for FnHandler<S, R, F>
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Into<HttpResponse> + 'static, R: Into<HttpResponse> + 'static,
S: 'static, S: 'static,
{ {
fn handle(&self, req: &mut HttpRequest, state: Rc<S>) -> Task { fn handle(&self, req: HttpRequest, state: Rc<S>) -> Task {
Task::reply((self.f)(req, &state).into()) Task::reply((self.f)(req, &state).into())
} }
} }
@ -145,7 +145,7 @@ impl<S, R, F> RouteHandler<S> for FnHandler<S, R, F>
/// Async route handler /// Async route handler
pub(crate) pub(crate)
struct StreamHandler<S, R, F> struct StreamHandler<S, R, F>
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static, R: Stream<Item=Frame, Error=Error> + 'static,
S: 'static, S: 'static,
{ {
@ -154,7 +154,7 @@ struct StreamHandler<S, R, F>
} }
impl<S, R, F> StreamHandler<S, R, F> impl<S, R, F> StreamHandler<S, R, F>
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static, R: Stream<Item=Frame, Error=Error> + 'static,
S: 'static, S: 'static,
{ {
@ -164,11 +164,11 @@ impl<S, R, F> StreamHandler<S, R, F>
} }
impl<S, R, F> RouteHandler<S> for StreamHandler<S, R, F> impl<S, R, F> RouteHandler<S> for StreamHandler<S, R, F>
where F: Fn(&mut HttpRequest, &S) -> R + 'static, where F: Fn(HttpRequest, &S) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static, R: Stream<Item=Frame, Error=Error> + 'static,
S: 'static, S: 'static,
{ {
fn handle(&self, req: &mut HttpRequest, state: Rc<S>) -> Task { fn handle(&self, req: HttpRequest, state: Rc<S>) -> Task {
Task::with_stream((self.f)(req, &state)) Task::with_stream((self.f)(req, &state))
} }
} }

View file

@ -137,7 +137,7 @@ impl<S: 'static> RouteHandler<S> for StaticFiles {
} }
} }
fn handle(&self, req: &mut HttpRequest, _: Rc<S>) -> Task { fn handle(&self, req: HttpRequest, _: Rc<S>) -> Task {
if !self.accessible { if !self.accessible {
Task::reply(HTTPNotFound) Task::reply(HTTPNotFound)
} else { } else {

View file

@ -22,14 +22,14 @@
//! impl Route for WsRoute { //! impl Route for WsRoute {
//! type State = (); //! type State = ();
//! //!
//! fn request(req: &mut HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self> //! fn request(mut req: HttpRequest, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
//! { //! {
//! // WebSocket handshake //! // WebSocket handshake
//! let resp = ws::handshake(&req)?; //! let resp = ws::handshake(&req)?;
//! // Send handshake response to peer //! // Send handshake response to peer
//! ctx.start(resp); //! ctx.start(resp);
//! // Map Payload into WsStream //! // Map Payload into WsStream
//! ctx.add_stream(ws::WsStream::new(req)); //! ctx.add_stream(ws::WsStream::new(&mut req));
//! // Start ws messages processing //! // Start ws messages processing
//! Reply::async(WsRoute) //! Reply::async(WsRoute)
//! } //! }