Skip to content

Commit

Permalink
fix 404 on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
biluohc committed Apr 24, 2017
1 parent 095a63b commit 89cf552
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 68 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ toml = "^0.3.2"
serde_derive = "^0.9.14"
serde = "^0.9.14"

stderr ="0.7.0" # 要和 poolite 所用的同一个版本,否则loger不能正常工作。
poolite = "0.6.1"
app = "0.5.5"
stderr ="0.7.1" # 要和 poolite 所用的同一个版本,否则loger不能正常工作。
poolite = "0.6.2"
app = "0.5.6"

# stderr = { git = "https://github.com/biluohc/stderr", branch = "master", version = "0.7.0"}
# poolite = { git = "https://github.com/biluohc/poolite",branch = "master", version = "0.6.1" }
# app = { git = "https://github.com/biluohc/app-rs", branch = "master", version = "0.5.5" }
# stderr = { path = "../stderr" }
# poolite = { path ="../poolite" }
# app = { path = "../app" }

3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ fn main() {
// Cargo build --release -vv 可以看到println的内容。。
if cfg!(target_os = "windows") {
Command::new("windres")
.args(&["config/favicon.rc", "-o"])
.arg("config/favicon.rc")
.arg("-o")
.arg(&format!("{}/favicon.o", out_dir))
.status()
.unwrap();
Expand Down
30 changes: 6 additions & 24 deletions src/server/methods/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ fn path_is_403(path: &Path) -> bool {
}
}
fn file_to_resp(rc_s: Rc<RcStream>, req: &Request, path: &Path, mut map: HashMap<String, String>) -> Response {
dbstln!("{}@{} file_to_resp(): {:?}",
module_path!(),
rc_s.time().ls(),
path);
dbstln!("file_to_resp(): {:?}", path);
let ct = file_content_type(rc_s.clone(), path);
let content = Content::File(File::open(path).unwrap());
map.insert("Content-Type".to_owned(), ct);
Expand Down Expand Up @@ -96,10 +93,7 @@ fn file_content_type(rc_s: Rc<RcStream>, path: &Path) -> String {
}

fn sfs_to_resp(rc_s: Rc<RcStream>, req: &Request, path: &Path, mut map: HashMap<String, String>) -> Response {
dbstln!("{}@{} sfs_to_resp(): {:?}",
module_path!(),
rc_s.time().ls(),
path);
dbstln!("sfs_to_resp(): {:?}", path);
let exname = path.extension().unwrap().to_str().unwrap();
let doc = rc_s.arc().ents_doc(exname);
let ct = if doc.as_str() != "text/plain; charset=utf-8" {
Expand All @@ -123,10 +117,7 @@ fn sfs_to_resp(rc_s: Rc<RcStream>, req: &Request, path: &Path, mut map: HashMap<
}

pub fn other_status_code_to_resp(rc_s: Rc<RcStream>, req: &Request, mut map: HashMap<String, String>) -> Response {
dbstln!("{}@{} other_status_code_to_resp(): {:?}",
module_path!(),
rc_s.time().ls(),
req.status);
dbstln!("other_status_code_to_resp(): {:?}", req.status);
let code_name = (*rc_s.arc().cns().get(req.status()).unwrap()).to_owned();
let title = format!("{} {}", req.status(), code_name);
let h1 = TagDouble::new("h1")
Expand All @@ -153,10 +144,7 @@ pub fn other_status_code_to_resp(rc_s: Rc<RcStream>, req: &Request, mut map: Has
}

fn dir_to_resp(rc_s: Rc<RcStream>, req: &Request, path: &Path, mut map: HashMap<String, String>) -> Response {
dbstln!("{}@{} dir_to_resp(): {:?}",
module_path!(),
rc_s.time().ls(),
path);
dbstln!("dir_to_resp(): {:?}", path);
let content = Content::Str(dir_to_string(rc_s.clone(), req, path));
let ct = rc_s.arc().ents_doc("html");
map.insert("Content-Length".to_owned(), format!("{}", content.len()));
Expand Down Expand Up @@ -236,20 +224,14 @@ fn dir_to_string(rc_s: Rc<RcStream>, req: &Request, path: &Path) -> Vec<u8> {
let path_encoded = match quote(entry_name.clone(), b"") {
Ok(ok) => ok,
Err(_) => {
errstln!("{}@{}_Entry_encoding_error:\n{:?}",
module_path!(),
rc_s.time().ls(),
&path);
errstln!("Entry_encoding_error:\n{:?}", &path);
entry_name.clone()
}
};

// 比如当前目录下有断开的链接时会陷入。
// if !Path::new(&entry_path).exists() {
// panic!("{}@{}_path_拼接_error:\n{:?}\n",
// module_path!(),
// rc_s.time().ls(),
// &entry_path);
// panic!("path_拼接_error:\n{:?}\n", &entry_path);
// }

// "/" 区分目录与文件(视觉),并且如果没有它,浏览器不会自动拼路径,这上面坑了好多时间。
Expand Down
39 changes: 12 additions & 27 deletions src/server/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ fn deal_client(config: Arc<ArcConfig>, mut stream: TcpStream) -> Result<(), io::
stream.set_write_timeout(Some(time_out))?;
let rc_stream = Rc::from(RcStream::new(config, server_addr, client_addr));

// println!("二分法 {:?}@{:?} {:?}@{:?}",
// println!("binary_search {:?}@{:?}",
// rc_stream.time().as_str(),
// rc_stream.client_addr(),
// module_path!(),
// line!());
// rc_stream.client_addr());
loop {
let rc_s = rc_stream.clone();
let request_read = read_request(&mut stream)?;
Expand Down Expand Up @@ -134,10 +132,7 @@ fn read_request(stream: &mut TcpStream) -> std::io::Result<Vec<u8>> {
fn to_request(vec: &[u8], rc_s: Rc<RcStream>) -> Request {
// "GET /favicon.ico HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nCookie: _ga=GA1.1.1204164507.1467299031\r\nConnection: keep-alive\r\n\r\n"
let req_str = String::from_utf8_lossy(&vec[..]).into_owned();
dbstln!("\n{}@{} req_raw:\n{:?}",
module_path!(),
rc_s.time().ls(),
req_str);
dbstln!("req_raw:\n{:?}", req_str);
let mut req_str = req_str.lines();
let line = req_str.next().unwrap();
// 请求行
Expand All @@ -153,20 +148,11 @@ fn to_request(vec: &[u8], rc_s: Rc<RcStream>) -> Request {
}
}
}
dbstln!("{}@{} header_HashMap:\n{:?}",
module_path!(),
rc_s.time().ls(),
header);
dbstln!("header_HashMap:\n{:?}", header);
let url_raw = line[1].to_string();
dbstln!("{}@{} url_raw: {:?}",
module_path!(),
rc_s.time().ls(),
&line[1]);
dbstln!("url_raw: {:?}", &line[1]);
let url = url_handle_pre(&url_raw);
dbstln!("{}@{} url_handle_pre(): {:?}",
module_path!(),
rc_s.time().ls(),
&url);
dbstln!("url_handle_pre(): {:?}", &url);
// 如果路径本身就存在,就不二次解码,(三次编码则会产生多余"/"等字符,不可能是真实路径。浏览器对URL只编码一次。
// Option<(String, String)> 完全匹配才赋值,is_none() 用于是否继续寻找。
let mut vp_rp_full_match: Option<(String, String)> = None;
Expand Down Expand Up @@ -241,10 +227,8 @@ fn url_to_vp_rp(rc_s: Rc<RcStream>, url: &str, mut vp_rp_full_match: &mut Option
if decoded_url[vp.len()..].starts_with('/') {
break;
}
dbstln!("{}@{}_url_sub_'/'_decoded: '{}' start_with(rp): \
dbstln!("url_sub_'/'_decoded: '{}' start_with(rp): \
{:?}\nrp.join(decoded_url): {:?}",
module_path!(),
rc_s.time().ls(),
&decoded_url,
vp,
Path::new(rp).join(Path::new(&decoded_url[vp.len()..])));
Expand All @@ -257,10 +241,7 @@ fn url_to_vp_rp(rc_s: Rc<RcStream>, url: &str, mut vp_rp_full_match: &mut Option
}
}
} else {
dbstln!("{}_Warning@{}::url_to_vp_rp(): '{}' decode failed",
NAME,
module_path!(),
url);
dbstln!("{}_Warning@url_to_vp_rp(): '{}' decode failed", NAME, url);
}
}

Expand Down Expand Up @@ -296,5 +277,9 @@ fn url_handle_pre(msg: &str) -> String {
if !msg.ends_with('/') {
raw.pop();
}
// win 上Path会自动加 "\\" ,然后尽数404
if cfg!(windows) {
raw = raw[1..].to_string();
}
raw
}
13 changes: 3 additions & 10 deletions src/server/methods/rc_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ impl Response {
Ok(())
}
if let Err(e) = write_response(self, req, &mut stream) {
dbstln!("{}_Warning@{}::write_response(): {}",
NAME,
module_path!(),
e.description());
dbstln!("{}_Warning@write_response(): {}", NAME, e.description());
};
}
}
Expand All @@ -194,9 +191,8 @@ impl Content {
match file.metadata() {
Ok(ok) => ok.len(),
Err(e) => {
dbstln!("{}_Warning@{}::file_lenth(): {}@{:?}",
dbstln!("{}_Warning@file_lenth(): {}@{:?}",
NAME,
module_path!(),
e.description(),
file);
unreachable!();
Expand All @@ -211,10 +207,7 @@ impl Content {
}
pub fn write_content(self, mut stream: &mut TcpStream) {
if let Err(e) = write_content_result(self, &mut stream) {
dbstln!("{}_Warning@{}::write_content(): {}",
NAME,
module_path!(),
e.description());
dbstln!("{}_Warning@write_content(): {}", NAME, e.description());
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions util/bs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "二分法叉虫子"
name = "binary_search"
version = "0.1.0"
authors = ["biluohc <biluohc@qq.com>"]

[dependencies]
stderr ="^0.7.0"
stderr ="0.7.1"

0 comments on commit 89cf552

Please sign in to comment.