lychee/examples/builder/builder.rs
Matthias d61105edbb
Fix parsing error of email addresses with query params (#809)
Email addresses with query parameters often get used in
contact forms on websites. They can also be found in
other documents like Markdown.

A common use-case is to add a subject line to the email
as a parameter e.g. `mailto:mail@example.com?subject="Hello"`.

Previously we handled such cases incorrectly by recognizing
them as files. The reason was that our email parsing was too strict
to allow for that use-case.
With `email_address` we switched to a more permissive parser.

Note that this does not affect the actual address email checking,
as this is still done `check-if-email-exists`, which has more strict
check functionality.
2022-11-05 23:40:33 +01:00

46 lines
1.3 KiB
Rust

use http::header::{self, HeaderMap};
use http::StatusCode;
use lychee_lib::{ClientBuilder, Result};
use regex::RegexSet;
use reqwest::Method;
use std::{collections::HashSet, time::Duration};
#[tokio::main]
#[allow(clippy::trivial_regex)]
async fn main() -> Result<()> {
// Excludes
let excludes = Some(RegexSet::new([r"example"]).unwrap());
// Includes take precedence over excludes
let includes = Some(RegexSet::new([r"example.com"]).unwrap());
// Set custom request headers
let mut headers = HeaderMap::new();
headers.insert(header::ACCEPT, "text/html".parse().unwrap());
let accepted = Some(HashSet::from_iter(vec![
StatusCode::OK,
StatusCode::NO_CONTENT,
]));
let client = ClientBuilder::builder()
.excludes(excludes)
.includes(includes)
.max_redirects(3u8)
.user_agent("custom useragent")
.allow_insecure(true)
.custom_headers(headers)
.method(Method::HEAD)
.timeout(Duration::from_secs(5))
.schemes(HashSet::from_iter(vec![
"http".to_string(),
"https".to_string(),
]))
.accepted(accepted)
.build()
.client()?;
let response = client.check("https://example.com").await?;
dbg!(&response);
assert!(response.status().is_success());
Ok(())
}