Handle failed redirects

This commit is contained in:
spikecodes 2021-01-14 14:56:28 -08:00
parent 5ed122d92c
commit 32b8637c7e
2 changed files with 13 additions and 6 deletions

View File

@ -133,9 +133,10 @@ pub fn prefs(req: HttpRequest) -> Preferences {
// Grab a query param from a url // Grab a query param from a url
pub fn param(path: &str, value: &str) -> String { pub fn param(path: &str, value: &str) -> String {
let url = Url::parse(format!("https://libredd.it/{}", path).as_str()).unwrap(); match Url::parse(format!("https://libredd.it/{}", path).as_str()) {
let pairs: HashMap<_, _> = url.query_pairs().into_owned().collect(); Ok(url) => url.query_pairs().into_owned().collect::<HashMap<_, _>>().get(value).unwrap_or(&String::new()).to_owned(),
pairs.get(value).unwrap_or(&String::new()).to_owned() _ => String::new(),
}
} }
// Parse Cookie value from request // Parse Cookie value from request
@ -360,9 +361,12 @@ pub async fn request(path: &str) -> Result<Value, String> {
// Get first number of response HTTP status code // Get first number of response HTTP status code
match payload.status().to_string().chars().next() { match payload.status().to_string().chars().next() {
// If success // If success
Some('2') => Ok(String::from_utf8(payload.body().limit(20_000_000).await.unwrap().to_vec()).unwrap()), Some('2') => Ok(String::from_utf8(payload.body().limit(20_000_000).await.unwrap_or_default().to_vec()).unwrap_or_default()),
// If redirection // If redirection
Some('3') => Err((true, payload.headers().get("location").unwrap().to_str().unwrap().to_string())), Some('3') => match payload.headers().get("location") {
Some(location) => Err((true, location.to_str().unwrap_or_default().to_string())),
None => Err((false, "Page not found".to_string()))
}
// Otherwise // Otherwise
_ => Err((false, "Page not found".to_string())), _ => Err((false, "Page not found".to_string())),
} }

View File

@ -28,5 +28,8 @@
{%- endmacro %} {%- endmacro %}
{% macro render_flair(flair) -%} {% macro render_flair(flair) -%}
{% for flair_part in flair %}{% if flair_part.flair_part_type == "emoji" %}<span class="emoji" style="background-image:url('{{ flair_part.value }}')"></span>{% else if flair_part.flair_part_type == "text" %}<span>{{ flair_part.value }}</span>{% endif %}{% endfor %} {% for flair_part in flair %}
{% if flair_part.flair_part_type == "emoji" %}<span class="emoji" style="background-image:url('{{ flair_part.value }}')"></span>
{% else if flair_part.flair_part_type == "text" %}<span>{{ flair_part.value }}</span>{% endif %}
{% endfor %}
{%- endmacro %} {%- endmacro %}