Skip to content

Commit cfacb9f

Browse files
authored
Merge pull request #63 from jayjamesjay/v0.10
V0.10
2 parents 1c8cba1 + 33e7912 commit cfacb9f

File tree

11 files changed

+261
-176
lines changed

11 files changed

+261
-176
lines changed

Cargo.lock

Lines changed: 42 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "http_req"
3-
version = "0.9.3"
3+
version = "0.10.0"
44
license = "MIT"
55
description = "simple and lightweight HTTP client with built-in HTTPS support"
66
repository = "https://github.com/jayjamesjay/http_req"
@@ -11,24 +11,28 @@ keywords = ["http", "client", "request"]
1111
edition = "2021"
1212

1313
[dependencies]
14-
unicase = "^2.6"
14+
unicase = "^2.7"
1515

1616
[features]
1717
default = ["native-tls"]
18-
rust-tls = ["rustls", "webpki", "webpki-roots"]
18+
rust-tls = ["rustls", "webpki", "webpki-roots", "rustls-pemfile"]
1919

2020
[dependencies.native-tls]
2121
version = "^0.2"
2222
optional = true
2323

2424
[dependencies.rustls]
25-
version = "^0.19"
25+
version = "^0.21"
26+
optional = true
27+
28+
[dependencies.rustls-pemfile]
29+
version = "^1.0"
2630
optional = true
2731

2832
[dependencies.webpki]
29-
version = "^0.21"
33+
version = "^0.22"
3034
optional = true
3135

