Skip to content

Commit d8260cb

Browse files
authored
Merge pull request #8 from jtgeibel/use-http-types
Bump to latest conduit alpha and use `http` types
2 parents 3f037f8 + 3cefd7e commit d8260cb

File tree

2 files changed

+39
-58
lines changed

2 files changed

+39
-58
lines changed

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
[package]
22

33
name = "conduit-static"
4-
version = "0.9.0-alpha.0"
4+
version = "0.9.0-alpha.1"
55
authors = ["[email protected]",
66
"Alex Crichton <[email protected]>"]
77
description = "Middleware for serving static files for conduit"
88
repository = "https://github.com/conduit-rust/conduit-static"
99
license = "MIT"
1010

1111
[dependencies]
12-
conduit = "0.9.0-alpha.0"
12+
conduit = "0.9.0-alpha.1"
1313
conduit-mime-types = "0.7"
1414
time = "0.1"
1515
filetime = "0.2"
1616

1717
[dev-dependencies]
18-
civet = "0.12.0-alpha.0"
19-
conduit-test = "0.9.0-alpha.0"
18+
civet = "0.12.0-alpha.2"
19+
conduit-test = "0.9.0-alpha.1"
2020
tempdir = "0.3"

src/lib.rs

+35-54
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ extern crate filetime;
99
extern crate tempdir;
1010
extern crate time;
1111

12-
use conduit::{Handler, Request, Response};
12+
use conduit::{box_error, header, Body, Handler, HandlerResult, RequestExt, Response, StatusCode};
1313
use filetime::FileTime;
14-
use std::collections::HashMap;
15-
use std::error::Error;
1614
use std::fs::File;
1715
use std::io;
1816
use std::path::{Path, PathBuf};
@@ -33,7 +31,7 @@ impl Static {
3331

3432
impl Handler for Static {
3533
#[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 {
3735
let request_path = &request.path()[1..];
3836
if request_path.contains("..") {
3937
return Ok(not_found());
@@ -45,9 +43,7 @@ impl Handler for Static {
4543
Ok(f) => f,
4644
Err(..) => return Ok(not_found()),
4745
};
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)?;
5147
if data.is_dir() {
5248
return Ok(not_found());
5349
}
@@ -58,31 +54,25 @@ impl Handler for Static {
5854
};
5955
let tm = time::at(ts).to_utc();
6056

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)
7466
}
7567
}
7668

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()
8676
}
8777

8878
#[cfg(test)]
@@ -93,7 +83,7 @@ mod tests {
9383
use std::io::prelude::*;
9484
use tempdir::TempDir;
9585

96-
use conduit::{Handler, Method};
86+
use conduit::{header, Handler, Method, StatusCode};
9787
use Static;
9888

9989
#[test]
@@ -105,19 +95,16 @@ mod tests {
10595
.unwrap()
10696
.write_all(b"[package]")
10797
.unwrap();
108-
let mut req = test::MockRequest::new(Method::Get, "/Cargo.toml");
98+
let mut req = test::MockRequest::new(Method::GET, "/Cargo.toml");
10999
let mut res = handler.call(&mut req).ok().expect("No response");
110100
let mut body = Vec::new();
111-
res.body.write_body(&mut body).unwrap();
101+
res.body_mut().write_body(&mut body).unwrap();
112102
assert_eq!(body, b"[package]");
113103
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"
120106
);
107+
assert_eq!(res.headers().get(header::CONTENT_LENGTH).unwrap(), "9");
121108
}
122109

123110
#[test]
@@ -128,16 +115,10 @@ mod tests {
128115
File::create(&root.join("src/fixture.css")).unwrap();
129116

130117
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");
132119
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");
141122
}
142123

143124
#[test]
@@ -146,9 +127,9 @@ mod tests {
146127
let root = td.path();
147128

148129
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");
150131
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);
152133
}
153134

154135
#[test]
@@ -159,9 +140,9 @@ mod tests {
159140
fs::create_dir(&root.join("foo")).unwrap();
160141

161142
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");
163144
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);
165146
}
166147

167148
#[test]
@@ -170,9 +151,9 @@ mod tests {
170151
let root = td.path();
171152
File::create(&root.join("test")).unwrap();
172153
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");
174155
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());
177158
}
178159
}

0 commit comments

Comments
 (0)