Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.12 #67

Merged
merged 5 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "http_req"
version = "0.11.1"
version = "0.12.0"
license = "MIT"
description = "simple and lightweight HTTP client with built-in HTTPS support"
repository = "https://github.com/jayjamesjay/http_req"
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# http_req
> [!CAUTION]
> v0.11.0 introduces major changes to design of `RequestBuilder` and `Request`. Please review [documentation](https://docs.rs/http_req/0.11.0/http_req/) before migrating from previous versions.
> v0.12.0 replaces `RequestBuilder` with `RequestMessage`. Please review [documentation](https://docs.rs/http_req/0.12.0/http_req/) before migrating from previous versions.

[![Rust](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml/badge.svg)](https://github.com/jayjamesjay/http_req/actions/workflows/rust.yml)
[![Crates.io](https://img.shields.io/badge/crates.io-v0.11.1-orange.svg?longCache=true)](https://crates.io/crates/http_req)
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.11.1/http_req/)
[![Crates.io](https://img.shields.io/badge/crates.io-v0.12.0-orange.svg?longCache=true)](https://crates.io/crates/http_req)
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.12.0/http_req/)

Simple and lightweight HTTP client with built-in HTTPS support.
- HTTP and HTTPS via [rust-native-tls](https://github.com/sfackler/rust-native-tls) (or optionally [rus-tls](https://crates.io/crates/rustls))
- Small binary size (less than 0.7 MB for basic GET request)
- Minimal amount of dependencies

## Requirements
http_req by default uses [rust-native-tls](https://github.com/sfackler/rust-native-tls),
Expand All @@ -32,7 +35,7 @@ Take a look at [more examples](https://github.com/jayjamesjay/http_req/tree/mast
In order to use `http_req` with `rustls` in your project, add the following lines to `Cargo.toml`:
```toml
[dependencies]
http_req = {version="^0.11", default-features = false, features = ["rust-tls"]}
http_req = {version="^0.12", default-features = false, features = ["rust-tls"]}
```

## License
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use http_req::{
request::RequestBuilder,
request::RequestMessage,
response::Response,
stream::{self, Stream},
uri::Uri,
Expand All @@ -19,12 +19,12 @@ fn main() {
let mut body = Vec::new();

// Prepares a request message.
let request_msg = RequestBuilder::new(&addr)
let request_msg = RequestMessage::new(&addr)
.header("Connection", "Close")
.parse();

// Connects to a server. Uses information from `addr`.
let mut stream = Stream::new(&addr, Some(Duration::from_secs(60))).unwrap();
let mut stream = Stream::connect(&addr, Some(Duration::from_secs(60))).unwrap();
stream = Stream::try_to_https(stream, &addr, None).unwrap();

// Makes a request to server. Sends the prepared message.
Expand Down
2 changes: 1 addition & 1 deletion src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn is_ascii_space(b: u8) -> bool {
}
}

fn parse_hex_uint(data: Vec<u8>) -> Result<usize, &'static str> {
fn parse_hex_uint<'a>(data: Vec<u8>) -> Result<usize, &'a str> {
let mut n = 0usize;
for (i, v) in data.iter().enumerate() {
if i == 16 {
Expand Down
61 changes: 34 additions & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ impl fmt::Display for ParseErr {
use self::ParseErr::*;

let err = match self {
Utf8(_) => "invalid character",
Int(_) => "cannot parse number",
Invalid => "invalid value",
Empty => "nothing to parse",
StatusErr => "status line contains invalid values",
HeadersErr => "headers contain invalid values",
UriErr => "uri contains invalid characters",
Utf8(_) => "Invalid character",
Int(_) => "Cannot parse number",
Invalid => "Invalid value",
Empty => "Nothing to parse",
StatusErr => "Status line contains invalid values",
HeadersErr => "Headers contain invalid values",
UriErr => "URI contains invalid characters",
};
write!(f, "ParseErr: {}", err)
}
Expand All @@ -57,8 +57,9 @@ impl From<str::Utf8Error> for ParseErr {
pub enum Error {
IO(io::Error),
Parse(ParseErr),
Timeout(mpsc::RecvTimeoutError),
Timeout,
Tls,
Thread,
}

impl error::Error for Error {
Expand All @@ -68,8 +69,7 @@ impl error::Error for Error {
match self {
IO(e) => Some(e),
Parse(e) => Some(e),
Timeout(e) => Some(e),
Tls => None,
Timeout | Tls | Thread => None,
}
}
}
Expand All @@ -81,27 +81,14 @@ impl fmt::Display for Error {
let err = match self {
IO(_) => "IO error",
Parse(err) => return err.fmt(f),
Timeout(_) => "Timeout error",
Timeout => "Timeout error",
Tls => "TLS error",
Thread => "Thread communication error",
};
write!(f, "Error: {}", err)
}
}

#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
fn from(_e: native_tls::Error) -> Self {
Error::Tls
}
}

#[cfg(feature = "native-tls")]
impl<T> From<native_tls::HandshakeError<T>> for Error {
fn from(_e: native_tls::HandshakeError<T>) -> Self {
Error::Tls
}
}

impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::IO(e)
Expand All @@ -121,8 +108,8 @@ impl From<str::Utf8Error> for Error {
}

impl From<mpsc::RecvTimeoutError> for Error {
fn from(e: mpsc::RecvTimeoutError) -> Self {
Error::Timeout(e)
fn from(_e: mpsc::RecvTimeoutError) -> Self {
Error::Timeout
}
}

Expand All @@ -132,3 +119,23 @@ impl From<rustls::Error> for Error {
Error::Tls
}
}

#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
fn from(_e: native_tls::Error) -> Self {
Error::Tls
}
}

#[cfg(feature = "native-tls")]
impl<T> From<native_tls::HandshakeError<T>> for Error {
fn from(_e: native_tls::HandshakeError<T>) -> Self {
Error::Tls
}
}

impl<T> From<mpsc::SendError<T>> for Error {
fn from(_e: mpsc::SendError<T>) -> Self {
Error::Thread
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Simple HTTP client with built-in HTTPS support.
//!
//!
//! By default uses [rust-native-tls](https://github.com/sfackler/rust-native-tls),
//! which relies on TLS framework provided by OS on Windows and macOS, and OpenSSL
//! on all other platforms. But it also supports [rus-tls](https://crates.io/crates/rustls).
Expand Down
Loading