Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdaq committed Sep 1, 2023
1 parent d9f1ce9 commit 247221c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 44 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<li><a href="/static/content.png">image</a></li>
<li><a href="/static/form">form</a></li>
<li><a href="/static/file">file</a></li>
<li><a href="/static">folder with index.html</a></li>
<li><a href="/404">not found resource</a></li>
</ul>
</body>
Expand Down
130 changes: 89 additions & 41 deletions src/app/controller/static_resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Controller for StaticResourceController {
}

let html_suffix = ".html";
let html_file = [&components.path, html_suffix].join(SYMBOL.empty_string);
let html_file = [&components.path.replace(SYMBOL.slash, &FileExt::get_path_separator()), html_suffix].join(SYMBOL.empty_string);
let boxed_static_filepath = FileExt::get_static_filepath(&html_file);
if boxed_static_filepath.is_err() {
return false
Expand Down Expand Up @@ -285,9 +285,20 @@ impl StaticResourceController {

let mut content_range_list = Vec::new();

let boxed_file = File::open(&static_filepath);
if boxed_file.is_ok() {
let md = metadata(&static_filepath).unwrap();

let mut boxed_md = metadata(&static_filepath);
if boxed_md.is_err() {
let dot_html = format!("{}{}", &static_filepath, ".html");
boxed_md = metadata(&dot_html);

if boxed_md.is_err() {
let slash_index_html = format!("{}{}{}", &static_filepath, os_specific_separator, "index.html");
boxed_md = metadata(&slash_index_html);
}
}
if boxed_md.is_ok() {
let md = boxed_md.unwrap();

if md.is_dir() {
let mut range_header = &Header {
name: Header::_RANGE.to_string(),
Expand Down Expand Up @@ -316,38 +327,14 @@ impl StaticResourceController {
let error = boxed_content_range_list.err().unwrap();
return Err(error)
}
}

if md.is_file() {
let mut range_header = &Header {
name: Header::_RANGE.to_string(),
value: "bytes=0-".to_string()
};

let boxed_header = request.get_header(Header::_RANGE.to_string());
if boxed_header.is_some() {
range_header = boxed_header.unwrap();
}

let boxed_content_range_list = Range::get_content_range_list(&request.request_uri, range_header);
if boxed_content_range_list.is_ok() {
content_range_list = boxed_content_range_list.unwrap();
} else {
let error = boxed_content_range_list.err().unwrap();
return Err(error)
}
return Ok(content_range_list);
}
}


if boxed_file.is_err() {
//check if .html file exists
let static_filepath = [working_directory, components.path.as_str(), ".html"].join(SYMBOL.empty_string);

let boxed_file = File::open(&static_filepath);
if boxed_file.is_ok() {
let md = metadata(&static_filepath).unwrap();
if md.is_file() {
if md.is_dir() {
let mut range_header = &Header {
name: Header::_RANGE.to_string(),
value: "bytes=0-".to_string()
Expand All @@ -358,23 +345,37 @@ impl StaticResourceController {
range_header = boxed_header.unwrap();
}

let url_array = ["http://", "localhost", &request.request_uri];
let url = url_array.join(SYMBOL.empty_string);
let mut directory_index : String = "index.html".to_string();

let boxed_url_components = URL::parse(&url);
if boxed_url_components.is_err() {
let message = boxed_url_components.as_ref().err().unwrap().to_string();
// unfallable
println!("unexpected error, {}", message);
let last_char = components.path.chars().last().unwrap();
if last_char != '/' {
let index : String = "index.html".to_string();
directory_index = format!("{}{}", os_specific_separator, index);
}
let index_html_in_directory = format!("{}{}", os_specific_path, directory_index);

let components = boxed_url_components.unwrap();

// let html_file = [SYMBOL.slash, ].join(SYMBOL.empty_string);
let boxed_content_range_list = Range::get_content_range_list(&index_html_in_directory, range_header);
if boxed_content_range_list.is_ok() {
content_range_list = boxed_content_range_list.unwrap();
} else {
let error = boxed_content_range_list.err().unwrap();
return Err(error)
}
}

if md.is_file() {
let mut range_header = &Header {
name: Header::_RANGE.to_string(),
value: "bytes=0-".to_string()
};

let boxed_header = request.get_header(Header::_RANGE.to_string());
if boxed_header.is_some() {
range_header = boxed_header.unwrap();
}

let html_file = [components.path.as_str(), ".html"].join(SYMBOL.empty_string);
let boxed_content_range_list = Range::get_content_range_list(html_file.as_str(), range_header);
let boxed_content_range_list = Range::get_content_range_list(&request.request_uri, range_header);
if boxed_content_range_list.is_ok() {
content_range_list = boxed_content_range_list.unwrap();
} else {
Expand All @@ -383,8 +384,55 @@ impl StaticResourceController {
}
}
}


if boxed_file.is_err() {
//check if .html file exists
let static_filepath = [working_directory, components.path.as_str(), ".html"].join(SYMBOL.empty_string);

let boxed_file = File::open(&static_filepath);
if boxed_file.is_ok() {
let md = metadata(&static_filepath).unwrap();
if md.is_file() {
let mut range_header = &Header {
name: Header::_RANGE.to_string(),
value: "bytes=0-".to_string()
};

let boxed_header = request.get_header(Header::_RANGE.to_string());
if boxed_header.is_some() {
range_header = boxed_header.unwrap();
}

let url_array = ["http://", "localhost", &request.request_uri];
let url = url_array.join(SYMBOL.empty_string);

let boxed_url_components = URL::parse(&url);
if boxed_url_components.is_err() {
let message = boxed_url_components.as_ref().err().unwrap().to_string();
// unfallable
println!("unexpected error, {}", message);
}

let components = boxed_url_components.unwrap();

// let html_file = [SYMBOL.slash, ].join(SYMBOL.empty_string);


let html_file = [components.path.as_str(), ".html"].join(SYMBOL.empty_string);
let boxed_content_range_list = Range::get_content_range_list(html_file.as_str(), range_header);
if boxed_content_range_list.is_ok() {
content_range_list = boxed_content_range_list.unwrap();
} else {
let error = boxed_content_range_list.err().unwrap();
return Err(error)
}
}
}
}
}


Ok(content_range_list)
}
}
2 changes: 1 addition & 1 deletion src/range/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Range {
pub fn get_content_range_list(request_uri: &str, range: &Header) -> Result<Vec<ContentRange>, Error> {
let mut content_range_list : Vec<ContentRange> = vec![];

let url_array = ["http://", "localhost", &request_uri];
let url_array = ["http://", "localhost", &request_uri.replace(&FileExt::get_path_separator(), SYMBOL.slash)];
let url = url_array.join(SYMBOL.empty_string);

let boxed_url_components = URL::parse(&url);
Expand Down
2 changes: 1 addition & 1 deletion static/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h4>Form Method Post Url Encoded Enctype</h4>
</form>

<h4>Form Method Get</h4>
<p><a href="https://github.com/bohdaq/rust-web-server/blob/main/src/app/controller/form/get_method/mod.rs">FormGetMethodController</a> controller capable of handling form with method <i>GET</i> and without enctype specified.</p>
<p><a href="https://github.com/bohdaq/rust-web-server/blob/main/src/app/controller/form/get_method/mod.rs">FormGetMethodController</a> controller capable of handling form with method <i>GET</i> and without enctype specified. Most reliable method for submitting the form, works among all popular browsers.</p>
<form method="get" action="/form-get-method">
<label>Field 1:
<input name="field-1" />
Expand Down
4 changes: 3 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
</style>
</head>
<body>
Index html test
<p><a href="/"><< Back to main</a></p>

<p>Index html test</p>

</body>
</html>

0 comments on commit 247221c

Please sign in to comment.