Skip to content

(fix) Update for new URL and Extensions APIs #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "staticfile"
version = "0.1.0"
authors = ["Zach Pomerantz <[email protected]>"]

[[lib]]
[lib]

name = "staticfile"
path = "src/lib.rs"
Expand All @@ -16,7 +16,3 @@ git = "https://github.com/iron/iron.git"
[dependencies.mount]

git = "https://github.com/iron/mount.git"

[dependencies.url]

git = "https://github.com/servo/rust-url.git"
43 changes: 5 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

//! Static file-serving middleware.

extern crate url;
#[phase(plugin)]
extern crate regex_macros;
extern crate regex;
Expand All @@ -17,7 +16,7 @@ extern crate log;
extern crate mount;

use http::headers::content_type::MediaType;
use iron::{Request, Response, Middleware, Status, Continue, Unwind};
use iron::{Request, Response, Middleware, Status, Continue, Unwind, Url};
use mount::OriginalUrl;

/// The static file-serving `Middleware`.
Expand Down Expand Up @@ -76,9 +75,8 @@ impl Static {

impl Middleware for Static {
fn enter(&mut self, req: &mut Request, res: &mut Response) -> Status {
// Get the URL path as a list of Strings.
// Unwrapping is safe because we're using a relative scheme (http), see rust-url.
let url_path: &[String] = req.url.path().unwrap();
// Get the URL path as a slice of Strings.
let url_path: &[String] = req.url.path.as_slice();

// Create a file path by combining the Middleware's root path and the URL path.
let requested_path = self.root_path.join(Path::new("").join_many(url_path));
Expand Down Expand Up @@ -108,7 +106,6 @@ impl Middleware for Static {
// appending a forward slash to URLs like http://example.com
// Just in case a Middleware has mutated the URL's path to violate this property,
// the empty list case is handled as a redirect.
// XXX: iron/mount currently violates this property (2014-08-09).
match url_path.last().as_ref().map(|s| s.as_slice()) {
Some("") => {
match res.serve_file(&index_path) {
Expand All @@ -129,7 +126,7 @@ impl Middleware for Static {
}

// Perform an HTTP 301 Redirect.
let redirect_path = match req.alloy.find::<OriginalUrl>() {
let redirect_path = match req.extensions.find::<OriginalUrl>() {
Some(&OriginalUrl(ref original_url)) => format!("{}/", original_url),
None => format!("{}/", req.url)
};
Expand All @@ -146,9 +143,7 @@ impl Middleware for Static {

impl Middleware for Favicon {
fn enter(&mut self, req: &mut Request, res: &mut Response) -> Status {
// Note: Unwrap is safe, see comment for `Static`.
let url_query = req.url.serialize_path().unwrap();
if regex!("/favicon.ico$").is_match(url_query.as_slice()) {
if req.url.path.as_slice() == ["favicon.ico".to_string()] {
res.headers.content_type = Some(MediaType {
type_: "image".to_string(),
subtype: "x-icon".to_string(),
Expand All @@ -169,31 +164,3 @@ impl Middleware for Favicon {
}
}

#[cfg(test)]
mod test {
use url::Url;

/// Test that rust-url always provides a path for http URLs.
#[test]
fn test_rust_url_path_unwrap() {
assert!(Url::parse("http://example.com").unwrap().path().is_some());
}

/// Test that rust-url always provides a *non-empty* path for http URLs.
/// Depends on `test_rust_url_path_unwrap`.
#[test]
fn test_rust_url_path_non_empty() {
let url = Url::parse("http://example.com").unwrap();
assert_eq!(url.path().unwrap(), &["".to_string()]);
}

/// Test that rust-url differentiates paths which end in slashes by storing "".
/// Depends on `test_rust_url_path_unwrap` and `test_rust_url_path_non_empty`.
#[test]
fn test_rust_url_path_slash_append() {
let url = Url::parse("http://example.com/doc/").unwrap();
assert_eq!(url.path().unwrap().last().unwrap(), &"".to_string());
let url = Url::parse("http://example.com/doc").unwrap();
assert_eq!(url.path().unwrap().last().unwrap(), &"doc".to_string());
}
}