Handle non-ASCII post headers

This commit is contained in:
spikecodes 2021-03-17 21:26:06 -07:00
parent a5203fe8dd
commit 0ce2d9054e
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB

View File

@ -54,41 +54,37 @@ fn request(url: String) -> Boxed<Result<Response<Body>, String>> {
// Build the hyper client from the HTTPS connector. // Build the hyper client from the HTTPS connector.
let client: client::Client<_, hyper::Body> = client::Client::builder().build(https); let client: client::Client<_, hyper::Body> = client::Client::builder().build(https);
let req = |uri: String| { let builder = Request::builder()
Request::builder()
.method("GET") .method("GET")
.uri(&uri) .uri(&url)
.header("User-Agent", format!("web:libreddit:{}", env!("CARGO_PKG_VERSION"))) .header("User-Agent", format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")))
.header("Host", "www.reddit.com") .header("Host", "www.reddit.com")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
.header("Accept-Language", "en-US,en;q=0.5") .header("Accept-Language", "en-US,en;q=0.5")
.header("Connection", "keep-alive") .header("Connection", "keep-alive")
.body(Body::empty()) .body(Body::empty());
.map_err(|e| {
println!("Error building request to send to Reddit: {} - URL: {}", e.to_string(), uri);
e
})
.unwrap_or_default()
};
async move { async move {
match client.request(req(url)).await { match builder {
Ok(response) => { Ok(req) => match client.request(req).await {
if response.status().to_string().starts_with('3') { Ok(response) => {
request( if response.status().to_string().starts_with('3') {
response request(
.headers() response
.get("Location") .headers()
.map(|val| val.to_str().unwrap_or_default()) .get("Location")
.unwrap_or_default() .map(|val| val.to_str().unwrap_or_default())
.to_string(), .unwrap_or_default()
) .to_string(),
.await )
} else { .await
Ok(response) } else {
Ok(response)
}
} }
} Err(e) => Err(e.to_string()),
Err(e) => Err(e.to_string()), },
Err(_) => Err("Post url contains non-ASCII characters".to_string())
} }
} }
.boxed() .boxed()
@ -102,8 +98,8 @@ pub async fn json(path: String) -> Result<Value, String> {
// Closure to quickly build errors // Closure to quickly build errors
let err = |msg: &str, e: String| -> Result<Value, String> { let err = |msg: &str, e: String| -> Result<Value, String> {
eprintln!("{} - {}: {}", url, msg, e); // eprintln!("{} - {}: {}", url, msg, e);
Err(msg.to_string()) Err(format!("{}: {}", msg, e))
}; };
// Fetch the url... // Fetch the url...
@ -136,7 +132,7 @@ pub async fn json(path: String) -> Result<Value, String> {
Err(e) => err("Failed to parse page JSON data", e.to_string()), Err(e) => err("Failed to parse page JSON data", e.to_string()),
} }
} }
Err(e) => err("Failed receiving JSON body from Reddit", e.to_string()), Err(e) => err("Failed receiving body from Reddit", e.to_string()),
} }
} }
Err(e) => err("Couldn't send request to Reddit", e.to_string()), Err(e) => err("Couldn't send request to Reddit", e.to_string()),