-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from jayjamesjay/v0.11
V0.11
- Loading branch information
Showing
18 changed files
with
1,799 additions
and
1,289 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use http_req::request; | ||
|
||
fn main() { | ||
// Sends a HTTP GET request and processes the response. | ||
let mut body = Vec::new(); | ||
let res = request::get("https://jigsaw.w3.org/HTTP/ChunkedScript", &mut body).unwrap(); | ||
|
||
// Prints details about the response. | ||
println!("Status: {} {}", res.status_code(), res.reason()); | ||
println!("Headers: {}", res.headers()); | ||
//println!("{}", String::from_utf8_lossy(&body)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
use http_req::request; | ||
|
||
fn main() { | ||
//Sends a HTTP HEAD request and processes the response. | ||
// Sends a HTTP HEAD request and processes the response. | ||
let res = request::head("https://www.rust-lang.org/learn").unwrap(); | ||
|
||
//Prints details about the response. | ||
// Prints the details about the response. | ||
println!("Status: {} {}", res.status_code(), res.reason()); | ||
println!("Headers: {}", res.headers()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
use http_req::request; | ||
|
||
fn main() { | ||
//Container for body of a response. | ||
// Container for body of a response. | ||
let mut res_body = Vec::new(); | ||
|
||
//Body of a request. | ||
// Body of a request. | ||
const REQ_BODY: &[u8; 27] = b"field1=value1&field2=value2"; | ||
|
||
//Sends a HTTP POST request and processes the response. | ||
// Sends a HTTP POST request and processes the response. | ||
let res = request::post("https://httpbin.org/post", REQ_BODY, &mut res_body).unwrap(); | ||
|
||
//Prints details about the response. | ||
// Prints details about the response. | ||
println!("Status: {} {}", res.status_code(), res.reason()); | ||
println!("Headers: {}", res.headers()); | ||
println!("{}", String::from_utf8_lossy(&res_body)); | ||
//println!("{}", String::from_utf8_lossy(&res_body)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,46 @@ | ||
use http_req::{request::RequestBuilder, tls, uri::Uri}; | ||
use std::{convert::TryFrom, net::TcpStream}; | ||
use http_req::{ | ||
request::RequestBuilder, | ||
response::Response, | ||
stream::{self, Stream}, | ||
uri::Uri, | ||
}; | ||
use std::{ | ||
convert::TryFrom, | ||
io::{BufReader, Read, Write}, | ||
time::Duration, | ||
}; | ||
|
||
fn main() { | ||
//Parses a URI and assigns it to a variable `addr`. | ||
// Parses a URI and assigns it to a variable `addr`. | ||
let addr: Uri = Uri::try_from("https://www.rust-lang.org/learn").unwrap(); | ||
|
||
//Connects to a remote host. Uses information from `addr`. | ||
let stream = TcpStream::connect((addr.host().unwrap(), addr.corr_port())).unwrap(); | ||
// Containers for a server's response. | ||
let raw_head; | ||
let mut body = Vec::new(); | ||
|
||
//Opens a secure connection over TlsStream. This is required due to use of `https` protocol. | ||
let mut stream = tls::Config::default() | ||
.connect(addr.host().unwrap_or(""), stream) | ||
.unwrap(); | ||
// Prepares a request message. | ||
let request_msg = RequestBuilder::new(&addr) | ||
.header("Connection", "Close") | ||
.parse(); | ||
|
||
//Container for a response's body. | ||
let mut writer = Vec::new(); | ||
// 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(); | ||
|
||
//Adds a header `Connection: Close`. | ||
let response = RequestBuilder::new(&addr) | ||
.header("Connection", "Close") | ||
.send(&mut stream, &mut writer) | ||
.unwrap(); | ||
// Makes a request to server. Sends the prepared message. | ||
stream.write_all(&request_msg).unwrap(); | ||
|
||
// 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); | ||
raw_head = stream::read_head(&mut stream); | ||
stream.read_to_end(&mut body).unwrap(); | ||
|
||
// Parses and processes the response. | ||
let response = Response::from_head(&raw_head).unwrap(); | ||
|
||
// Prints infromation about the response. | ||
println!("Status: {} {}", response.status_code(), response.reason()); | ||
println!("Headers: {}", response.headers()); | ||
//println!("{}", String::from_utf8_lossy(&writer)); | ||
//println!("{}", String::from_utf8_lossy(&body)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.