readme
This commit is contained in:
parent
168b8937eb
commit
732d7d8306
2 changed files with 44 additions and 5 deletions
38
README.md
Normal file
38
README.md
Normal file
|
@ -0,0 +1,38 @@
|
|||
# http
|
||||
|
||||
A rust http parsing library
|
||||
|
||||
### Example
|
||||
|
||||
```rs
|
||||
use http::{request::Request, parse::TryParse, error::HTTPError, response::Response, status, header::HeaderName};
|
||||
|
||||
fn main() -> Result<(), HTTPError> {
|
||||
|
||||
// http request as a string
|
||||
let request_str = "GET / HTTP/1.1\n\
|
||||
Host: www.example.com\n\
|
||||
User-Agent: Mozilla/4.0\n\
|
||||
Accept-language: en\n\
|
||||
\n\
|
||||
";
|
||||
|
||||
// here we set what we want the body to be parsed into if it exists
|
||||
// the body type must implement FromStr and ToString
|
||||
let request = Request::<String>::try_parse(request_str)?; // try parse since it can fail
|
||||
|
||||
let mut response = Response::<String>::new(); // create a new response
|
||||
response
|
||||
.set_body("hello world!") // add a body that is Into<T>
|
||||
.set_status(status::SUCCESS) // set the status
|
||||
.add_header((HeaderName::try_from("Content-Type")?, "text/plain")); // set content-type header
|
||||
|
||||
let response_str = response.to_string(); // get http response in text form
|
||||
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
### License
|
||||
|
||||
This project is licensed under the [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html)
|
|
@ -18,25 +18,26 @@ impl<T: ToString + FromStr> Response<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_version(mut self, version: impl Into<Version>) -> Self {
|
||||
pub fn set_version(&mut self, version: impl Into<Version>) -> &mut Self {
|
||||
self.version = version.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_status(mut self, status: impl Into<Status>) -> Self {
|
||||
pub fn set_status(&mut self, status: impl Into<Status>) -> &mut Self {
|
||||
self.status = status.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_header(mut self, header: impl Into<Header>) -> Self {
|
||||
pub fn add_header(&mut self, header: impl Into<Header>) -> &mut Self {
|
||||
self.headers.insert(header.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_body(mut self, body: T) -> Self {
|
||||
self.body = Some(body);
|
||||
pub fn set_body(&mut self, body: impl Into<T>) -> &mut Self {
|
||||
self.body = Some(body.into());
|
||||
self
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<T: ToString + FromStr> TryParse for Response<T> {
|
||||
|
|
Loading…
Reference in a new issue