Skip to content

Commit e3df3b4

Browse files
committed
fix: file explorer entries with invalid links
Fix issue where `FileExplorer` entry link is not prefixed with `/` on some paths causing the link to be invalid.
1 parent 4ba2f13 commit e3df3b4

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
<Empty>
44

5+
<a name="v0.2.1"></a>
6+
## v0.2.1 (2021-04-22)
7+
8+
> Requires Rust: rustc 1.49.0 (e1884a8e3 2020-12-29)
9+
10+
#### Fixes
11+
12+
* Fix issue where `FileExplorer` entry link is not prefixed with `/` on some
13+
paths causing the link to be invalid.
14+
515
<a name="v0.2.0"></a>
616
## v0.2.0 (2021-04-19)
717

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "http-server"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = ["Esteban Borai <[email protected]>"]
55
edition = "2018"
66
description = "Simple and configurable command-line HTTP server"

src/server/service/file_explorer.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ impl<'a> FileExplorer<'a> {
209209
/// Creates a `DirectoryIndex` with the provided `root_dir` and `path`
210210
/// (HTTP Request URI)
211211
fn index_directory(root_dir: PathBuf, path: PathBuf) -> Result<DirectoryIndex> {
212-
let root_dir: &str = root_dir.to_str().unwrap();
213212
let entries = read_dir(path).context("Unable to read directory")?;
214213
let mut directory_entries: Vec<DirectoryEntry> = Vec::new();
215214

@@ -235,11 +234,7 @@ impl<'a> FileExplorer<'a> {
235234
.to_string(),
236235
is_dir: metadata.is_dir(),
237236
size: FileExplorer::format_bytes(metadata.len() as f64),
238-
entry_path: FileExplorer::make_entry_relative_path(
239-
root_dir,
240-
entry.path().to_str().unwrap(),
241-
)
242-
.to_string(),
237+
entry_path: FileExplorer::make_dir_entry_link(&root_dir, entry.path()),
243238
created_at,
244239
updated_at,
245240
});
@@ -277,8 +272,21 @@ impl<'a> FileExplorer<'a> {
277272

278273
/// Creates entry's relative path. Used by Handlebars template engine to
279274
/// provide navigation through `FileExplorer`
280-
fn make_entry_relative_path<'b>(current_dir_path: &'b str, entry_path: &'b str) -> &'b str {
281-
&entry_path[current_dir_path.len()..]
275+
///
276+
/// If the root_dir is: `https-server/src`
277+
/// The entry path is: `https-server/src/server/service/file_explorer.rs`
278+
///
279+
/// Then the resulting path from this function is the absolute path to
280+
/// the "entry path" in relation to the "root_dir" path.
281+
///
282+
/// This happens because links should behave relative to the `/` path
283+
/// which in this case is `http-server/src` instead of system's root path.
284+
fn make_dir_entry_link(root_dir: &PathBuf, entry_path: PathBuf) -> String {
285+
// format!("/{}", &entry_path[current_dir_path.len()..])
286+
let root_dir = root_dir.to_str().unwrap();
287+
let entry_path = entry_path.to_str().unwrap();
288+
289+
entry_path[root_dir.len() - 1..].to_string()
282290
}
283291

284292
/// Calculates the format of the `Bytes` by converting `bytes` to the
@@ -317,7 +325,7 @@ mod tests {
317325
use super::*;
318326

319327
#[test]
320-
fn format_bytes() {
328+
fn formats_bytes() {
321329
let byte_sizes = vec![1024., 1048576., 1073741824., 1099511627776.];
322330

323331
let expect = vec![
@@ -331,4 +339,17 @@ mod tests {
331339
assert_eq!(FileExplorer::format_bytes(size), expect[idx]);
332340
}
333341
}
342+
343+
#[test]
344+
fn makes_dir_entry_link() {
345+
let root_dir = PathBuf::from_str("/Users/bob/sources/http-server").unwrap();
346+
let entry_path =
347+
PathBuf::from_str("/Users/bob/sources/http-server/src/server/service/file_explorer.rs")
348+
.unwrap();
349+
350+
assert_eq!(
351+
"/src/server/service/file_explorer.rs",
352+
FileExplorer::make_dir_entry_link(&root_dir, entry_path)
353+
);
354+
}
334355
}

0 commit comments

Comments
 (0)