Skip to content

Commit a9f6325

Browse files
authored
static: Extract lookup(path) method (rust-lang#109)
This allows us to lookup specific files without having to mutate the `path` of the request.
1 parent 8107380 commit a9f6325

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

conduit-static/src/lib.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ impl Static {
1515
path: path.as_ref().to_path_buf(),
1616
}
1717
}
18-
}
1918

20-
impl Handler for Static {
21-
fn call(&self, request: &mut dyn RequestExt) -> HandlerResult {
22-
let request_path = request.path();
19+
pub fn lookup(&self, request_path: &str) -> HandlerResult {
2320
let request_path = request_path.strip_prefix('/').unwrap_or(request_path);
2421
if request_path.contains("..") {
2522
return Ok(not_found());
@@ -47,6 +44,12 @@ impl Handler for Static {
4744
}
4845
}
4946

47+
impl Handler for Static {
48+
fn call(&self, request: &mut dyn RequestExt) -> HandlerResult {
49+
self.lookup(request.path())
50+
}
51+
}
52+
5053
fn not_found() -> Response<Body> {
5154
Response::builder()
5255
.status(StatusCode::NOT_FOUND)
@@ -85,6 +88,25 @@ mod tests {
8588
assert_eq!(*res.into_cow(), b"[package]"[..]);
8689
}
8790

91+
#[test]
92+
fn test_lookup() {
93+
let td = TempDir::new("conduit-static").unwrap();
94+
let root = td.path();
95+
let handler = Static::new(root);
96+
File::create(&root.join("Cargo.toml"))
97+
.unwrap()
98+
.write_all(b"[package]")
99+
.unwrap();
100+
101+
let res = handler.lookup("Cargo.toml").expect("No response");
102+
assert_eq!(
103+
res.headers().get(header::CONTENT_TYPE).unwrap(),
104+
"application/toml"
105+
);
106+
assert_eq!(res.headers().get(header::CONTENT_LENGTH).unwrap(), "9");
107+
assert_eq!(*res.into_cow(), b"[package]"[..]);
108+
}
109+
88110
#[test]
89111
fn test_mime_types() {
90112
let td = TempDir::new("conduit-static").unwrap();

0 commit comments

Comments
 (0)