Skip to content

Commit 76f0536

Browse files
committed
improve docs and refactor stream
1 parent 666c218 commit 76f0536

File tree

11 files changed

+787
-738
lines changed

11 files changed

+787
-738
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018-2023 jayjamesjay
3+
Copyright (c) 2018-2024 jayjamesjay
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# http_req
2+
> [!CAUTION]
3+
> 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.
4+
25
[![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.10.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.10.3/http_req/)
6+
[![Crates.io](https://img.shields.io/badge/crates.io-v0.11.0-orange.svg?longCache=true)](https://crates.io/crates/http_req)
7+
[![Docs.rs](https://docs.rs/http_req/badge.svg)](https://docs.rs/http_req/0.11.0/http_req/)
58

69
Simple and lightweight HTTP client with built-in HTTPS support.
710

@@ -29,7 +32,7 @@ Take a look at [more examples](https://github.com/jayjamesjay/http_req/tree/mast
2932
In order to use `http_req` with `rustls` in your project, add the following lines to `Cargo.toml`:
3033
```toml
3134
[dependencies]
32-
http_req = {version="^0.10", default-features = false, features = ["rust-tls"]}
35+
http_req = {version="^0.11", default-features = false, features = ["rust-tls"]}
3336
```
3437

3538
## License

examples/request_builder_get.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
use http_req::{request::RequestBuilder, response::Response, stream::Stream, uri::Uri};
1+
use http_req::{
2+
request::RequestBuilder,
3+
response::Response,
4+
stream::{self, Stream},
5+
uri::Uri,
6+
};
27
use std::{
38
convert::TryFrom,
4-
io::{BufRead, BufReader, Read, Write},
9+
io::{BufReader, Read, Write},
510
time::Duration,
611
};
712

@@ -10,16 +15,14 @@ fn main() {
1015
let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
1116

1217
//Containers for a server's response.
13-
let mut raw_head = Vec::new();
18+
let raw_head;
1419
let mut body = Vec::new();
1520

1621
//Prepares a request message.
1722
let request_msg = RequestBuilder::new(&addr)
1823
.header("Connection", "Close")
1924
.parse();
2025

21-
println!("{:?}", String::from_utf8(request_msg.clone()));
22-
2326
//Connects to a server. Uses information from `addr`.
2427
let mut stream = Stream::new(&addr, Some(Duration::from_secs(60))).unwrap();
2528
stream = Stream::try_to_https(stream, &addr, None).unwrap();
@@ -30,18 +33,7 @@ fn main() {
3033
//Wraps the stream in BufReader to make it easier to read from it.
3134
//Reads a response from the server and saves the head to `raw_head`, and the body to `body`.
3235
let mut stream = BufReader::new(stream);
33-
loop {
34-
match stream.read_until(0xA, &mut raw_head) {
35-
Ok(0) | Err(_) => break,
36-
Ok(len) => {
37-
let full_len = raw_head.len();
38-
39-
if len == 2 && &raw_head[full_len - 2..] == b"\r\n" {
40-
break;
41-
}
42-
}
43-
}
44-
}
36+
raw_head = stream::read_head(&mut stream);
4537
stream.read_to_end(&mut body).unwrap();
4638

4739
//Parses and processes the response.

src/chunked.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
//!implements the wire protocol for HTTP's "chunked" Transfer-Encoding.
2-
///
3-
///It's a Rust version of the reference implementation in [Go][1].
1+
//! implements the wire protocol for HTTP's "chunked" Transfer-Encoding.
2+
use crate::CR_LF;
43
///
5-
///[1]: https://golang.google.cn/src/net/http/internal/chunked.go
4+
/// It's a Rust version of the reference implementation in [Go][1].
5+
///
6+
/// [1]: https://golang.google.cn/src/net/http/internal/chunked.go
67
///
7-
88
use std::io::{self, BufRead, BufReader, Error, ErrorKind, Read};
99

1010
const MAX_LINE_LENGTH: usize = 4096;
11-
const CR_LF: [u8; 2] = [b'\r', b'\n'];
1211

1312
pub struct ChunkReader<R> {
1413
check_end: bool,
@@ -37,7 +36,7 @@ where
3736
}
3837

3938
if let Ok(_) = self.reader.read_exact(&mut footer) {
40-
if footer != CR_LF {
39+
if &footer != CR_LF {
4140
self.err = Some(error_malformed_chunked_encoding());
4241
break;
4342
}

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//!error system
1+
//! error system used around the library.
22
use std::{error, fmt, io, num, str, sync::mpsc};
33

44
#[derive(Debug, PartialEq)]

src/lib.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
//!Simple HTTP client with built-in HTTPS support.
2-
//!Currently it's in heavy development and may frequently change.
1+
//! Simple HTTP client with built-in HTTPS support.
2+
//! Currently it's in heavy development and may frequently change.
33
//!
4-
//!## Example
5-
//!Basic GET request
6-
//!```
7-
//!use http_req::request;
4+
//! ## Example
5+
//! Basic GET request
6+
//! ```
7+
//! use http_req::request;
88
//!
9-
//!fn main() {
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();
9+
//! fn main() {
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();
1313
//!
14-
//! println!("Status: {} {}", res.status_code(), res.reason());
15-
//!}
16-
//!```
17-
pub mod uri;
14+
//! println!("Status: {} {}", res.status_code(), res.reason());
15+
//! }
16+
//! ```
17+
pub mod chunked;
18+
pub mod error;
1819
pub mod request;
1920
pub mod response;
2021
pub mod stream;
21-
pub mod chunked;
2222
pub mod tls;
23-
pub mod error;
23+
pub mod uri;
24+
25+
pub(crate) const CR_LF: &[u8; 2] = b"\r\n";
26+
pub(crate) const LF: u8 = 0xA;

0 commit comments

Comments
 (0)