Skip to content

Commit

Permalink
improve docs and refactor stream
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjamesjay committed Jun 25, 2024
1 parent 666c218 commit 76f0536
Show file tree
Hide file tree
Showing 11 changed files with 787 additions and 738 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018-2023 jayjamesjay
Copyright (c) 2018-2024 jayjamesjay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# 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.
[![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.10.3-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.10.3/http_req/)
[![Crates.io](https://img.shields.io/badge/crates.io-v0.11.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.11.0/http_req/)

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

Expand Down Expand Up @@ -29,7 +32,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.10", default-features = false, features = ["rust-tls"]}
http_req = {version="^0.11", default-features = false, features = ["rust-tls"]}
```

## License
Expand Down
26 changes: 9 additions & 17 deletions examples/request_builder_get.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use http_req::{request::RequestBuilder, response::Response, stream::Stream, uri::Uri};
use http_req::{
request::RequestBuilder,
response::Response,
stream::{self, Stream},
uri::Uri,
};
use std::{
convert::TryFrom,
io::{BufRead, BufReader, Read, Write},
io::{BufReader, Read, Write},
time::Duration,
};

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

//Containers for a server's response.
let mut raw_head = Vec::new();
let raw_head;
let mut body = Vec::new();

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

println!("{:?}", String::from_utf8(request_msg.clone()));

//Connects to a server. Uses information from `addr`.
let mut stream = Stream::new(&addr, Some(Duration::from_secs(60))).unwrap();
stream = Stream::try_to_https(stream, &addr, None).unwrap();
Expand All @@ -30,18 +33,7 @@ fn main() {
//Wraps the stream in BufReader to make it easier to read from it.
//Reads a response from the server and saves the head to `raw_head`, and the body to `body`.
let mut stream = BufReader::new(stream);
loop {
match stream.read_until(0xA, &mut raw_head) {
Ok(0) | Err(_) => break,
Ok(len) => {
let full_len = raw_head.len();

if len == 2 && &raw_head[full_len - 2..] == b"\r\n" {
break;
}
}
}
}
raw_head = stream::read_head(&mut stream);
stream.read_to_end(&mut body).unwrap();

//Parses and processes the response.
Expand Down
13 changes: 6 additions & 7 deletions src/chunked.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//!implements the wire protocol for HTTP's "chunked" Transfer-Encoding.
///
///It's a Rust version of the reference implementation in [Go][1].
//! implements the wire protocol for HTTP's "chunked" Transfer-Encoding.
use crate::CR_LF;
///
///[1]: https://golang.google.cn/src/net/http/internal/chunked.go
/// It's a Rust version of the reference implementation in [Go][1].
///
/// [1]: https://golang.google.cn/src/net/http/internal/chunked.go
///

use std::io::{self, BufRead, BufReader, Error, ErrorKind, Read};

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

pub struct ChunkReader<R> {
check_end: bool,
Expand Down Expand Up @@ -37,7 +36,7 @@ where
}

if let Ok(_) = self.reader.read_exact(&mut footer) {
if footer != CR_LF {
if &footer != CR_LF {
self.err = Some(error_malformed_chunked_encoding());
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//!error system
//! error system used around the library.
use std::{error, fmt, io, num, str, sync::mpsc};

#[derive(Debug, PartialEq)]
Expand Down
35 changes: 19 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
//!Simple HTTP client with built-in HTTPS support.
//!Currently it's in heavy development and may frequently change.
//! Simple HTTP client with built-in HTTPS support.
//! Currently it's in heavy development and may frequently change.
//!
//!## Example
//!Basic GET request
//!```
//!use http_req::request;
//! ## Example
//! Basic GET request
//! ```
//! use http_req::request;
//!
//!fn main() {
//! //Container for body of a response
//! let mut body = Vec::new();
//! let res = request::get("https://doc.rust-lang.org/", &mut body).unwrap();
//! fn main() {
//! //Container for body of a response
//! let mut body = Vec::new();
//! let res = request::get("https://doc.rust-lang.org/", &mut body).unwrap();
//!
//! println!("Status: {} {}", res.status_code(), res.reason());
//!}
//!```
pub mod uri;
//! println!("Status: {} {}", res.status_code(), res.reason());
//! }
//! ```
pub mod chunked;
pub mod error;
pub mod request;
pub mod response;
pub mod stream;
pub mod chunked;
pub mod tls;
pub mod error;
pub mod uri;

pub(crate) const CR_LF: &[u8; 2] = b"\r\n";
pub(crate) const LF: u8 = 0xA;
Loading

0 comments on commit 76f0536

Please sign in to comment.