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
1 changed files with 25 additions and 29 deletions

View File

@ -54,25 +54,19 @@ fn request(url: String) -> Boxed<Result<Response<Body>, String>> {
// Build the hyper client from the HTTPS connector.
let client: client::Client<_, hyper::Body> = client::Client::builder().build(https);
let req = |uri: String| {
Request::builder()
let builder = Request::builder()
.method("GET")
.uri(&uri)
.uri(&url)
.header("User-Agent", format!("web:libreddit:{}", env!("CARGO_PKG_VERSION")))
.header("Host", "www.reddit.com")
.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("Connection", "keep-alive")
.body(Body::empty())
.map_err(|e| {
println!("Error building request to send to Reddit: {} - URL: {}", e.to_string(), uri);
e
})
.unwrap_or_default()
};
.body(Body::empty());
async move {
match client.request(req(url)).await {
match builder {
Ok(req) => match client.request(req).await {
Ok(response) => {
if response.status().to_string().starts_with('3') {
request(
@ -89,6 +83,8 @@ fn request(url: String) -> Boxed<Result<Response<Body>, String>> {
}
}
Err(e) => Err(e.to_string()),
},
Err(_) => Err("Post url contains non-ASCII characters".to_string())
}
}
.boxed()
@ -102,8 +98,8 @@ pub async fn json(path: String) -> Result<Value, String> {
// Closure to quickly build errors
let err = |msg: &str, e: String| -> Result<Value, String> {
eprintln!("{} - {}: {}", url, msg, e);
Err(msg.to_string())
// eprintln!("{} - {}: {}", url, msg, e);
Err(format!("{}: {}", msg, e))
};
// 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 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()),