mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
better query() method impl; update doc strings
This commit is contained in:
parent
009874125e
commit
2bad99b645
@ -317,10 +317,18 @@ resource with the name "foo" and the pattern "{a}/{b}/{c}", you might do this.
|
|||||||
# use actix_web::httpcodes::*;
|
# use actix_web::httpcodes::*;
|
||||||
#
|
#
|
||||||
fn index(req: HttpRequest) -> HttpResponse {
|
fn index(req: HttpRequest) -> HttpResponse {
|
||||||
let url = req.url_for("foo", &["1", "2", "3"]);
|
let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
|
||||||
HTTPOk.into()
|
HTTPOk.into()
|
||||||
}
|
}
|
||||||
# fn main() {}
|
|
||||||
|
fn main() {
|
||||||
|
let app = Application::new()
|
||||||
|
.resource("/test/{one}/{two}/{three}", |r| {
|
||||||
|
r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
||||||
|
r.method(Method::GET).f(|_| httpcodes::HTTPOk);
|
||||||
|
})
|
||||||
|
.finish();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This would return something like the string *http://example.com/1/2/3* (at least if
|
This would return something like the string *http://example.com/1/2/3* (at least if
|
||||||
|
@ -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::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;
|
||||||
@ -235,6 +236,27 @@ impl<S> HttpRequest<S> {
|
|||||||
self.as_ref().info.as_ref().unwrap()
|
self.as_ref().info.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate url for named resource
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # extern crate actix_web;
|
||||||
|
/// # use actix_web::*;
|
||||||
|
/// # use actix_web::httpcodes::*;
|
||||||
|
/// #
|
||||||
|
/// fn index(req: HttpRequest) -> HttpResponse {
|
||||||
|
/// let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
|
||||||
|
/// HTTPOk.into()
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let app = Application::new()
|
||||||
|
/// .resource("/test/{one}/{two}/{three}", |r| {
|
||||||
|
/// r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
||||||
|
/// r.method(Method::GET).f(|_| httpcodes::HTTPOk);
|
||||||
|
/// })
|
||||||
|
/// .finish();
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn url_for<U, I>(&self, name: &str, elements: U) -> Result<Url, UrlGenerationError>
|
pub fn url_for<U, I>(&self, name: &str, elements: U) -> Result<Url, UrlGenerationError>
|
||||||
where U: IntoIterator<Item=I>,
|
where U: IntoIterator<Item=I>,
|
||||||
I: AsRef<str>,
|
I: AsRef<str>,
|
||||||
@ -252,11 +274,18 @@ impl<S> HttpRequest<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This method returns reference to current `Router` object.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn router(&self) -> Option<&Router<S>> {
|
pub fn router(&self) -> Option<&Router<S>> {
|
||||||
self.2.as_ref()
|
self.2.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Peer socket address
|
||||||
|
///
|
||||||
|
/// Peer address is actuall socket address, if proxy is used in front of
|
||||||
|
/// actix http server, then peer address would be address of this proxy.
|
||||||
|
///
|
||||||
|
/// To get client connection information `connection_info()` method should be used.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn peer_addr(&self) -> Option<&SocketAddr> {
|
pub fn peer_addr(&self) -> Option<&SocketAddr> {
|
||||||
self.as_ref().addr.as_ref()
|
self.as_ref().addr.as_ref()
|
||||||
@ -268,12 +297,10 @@ impl<S> HttpRequest<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
||||||
pub fn query(&self) -> HashMap<String, String> {
|
pub fn query(&self) -> HashMap<Cow<str>, Cow<str>> {
|
||||||
let mut q: HashMap<String, String> = HashMap::new();
|
let mut q = HashMap::new();
|
||||||
if let Some(query) = self.as_ref().uri.query().as_ref() {
|
for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) {
|
||||||
for (key, val) in form_urlencoded::parse(query.as_ref()) {
|
q.insert(key, val);
|
||||||
q.insert(key.to_string(), val.to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
q
|
q
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user