@@ -8,7 +8,7 @@ extern crate time;
8
8
use conduit:: { header, Body , HeaderMap , Method , RequestExt , Response , StatusCode } ;
9
9
use conduit_middleware:: { AfterResult , Middleware } ;
10
10
use std:: borrow:: Cow ;
11
- use time:: { ParseError , Tm } ;
11
+ use time:: { OffsetDateTime , ParseError , PrimitiveDateTime } ;
12
12
13
13
#[ allow( missing_copy_implementations) ]
14
14
pub struct ConditionalGet ;
@@ -66,14 +66,14 @@ fn etag_matches(none_match: &[u8], res: &Response<Body>) -> bool {
66
66
value == none_match
67
67
}
68
68
69
- fn is_modified_since ( modified_since : Tm , res : & Response < Body > ) -> bool {
69
+ fn is_modified_since ( modified_since : OffsetDateTime , res : & Response < Body > ) -> bool {
70
70
let last_modified = get_and_concat_header ( res. headers ( ) , header:: LAST_MODIFIED ) ;
71
71
72
72
match std:: str:: from_utf8 ( & last_modified) {
73
73
Err ( _) => false ,
74
74
Ok ( last_modified) => match parse_http_date ( last_modified) {
75
75
Err ( _) => false ,
76
- Ok ( last_modified) => modified_since. to_timespec ( ) >= last_modified. to_timespec ( ) ,
76
+ Ok ( last_modified) => modified_since. timestamp ( ) >= last_modified. timestamp ( ) ,
77
77
} ,
78
78
}
79
79
}
@@ -90,23 +90,24 @@ fn get_and_concat_header(headers: &HeaderMap, name: header::HeaderName) -> Cow<'
90
90
}
91
91
}
92
92
93
- fn parse_http_date ( string : & str ) -> Result < Tm , ( ) > {
93
+ fn parse_http_date ( string : & str ) -> Result < OffsetDateTime , ( ) > {
94
94
parse_rfc1123 ( string)
95
95
. or_else ( |_| parse_rfc850 ( string) )
96
96
. or_else ( |_| parse_asctime ( string) )
97
97
. map_err ( |_| ( ) )
98
98
}
99
99
100
- fn parse_rfc1123 ( string : & str ) -> Result < Tm , ParseError > {
101
- time :: strptime ( string, "%a, %d %b %Y %T GMT" )
100
+ fn parse_rfc1123 ( string : & str ) -> Result < OffsetDateTime , ParseError > {
101
+ Ok ( PrimitiveDateTime :: parse ( string, "%a, %d %b %Y %T GMT" ) ? . assume_utc ( ) )
102
102
}
103
103
104
- fn parse_rfc850 ( string : & str ) -> Result < Tm , ParseError > {
105
- time :: strptime ( string, "%a, %d-%m-%y %T GMT" )
104
+ fn parse_rfc850 ( string : & str ) -> Result < OffsetDateTime , ParseError > {
105
+ Ok ( PrimitiveDateTime :: parse ( string, "%a, %d-%m-%y %T GMT" ) ? . assume_utc ( ) )
106
106
}
107
107
108
- fn parse_asctime ( string : & str ) -> Result < Tm , ParseError > {
109
- time:: strptime ( string, "%a %m%t%d %T %Y" )
108
+ fn parse_asctime ( string : & str ) -> Result < OffsetDateTime , ParseError > {
109
+ // TODO: should this be "%a %b %d %T %Y"?
110
+ Ok ( PrimitiveDateTime :: parse ( string, "%a %m\t %d %T %Y" ) ?. assume_utc ( ) )
110
111
}
111
112
112
113
#[ cfg( test) ]
@@ -119,8 +120,7 @@ mod tests {
119
120
StatusCode ,
120
121
} ;
121
122
use conduit_middleware:: MiddlewareBuilder ;
122
- use time;
123
- use time:: Tm ;
123
+ use time:: { Duration , OffsetDateTime } ;
124
124
125
125
use super :: ConditionalGet ;
126
126
@@ -149,17 +149,17 @@ mod tests {
149
149
150
150
#[ test]
151
151
fn test_sends_304 ( ) {
152
- let handler = returning ! ( header:: LAST_MODIFIED => httpdate( time :: now ( ) ) ) ;
152
+ let handler = returning ! ( header:: LAST_MODIFIED => httpdate( OffsetDateTime :: now_utc ( ) ) ) ;
153
153
expect_304 ( handler. call ( & mut request ! (
154
- header:: IF_MODIFIED_SINCE => httpdate( time :: now ( ) )
154
+ header:: IF_MODIFIED_SINCE => httpdate( OffsetDateTime :: now_utc ( ) )
155
155
) ) ) ;
156
156
}
157
157
158
158
#[ test]
159
159
fn test_sends_304_if_older_than_now ( ) {
160
160
let handler = returning ! ( header:: LAST_MODIFIED => before_now( ) ) ;
161
161
expect_304 ( handler. call ( & mut request ! (
162
- header:: IF_MODIFIED_SINCE => httpdate( time :: now ( ) )
162
+ header:: IF_MODIFIED_SINCE => httpdate( OffsetDateTime :: now_utc ( ) )
163
163
) ) ) ;
164
164
}
165
165
@@ -228,10 +228,7 @@ mod tests {
228
228
229
229
#[ test]
230
230
fn test_does_not_affect_malformed_timestamp ( ) {
231
- let bad_stamp = time:: now ( )
232
- . strftime ( "%Y-%m-%d %H:%M:%S %z" )
233
- . unwrap ( )
234
- . to_string ( ) ;
231
+ let bad_stamp = OffsetDateTime :: now_utc ( ) . format ( "%Y-%m-%d %H:%M:%S %z" ) ;
235
232
let handler = returning ! ( header:: LAST_MODIFIED => before_now( ) ) ;
236
233
expect_200 ( handler. call ( & mut request ! (
237
234
header:: IF_MODIFIED_SINCE => bad_stamp
@@ -281,16 +278,15 @@ mod tests {
281
278
}
282
279
283
280
fn before_now ( ) -> String {
284
- let mut now = time:: now ( ) ;
285
- now. tm_year -= 1 ;
286
- httpdate ( now)
281
+ let now = OffsetDateTime :: now_utc ( ) ;
282
+ httpdate ( now - Duration :: weeks ( 52 ) )
287
283
}
288
284
289
285
fn now ( ) -> String {
290
- httpdate ( time :: now ( ) )
286
+ httpdate ( OffsetDateTime :: now_utc ( ) )
291
287
}
292
288
293
- fn httpdate ( time : Tm ) -> String {
294
- time. strftime ( "%a, %d-%m-%y %T GMT" ) . unwrap ( ) . to_string ( )
289
+ fn httpdate ( time : OffsetDateTime ) -> String {
290
+ time. format ( "%a, %d-%m-%y %T GMT" )
295
291
}
296
292
}
0 commit comments