3236
[dependencies.webpki-roots]
33-
version = "^0.21"
37+
version = "^0.25"
3438
optional = true

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
# http_req
22
[![Rust](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml/badge.svg)](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml)
3-
[![Crates.io](https://img.shields.io/badge/crates.io-v0.9.3-orange.svg?longCache=true)](https://crates.io/crates/http_req)
4-
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.9.3/http_req/)
3+
[![Crates.io](https://img.shields.io/badge/crates.io-v0.10.0-orange.svg?longCache=true)](https://crates.io/crates/http_req)
4+
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.10.0/http_req/)
55

66
Simple and lightweight HTTP client with built-in HTTPS support.
77

88
## Requirements
99
http_req by default uses [rust-native-tls](https://github.com/sfackler/rust-native-tls),
10-
which uses TLS framework provided by OS on Windows and macOS, and OpenSSL
10+
which relies on TLS framework provided by OS on Windows and macOS, and OpenSSL
1111
on all other platforms. But it also supports [rus-tls](https://crates.io/crates/rustls).
1212

1313
## Example
14-
Basic GET request
14+
Basic HTTP GET request
1515
```rust
1616
use http_req::request;
1717

1818
fn main() {
19-
let mut writer = Vec::new(); //container for body of a response
20-
let res = request::get("https://doc.rust-lang.org/", &mut writer).unwrap();
19+
let mut body = Vec::new(); //Container for body of a response.
20+
let res = request::get("https://doc.rust-lang.org/", &mut body).unwrap();
2121

2222
println!("Status: {} {}", res.status_code(), res.reason());
2323
}
2424
```
2525

26+
Take a look at [more examples](https://github.com/jayjamesjay/http_req/tree/master/examples)
27+
2628
## How to use with `rustls`:
2729
In order to use `http_req` with `rustls` in your project, add following lines to `Cargo.toml`:
2830
```toml
2931
[dependencies]
30-
http_req = {version="^0.9", default-features = false, features = ["rust-tls"]}
32+
http_req = {version="^0.10", default-features = false, features = ["rust-tls"]}
3133
```
3234

3335
## License

examples/get.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use http_req::request;
22

33
fn main() {
4-
let mut writer = Vec::new(); //container for body of a response
5-
let res = request::get("https://www.rust-lang.org/learn", &mut writer).unwrap();
4+
//Container for body of a response.
5+
let mut body = Vec::new();
66

7+
//Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
8+
let res = request::get("https://www.rust-lang.org/learn", &mut body).unwrap();
9+
10+
//Prints details about the response.
711
println!("Status: {} {}", res.status_code(), res.reason());
8-
println!("Headers {}", res.headers());
9-
//println!("{}", String::from_utf8_lossy(&writer));
12+
println!("Headers: {}", res.headers());
13+
//println!("{}", String::from_utf8_lossy(&body));
1014
}

examples/head.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use http_req::request;
22

33
fn main() {
4+
//Sends a HTTP HEAD request and processes the response.
45
let res = request::head("https://www.rust-lang.org/learn").unwrap();
56

7+
//Prints details about the response.
68
println!("Status: {} {}", res.status_code(), res.reason());
7-
println!("{:?}", res.headers());
9+
println!("Headers: {}", res.headers());
810
}

examples/post.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use http_req::request;
22

33
fn main() {
4-
let mut writer = Vec::new(); //container for body of a response
5-
const BODY: &[u8; 27] = b"field1=value1&field2=value2";
6-
let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();
4+
//Container for body of a response.
5+
let mut res_body = Vec::new();
76

7+
//Body of a request.
8+
const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2";
9+
10+
//Sends a HTTP POST request and processes the response.
11+
let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap();
12+
13+
//Prints details about the response.
814
println!("Status: {} {}", res.status_code(), res.reason());
9-
println!("Headers {}", res.headers());
10-
//println!("{}", String::from_utf8_lossy(&writer));
15+
println!("Headers: {}", res.headers());
16+
println!("{}", String::from_utf8_lossy(&res_body));
1117
}

examples/request_builder_get.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ use http_req::{request::RequestBuilder, tls, uri::Uri};
22
use std::{convert::TryFrom, net::TcpStream};
33

44
fn main() {
5-
//Parse uri and assign it to variable `addr`
6-
let addr: Uri = Uri::try_from("https://doc.rust-lang.org/").unwrap();
5+
//Parses a URI and assigns it to a variable `addr`.
6+
let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
77

8-
//Connect to remote host
8+
//Connects to a remote host. Uses information from `addr`.
99
let stream = TcpStream::connect((addr.host().unwrap(), addr.corr_port())).unwrap();
1010

11-
//Open secure connection over TlsStream, because of `addr` (https)
11+
//Opens a secure connection over TlsStream. This is required due to use of `https` protocol.
1212
let mut stream = tls::Config::default()
1313
.connect(addr.host().unwrap_or(""), stream)
1414
.unwrap();
1515

16-
//Container for response's body
16+
//Container for a response's body.
1717
let mut writer = Vec::new();
1818

19-
//Add header `Connection: Close`
19+
//Adds a header `Connection: Close`.
2020
let response = RequestBuilder::new(&addr)
2121
.header("Connection", "Close")
2222
.send(&mut stream, &mut writer)
2323
.unwrap();
2424

2525
println!("Status: {} {}", response.status_code(), response.reason());
26+
println!("Headers: {}", response.headers());
2627
//println!("{}", String::from_utf8_lossy(&writer));
2728
}

src/error.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub enum ParseErr {
1010
UriErr,
1111
Invalid,
1212
Empty,
13+
#[cfg(feature = "rust-tls")]
14+
Rustls(rustls::Error),
1315
}
1416

1517
impl error::Error for ParseErr {
@@ -20,6 +22,8 @@ impl error::Error for ParseErr {
2022
Utf8(e) => Some(e),
2123
Int(e) => Some(e),
2224
StatusErr | HeadersErr | UriErr | Invalid | Empty => None,
25+
#[cfg(feature = "rust-tls")]
26+
Rustls(e) => Some(e),
2327
}
2428
}
2529
}
@@ -36,11 +40,20 @@ impl fmt::Display for ParseErr {
3640
StatusErr => "status line contains invalid values",
3741
HeadersErr => "headers contain invalid values",
3842
UriErr => "uri contains invalid characters",
43+
#[cfg(feature = "rust-tls")]
44+
Rustls(_) => "rustls error",
3945
};
4046
write!(f, "ParseErr: {}", err)
4147
}
4248
}
4349

50+
#[cfg(feature = "rust-tls")]
51+
impl From<rustls::Error> for ParseErr {
52+
fn from(e: rustls::Error) -> Self {
53+
ParseErr::Rustls(e)
54+
}
55+
}
56+
4457
impl From<num::ParseIntError> for ParseErr {
4558
fn from(e: num::ParseIntError) -> Self {
4659
ParseErr::Int(e)

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
//!use http_req::request;
88
//!
99
//!fn main() {
10-
//! let mut writer = Vec::new(); //container for body of a response
11-
//! let res = request::get("https://doc.rust-lang.org/", &mut writer).unwrap();
10+
//! //Container for body of a response
11+
//! let mut body = Vec::new();
12+
//! let res = request::get("https://doc.rust-lang.org/", &mut body).unwrap();
1213
//!
1314
//! println!("Status: {} {}", res.status_code(), res.reason());
1415
//!}

0 commit comments

Comments
 (0)