mirror of https://github.com/fafhrd91/actix-web
use existing Method guard instead of creating a new Custom gueard
This commit is contained in:
parent
7115c29a55
commit
28d928a660
|
@ -29,7 +29,7 @@ macro_rules! method_type {
|
||||||
$(stringify!($upper) => Ok(Self::$variant),)+
|
$(stringify!($upper) => Ok(Self::$variant),)+
|
||||||
_ => {
|
_ => {
|
||||||
if method.chars().all(|c| c.is_ascii_uppercase()) {
|
if method.chars().all(|c| c.is_ascii_uppercase()) {
|
||||||
Ok(Self::Custom)
|
Ok(Self::Method)
|
||||||
} else {
|
} else {
|
||||||
Err(format!("HTTP method must be uppercase: `{}`", method))
|
Err(format!("HTTP method must be uppercase: `{}`", method))
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ method_type! {
|
||||||
Options, OPTIONS, options,
|
Options, OPTIONS, options,
|
||||||
Trace, TRACE, trace,
|
Trace, TRACE, trace,
|
||||||
Patch, PATCH, patch,
|
Patch, PATCH, patch,
|
||||||
Custom, CUSTOM, custom,
|
Method, METHOD, method,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToTokens for MethodType {
|
impl ToTokens for MethodType {
|
||||||
|
@ -76,7 +76,7 @@ impl ToTokens for MethodType {
|
||||||
impl ToTokens for MethodTypeExt {
|
impl ToTokens for MethodTypeExt {
|
||||||
fn to_tokens(&self, stream: &mut TokenStream2) {
|
fn to_tokens(&self, stream: &mut TokenStream2) {
|
||||||
match self.method {
|
match self.method {
|
||||||
MethodType::Custom => {
|
MethodType::Method => {
|
||||||
let ident = Ident::new(
|
let ident = Ident::new(
|
||||||
self.custom_method.as_ref().unwrap().value().as_str(),
|
self.custom_method.as_ref().unwrap().value().as_str(),
|
||||||
Span::call_site(),
|
Span::call_site(),
|
||||||
|
@ -186,7 +186,7 @@ impl Args {
|
||||||
} else if let syn::Lit::Str(ref lit) = nv.lit {
|
} else if let syn::Lit::Str(ref lit) = nv.lit {
|
||||||
let method = MethodType::try_from(lit)?;
|
let method = MethodType::try_from(lit)?;
|
||||||
if !methods.insert({
|
if !methods.insert({
|
||||||
if method == MethodType::Custom {
|
if method == MethodType::Method {
|
||||||
MethodTypeExt {
|
MethodTypeExt {
|
||||||
method,
|
method,
|
||||||
custom_method: Some(lit.clone()),
|
custom_method: Some(lit.clone()),
|
||||||
|
@ -355,7 +355,7 @@ impl ToTokens for Route {
|
||||||
match custom_method {
|
match custom_method {
|
||||||
Some(lit) => {
|
Some(lit) => {
|
||||||
mult_method_guards.push(quote! {
|
mult_method_guards.push(quote! {
|
||||||
.or(::actix_web::guard::#method_type(#lit.clone()))
|
.or(::actix_web::guard::#method_type(::actix_web::http::Method::from_bytes(#lit.as_bytes()).unwrap()))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
@ -369,7 +369,7 @@ impl ToTokens for Route {
|
||||||
Some(lit) => {
|
Some(lit) => {
|
||||||
quote! {
|
quote! {
|
||||||
.guard(
|
.guard(
|
||||||
::actix_web::guard::Any(::actix_web::guard::#first_method(#lit.clone()))
|
::actix_web::guard::Any(::actix_web::guard::#first_method(::actix_web::http::Method::from_bytes(#lit.as_bytes()).unwrap()))
|
||||||
#(#mult_method_guards)*
|
#(#mult_method_guards)*
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -387,7 +387,7 @@ impl ToTokens for Route {
|
||||||
match &first.custom_method {
|
match &first.custom_method {
|
||||||
Some(lit) => {
|
Some(lit) => {
|
||||||
quote! {
|
quote! {
|
||||||
.guard(::actix_web::guard::#first_method(#lit.clone()))
|
.guard(::actix_web::guard::#first_method(::actix_web::http::Method::from_bytes(#lit.as_bytes()).unwrap()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
|
|
@ -91,7 +91,7 @@ async fn get_param_test(_: web::Path<String>) -> impl Responder {
|
||||||
method = "GET",
|
method = "GET",
|
||||||
method = "POST",
|
method = "POST",
|
||||||
method = "HEAD",
|
method = "HEAD",
|
||||||
method = "CREATE"
|
method = "HELLO"
|
||||||
)]
|
)]
|
||||||
async fn route_test() -> impl Responder {
|
async fn route_test() -> impl Responder {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## Unreleased - 2022-xx-xx
|
## Unreleased - 2022-xx-xx
|
||||||
### Added
|
|
||||||
- Add `guard::Custom()` for handling `#[route]` macro with custom Methods. [#2969]
|
|
||||||
|
|
||||||
[#2969]: https://github.com/actix/actix-web/pull/2969
|
|
||||||
|
|
||||||
## 4.3.0 - 2023-01-21
|
## 4.3.0 - 2023-01-21
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -342,21 +342,6 @@ method_guard!(Connect, CONNECT);
|
||||||
method_guard!(Patch, PATCH);
|
method_guard!(Patch, PATCH);
|
||||||
method_guard!(Trace, TRACE);
|
method_guard!(Trace, TRACE);
|
||||||
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
#[doc = "The route in this example will respond to all uppercase ASCII requests."]
|
|
||||||
/// ```
|
|
||||||
/// use actix_web::{guard, web, HttpResponse};
|
|
||||||
///
|
|
||||||
/// web::route()
|
|
||||||
#[doc = " .guard(guard::Custom(\"HELLO\"))"]
|
|
||||||
/// .to(|| HttpResponse::Ok());
|
|
||||||
/// ```
|
|
||||||
#[allow(non_snake_case)]
|
|
||||||
pub fn Custom(custom_method: &str) -> impl Guard {
|
|
||||||
MethodGuard(HttpMethod::from_bytes(custom_method.as_bytes()).unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a guard that matches if request contains given header name and value.
|
/// Creates a guard that matches if request contains given header name and value.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
|
Loading…
Reference in New Issue