Skip to content

Commit

Permalink
Update reqwest to v0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
samford committed Dec 26, 2019
1 parent 2773ca8 commit 411dc56
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 206 deletions.
516 changes: 328 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/link_checker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Vincent Prouillet <prouillet.vincent@gmail.com>"]
edition = "2018"

[dependencies]
reqwest = "0.9"
reqwest = { git = "https://github.com/seanmonstar/reqwest", features = ["blocking"] }
lazy_static = "1"

config = { path = "../config" }
Expand Down
23 changes: 14 additions & 9 deletions components/link_checker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lazy_static::lazy_static;
use reqwest::header::{HeaderMap, ACCEPT};
use reqwest::StatusCode;
use reqwest::{blocking::Client, StatusCode};

use config::LinkChecker;
use errors::Result;
Expand Down Expand Up @@ -59,14 +59,20 @@ pub fn check_url(url: &str, config: &LinkChecker) -> LinkResult {
headers.insert(ACCEPT, "text/html".parse().unwrap());
headers.append(ACCEPT, "*/*".parse().unwrap());

let client = reqwest::Client::new();
let client = Client::new();

let check_anchor = !config.skip_anchor_prefixes.iter().any(|prefix| url.starts_with(prefix));

// Need to actually do the link checking
let res = match client.get(url).headers(headers).send() {
Ok(ref mut response) if check_anchor && has_anchor(url) => {
match check_page_for_anchor(url, response.text()) {
let body = {
let mut buf: Vec<u8> = vec![];
response.copy_to(&mut buf).unwrap();
String::from_utf8(buf).unwrap()
};

match check_page_for_anchor(url, body) {
Ok(_) => LinkResult { code: Some(response.status()), error: None },
Err(e) => LinkResult { code: None, error: Some(e.to_string()) },
}
Expand Down Expand Up @@ -107,8 +113,7 @@ fn has_anchor(url: &str) -> bool {
}
}

fn check_page_for_anchor(url: &str, body: reqwest::Result<String>) -> Result<()> {
let body = body.unwrap();
fn check_page_for_anchor(url: &str, body: String) -> Result<()> {
let index = url.find('#').unwrap();
let anchor = url.get(index + 1..).unwrap();
let checks: [String; 4] = [
Expand Down Expand Up @@ -245,31 +250,31 @@ mod tests {
fn can_validate_anchors() {
let url = "https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect";
let body = r#"<body><h3 id="method.collect">collect</h3></body>"#.to_string();
let res = check_page_for_anchor(url, Ok(body));
let res = check_page_for_anchor(url, body);
assert!(res.is_ok());
}

#[test]
fn can_validate_anchors_with_other_quotes() {
let url = "https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect";
let body = r#"<body><h3 id="method.collect">collect</h3></body>"#.to_string();
let res = check_page_for_anchor(url, Ok(body));
let res = check_page_for_anchor(url, body);
assert!(res.is_ok());
}

#[test]
fn can_validate_anchors_with_name_attr() {
let url = "https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect";
let body = r#"<body><h3 name="method.collect">collect</h3></body>"#.to_string();
let res = check_page_for_anchor(url, Ok(body));
let res = check_page_for_anchor(url, body);
assert!(res.is_ok());
}

#[test]
fn can_fail_when_anchor_not_found() {
let url = "https://doc.rust-lang.org/std/iter/trait.Iterator.html#me";
let body = r#"<body><h3 id="method.collect">collect</h3></body>"#.to_string();
let res = check_page_for_anchor(url, Ok(body));
let res = check_page_for_anchor(url, body);
assert!(res.is_err());
}

Expand Down
12 changes: 7 additions & 5 deletions components/rendering/benches/all.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(test)]
extern crate test;

use std::collections::HashMap;
use std::path::Path;

use config::Config;
use front_matter::InsertAnchor;
Expand Down Expand Up @@ -85,7 +87,7 @@ fn bench_render_content_with_highlighting(b: &mut test::Bencher) {
let permalinks_ctx = HashMap::new();
let config = Config::default();
let context =
RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new(""), InsertAnchor::None);
RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
b.iter(|| render_content(CONTENT, &context).unwrap());
}

Expand All @@ -97,7 +99,7 @@ fn bench_render_content_without_highlighting(b: &mut test::Bencher) {
let mut config = Config::default();
config.highlight_code = false;
let context =
RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new(""), InsertAnchor::None);
RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);
b.iter(|| render_content(CONTENT, &context).unwrap());
}

Expand All @@ -109,7 +111,7 @@ fn bench_render_content_no_shortcode(b: &mut test::Bencher) {
config.highlight_code = false;
let permalinks_ctx = HashMap::new();
let context =
RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new(""), InsertAnchor::None);
RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);

b.iter(|| render_content(&content2, &context).unwrap());
}
Expand All @@ -121,7 +123,7 @@ fn bench_render_shortcodes_one_present(b: &mut test::Bencher) {
let config = Config::default();
let permalinks_ctx = HashMap::new();
let context =
RenderContext::new(&tera, &config, "", &permalinks_ctx, Path::new(""), InsertAnchor::None);
RenderContext::new(&tera, &config, "", &permalinks_ctx, InsertAnchor::None);

b.iter(|| render_shortcodes(CONTENT, &context));
}
3 changes: 3 additions & 0 deletions components/site/benches/load.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Benchmarking loading/markdown rendering of generated sites of various sizes
#![feature(test)]
extern crate test;

use std::env;

use site::Site;
Expand Down
3 changes: 3 additions & 0 deletions components/site/benches/site.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![feature(test)]
extern crate test;

use std::env;

use library::Paginator;
Expand Down
2 changes: 1 addition & 1 deletion components/templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ toml = "0.5"
csv = "1"
image = "0.22"
serde_json = "1.0"
reqwest = "0.9"
reqwest = { git = "https://github.com/seanmonstar/reqwest", features = ["blocking"] }
url = "2"

errors = { path = "../errors" }
Expand Down
4 changes: 2 additions & 2 deletions components/templates/src/global_fns/load_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use utils::de::fix_toml_dates;
use utils::fs::{get_file_time, is_path_in_directory, read_file};

use reqwest::{header, Client};
use reqwest::{blocking::Client, header};
use std::collections::hash_map::DefaultHasher;
use std::fmt;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -199,7 +199,7 @@ impl TeraFn for LoadData {
let data = match data_source {
DataSource::Path(path) => read_data_file(&self.base_path, path),
DataSource::Url(url) => {
let mut response = response_client
let response = response_client
.get(url.as_str())
.header(header::ACCEPT, file_format.as_accept_header())
.send()
Expand Down

0 comments on commit 411dc56

Please sign in to comment.