-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52713fb
commit 745a052
Showing
9 changed files
with
159 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "http-request" | ||
version = "5.3.0" | ||
version = "6.0.0" | ||
edition = "2021" | ||
authors = ["ltpp-universe <[email protected]>"] | ||
license = "MIT" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use super::r#type::HttpResponseText; | ||
use crate::response::{http_response_binary::r#type::HttpResponseBinary, r#trait::HttpResponse}; | ||
|
||
/// Implements the `HttpResponse` trait for `HttpResponseText`. | ||
/// | ||
/// This implementation allows `HttpResponseText` to convert between text and binary | ||
/// representations of HTTP responses. It provides methods for parsing raw responses, as well | ||
/// as accessing text and binary formats. | ||
/// | ||
/// # Associated Types | ||
/// - `OutputText`: Specifies the text representation of an HTTP response (`HttpResponseText`). | ||
/// - `OutputBinary`: Specifies the binary representation of an HTTP response (`HttpResponseBinary`). | ||
impl HttpResponse for HttpResponseText { | ||
type OutputText = HttpResponseText; | ||
type OutputBinary = HttpResponseBinary; | ||
|
||
/// Parses a raw HTTP response from a byte slice and converts it to a `HttpResponseText` instance. | ||
/// | ||
/// This method utilizes the `from` implementation of `HttpResponseBinary` to parse the binary | ||
/// response and then converts it to a text representation. | ||
/// | ||
/// # Parameters | ||
/// - `response`: A byte slice representing the raw HTTP response. | ||
/// | ||
/// # Returns | ||
/// - `Self::OutputText`: A `HttpResponseText` instance with the parsed response. | ||
/// | ||
/// # Panics | ||
/// - This method will panic if the binary parsing or text conversion fails unexpectedly. | ||
fn from(response: &[u8]) -> Self::OutputText | ||
where | ||
Self: Sized, | ||
{ | ||
<HttpResponseBinary as HttpResponse>::from(response).text() | ||
} | ||
|
||
/// Returns a clone of the current text representation of the HTTP response. | ||
/// | ||
/// This method allows for retrieving the current instance as the text representation without | ||
/// modification. | ||
/// | ||
/// # Returns | ||
/// - `Self::OutputText`: A clone of the current instance. | ||
fn text(&self) -> Self::OutputText { | ||
self.clone() | ||
} | ||
|
||
/// Converts the text representation to a binary representation of the HTTP response. | ||
/// | ||
/// This method constructs a new `HttpResponseBinary` instance, copying all fields and | ||
/// converting the body from a string to a byte vector. | ||
/// | ||
/// # Returns | ||
/// - `HttpResponseBinary`: The binary representation of the HTTP response. | ||
fn binary(&self) -> HttpResponseBinary { | ||
HttpResponseBinary { | ||
http_version: self.http_version.clone(), | ||
status_code: self.status_code, | ||
status_text: self.status_text.clone(), | ||
headers: self.headers.clone(), | ||
body: self.body.clone().into_bytes(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod r#impl; | ||
pub mod r#type; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod http_response_binary; | ||
pub mod http_response_text; | ||
pub mod r#trait; | ||
pub mod r#type; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,45 @@ | ||
/// A trait representing common behaviors for HTTP response types. | ||
/// | ||
/// This trait provides a generic `text` method and a `from` method for | ||
/// parsing and transforming HTTP responses. | ||
/// This trait provides methods for transforming an HTTP response into | ||
/// different formats (text and binary) and parsing raw HTTP response data. | ||
/// Implementing types should define how to convert the response into text | ||
/// and binary formats, as well as how to parse raw response data into a | ||
/// structured representation. | ||
/// | ||
/// # Associated Types | ||
/// - `Output`: The type returned by the `from` method. | ||
/// - `OutputText`: The type returned by the `text` method, typically a text-based HTTP response. | ||
/// - `OutputBinary`: The type returned by the `binary` method, typically a binary-based HTTP response. | ||
pub trait HttpResponse { | ||
type OutputText; | ||
type OutputBinary; | ||
|
||
/// Transforms the HTTP response into a text representation. | ||
/// | ||
/// This method converts the body of the HTTP response into a string format. | ||
/// | ||
/// # Returns | ||
/// - `Self::OutputText`: The text representation of the HTTP response, typically a string. | ||
fn text(&self) -> Self::OutputText; | ||
|
||
/// Transforms the HTTP response into a binary representation. | ||
/// | ||
/// This method converts the body of the HTTP response into a byte-based format. | ||
/// | ||
/// # Returns | ||
/// Returns the body of the HTTP response as a string. | ||
fn text(self) -> Self::OutputText; | ||
/// - `Self::OutputBinary`: The binary representation of the HTTP response, typically a byte vector. | ||
fn binary(&self) -> Self::OutputBinary; | ||
|
||
/// Parses a raw HTTP response into the associated type `Output`. | ||
/// | ||
/// This method is responsible for parsing a byte slice representing a raw HTTP response | ||
/// and transforming it into a structured HTTP response object. | ||
/// | ||
/// # Parameters | ||
/// - `response`: A byte slice representing the raw HTTP response. | ||
/// | ||
/// # Returns | ||
/// Returns an instance of the implementing type. | ||
fn from(response: &[u8]) -> Self::OutputBinary; | ||
/// - `Self`: An instance of the implementing type, populated with parsed data. | ||
fn from(response: &[u8]) -> Self | ||
where | ||
Self: Sized; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use super::{ | ||
http_response_binary::r#type::HttpResponseBinary, http_response_text::r#type::HttpResponseText, | ||
}; | ||
use crate::HttpResponse; | ||
|
||
/// A type alias for a boxed dynamic trait object implementing the `HttpResponse` trait. | ||
/// | ||
/// This alias defines a `Response` as a `Box` containing any type that implements the | ||
/// `HttpResponse` trait, with associated types `OutputText` set to `HttpResponseText` | ||
/// and `OutputBinary` set to `HttpResponseBinary`. It allows for flexible handling of | ||
/// HTTP responses that can be either in text or binary format. | ||
pub type BoxHttpResponse = | ||
Box<dyn HttpResponse<OutputText = HttpResponseText, OutputBinary = HttpResponseBinary>>; |