@@ -9,10 +9,8 @@ extern crate filetime;
9
9
extern crate tempdir;
10
10
extern crate time;
11
11
12
- use conduit:: { Handler , Request , Response } ;
12
+ use conduit:: { box_error , header , Body , Handler , HandlerResult , RequestExt , Response , StatusCode } ;
13
13
use filetime:: FileTime ;
14
- use std:: collections:: HashMap ;
15
- use std:: error:: Error ;
16
14
use std:: fs:: File ;
17
15
use std:: io;
18
16
use std:: path:: { Path , PathBuf } ;
@@ -33,7 +31,7 @@ impl Static {
33
31
34
32
impl Handler for Static {
35
33
#[ allow( deprecated) ]
36
- fn call ( & self , request : & mut dyn Request ) -> Result < Response , Box < dyn Error + Send > > {
34
+ fn call ( & self , request : & mut dyn RequestExt ) -> HandlerResult {
37
35
let request_path = & request. path ( ) [ 1 ..] ;
38
36
if request_path. contains ( ".." ) {
39
37
return Ok ( not_found ( ) ) ;
@@ -45,9 +43,7 @@ impl Handler for Static {
45
43
Ok ( f) => f,
46
44
Err ( ..) => return Ok ( not_found ( ) ) ,
47
45
} ;
48
- let data = file
49
- . metadata ( )
50
- . map_err ( |e| Box :: new ( e) as Box < dyn Error + Send > ) ?;
46
+ let data = file. metadata ( ) . map_err ( box_error) ?;
51
47
if data. is_dir ( ) {
52
48
return Ok ( not_found ( ) ) ;
53
49
}
@@ -58,31 +54,25 @@ impl Handler for Static {
58
54
} ;
59
55
let tm = time:: at ( ts) . to_utc ( ) ;
60
56
61
- let mut headers = HashMap :: new ( ) ;
62
- headers. insert ( "Content-Type" . to_string ( ) , vec ! [ mime. to_string( ) ] ) ;
63
- headers. insert ( "Content-Length" . to_string ( ) , vec ! [ data. len( ) . to_string( ) ] ) ;
64
- headers. insert (
65
- "Last-Modified" . to_string ( ) ,
66
- vec ! [ tm. strftime( "%a, %d %b %Y %T GMT" ) . unwrap( ) . to_string( ) ] ,
67
- ) ;
68
-
69
- Ok ( Response {
70
- status : ( 200 , "OK" ) ,
71
- headers,
72
- body : Box :: new ( file) ,
73
- } )
57
+ Response :: builder ( )
58
+ . header ( header:: CONTENT_TYPE , mime)
59
+ . header ( header:: CONTENT_LENGTH , data. len ( ) )
60
+ . header (
61
+ header:: LAST_MODIFIED ,
62
+ tm. strftime ( "%a, %d %b %Y %T GMT" ) . unwrap ( ) . to_string ( ) ,
63
+ )
64
+ . body ( Box :: new ( file) as Body )
65
+ . map_err ( box_error)
74
66
}
75
67
}
76
68
77
- fn not_found ( ) -> Response {
78
- let mut headers = HashMap :: new ( ) ;
79
- headers. insert ( "Content-Length" . to_string ( ) , vec ! [ "0" . to_string( ) ] ) ;
80
- headers. insert ( "Content-Type" . to_string ( ) , vec ! [ "text/plain" . to_string( ) ] ) ;
81
- Response {
82
- status : ( 404 , "Not Found" ) ,
83
- headers,
84
- body : Box :: new ( io:: empty ( ) ) ,
85
- }
69
+ fn not_found ( ) -> Response < Body > {
70
+ Response :: builder ( )
71
+ . status ( StatusCode :: NOT_FOUND )
72
+ . header ( header:: CONTENT_LENGTH , 0 )
73
+ . header ( header:: CONTENT_TYPE , "text/plain" )
74
+ . body ( Box :: new ( io:: empty ( ) ) as Body )
75
+ . unwrap ( )
86
76
}
87
77
88
78
#[ cfg( test) ]
@@ -93,7 +83,7 @@ mod tests {
93
83
use std:: io:: prelude:: * ;
94
84
use tempdir:: TempDir ;
95
85
96
- use conduit:: { Handler , Method } ;
86
+ use conduit:: { header , Handler , Method , StatusCode } ;
97
87
use Static ;
98
88
99
89
#[ test]
@@ -105,19 +95,16 @@ mod tests {
105
95
. unwrap ( )
106
96
. write_all ( b"[package]" )
107
97
. unwrap ( ) ;
108
- let mut req = test:: MockRequest :: new ( Method :: Get , "/Cargo.toml" ) ;
98
+ let mut req = test:: MockRequest :: new ( Method :: GET , "/Cargo.toml" ) ;
109
99
let mut res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
110
100
let mut body = Vec :: new ( ) ;
111
- res. body . write_body ( & mut body) . unwrap ( ) ;
101
+ res. body_mut ( ) . write_body ( & mut body) . unwrap ( ) ;
112
102
assert_eq ! ( body, b"[package]" ) ;
113
103
assert_eq ! (
114
- res. headers. get( "Content-Type" ) ,
115
- Some ( & vec!( "text/plain" . to_string( ) ) )
116
- ) ;
117
- assert_eq ! (
118
- res. headers. get( "Content-Length" ) ,
119
- Some ( & vec![ "9" . to_string( ) ] )
104
+ res. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
105
+ "text/plain"
120
106
) ;
107
+ assert_eq ! ( res. headers( ) . get( header:: CONTENT_LENGTH ) . unwrap( ) , "9" ) ;
121
108
}
122
109
123
110
#[ test]
@@ -128,16 +115,10 @@ mod tests {
128
115
File :: create ( & root. join ( "src/fixture.css" ) ) . unwrap ( ) ;
129
116
130
117
let handler = Static :: new ( root. clone ( ) ) ;
131
- let mut req = test:: MockRequest :: new ( Method :: Get , "/src/fixture.css" ) ;
118
+ let mut req = test:: MockRequest :: new ( Method :: GET , "/src/fixture.css" ) ;
132
119
let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
133
- assert_eq ! (
134
- res. headers. get( "Content-Type" ) ,
135
- Some ( & vec!( "text/css" . to_string( ) ) )
136
- ) ;
137
- assert_eq ! (
138
- res. headers. get( "Content-Length" ) ,
139
- Some ( & vec![ "0" . to_string( ) ] )
140
- ) ;
120
+ assert_eq ! ( res. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) , "text/css" ) ;
121
+ assert_eq ! ( res. headers( ) . get( header:: CONTENT_LENGTH ) . unwrap( ) , "0" ) ;
141
122
}
142
123
143
124
#[ test]
@@ -146,9 +127,9 @@ mod tests {
146
127
let root = td. path ( ) ;
147
128
148
129
let handler = Static :: new ( root. clone ( ) ) ;
149
- let mut req = test:: MockRequest :: new ( Method :: Get , "/nope" ) ;
130
+ let mut req = test:: MockRequest :: new ( Method :: GET , "/nope" ) ;
150
131
let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
151
- assert_eq ! ( res. status. 0 , 404 ) ;
132
+ assert_eq ! ( res. status( ) , StatusCode :: NOT_FOUND ) ;
152
133
}
153
134
154
135
#[ test]
@@ -159,9 +140,9 @@ mod tests {
159
140
fs:: create_dir ( & root. join ( "foo" ) ) . unwrap ( ) ;
160
141
161
142
let handler = Static :: new ( root. clone ( ) ) ;
162
- let mut req = test:: MockRequest :: new ( Method :: Get , "/foo" ) ;
143
+ let mut req = test:: MockRequest :: new ( Method :: GET , "/foo" ) ;
163
144
let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
164
- assert_eq ! ( res. status. 0 , 404 ) ;
145
+ assert_eq ! ( res. status( ) , StatusCode :: NOT_FOUND ) ;
165
146
}
166
147
167
148
#[ test]
@@ -170,9 +151,9 @@ mod tests {
170
151
let root = td. path ( ) ;
171
152
File :: create ( & root. join ( "test" ) ) . unwrap ( ) ;
172
153
let handler = Static :: new ( root. clone ( ) ) ;
173
- let mut req = test:: MockRequest :: new ( Method :: Get , "/test" ) ;
154
+ let mut req = test:: MockRequest :: new ( Method :: GET , "/test" ) ;
174
155
let res = handler. call ( & mut req) . ok ( ) . expect ( "No response" ) ;
175
- assert_eq ! ( res. status. 0 , 200 ) ;
176
- assert ! ( res. headers. get( "Last-Modified" ) . is_some( ) ) ;
156
+ assert_eq ! ( res. status( ) , StatusCode :: OK ) ;
157
+ assert ! ( res. headers( ) . get( header :: LAST_MODIFIED ) . is_some( ) ) ;
177
158
}
178
159
}
0 commit comments