Skip to content

Commit a1202b8

Browse files
committed
hot fix index
fixed a bug where the server will not serve anything but the index
1 parent 30fc408 commit a1202b8

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hteapot"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
exclude = ["hteapot.toml", "public/", "readme.md"]
55
license = "MIT"
66
keywords = ["HTTP", "HTTP-SERVER"]

src/main.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod config;
44
pub mod hteapot;
55
mod logger;
66

7+
use std::fmt::format;
78
use std::fs;
89
use std::io;
910
use std::path::Path;
@@ -44,12 +45,7 @@ fn serve_proxy(proxy_url: String) -> HttpResponse {
4445
}
4546
}
4647

47-
fn get_mime_tipe(config: &Config, path: String) -> String {
48-
let mut path = format!("{}{}", config.root, path);
49-
if Path::new(path.as_str()).is_dir() {
50-
let separator = if path.ends_with('/') { "" } else { "/" };
51-
path = format!("{}{}{}", path, separator, config.index);
52-
}
48+
fn get_mime_tipe(path: &String) -> String {
5349
let extension = Path::new(path.as_str())
5450
.extension()
5551
.unwrap()
@@ -60,18 +56,14 @@ fn get_mime_tipe(config: &Config, path: String) -> String {
6056
"json" => "application/json",
6157
"css" => "text/css",
6258
"html" => "text/html",
59+
"ico" => "image/x-icon",
6360
_ => "text/plain",
6461
};
6562

6663
mimetipe.to_string()
6764
}
6865

69-
fn serve_file(config: &Config, path: String) -> Option<Vec<u8>> {
70-
let mut path = format!("{}{}", config.root, path);
71-
if Path::new(path.as_str()).is_dir() {
72-
let separator = if path.ends_with('/') { "" } else { "/" };
73-
path = format!("{}{}{}", path, separator, config.index);
74-
}
66+
fn serve_file(path: &String) -> Option<Vec<u8>> {
7567
let r = fs::read(path);
7668
if r.is_ok() {
7769
Some(r.unwrap())
@@ -166,22 +158,32 @@ fn main() {
166158
if proxy_only || is_proxy.is_some() {
167159
return serve_proxy(is_proxy.unwrap());
168160
}
169-
if !Path::new(req.path.clone().as_str()).exists() {
161+
162+
let mut full_path = format!("{}{}", config.root, req.path.clone());
163+
if Path::new(full_path.as_str()).is_dir() {
164+
let separator = if full_path.ends_with('/') { "" } else { "/" };
165+
full_path = format!("{}{}{}", full_path, separator, config.index);
166+
}
167+
if !Path::new(full_path.as_str()).exists() {
168+
logger
169+
.lock()
170+
.expect("this doesnt work :C")
171+
.msg(format!("path {} does not exist", req.path));
170172
return HttpResponse::new(HttpStatus::NotFound, "Not found", None);
171173
}
172-
let mimetype = get_mime_tipe(&config, req.path.clone());
174+
let mimetype = get_mime_tipe(&full_path);
173175
let content: Option<Vec<u8>> = if config.cache {
174176
let mut cachee = cache.lock().expect("Error locking cache");
175177
let mut r = cachee.get(req.path.clone());
176178
if r.is_none() {
177-
r = serve_file(&config, req.path.clone());
179+
r = serve_file(&full_path);
178180
if r.is_some() {
179181
cachee.set(req.path.clone(), r.clone().unwrap());
180182
}
181183
}
182184
r
183185
} else {
184-
serve_file(&config, req.path)
186+
serve_file(&full_path)
185187
};
186188
match content {
187189
Some(c) => HttpResponse::new(HttpStatus::OK, c, headers!("Content-Type" => mimetype)),

0 commit comments

Comments
 (0)