Skip to content

Commit

Permalink
misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mateocabanal committed Jul 4, 2024
1 parent cbef283 commit dba2819
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/tinyhttp.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions profile.json

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions tinyhttp-internal/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ fn build_and_parse_req<P: Read>(conn: &mut P) -> Result<Request, RequestError> {
.parse::<usize>()
.unwrap();

let mut raw_body = Vec::with_capacity(body_len);

// Zero-init raw_body
(0..body_len).for_each(|_| {
raw_body.push(0);
});
let mut raw_body = vec![0; body_len];

buf_reader.read_exact(&mut raw_body).unwrap();

Expand Down
8 changes: 6 additions & 2 deletions tinyhttp-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ pub mod async_http;

#[cfg(test)]
mod tests {
use std::collections::HashMap;

#[test]
fn build_request() {
use crate::request::Request;
let mut headers = HashMap::new();
headers.insert("content-type".to_string(), "text/plain".to_string());

let request = Request::new(
b"Hello, World!",
vec!["Content-Type: text/plain".to_string()],
b"Hello, World!".to_vec(),
headers,
vec![
"GET".to_string(),
"/test".to_string(),
Expand Down
43 changes: 40 additions & 3 deletions tinyhttp-internal/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
error::Error,
io::{Read, Write},
};

Expand All @@ -23,19 +24,55 @@ impl Default for Response {

impl<'a> From<&'a str> for Response {
fn from(value: &'a str) -> Self {
Response::new().body(value.into()).mime("text/plain").status_line("HTTP/1.1 200 OK")
Response::new()
.body(value.into())
.mime("text/plain")
.status_line("HTTP/1.1 200 OK")
}
}

impl From<String> for Response {
fn from(value: String) -> Self {
Response::new().body(value.into_bytes()).mime("text/plain").status_line("HTTP/1.1 200 OK")
Response::new()
.body(value.into_bytes())
.mime("text/plain")
.status_line("HTTP/1.1 200 OK")
}
}

impl From<Vec<u8>> for Response {
fn from(value: Vec<u8>) -> Self {
Response::new().body(value).mime("application/octet-stream").status_line("HTTP/1.1 200 OK")
Response::new()
.body(value)
.mime("application/octet-stream")
.status_line("HTTP/1.1 200 OK")
}
}

impl From<()> for Response {
fn from(_value: ()) -> Self {
Response::new()
.body(vec![])
.mime("text/plain")
.status_line("HTTP/1.1 403 Forbidden")
}
}

impl<T: Into<Response>, E: Error + Into<Response>> From<Result<T, E>> for Response {
fn from(value: Result<T, E>) -> Self {
match value {
Ok(body) => body.into(),
Err(e) => e.into(),
}
}
}

impl From<Box<dyn Error>> for Response {
fn from(value: Box<dyn Error>) -> Self {
Response::new()
.body(value.to_string().into_bytes())
.mime("text/plain")
.status_line("HTTP/1.1 403 Forbidden")
}
}

Expand Down
12 changes: 8 additions & 4 deletions tinyhttp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod prelude {

#[cfg(test)]
mod tests {
use std::{sync::OnceLock, thread, time::Duration};
use std::{collections::HashMap, sync::OnceLock, thread, time::Duration};

static HTTP_ENABLED: OnceLock<bool> = OnceLock::new();

Expand Down Expand Up @@ -189,7 +189,7 @@ mod tests {
let headers = body.get_headers();
format!(
"Accept-Encoding: {}",
headers.get("Accept-Encoding").unwrap()
headers.get("accept-encoding").unwrap()
)
}

Expand All @@ -202,9 +202,13 @@ mod tests {
.unwrap()
.wildcard()
.is_none());

let mut headers = HashMap::new();
headers.insert("accept-encoding".to_string(), "gzip".to_string());

let request = Request::new(
b"Hello",
vec!["Accept-Encoding: gzip".to_string()],
b"Hello".to_vec(),
headers,
vec!["GET".to_string(), "/".to_string(), "HTTP/1.1".to_string()],
None,
);
Expand Down

0 comments on commit dba2819

Please sign in to comment.