1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

fix drain support for actor; make pattern more reusable

This commit is contained in:
Nikolay Kim 2018-01-02 23:43:17 -08:00
parent 9e6d090fd0
commit 70ea43b3c0
6 changed files with 82 additions and 30 deletions

View File

@ -231,7 +231,7 @@ impl<S> Application<S> where S: 'static {
let mut resource = Resource::default();
f(&mut resource);
let pattern = Pattern::new(resource.get_name(), path);
let pattern = Pattern::new(resource.get_name(), path, "^/");
if parts.resources.contains_key(&pattern) {
panic!("Resource {:?} is registered.", path);
}
@ -285,7 +285,7 @@ impl<S> Application<S> where S: 'static {
panic!("External resource {:?} is registered.", name.as_ref());
}
parts.external.insert(
String::from(name.as_ref()), Pattern::new(name.as_ref(), url.as_ref()));
String::from(name.as_ref()), Pattern::new(name.as_ref(), url.as_ref(), "^/"));
}
self
}

View File

@ -144,6 +144,7 @@ impl<T: Responder, E: Into<Error>> Responder for Result<T, E>
}
impl<E: Into<Error>> From<Result<Reply, E>> for Reply {
#[inline]
fn from(res: Result<Reply, E>) -> Self {
match res {
Ok(val) => val,
@ -152,6 +153,23 @@ impl<E: Into<Error>> From<Result<Reply, E>> for Reply {
}
}
impl<E: Into<Error>> From<Result<HttpResponse, E>> for Reply {
#[inline]
fn from(res: Result<HttpResponse, E>) -> Self {
match res {
Ok(val) => Reply(ReplyItem::Message(val)),
Err(err) => Reply(ReplyItem::Message(err.into().into())),
}
}
}
impl From<Box<Future<Item=HttpResponse, Error=Error>>> for Reply {
#[inline]
fn from(fut: Box<Future<Item=HttpResponse, Error=Error>>) -> Reply {
Reply(ReplyItem::Future(fut))
}
}
impl<I, E> Responder for Box<Future<Item=I, Error=E>>
where I: Responder + 'static,
E: Into<Error> + 'static

View File

@ -93,6 +93,12 @@ impl From<StaticResponse> for HttpResponse {
}
}
impl From<StaticResponse> for Reply {
fn from(st: StaticResponse) -> Self {
HttpResponse::new(st.0, Body::Empty).into()
}
}
macro_rules! STATIC_RESP {
($name:ident, $status:expr) => {
#[allow(non_snake_case)]

View File

@ -137,8 +137,8 @@ impl<S> HttpRequest<S> {
#[inline]
/// Construct new http request with state.
pub fn change_state<NS>(self, state: Rc<NS>) -> HttpRequest<NS> {
HttpRequest(self.0, Some(state), self.2.clone())
pub fn change_state<NS>(&self, state: Rc<NS>) -> HttpRequest<NS> {
HttpRequest(self.0.clone(), Some(state), self.2.clone())
}
#[inline]
@ -726,7 +726,7 @@ mod tests {
let mut resource = Resource::<()>::default();
resource.name("index");
let mut map = HashMap::new();
map.insert(Pattern::new("index", "/{key}/"), Some(resource));
map.insert(Pattern::new("index", "/{key}/", "^/"), Some(resource));
let (router, _) = Router::new("", ServerSettings::default(), map);
assert!(router.recognize(&mut req).is_some());
@ -828,7 +828,7 @@ mod tests {
let mut resource = Resource::<()>::default();
resource.name("index");
let mut map = HashMap::new();
map.insert(Pattern::new("index", "/user/{name}.{ext}"), Some(resource));
map.insert(Pattern::new("index", "/user/{name}.{ext}", "^/"), Some(resource));
let (router, _) = Router::new("/", ServerSettings::default(), map);
assert!(router.has_route("/user/test.html"));
assert!(!router.has_route("/test/unknown"));
@ -857,7 +857,7 @@ mod tests {
let mut resource = Resource::<()>::default();
resource.name("index");
let mut map = HashMap::new();
map.insert(Pattern::new("index", "/user/{name}.{ext}"), Some(resource));
map.insert(Pattern::new("index", "/user/{name}.{ext}", "^/"), Some(resource));
let (router, _) = Router::new("/prefix/", ServerSettings::default(), map);
assert!(router.has_route("/user/test.html"));
assert!(!router.has_route("/prefix/user/test.html"));
@ -876,7 +876,7 @@ mod tests {
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);
map.insert(Pattern::new("youtube", "https://youtube.com/watch/{video_id}", "^/"), None);
let (router, _) = Router::new::<()>("", ServerSettings::default(), map);
assert!(!router.has_route("https://youtube.com/watch/unknown"));

View File

@ -40,6 +40,7 @@ struct PipelineInfo<S> {
mws: Rc<Vec<Box<Middleware<S>>>>,
context: Option<Box<ActorHttpContext>>,
error: Option<Error>,
disconnected: Option<bool>,
}
impl<S> PipelineInfo<S> {
@ -50,6 +51,7 @@ impl<S> PipelineInfo<S> {
mws: Rc::new(Vec::new()),
error: None,
context: None,
disconnected: None,
}
}
@ -84,6 +86,7 @@ impl<S, H: PipelineHandler<S>> Pipeline<S, H> {
mws: mws,
error: None,
context: None,
disconnected: None,
};
let state = StartMiddlewares::init(&mut info, handler);
@ -114,9 +117,7 @@ impl<S, H> Pipeline<S, H> {
impl<S, H: PipelineHandler<S>> HttpHandlerTask for Pipeline<S, H> {
fn disconnected(&mut self) {
if let Some(ref mut context) = self.0.context {
context.disconnected();
}
self.0.disconnected = Some(true);
}
fn poll_io(&mut self, io: &mut Writer) -> Poll<bool, Error> {
@ -592,10 +593,14 @@ impl<S, H> ProcessResponse<S, H> {
}
},
IOState::Actor(mut ctx) => {
if info.disconnected.take().is_some() {
ctx.disconnected();
}
match ctx.poll() {
Ok(Async::Ready(Some(frame))) => {
match frame {
Frame::Payload(None) => {
println!("ACTOR PAYLOAD EOF");
info.context = Some(ctx);
self.iostate = IOState::Done;
if let Err(err) = io.write_eof() {
@ -606,6 +611,7 @@ impl<S, H> ProcessResponse<S, H> {
break
},
Frame::Payload(Some(chunk)) => {
println!("ACTOR PAYLOAD");
self.iostate = IOState::Actor(ctx);
match io.write(chunk.as_ref()) {
Err(err) => {
@ -617,7 +623,9 @@ impl<S, H> ProcessResponse<S, H> {
}
},
Frame::Drain(fut) => {
println!("ACTOR DRAIN");
self.drain = Some(fut);
self.iostate = IOState::Actor(ctx);
break
}
}

View File

@ -88,8 +88,7 @@ impl Router {
}
if let Some(idx) = idx {
let path: &str = unsafe{ mem::transmute(&req.path()[self.0.prefix_len..]) };
self.0.patterns[idx].update_match_info(path, req);
self.0.patterns[idx].update_match_info(req, self.0.prefix_len);
return Some(idx)
} else {
None
@ -159,8 +158,8 @@ impl Pattern {
/// Parse path pattern and create new `Pattern` instance.
///
/// Panics if path pattern is wrong.
pub fn new(name: &str, path: &str) -> Self {
let (pattern, elements) = Pattern::parse(path);
pub fn new(name: &str, path: &str, starts: &str) -> Self {
let (pattern, elements) = Pattern::parse(path, starts);
let re = match Regex::new(&pattern) {
Ok(re) => re,
@ -190,8 +189,9 @@ impl Pattern {
}
/// Extract pattern parameters from the text
pub fn update_match_info<S>(&self, text: &str, req: &mut HttpRequest<S>) {
pub fn update_match_info<S>(&self, req: &mut HttpRequest<S>, prefix: usize) {
if !self.names.is_empty() {
let text: &str = unsafe{ mem::transmute(&req.path()[prefix..]) };
if let Some(captures) = self.re.captures(text) {
let mut idx = 0;
for capture in captures.iter() {
@ -207,6 +207,25 @@ impl Pattern {
}
}
/// Extract pattern parameters from the text
pub fn get_match_info<'a>(&self, text: &'a str) -> HashMap<&str, &'a str> {
let mut info = HashMap::new();
if !self.names.is_empty() {
if let Some(captures) = self.re.captures(text) {
let mut idx = 0;
for capture in captures.iter() {
if let Some(ref m) = capture {
if idx != 0 {
info.insert(self.names[idx-1].as_str(), m.as_str());
}
idx += 1;
}
}
};
}
info
}
/// Build pattern path.
pub fn path<U, I>(&self, prefix: Option<&str>, elements: U) -> Result<String, UrlGenerationError>
where U: IntoIterator<Item=I>,
@ -218,7 +237,6 @@ impl Pattern {
} else {
String::new()
};
println!("TEST: {:?} {:?}", path, prefix);
for el in &self.elements {
match *el {
PatternElement::Str(ref s) => path.push_str(s),
@ -234,10 +252,10 @@ impl Pattern {
Ok(path)
}
fn parse(pattern: &str) -> (String, Vec<PatternElement>) {
fn parse(pattern: &str, starts: &str) -> (String, Vec<PatternElement>) {
const DEFAULT_PATTERN: &str = "[^/]+";
let mut re = String::from("^/");
let mut re = String::from(starts);
let mut el = String::new();
let mut in_param = false;
let mut in_param_pattern = false;
@ -312,12 +330,14 @@ mod tests {
#[test]
fn test_recognizer() {
let mut routes = HashMap::new();
routes.insert(Pattern::new("", "/name"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}/index.html"), Some(Resource::default()));
routes.insert(Pattern::new("", "/v{val}/{val2}/index.html"), Some(Resource::default()));
routes.insert(Pattern::new("", "/v/{tail:.*}"), Some(Resource::default()));
routes.insert(Pattern::new("", "{test}/index.html"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}/index.html", "^/"),
Some(Resource::default()));
routes.insert(Pattern::new("", "/v{val}/{val2}/index.html", "^/"),
Some(Resource::default()));
routes.insert(Pattern::new("", "/v/{tail:.*}", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "{test}/index.html", "^/"), Some(Resource::default()));
let (rec, _) = Router::new::<()>("", ServerSettings::default(), routes);
let mut req = TestRequest::with_uri("/name").finish();
@ -350,8 +370,8 @@ mod tests {
#[test]
fn test_recognizer_with_prefix() {
let mut routes = HashMap::new();
routes.insert(Pattern::new("", "/name"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}", "^/"), Some(Resource::default()));
let (rec, _) = Router::new::<()>("/test", ServerSettings::default(), routes);
let mut req = TestRequest::with_uri("/name").finish();
@ -367,8 +387,8 @@ mod tests {
// same patterns
let mut routes = HashMap::new();
routes.insert(Pattern::new("", "/name"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}", "^/"), Some(Resource::default()));
let (rec, _) = Router::new::<()>("/test2", ServerSettings::default(), routes);
let mut req = TestRequest::with_uri("/name").finish();
@ -378,7 +398,7 @@ mod tests {
}
fn assert_parse(pattern: &str, expected_re: &str) -> Regex {
let (re_str, _) = Pattern::parse(pattern);
let (re_str, _) = Pattern::parse(pattern, "^/");
assert_eq!(&*re_str, expected_re);
Regex::new(&re_str).unwrap()
}