@@ -2,7 +2,7 @@ use std::fmt::{self, Display};
22
33use actix_web:: {
44 http:: {
5- header:: { ContentDisposition , DispositionParam , DispositionType } ,
5+ header:: { self , ContentDisposition , DispositionParam , DispositionType } ,
66 StatusCode ,
77 } ,
88 HttpResponse , HttpResponseBuilder ,
@@ -51,7 +51,7 @@ pub struct Response<T> {
5151 pub code : i64 ,
5252 pub message : String ,
5353 pub data : Option < T > ,
54- pub builder : Option < ResponseBuilderFn > ,
54+ pub builder : Vec < ResponseBuilderFn > ,
5555 #[ cfg( feature = "i18n" ) ]
5656 pub translate : bool ,
5757}
@@ -66,7 +66,7 @@ impl<T> Response<T> {
6666 code : r. code ( ) ,
6767 message : r. message ( ) . to_owned ( ) ,
6868 data : None ,
69- builder : None ,
69+ builder : Vec :: new ( ) ,
7070 #[ cfg( feature = "i18n" ) ]
7171 translate : true ,
7272 }
@@ -78,7 +78,7 @@ impl<T> Response<T> {
7878 code : 0 ,
7979 message : String :: new ( ) ,
8080 data : None ,
81- builder : None ,
81+ builder : Vec :: new ( ) ,
8282 #[ cfg( feature = "i18n" ) ]
8383 translate : false ,
8484 }
@@ -88,15 +88,26 @@ impl<T> Response<T> {
8888 Self :: new_code ( 400 ) . message ( s)
8989 }
9090
91+ pub fn forbidden ( ) -> Self {
92+ Self :: new_code ( 403 )
93+ }
94+
9195 pub fn not_found ( ) -> Self {
9296 Self :: new_code ( 404 )
9397 }
9498
99+ pub fn redirect < S : Into < String > > ( code : u16 , s : S ) -> Self {
100+ let s: String = s. into ( ) ;
101+ Self :: new_code ( code) . builder ( move |r| {
102+ r. insert_header ( ( header:: LOCATION , s. clone ( ) ) ) ;
103+ } )
104+ }
105+
95106 pub fn builder < F > ( mut self , f : F ) -> Self
96107 where
97108 F : Fn ( & mut HttpResponseBuilder ) + ' static ,
98109 {
99- self . builder = Some ( Box :: new ( f) ) ;
110+ self . builder . push ( Box :: new ( f) ) ;
100111 self
101112 }
102113
@@ -190,15 +201,17 @@ impl actix_web::Responder for JsonResponse {
190201 let mut rsp =
191202 HttpResponse :: build ( actix_web:: http:: StatusCode :: from_u16 ( self . http_code ) . unwrap ( ) ) ;
192203 rsp. content_type ( actix_web:: http:: header:: ContentType :: json ( ) ) ;
193- if let Some ( builder) = self . builder {
204+ for builder in self . builder {
194205 builder ( & mut rsp) ;
195206 }
196207 rsp. message_body ( body) . unwrap ( ) . map_into_left_body ( )
197208 } else {
198- HttpResponse :: build ( actix_web:: http:: StatusCode :: from_u16 ( self . http_code ) . unwrap ( ) )
199- . message_body ( self . message )
200- . unwrap ( )
201- . map_into_left_body ( )
209+ let mut rsp =
210+ HttpResponse :: build ( actix_web:: http:: StatusCode :: from_u16 ( self . http_code ) . unwrap ( ) ) ;
211+ for builder in self . builder {
212+ builder ( & mut rsp) ;
213+ }
214+ rsp. message_body ( self . message ) . unwrap ( ) . map_into_left_body ( )
202215 }
203216 }
204217}
0 commit comments