mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
undeprecate query() and store query in extensions
This commit is contained in:
parent
953a0d4e4a
commit
ef89430f9b
@ -32,7 +32,7 @@
|
|||||||
https://actix.rs/actix-web/actix_web/trait.Responder.html#tymethod.respond_to)
|
https://actix.rs/actix-web/actix_web/trait.Responder.html#tymethod.respond_to)
|
||||||
is generic over `S`
|
is generic over `S`
|
||||||
|
|
||||||
* `HttpRequest::query()` is deprecated. Use `Query` extractor.
|
* Use `Query` extractor instead of HttpRequest::query()`.
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn index(q: Query<HashMap<String, String>>) -> Result<..> {
|
fn index(q: Query<HashMap<String, String>>) -> Result<..> {
|
||||||
|
@ -27,7 +27,6 @@ use uri::Url as InnerUrl;
|
|||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
pub(crate) struct MessageFlags: u8 {
|
pub(crate) struct MessageFlags: u8 {
|
||||||
const QUERY = 0b0000_0001;
|
|
||||||
const KEEPALIVE = 0b0000_0010;
|
const KEEPALIVE = 0b0000_0010;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,7 +40,6 @@ pub struct HttpInnerMessage {
|
|||||||
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 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>>,
|
||||||
@ -49,6 +47,8 @@ pub struct HttpInnerMessage {
|
|||||||
resource: RouterResource,
|
resource: RouterResource,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Query(Params<'static>);
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
enum RouterResource {
|
enum RouterResource {
|
||||||
Notset,
|
Notset,
|
||||||
@ -64,7 +64,6 @@ impl Default for HttpInnerMessage {
|
|||||||
headers: HeaderMap::with_capacity(16),
|
headers: HeaderMap::with_capacity(16),
|
||||||
flags: MessageFlags::empty(),
|
flags: MessageFlags::empty(),
|
||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
query: Params::new(),
|
|
||||||
addr: None,
|
addr: None,
|
||||||
cookies: None,
|
cookies: None,
|
||||||
payload: None,
|
payload: None,
|
||||||
@ -109,7 +108,10 @@ impl HttpRequest<()> {
|
|||||||
/// Construct a new Request.
|
/// Construct a new Request.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
method: Method, uri: Uri, version: Version, headers: HeaderMap,
|
method: Method,
|
||||||
|
uri: Uri,
|
||||||
|
version: Version,
|
||||||
|
headers: HeaderMap,
|
||||||
payload: Option<Payload>,
|
payload: Option<Payload>,
|
||||||
) -> HttpRequest {
|
) -> HttpRequest {
|
||||||
let url = InnerUrl::new(uri);
|
let url = InnerUrl::new(uri);
|
||||||
@ -121,7 +123,6 @@ impl HttpRequest<()> {
|
|||||||
headers,
|
headers,
|
||||||
payload,
|
payload,
|
||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
query: Params::new(),
|
|
||||||
extensions: Extensions::new(),
|
extensions: Extensions::new(),
|
||||||
cookies: None,
|
cookies: None,
|
||||||
addr: None,
|
addr: None,
|
||||||
@ -306,7 +307,9 @@ impl<S> HttpRequest<S> {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn url_for<U, I>(
|
pub fn url_for<U, I>(
|
||||||
&self, name: &str, elements: U,
|
&self,
|
||||||
|
name: &str,
|
||||||
|
elements: U,
|
||||||
) -> Result<Url, UrlGenerationError>
|
) -> Result<Url, UrlGenerationError>
|
||||||
where
|
where
|
||||||
U: IntoIterator<Item = I>,
|
U: IntoIterator<Item = I>,
|
||||||
@ -369,20 +372,20 @@ impl<S> HttpRequest<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[deprecated(since = "0.6.0", note = "please use `Query<T>` extractor")]
|
|
||||||
/// Get a reference to the Params object.
|
/// Get a reference to the Params object.
|
||||||
/// Params is a container for url query parameters.
|
/// Params is a container for url query parameters.
|
||||||
pub fn query(&self) -> &Params {
|
pub fn query<'a>(&'a self) -> &'a Params {
|
||||||
if !self.as_ref().flags.contains(MessageFlags::QUERY) {
|
if let None = self.extensions().get::<Query>() {
|
||||||
self.as_mut().flags.insert(MessageFlags::QUERY);
|
let mut params: Params<'a> = Params::new();
|
||||||
let params: &mut Params =
|
|
||||||
unsafe { mem::transmute(&mut self.as_mut().query) };
|
|
||||||
params.clear();
|
|
||||||
for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) {
|
for (key, val) in form_urlencoded::parse(self.query_string().as_ref()) {
|
||||||
params.add(key, val);
|
params.add(key, val);
|
||||||
}
|
}
|
||||||
|
let params: Params<'static> = unsafe { mem::transmute(params) };
|
||||||
|
self.as_mut().extensions.insert(Query(params));
|
||||||
}
|
}
|
||||||
unsafe { mem::transmute(&self.as_ref().query) }
|
let params: &Params<'a> =
|
||||||
|
unsafe { mem::transmute(&self.extensions().get::<Query>().unwrap().0) };
|
||||||
|
params
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The query string in the URL.
|
/// The query string in the URL.
|
||||||
@ -664,10 +667,8 @@ mod tests {
|
|||||||
|
|
||||||
let mut resource = ResourceHandler::<()>::default();
|
let mut resource = ResourceHandler::<()>::default();
|
||||||
resource.name("index");
|
resource.name("index");
|
||||||
let routes = vec![(
|
let routes =
|
||||||
Resource::new("index", "/user/{name}.{ext}"),
|
vec![(Resource::new("index", "/user/{name}.{ext}"), Some(resource))];
|
||||||
Some(resource),
|
|
||||||
)];
|
|
||||||
let (router, _) = Router::new("/", ServerSettings::default(), routes);
|
let (router, _) = Router::new("/", ServerSettings::default(), routes);
|
||||||
assert!(router.has_route("/user/test.html"));
|
assert!(router.has_route("/user/test.html"));
|
||||||
assert!(!router.has_route("/test/unknown"));
|
assert!(!router.has_route("/test/unknown"));
|
||||||
@ -696,10 +697,8 @@ mod tests {
|
|||||||
|
|
||||||
let mut resource = ResourceHandler::<()>::default();
|
let mut resource = ResourceHandler::<()>::default();
|
||||||
resource.name("index");
|
resource.name("index");
|
||||||
let routes = vec![(
|
let routes =
|
||||||
Resource::new("index", "/user/{name}.{ext}"),
|
vec![(Resource::new("index", "/user/{name}.{ext}"), Some(resource))];
|
||||||
Some(resource),
|
|
||||||
)];
|
|
||||||
let (router, _) = Router::new("/prefix/", ServerSettings::default(), routes);
|
let (router, _) = Router::new("/prefix/", ServerSettings::default(), routes);
|
||||||
assert!(router.has_route("/user/test.html"));
|
assert!(router.has_route("/user/test.html"));
|
||||||
assert!(!router.has_route("/prefix/user/test.html"));
|
assert!(!router.has_route("/prefix/user/test.html"));
|
||||||
|
Loading…
Reference in New Issue
Block a user