From 6bb893deabe885b8e3deaed3d727c86f7dc70bf8 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 27 Dec 2017 19:02:29 -0800 Subject: [PATCH] use Params object for query --- guide/src/qs_5.md | 4 ++-- guide/src/qs_7.md | 8 ++++---- src/httprequest.rs | 25 ++++++++++++++++++------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/guide/src/qs_5.md b/guide/src/qs_5.md index 5f8042179..dba0b6370 100644 --- a/guide/src/qs_5.md +++ b/guide/src/qs_5.md @@ -146,9 +146,9 @@ and: /{foo}/bar/baz ``` -A *variable part*(replacement marker) is specified in the form *{identifier}*, +A *variable part* (replacement marker) is specified in the form *{identifier}*, where this means "accept any characters up to the next slash character and use this -as the name in the `HttpRequest.match_info` object". +as the name in the `HttpRequest.match_info()` object". A replacement marker in a pattern matches the regular expression `[^{}/]+`. diff --git a/guide/src/qs_7.md b/guide/src/qs_7.md index f4fdf5e97..5d3447514 100644 --- a/guide/src/qs_7.md +++ b/guide/src/qs_7.md @@ -262,14 +262,14 @@ At the same time *Payload* implements *Stream* trait, so it could be used with v stream combinators. Also *Payload* provides serveral convinience methods that return future object that resolve to Bytes object. -* *readany* method returns *Stream* of *Bytes* objects. +* *readany()* method returns *Stream* of *Bytes* objects. -* *readexactly* method returns *Future* that resolves when specified number of bytes +* *readexactly()* method returns *Future* that resolves when specified number of bytes get received. -* *readline* method returns *Future* that resolves when `\n` get received. +* *readline()* method returns *Future* that resolves when `\n` get received. -* *readuntil* method returns *Future* that resolves when specified bytes string +* *readuntil()* method returns *Future* that resolves when specified bytes string matches in input bytes stream In this example handle reads request payload chunk by chunk and prints every chunk. diff --git a/src/httprequest.rs b/src/httprequest.rs index ddaa4e981..4b8f18b07 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -1,6 +1,5 @@ //! HTTP Request message related code. use std::{str, fmt, mem}; -use std::borrow::Cow; use std::rc::Rc; use std::net::SocketAddr; use std::collections::HashMap; @@ -30,6 +29,8 @@ pub struct HttpMessage { pub extensions: Extensions, pub params: Params<'static>, pub cookies: Option>>, + pub query: Params<'static>, + pub query_loaded: bool, pub addr: Option, pub payload: Option, pub info: Option>, @@ -44,6 +45,8 @@ impl Default for HttpMessage { version: Version::HTTP_11, headers: HeaderMap::with_capacity(16), params: Params::default(), + query: Params::default(), + query_loaded: false, cookies: None, addr: None, payload: None, @@ -79,6 +82,8 @@ impl HttpMessage { self.headers.clear(); self.extensions.clear(); self.params.clear(); + self.query.clear(); + self.query_loaded = false; self.cookies = None; self.addr = None; self.info = None; @@ -102,6 +107,8 @@ impl HttpRequest<()> { version: version, headers: headers, params: Params::default(), + query: Params::default(), + query_loaded: false, cookies: None, addr: None, payload: payload, @@ -272,13 +279,17 @@ impl HttpRequest { self.as_mut().addr = addr } - /// Return a new iterator that yields pairs of `Cow` for query parameters - pub fn query(&self) -> HashMap, Cow> { - let mut q = HashMap::new(); - for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) { - q.insert(key, val); + /// Get a reference to the Params object. + /// Params is a container for url query parameters. + pub fn query(&self) -> &Params { + if !self.as_ref().query_loaded { + let params: &mut Params = unsafe{ mem::transmute(&mut self.as_mut().query) }; + self.as_mut().query_loaded = true; + for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) { + params.add(key, val); + } } - q + unsafe{ mem::transmute(&self.as_ref().query) } } /// The query string in the URL.