Handle non-ASCII post headers
This commit is contained in:
parent
a5203fe8dd
commit
0ce2d9054e
@ -54,25 +54,19 @@ 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(req) => match client.request(req).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
if response.status().to_string().starts_with('3') {
|
if response.status().to_string().starts_with('3') {
|
||||||
request(
|
request(
|
||||||
@ -89,6 +83,8 @@ fn request(url: String) -> Boxed<Result<Response<Body>, String>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
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()),
|
||||||
|
Loading…
Reference in New Issue
Block a user