Skip to content

Commit fc13d1a

Browse files
committed
update feature setup to allow to use with http only
1 parent 2d70581 commit fc13d1a

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ http_req by default uses [rust-native-tls](https://crates.io/crates/native-tls),
1616
which relies on TLS framework provided by OS on Windows and macOS, and OpenSSL
1717
on all other platforms. But it also supports [rustls](https://crates.io/crates/rustls).
1818

19-
## Features
19+
## All features
2020

2121
- Support for both HTTP and HTTPS protocols via [rust-native-tls](https://crates.io/crates/native-tls) (or optionally [rustls](https://crates.io/crates/rustls))
2222
- Creating and sending HTTP requests using the `Request` type (with extended capabilities provided via `RequestMessage` and `Stream`)
@@ -50,6 +50,15 @@ In order to use `http_req` with `rustls` in your project, add the following line
5050
http_req = { version="^0.14", default-features = false, features = ["rust-tls"] }
5151
```
5252

53+
### HTTP only
54+
55+
In order to use `http_req` without any additional features in your project (no HTTPS, no Authentication), add the following lines to `Cargo.toml`:
56+
57+
```toml
58+
[dependencies]
59+
http_req = { version="^0.14", default-features = false }
60+
```
61+
5362
## Example
5463

5564
Basic HTTP GET request

examples/authentication.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
// Container for body of a response.
88
let mut body = Vec::new();
99
// URL of the website.
10-
let uri = Uri::try_from("http://httpbin.org/basic-auth/foo/bar").unwrap();
10+
let uri = Uri::try_from("https://httpbin.org/basic-auth/foo/bar").unwrap();
1111
// Authentication details: username and password.
1212
let auth = Authentication::basic("foo", "bar");
1313

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod error;
2323
pub mod request;
2424
pub mod response;
2525
pub mod stream;
26+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
2627
pub mod tls;
2728
pub mod uri;
2829

src/request.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,11 @@ impl<'a> Request<'a> {
832832
let mut stream = Stream::connect(self.message.uri, self.connect_timeout)?;
833833
stream.set_read_timeout(self.read_timeout)?;
834834
stream.set_write_timeout(self.write_timeout)?;
835-
stream = Stream::try_to_https(stream, self.message.uri, self.root_cert_file_pem)?;
835+
836+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
837+
{
838+
stream = Stream::try_to_https(stream, self.message.uri, self.root_cert_file_pem)?;
839+
}
836840

837841
// Send the request message to the stream.
838842
let request_msg = self.message.parse();

src/stream.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
//! TCP stream
22
3+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
4+
use crate::tls::{self, Conn};
35
use crate::{
46
error::{Error, ParseErr},
5-
tls::{self, Conn},
67
uri::Uri,
78
CR_LF, LF,
89
};
10+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
11+
use std::path::Path;
912
use std::{
1013
io::{self, BufRead, Read, Write},
1114
net::{TcpStream, ToSocketAddrs},
12-
path::Path,
1315
sync::mpsc::{Receiver, RecvTimeoutError, Sender},
1416
time::{Duration, Instant},
1517
};
@@ -21,6 +23,7 @@ const BUF_SIZE: usize = 16 * 1000;
2123
#[derive(Debug)]
2224
pub enum Stream {
2325
Http(TcpStream),
26+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
2427
Https(Conn<TcpStream>),
2528
}
2629

@@ -43,6 +46,7 @@ impl Stream {
4346
/// Checks if `uri` scheme denotes a HTTPS protocol:
4447
/// - If yes, attempts to establish a secure connection
4548
/// - Otherwise, returns the `stream` without any modification
49+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
4650
pub fn try_to_https(
4751
stream: Stream,
4852
uri: &Uri,
@@ -73,6 +77,7 @@ impl Stream {
7377
pub fn set_read_timeout(&mut self, dur: Option<Duration>) -> Result<(), Error> {
7478
match self {
7579
Stream::Http(stream) => Ok(stream.set_read_timeout(dur)?),
80+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
7681
Stream::Https(conn) => Ok(conn.get_mut().set_read_timeout(dur)?),
7782
}
7883
}
@@ -81,6 +86,7 @@ impl Stream {
8186
pub fn set_write_timeout(&mut self, dur: Option<Duration>) -> Result<(), Error> {
8287
match self {
8388
Stream::Http(stream) => Ok(stream.set_write_timeout(dur)?),
89+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
8490
Stream::Https(conn) => Ok(conn.get_mut().set_write_timeout(dur)?),
8591
}
8692
}
@@ -90,6 +96,7 @@ impl Read for Stream {
9096
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
9197
match self {
9298
Stream::Http(stream) => stream.read(buf),
99+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
93100
Stream::Https(stream) => stream.read(buf),
94101
}
95102
}
@@ -99,13 +106,15 @@ impl Write for Stream {
99106
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
100107
match self {
101108
Stream::Http(stream) => stream.write(buf),
109+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
102110
Stream::Https(stream) => stream.write(buf),
103111
}
104112
}
105113

106114
fn flush(&mut self) -> Result<(), io::Error> {
107115
match self {
108116
Stream::Http(stream) => stream.flush(),
117+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
109118
Stream::Https(stream) => stream.flush(),
110119
}
111120
}
@@ -318,6 +327,7 @@ mod tests {
318327
}
319328

320329
#[test]
330+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
321331
fn stream_try_to_https() {
322332
{
323333
let uri = Uri::try_from(URI_S).unwrap();
@@ -362,6 +372,7 @@ mod tests {
362372

363373
assert_eq!(inner_read_timeout, Some(TIMEOUT));
364374
}
375+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
365376
{
366377
let uri = Uri::try_from(URI_S).unwrap();
367378
let mut stream = Stream::connect(&uri, None).unwrap();
@@ -393,6 +404,7 @@ mod tests {
393404

394405
assert_eq!(inner_read_timeout, Some(TIMEOUT));
395406
}
407+
#[cfg(any(feature = "native-tls", feature = "rust-tls"))]
396408
{
397409
let uri = Uri::try_from(URI_S).unwrap();
398410
let mut stream = Stream::connect(&uri, None).unwrap();

0 commit comments

Comments
 (0)