1
1
use crate :: app:: AppState ;
2
2
use crate :: middleware:: log_request:: RequestLogExt ;
3
3
use crate :: middleware:: real_ip:: RealIp ;
4
- use crate :: util:: errors:: custom;
4
+ use crate :: util:: errors:: { BoxedAppError , custom} ;
5
5
use axum:: extract:: { Extension , MatchedPath , Request } ;
6
6
use axum:: middleware:: Next ;
7
7
use axum:: response:: { IntoResponse , Response } ;
@@ -14,9 +14,9 @@ pub async fn middleware(
14
14
req : Request ,
15
15
next : Next ,
16
16
) -> Result < impl IntoResponse , Response > {
17
- block_by_ip ( & real_ip, & state, req. headers ( ) ) ?;
18
- block_by_header ( & state, & req) ?;
19
- block_routes ( matched_path. as_ref ( ) , & state) ?;
17
+ block_by_ip ( & real_ip, & state, req. headers ( ) ) . map_err ( IntoResponse :: into_response ) ?;
18
+ block_by_header ( & state, & req) . map_err ( IntoResponse :: into_response ) ?;
19
+ block_routes ( matched_path. as_ref ( ) , & state) . map_err ( IntoResponse :: into_response ) ?;
20
20
21
21
Ok ( next. run ( req) . await )
22
22
}
@@ -29,7 +29,7 @@ pub async fn middleware(
29
29
/// to `User-Agent=BLOCKED_UAS` and `BLOCKED_UAS` to `curl/7.54.0,cargo 1.36.0 (c4fcfb725 2019-05-15)`
30
30
/// to block requests from the versions of curl or Cargo specified (values are nonsensical examples).
31
31
/// Values of the headers must match exactly.
32
- pub fn block_by_header ( state : & AppState , req : & Request ) -> Result < ( ) , Response > {
32
+ pub fn block_by_header ( state : & AppState , req : & Request ) -> Result < ( ) , impl IntoResponse > {
33
33
let blocked_traffic = & state. config . blocked_traffic ;
34
34
35
35
for ( header_name, blocked_values) in blocked_traffic {
@@ -53,15 +53,15 @@ pub fn block_by_ip(
53
53
real_ip : & RealIp ,
54
54
state : & AppState ,
55
55
headers : & HeaderMap ,
56
- ) -> Result < ( ) , Response > {
56
+ ) -> Result < ( ) , impl IntoResponse > {
57
57
if state. config . blocked_ips . contains ( real_ip) {
58
58
return Err ( rejection_response_from ( state, headers) ) ;
59
59
}
60
60
61
61
Ok ( ( ) )
62
62
}
63
63
64
- fn rejection_response_from ( state : & AppState , headers : & HeaderMap ) -> Response {
64
+ fn rejection_response_from ( state : & AppState , headers : & HeaderMap ) -> impl IntoResponse {
65
65
let domain_name = & state. config . domain_name ;
66
66
67
67
// Heroku should always set this header
@@ -77,17 +77,19 @@ fn rejection_response_from(state: &AppState, headers: &HeaderMap) -> Response {
77
77
Please email help@crates.io and provide the request id {request_id}"
78
78
) ;
79
79
80
- ( StatusCode :: FORBIDDEN , body) . into_response ( )
80
+ ( StatusCode :: FORBIDDEN , body)
81
81
}
82
82
83
83
/// Allow blocking individual routes by their pattern through the `BLOCKED_ROUTES`
84
84
/// environment variable.
85
- pub fn block_routes ( matched_path : Option < & MatchedPath > , state : & AppState ) -> Result < ( ) , Response > {
85
+ pub fn block_routes (
86
+ matched_path : Option < & MatchedPath > ,
87
+ state : & AppState ,
88
+ ) -> Result < ( ) , BoxedAppError > {
86
89
if let Some ( matched_path) = matched_path {
87
90
if state. config . blocked_routes . contains ( matched_path. as_str ( ) ) {
88
91
let body = "This route is temporarily blocked. See https://status.crates.io." ;
89
- let error = custom ( StatusCode :: SERVICE_UNAVAILABLE , body) ;
90
- return Err ( error. into_response ( ) ) ;
92
+ return Err ( custom ( StatusCode :: SERVICE_UNAVAILABLE , body) ) ;
91
93
}
92
94
}
93
95
0 commit comments