Skip to content

Commit ffd700f

Browse files
committed
fix(client): use string literals for wreq header lookups + convert Method via str
1 parent e002c1f commit ffd700f

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

src/client.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@ pub async fn canonical_path(path: String, tries: i8) -> Result<Option<String>, S
147147

148148
let res = res.ok_or_else(|| "Unable to make HEAD request to Reddit.".to_string())?;
149149
let status = res.status().as_u16();
150-
let policy_error = res.headers().get(header::RETRY_AFTER).is_some();
150+
// Use string literals: wreq uses http v1.x, hyper::header constants are http v0.2
151+
let policy_error = res.headers().get("retry-after").is_some();
151152

152153
match status {
153154
// If Reddit responds with a 2xx, then the path is already canonical.
154155
200..=299 => Ok(Some(path)),
155156

156157
// If Reddit responds with a 301, then the path is redirected.
157-
301 => match res.headers().get(header::LOCATION) {
158+
301 => match res.headers().get("location") {
158159
Some(val) => {
159160
let Ok(original) = val.to_str() else {
160161
return Err("Unable to decode Location header.".to_string());
@@ -179,7 +180,7 @@ pub async fn canonical_path(path: String, tries: i8) -> Result<Option<String>, S
179180
_ => Ok(
180181
res
181182
.headers()
182-
.get(header::LOCATION)
183+
.get("location")
183184
.map(|val| percent_encode(val.as_bytes(), CONTROLS).to_string().trim_start_matches(REDDIT_URL_BASE).to_string()),
184185
),
185186
}
@@ -283,7 +284,9 @@ fn request(method: Method, path: String, redirect: bool, quarantine: bool, base_
283284
fastrand::shuffle(&mut headers);
284285

285286
async move {
286-
let mut builder = CLIENT.request(method.clone(), &url);
287+
// Convert hyper::Method (http v0.2) to wreq::Method (http v1.x) via string
288+
let wreq_method = wreq::Method::from_bytes(method.as_str().as_bytes()).unwrap_or(wreq::Method::GET);
289+
let mut builder = CLIENT.request(wreq_method, &url);
287290

288291
for (key, value) in &headers {
289292
builder = builder.header(key.as_str(), value.as_str());
@@ -296,7 +299,8 @@ fn request(method: Method, path: String, redirect: bool, quarantine: bool, base_
296299
return resp.into_hyper().await;
297300
}
298301

299-
let location = resp.headers().get(header::LOCATION).map(|v| v.to_str().unwrap_or_default().to_string());
302+
// Use string literal: wreq uses http v1.x, hyper::header constants are http v0.2
303+
let location = resp.headers().get("location").map(|v| v.to_str().unwrap_or_default().to_string());
300304

301305
if location.as_deref() == Some(ALTERNATIVE_REDDIT_URL_BASE) {
302306
return Err("Reddit response was invalid".to_string());

0 commit comments

Comments
 (0)