Skip to content

Commit 1ceb872

Browse files
committed
Add semver support to rustdoc html server handler
Fixes: #26
1 parent 921adca commit 1ceb872

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

src/web/file.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,7 @@ pub struct DatabaseFileHandler;
6969
impl Handler for DatabaseFileHandler {
7070
fn handle(&self, req: &mut Request) -> IronResult<Response> {
7171

72-
let path = {
73-
let mut path = req.url.path.clone().join("/");
74-
if path.ends_with("/") {
75-
path.push_str("index.html");
76-
}
77-
// rustdoc javascripts have rustdoc prefix in database
78-
// FIXME: this is kinda lame I have to save all javascripts with rustdoc prefix
79-
// FIXME: this code became more retarded than before
80-
if path.ends_with(".js") && !path.starts_with("main-") &&
81-
!path.starts_with("jquery-") && !path.starts_with("playpen-") {
82-
format!("rustdoc/{}", path)
83-
} else {
84-
path
85-
}
86-
};
72+
let path = req.url.path.clone().join("/");
8773

8874
let conn = req.extensions.get::<Pool>().unwrap();
8975
if let Some(file) = File::from_path(&conn, &path) {

src/web/rustdoc.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,41 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
111111

112112
pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
113113

114-
let path = {
115-
let mut path = req.url.path.clone();
116-
// documentations have "rustdoc" prefix in database
117-
path.insert(0, "rustdoc".to_owned());
114+
let name = req.extensions.get::<Router>().unwrap().find("crate").unwrap_or("").to_string();
115+
let version = req.extensions
116+
.get::<Router>()
117+
.unwrap()
118+
.find("version");
119+
let conn = req.extensions.get::<Pool>().unwrap();
120+
let version = try!(match_version(&conn, &name, version)
121+
.ok_or(IronError::new(Nope::ResourceNotFound, status::NotFound)));
122+
123+
// remove name and version from path
124+
for _ in 0..2 {
125+
req.url.path.remove(0);
126+
}
127+
128+
// docs have "rustdoc" prefix in database
129+
req.url.path.insert(0, "rustdoc".to_owned());
130+
131+
// add crate name and version
132+
req.url.path.insert(1, name.clone());
133+
req.url.path.insert(2, version.clone());
118134

119-
let mut path = path.join("/");
135+
let path = {
136+
let mut path = req.url.path.join("/");
120137
if path.ends_with("/") {
121-
path.push_str("index.html")
138+
path.push_str("index.html");
139+
req.url.path.push("index.html".to_owned());
122140
}
123-
124141
path
125142
};
126-
143+
127144
// don't touch anything other than html files
128145
if !path.ends_with(".html") {
129146
return Err(IronError::new(Nope::ResourceNotFound, status::NotFound));
130147
}
131148

132-
let conn = req.extensions.get::<Pool>().unwrap();
133149

134150
let file = match File::from_path(&conn, &path) {
135151
Some(f) => f,
@@ -165,15 +181,6 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
165181
}
166182
}
167183

168-
169-
let name = req.extensions.get::<Router>().unwrap().find("crate").unwrap_or("").to_string();
170-
let version = req.extensions
171-
.get::<Router>()
172-
.unwrap()
173-
.find("version")
174-
.unwrap_or("")
175-
.to_string();
176-
177184
// content.metadata = MetaData::from_crate(&conn, &name, &version);
178185
let (metadata, platforms) = {
179186
let rows = conn.query("SELECT crates.name,

0 commit comments

Comments
 (0)