Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring HTTP #11

Merged
merged 8 commits into from
Feb 27, 2023
Merged

Refactoring HTTP #11

merged 8 commits into from
Feb 27, 2023

Conversation

hulxv
Copy link
Contributor

@hulxv hulxv commented Feb 12, 2023

What's new?

  • Clean Request implementation
  • Make Response implementation more clean & usable
  • print host address when running the web server:
$ cargo run
Blitzkrieg Web Server is running on 127.0.0.1:7818
  • add examples that are found in README to examples' crate

@adhamsalama
Copy link
Owner

Hello.

Thank you for the PR, I'll drop some notes on it.

src/http/mod.rs Outdated Show resolved Hide resolved
src/server.rs Outdated
@@ -39,7 +40,7 @@ impl Server {

pub fn build_http_request(mut stream: TcpStream) -> (TcpStream, Request) {
let (request, body) = parse_tcp_stream(&mut stream);
let request = parse_http_string((request, body));
let request = Request::from_str(&request).unwrap().body(body).clone();
Copy link
Owner

@adhamsalama adhamsalama Feb 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing from_str is a good idea.
However, this

(&request).unwrap().body(body).clone();

is why I don't like chainable methods.
I feel like it's hurting readability here.

IMO the previous code is a bit better, and a bit more abstract, instead of passing a reference then unwrapping then calling another method to set the body then cloning.

Maybe a good middle ground is implementing a method on the struct like what parse_http_string is doing and having it construct the struct with the body.

I think an even better solution would be implementing parse_tcp_stream on the Request struct, after all, it is related to it, and it's also the only thing using it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chainable methods is needed here, You can't passing any arguments other than string that you want parse, So It's important here and unavoidable (Unless there is a way that I don't know) if we want use from_str

PATCH,
DELETE,
HEAD,
OPTIONS,
Copy link
Owner

@adhamsalama adhamsalama Feb 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really need its own file? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https.rs was crowded with many things. Frankly, I like organization and I do not see that there is a problem with a large number of files, but everything is organized.
Maybe in future you can add Http status codes enum here, But the difficulty will be in naming the file :D

README.md Outdated Show resolved Hide resolved
src/http/mod.rs Outdated
true => {
self.headers.insert(
"Content-Type".to_string(),
"multipart/form-data".to_string(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, not particularly fond of chainable methods.

@adhamsalama
Copy link
Owner

Thanks for the PR!

@adhamsalama adhamsalama merged commit 800ce61 into adhamsalama:main Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants