Skip to content

Commit 3cbb03e

Browse files
committed
improve docs
1 parent 44ab978 commit 3cbb03e

File tree

8 files changed

+49
-40
lines changed

8 files changed

+49
-40
lines changed

examples/chunked.rs

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

33
fn main() {
4-
//Sends a HTTP GET request and processes the response.
4+
// Sends a HTTP GET request and processes the response.
55
let mut body = Vec::new();
66
let res = request::get("https://jigsaw.w3.org/HTTP/ChunkedScript", &mut body).unwrap();
77

8-
//Prints details about the response.
8+
// Prints details about the response.
99
println!("Status: {} {}", res.status_code(), res.reason());
1010
println!("Headers: {}", res.headers());
1111
//println!("{}", String::from_utf8_lossy(&body));

examples/get.rs

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

33
fn main() {
4-
//Container for body of a response.
4+
// Container for body of a response.
55
let mut body = Vec::new();
66

7-
//Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
7+
// Sends a HTTP GET request and processes the response. Saves body of the response to `body` variable.
88
let res = request::get("https://www.rust-lang.org/learn", &mut body).unwrap();
99

1010
//Prints details about the response.

examples/head.rs

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

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

7-
//Prints details about the response.
7+
// Prints the details about the response.
88
println!("Status: {} {}", res.status_code(), res.reason());
99
println!("Headers: {}", res.headers());
1010
}

examples/post.rs

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

33
fn main() {
4-
//Container for body of a response.
4+
// Container for body of a response.
55
let mut res_body = Vec::new();
66

7-
//Body of a request.
7+
// Body of a request.
88
const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2";
99

10-
//Sends a HTTP POST request and processes the response.
10+
// Sends a HTTP POST request and processes the response.
1111
let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap();
1212

13-
//Prints details about the response.
13+
// Prints details about the response.
1414
println!("Status: {} {}", res.status_code(), res.reason());
1515
println!("Headers: {}", res.headers());
16-
println!("{}", String::from_utf8_lossy(&res_body));
16+
//println!("{}", String::from_utf8_lossy(&res_body));
1717
}

examples/request_builder_get.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ use std::{
1111
};
1212

1313
fn main() {
14-
//Parses a URI and assigns it to a variable `addr`.
14+
// Parses a URI and assigns it to a variable `addr`.
1515
let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
1616

17-
//Containers for a server's response.
17+
// Containers for a server's response.
1818
let raw_head;
1919
let mut body = Vec::new();
2020

21-
//Prepares a request message.
21+
// Prepares a request message.
2222
let request_msg = RequestBuilder::new(&addr)
2323
.header("Connection", "Close")
2424
.parse();
2525

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

30-
//Makes a request to server - sends a prepared message.
30+
// Makes a request to server. Sends the prepared message.
3131
stream.write_all(&request_msg).unwrap();
3232

33-
//Wraps the stream in BufReader to make it easier to read from it.
34-
//Reads a response from the server and saves the head to `raw_head`, and the body to `body`.
33+
// Wraps the stream in BufReader to make it easier to read from it.
34+
// Reads a response from the server and saves the head to `raw_head`, and the body to `body`.
3535
let mut stream = BufReader::new(stream);
3636
raw_head = stream::read_head(&mut stream);
3737
stream.read_to_end(&mut body).unwrap();
3838

39-
//Parses and processes the response.
39+
// Parses and processes the response.
4040
let response = Response::from_head(&raw_head).unwrap();
4141

42-
//Prints infromation about the response.
42+
// Prints infromation about the response.
4343
println!("Status: {} {}", response.status_code(), response.reason());
4444
println!("Headers: {}", response.headers());
4545
//println!("{}", String::from_utf8_lossy(&body));

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Simple HTTP client with built-in HTTPS support.
2-
//! Currently it's in heavy development and may frequently change.
2+
//!
3+
//! By default uses [rust-native-tls](https://github.com/sfackler/rust-native-tls),
4+
//! which relies on TLS framework provided by OS on Windows and macOS, and OpenSSL
5+
//! on all other platforms. But it also supports [rus-tls](https://crates.io/crates/rustls).
36
//!
47
//! ## Example
58
//! Basic GET request

src/request.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ pub enum Method {
2626
POST,
2727
PUT,
2828
DELETE,
29+
CONNECT,
2930
OPTIONS,
31+
TRACE,
3032
PATCH,
3133
}
3234

@@ -40,7 +42,9 @@ impl fmt::Display for Method {
4042
POST => "POST",
4143
PUT => "PUT",
4244
DELETE => "DELETE",
45+
CONNECT => "CONNECT",
4346
OPTIONS => "OPTIONS",
47+
TRACE => "TRACE",
4448
PATCH => "PATCH",
4549
};
4650

@@ -300,10 +304,9 @@ impl<'a> Request<'a> {
300304
/// use http_req::{request::Request, uri::Uri};
301305
/// use std::convert::TryFrom;
302306
///
303-
/// let mut writer = Vec::new();
304307
/// let uri: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
305308
///
306-
/// let response = Request::new(&uri).send(&mut writer).unwrap();;
309+
/// let request = Request::new(&uri);
307310
/// ```
308311
pub fn new(uri: &'a Uri) -> Request<'a> {
309312
let mut builder = RequestBuilder::new(&uri);
@@ -328,7 +331,7 @@ impl<'a> Request<'a> {
328331
///
329332
/// let uri: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
330333
///
331-
/// let response = Request::new(&uri)
334+
/// let request = Request::new(&uri)
332335
/// .method(Method::HEAD);
333336
/// ```
334337
pub fn method<T>(&mut self, method: T) -> &mut Self
@@ -348,7 +351,7 @@ impl<'a> Request<'a> {
348351
///
349352
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
350353
///
351-
/// let response = Request::new(&uri)
354+
/// let request = Request::new(&uri)
352355
/// .version(HttpVersion::Http10);
353356
/// ```
354357
@@ -375,7 +378,7 @@ impl<'a> Request<'a> {
375378
/// headers.insert("Host", "rust-lang.org");
376379
/// headers.insert("Connection", "Close");
377380
///
378-
/// let response = Request::new(&uri)
381+
/// let request = Request::new(&uri)
379382
/// .headers(headers);
380383
/// ```
381384
pub fn headers<T>(&mut self, headers: T) -> &mut Self
@@ -395,7 +398,7 @@ impl<'a> Request<'a> {
395398
///
396399
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
397400
///
398-
/// let response = Request::new(&uri)
401+
/// let request = Request::new(&uri)
399402
/// .header("Accept-Language", "en-US");
400403
/// ```
401404
pub fn header<T, U>(&mut self, key: &T, val: &U) -> &mut Self
@@ -417,7 +420,7 @@ impl<'a> Request<'a> {
417420
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
418421
/// const body: &[u8; 27] = b"field1=value1&field2=value2";
419422
///
420-
/// let response = Request::new(&uri)
423+
/// let request = Request::new(&uri)
421424
/// .method(Method::POST)
422425
/// .header("Content-Length", &body.len())
423426
/// .body(body);
@@ -446,7 +449,7 @@ impl<'a> Request<'a> {
446449
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
447450
/// const time: Option<Duration> = Some(Duration::from_secs(10));
448451
///
449-
/// let response = Request::new(&uri)
452+
/// let request = Request::new(&uri)
450453
/// .connect_timeout(time);
451454
/// ```
452455
pub fn connect_timeout<T>(&mut self, timeout: Option<T>) -> &mut Self
@@ -472,7 +475,7 @@ impl<'a> Request<'a> {
472475
/// let uri: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
473476
/// const time: Option<Duration> = Some(Duration::from_secs(15));
474477
///
475-
/// let response = Request::new(&uri)
478+
/// let request = Request::new(&uri)
476479
/// .read_timeout(time);
477480
/// ```
478481
pub fn read_timeout<T>(&mut self, timeout: Option<T>) -> &mut Self
@@ -498,7 +501,7 @@ impl<'a> Request<'a> {
498501
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
499502
/// const time: Option<Duration> = Some(Duration::from_secs(5));
500503
///
501-
/// let response = Request::new(&uri)
504+
/// let request = Request::new(&uri)
502505
/// .write_timeout(time);
503506
/// ```
504507
pub fn write_timeout<T>(&mut self, timeout: Option<T>) -> &mut Self
@@ -520,7 +523,7 @@ impl<'a> Request<'a> {
520523
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
521524
/// const time: Duration = Duration::from_secs(5);
522525
///
523-
/// let response = Request::new(&uri)
526+
/// let request = Request::new(&uri)
524527
/// .timeout(time);
525528
/// ```
526529
pub fn timeout<T>(&mut self, timeout: T) -> &mut Self
@@ -541,7 +544,7 @@ impl<'a> Request<'a> {
541544
/// let uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap();
542545
/// let path = Path::new("./foo/bar.txt");
543546
///
544-
/// let response = Request::new(&uri)
547+
/// let request = Request::new(&uri)
545548
/// .root_cert_file_pem(&path);
546549
/// ```
547550
pub fn root_cert_file_pem(&mut self, file_path: &'a Path) -> &mut Self {

src/stream.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,14 @@ where
226226
/// Exexcutes a function in a loop until operation is completed or deadline is exceeded.
227227
///
228228
/// It checks if a timeout was exceeded every iteration, therefore it limits
229-
/// how many time a specific function can be called before deadline.
230-
/// However a deadline may be exceeded if a single function call takes too much time.
231-
///
232-
/// Function `func` needs to return `true` when the operation is complete.
233-
///
229+
/// how many time a specific function can be called before deadline.
230+
/// For the `execute_with_deadline` to meet the deadline, each call
231+
/// to `func` needs finish before the deadline.
232+
///
233+
/// Key information about function `func`:
234+
/// - is provided with information about remaining time
235+
/// - must ensure that its execution will not take more time than specified in `remaining_time`
236+
/// - needs to return `true` when the operation is complete
234237
pub fn execute_with_deadline<F>(deadline: Instant, mut func: F)
235238
where
236239
F: FnMut(Duration) -> bool,
@@ -247,8 +250,8 @@ where
247250

248251
/// Reads the head of HTTP response from `reader`.
249252
///
250-
/// Reads from `reader` (line by line) until a blank line is found
251-
/// indicating that all meta-information for the request has been sent.
253+
/// Reads from `reader` (line by line) until a blank line is identified,
254+
/// which indicates that all meta-information has been read,
252255
pub fn read_head<B>(reader: &mut B) -> Vec<u8>
253256
where
254257
B: BufRead,

0 commit comments

Comments
 (0)