1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

use Params object for query

This commit is contained in:
Nikolay Kim 2017-12-27 19:02:29 -08:00
parent 19e1c1b75b
commit 6bb893deab
3 changed files with 24 additions and 13 deletions

View File

@ -146,9 +146,9 @@ and:
/{foo}/bar/baz /{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 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 `[^{}/]+`. A replacement marker in a pattern matches the regular expression `[^{}/]+`.

View File

@ -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 stream combinators. Also *Payload* provides serveral convinience methods that return
future object that resolve to Bytes object. 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. 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 matches in input bytes stream
In this example handle reads request payload chunk by chunk and prints every chunk. In this example handle reads request payload chunk by chunk and prints every chunk.

View File

@ -1,6 +1,5 @@
//! HTTP Request message related code. //! HTTP Request message related code.
use std::{str, fmt, mem}; use std::{str, fmt, mem};
use std::borrow::Cow;
use std::rc::Rc; use std::rc::Rc;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::collections::HashMap; use std::collections::HashMap;
@ -30,6 +29,8 @@ pub struct HttpMessage {
pub extensions: Extensions, pub extensions: Extensions,
pub params: Params<'static>, pub params: Params<'static>,
pub cookies: Option<Vec<Cookie<'static>>>, pub cookies: Option<Vec<Cookie<'static>>>,
pub query: Params<'static>,
pub query_loaded: bool,
pub addr: Option<SocketAddr>, pub addr: Option<SocketAddr>,
pub payload: Option<Payload>, pub payload: Option<Payload>,
pub info: Option<ConnectionInfo<'static>>, pub info: Option<ConnectionInfo<'static>>,
@ -44,6 +45,8 @@ impl Default for HttpMessage {
version: Version::HTTP_11, version: Version::HTTP_11,
headers: HeaderMap::with_capacity(16), headers: HeaderMap::with_capacity(16),
params: Params::default(), params: Params::default(),
query: Params::default(),
query_loaded: false,
cookies: None, cookies: None,
addr: None, addr: None,
payload: None, payload: None,
@ -79,6 +82,8 @@ impl HttpMessage {
self.headers.clear(); self.headers.clear();
self.extensions.clear(); self.extensions.clear();
self.params.clear(); self.params.clear();
self.query.clear();
self.query_loaded = false;
self.cookies = None; self.cookies = None;
self.addr = None; self.addr = None;
self.info = None; self.info = None;
@ -102,6 +107,8 @@ impl HttpRequest<()> {
version: version, version: version,
headers: headers, headers: headers,
params: Params::default(), params: Params::default(),
query: Params::default(),
query_loaded: false,
cookies: None, cookies: None,
addr: None, addr: None,
payload: payload, payload: payload,
@ -272,13 +279,17 @@ impl<S> HttpRequest<S> {
self.as_mut().addr = addr self.as_mut().addr = addr
} }
/// Return a new iterator that yields pairs of `Cow<str>` for query parameters /// Get a reference to the Params object.
pub fn query(&self) -> HashMap<Cow<str>, Cow<str>> { /// Params is a container for url query parameters.
let mut q = HashMap::new(); pub fn query(&self) -> &Params {
for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) { if !self.as_ref().query_loaded {
q.insert(key, val); 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. /// The query string in the URL.