Skip to content

Commit

Permalink
wruster/*: first version of the http client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
manelmontilla committed Dec 22, 2023
1 parent 83615c4 commit f2d3743
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
10 changes: 5 additions & 5 deletions wruster/src/http/client/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::http::Response;
use crate::http::StatusCode;
use crate::router;
use crate::router::HttpHandler;
use crate::*;
use crate::Server;

#[test]
fn client_write_run_post_body() {
Expand All @@ -17,21 +17,21 @@ fn client_write_run_post_body() {
let request = Request::from_body(body, HttpMethod::POST, "/");
let response = c.run(&addr, request).expect("Error running request");

assert_eq!(response.status, http::StatusCode::OK);
assert_eq!(response.status, StatusCode::OK);

server.shutdown().expect("Error shutting down server");
}

#[test]
fn client_keep_alive_reuses_connection() {
fn client_keeps_connection_alive() {
let handler = handler_from_check_body(|content| String::from_utf8_lossy(&content) == "test");
let (server, addr) = run_server(handler, HttpMethod::POST, "/");

let c = Client::new();
let body = Body::from("test", mime::TEXT_PLAIN);
let request = Request::from_body(body, HttpMethod::POST, "/");
let response = c.run(&addr, request).expect("Error running request");
assert_eq!(response.status, http::StatusCode::OK);
assert_eq!(response.status, StatusCode::OK);

// Release the connection.
drop(response);
Expand All @@ -54,7 +54,7 @@ fn client_keep_alive_reuses_connection() {
assert_eq!(dont_exist, true);
drop(connection_pool);

assert_eq!(response.status, http::StatusCode::OK);
assert_eq!(response.status, StatusCode::OK);
drop(response);

// Check the connection is in the connection pool again.
Expand Down
6 changes: 3 additions & 3 deletions wruster/src/http/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub enum HttpError {
/// parsing a Request or a Response. Additional info about the error is
/// stored in the inner String of the variant.
Unknown(String),
/// It's generated when the connection is closed while reading o writing
/// It's generated when the connection is closed while waiting to star
/// reading a request.
ConnectionClosed,
/// It's generated when the maximum allowed time to read/write a request or
/// response has been exceed.
/// It's generated when the maximun allowed time to read a request or
/// write a response has been exceed.
Timeout,
/// It's generated when a syntactic error is found while reading a request.
InvalidRequest(String),
Expand Down
6 changes: 5 additions & 1 deletion wruster/src/http/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ impl Headers {
header.write(to)?
}
}
to.write_all("\r\n".as_bytes()).map_err(HttpError::from)?;

let written = to.write_all("\r\n".as_bytes());
if let Err(err) = written {
return Err(HttpError::Unknown(err.to_string()));
};
Ok(())
}
}
Expand Down
3 changes: 2 additions & 1 deletion wruster/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ pub mod status;
pub use self::status::StatusCode;

mod version;
pub use self::version::Version;

use headers::*;
use mime::Mime;

pub use self::version::Version;
use crate::errors::HttpError;
use crate::errors::HttpError::{ConnectionClosed, InvalidRequest, Timeout, Unknown};

Expand Down
6 changes: 3 additions & 3 deletions wruster/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ mod thread_pool;
pub const DEFAULT_READ_REQUEST_TIMEOUT: time::Duration = time::Duration::from_secs(30);

/// Defines the default max time for a response to be written
pub const DEFAULT_WRITE_RESPONSE_TIMEOUT: time::Duration = time::Duration::from_secs(30);
pub const DEFAULT_WRITE_RESPONSE_TIMEOUT: time::Duration = time::Duration::from_secs(60);

/// Defines the result type returned from the [Server] methods.
/// Defines the result type returned from the [``Server``] methods.
pub type ServerResult = Result<(), Box<dyn StdError>>;

/// Defines the timeouts used in [Server::from_timeouts] method.
Expand All @@ -95,7 +95,7 @@ pub struct Timeouts {
pub write_response_timeout: time::Duration,
}

/// Represents a web server that can be run by passing a [router::Router].
/// Represents a web server that can be run by passing a [`router::Router`].
pub struct Server {
stop: Arc<AtomicBool>,
addr: Option<String>,
Expand Down

0 comments on commit f2d3743

Please sign in to comment.