Skip to content
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Cargo.lock
# FILE SERVER

*.json
!*.example.json
!file_server_config_example.json
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Bash the following command:
file_server
```

This will start `file_server` with it's default configuration in the `cwd`.
This will start `file_server` with using the `cwd` as a base directory.

Now files can be requested from the `cwd` at `localhost:3000`:

Expand All @@ -39,18 +39,18 @@ curl localhost:3000

### Configuration

A valid [JSON configuration file](./file_server.example.json) matches the following schema.
A valid [JSON configuration file](./demo/file_server_config_example.json) matches the following schema.

```JSON
{
"directory": "./demo",
"directory": "./",
"host_and_port": "127.0.0.1:4000",
"content_encodings": ["gzip", "deflate", "br", "zstd"],
"filepath_404": "./demo/404.html"
"filepath_404": "./404.html"
}
```

Filepaths can be relative or absolute. Relative paths are "relative from" the filepath of the JSON configuration.
Filepaths can be relative or absolute. Relative paths are "relative from" the filepath of the JSON configuration file.

The `content_encodings` and `filepath_404` properties are optional.

Expand All @@ -66,19 +66,19 @@ Open a browser and visit `http://localhost:3000` and an encoded version of `inde

### Accept-Encoding

When an `accept-encoding` header is found in a request, `file_server` will return a corresponding `zip`-ed version of file if available.
When a request has an `accept-encoding` header, `file_server` will return a corresponding `zip`-ed version of file if available.

So if a request has the following header:

```
Accept-Encoding: gzip;
```

And the source file has a correspponding gziped file:
And the target file has a correspponding gziped file:

```sh
index.html # source file
index.html.gz # gzipped file
index.html.gz # gzipped file
```

`File_server` will send the encoded file, if available. Otherwise, it serves the source file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"directory": "./",
"host_and_port": "127.0.0.1:3000",
"host_and_port": "127.0.0.1:4000",
"content_encodings": ["zstd"],
"filepath_404": "./404.html"
}
34 changes: 19 additions & 15 deletions file_server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use serde_json;
use std::env;
use std::path;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use tokio::fs;

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand All @@ -28,9 +28,9 @@ impl Config {
})
}

pub async fn try_from(source_path: &PathBuf) -> Result<Config, String> {
pub async fn try_from(filepath: &PathBuf) -> Result<Config, String> {
// see if config exists
let config_json = match fs::read_to_string(source_path).await {
let config_json = match fs::read_to_string(filepath).await {
Ok(r) => r,
Err(e) => return Err(e.to_string()),
};
Expand All @@ -41,7 +41,7 @@ impl Config {
};

// get target directory
let config_path = match path::absolute(&source_path) {
let config_path = match path::absolute(&filepath) {
Ok(pb) => pb,
Err(e) => return Err(e.to_string()),
};
Expand All @@ -53,29 +53,33 @@ impl Config {
}
};

// get target directory relative to config path
let target_directory = parent_dir.join(config.directory);
let target_directory_abs = match path::absolute(target_directory) {
// https://doc.rust-lang.org/std/path/struct.Path.html#method.normalize_lexically
// normalize lexically
let target_directory = match fs::canonicalize(parent_dir.join(config.directory)).await {
Ok(pb) => pb,
Err(e) => return Err(e.to_string()),
};

config.directory = target_directory_abs;

if let Some(origin_404s) = config.filepath_404 {
config.filepath_404 = match get_path_relative_to_origin(parent_dir, &origin_404s) {
Ok(pb) => Some(pb),
Err(e) => return Err(e.to_string()),
};
config.filepath_404 =
match get_path_relative_to_origin(&target_directory, &origin_404s).await {
Ok(pb) => Some(pb),
Err(e) => return Err(e.to_string()),
};
}

config.directory = target_directory;

Ok(config)
}
}

fn get_path_relative_to_origin(source_dir: &Path, filepath: &PathBuf) -> Result<PathBuf, String> {
async fn get_path_relative_to_origin(
source_dir: &PathBuf,
filepath: &PathBuf,
) -> Result<PathBuf, String> {
let target_path = source_dir.join(filepath);
let target_path_abs = match path::absolute(target_path) {
let target_path_abs = match fs::canonicalize(target_path).await {
Ok(pb) => pb,
Err(e) => return Err(e.to_string()),
};
Expand Down
7 changes: 4 additions & 3 deletions response/src/response_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use hyper::body::Incoming;
use hyper::header::ACCEPT_ENCODING;
use hyper::http::Request;
use std::ffi::OsString;
use std::path;
use std::path::PathBuf;
use tokio::fs;

Expand All @@ -23,7 +22,9 @@ pub async fn get_path_from_request_url(
}

pub async fn get_path(directory: &PathBuf, filepath: &PathBuf) -> Option<PathBuf> {
let mut target_path = match path::absolute(directory.join(&filepath)) {
// https://doc.rust-lang.org/std/path/struct.Path.html#method.normalize_lexically
// normalize lexically in nightly
let mut target_path = match fs::canonicalize(directory.join(&filepath)).await {
Ok(pb) => pb,
_ => return None,
};
Expand All @@ -38,7 +39,7 @@ pub async fn get_path(directory: &PathBuf, filepath: &PathBuf) -> Option<PathBuf
_ => return None,
};

// if file bail early
// if file return early
if metadata.is_file() {
return Some(target_path);
}
Expand Down
6 changes: 3 additions & 3 deletions response/src/type_flyweight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl ResponseParams {
let available_encodings = AvailableEncodings::from(content_encodings);

ResponseParams {
directory: directory,
available_encodings: available_encodings,
filepath_404: filepath_404,
directory,
available_encodings,
filepath_404,
}
}
}