Skip to content

Commit 5e51bd2

Browse files
fast redirect option
1 parent c1eb8bc commit 5e51bd2

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

src/api/legacy_article.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct LegacyArticleBodyItem {
5454
pub fn fetch_legacy_article(client: &Client, path: &str) -> Result<ureq::Response, ureq::Error> {
5555
let link = format!("https://www.reuters.com{path}");
5656

57-
get(&client, &link).call()
57+
get(client, &link).call()
5858
}
5959

6060
pub fn parse_legacy_article(request: ureq::Response) -> ApiResult<LegacyArticle> {

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,25 @@ fn main() {
241241
}
242242

243243
fn render_api_error(err: &ApiError, path: &str, settings: &Settings) -> rouille::Response {
244+
if settings.fast_redirect {
245+
if let ApiError::Redirect(code, location) = err {
246+
return rouille::Response {
247+
status_code: *code,
248+
headers: vec![
249+
("Location".into(), strip_prefix(location).to_owned().into()),
250+
("Cache-Control".into(), "public, max-age=31536000".into()),
251+
],
252+
data: rouille::ResponseBody::empty(),
253+
upgrade: None,
254+
};
255+
}
256+
}
257+
244258
let (status, title) = match err {
245259
ApiError::Empty | ApiError::External(404, _) => {
246260
(404, "404 - Content not found".to_string())
247261
}
248-
ApiError::Redirect(_, _) => (200, format!("Redirect found")),
262+
ApiError::Redirect(_, _) => (200, "Redirect found".to_string()),
249263
ApiError::External(code, _) => (*code, format!("{code} - External error")),
250264
ApiError::Internal(message) => (500, format!("500 - Internal server error {message}")),
251265
};

src/render/images.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn render_image(thumbnail: &Image, settings: &Settings) -> maud::Markup {
66
let resizer_url = &thumbnail.resizer_url;
77

88
let url = if settings.proxy_images {
9-
if let Some(base_path) = proxy::strip_prefix(&resizer_url) {
9+
if let Some(base_path) = proxy::strip_prefix(resizer_url) {
1010
format!("/proxy/{base_path}")
1111
} else {
1212
return maud::html! {

src/routes/article.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
api::{article::fetch_article_by_url, common::Image, error::ApiResult},
2+
api::{article::fetch_article_by_url, error::ApiResult},
33
client::Client,
44
render::{byline, images::render_image},
55
settings::Settings,

src/routes/settings.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use maud::{html, Markup};
2-
use rouille::{input, post_input, try_or_400, Request, Response};
2+
use rouille::{post_input, try_or_400, Request, Response};
33

44
use crate::{
55
document,
6-
settings::{Settings, EMBED_EMBEDS, EMBED_IMAGES, PROXY_IMAGES, REDIRECT_TIMER},
6+
settings::{Settings, EMBED_EMBEDS, EMBED_IMAGES, FAST_REDIRECT, PROXY_IMAGES, REDIRECT_TIMER},
77
};
88

99
fn render_settings(
1010
embed_images: bool,
1111
embed_embeds: bool,
1212
proxy_images: bool,
13+
fast_redirect: bool,
1314
redirect_timer: u32,
1415
) -> Markup {
1516
document!(
@@ -35,6 +36,11 @@ fn render_settings(
3536
input type="checkbox" id=(PROXY_IMAGES) name=(PROXY_IMAGES) checked[proxy_images] {}
3637
}
3738

39+
label for=(FAST_REDIRECT) {
40+
"Use fast redirect"
41+
input type="checkbox" id=(FAST_REDIRECT) name=(FAST_REDIRECT) checked[fast_redirect] {}
42+
}
43+
3844
label for=(REDIRECT_TIMER) {
3945
"Redirect timer"
4046
input type="number" id=(REDIRECT_TIMER) name=(REDIRECT_TIMER) value=(redirect_timer) {}
@@ -52,6 +58,7 @@ fn store_settings(
5258
embed_images: bool,
5359
embed_embeds: bool,
5460
proxy_images: bool,
61+
fast_redirect: bool,
5562
redirect_timer: u32,
5663
) -> Response {
5764
Response::redirect_303("/settings")
@@ -67,6 +74,10 @@ fn store_settings(
6774
"Set-Cookie",
6875
format!("{PROXY_IMAGES}={}; Path=/; SameSite=Strict", proxy_images),
6976
)
77+
.with_additional_header(
78+
"Set-Cookie",
79+
format!("{FAST_REDIRECT}={}; Path=/; SameSite=Strict", fast_redirect),
80+
)
7081
.with_additional_header(
7182
"Set-Cookie",
7283
format!(
@@ -82,20 +93,23 @@ pub fn handle_settings(request: &Request, settings: &Settings) -> Response {
8293
embed_images: bool,
8394
embed_embeds: bool,
8495
proxy_images: bool,
96+
fast_redirect: bool,
8597
redirect_timer: i32,
8698
}));
8799

88100
store_settings(
89101
settings.embed_images,
90102
settings.embed_embeds,
91103
settings.proxy_images,
104+
settings.fast_redirect,
92105
settings.redirect_timer.clamp(0, 600) as u32,
93106
)
94107
} else {
95108
let page = render_settings(
96109
settings.embed_images,
97110
settings.embed_embeds,
98111
settings.proxy_images,
112+
settings.fast_redirect,
99113
settings.redirect_timer,
100114
);
101115
Response::html(page).with_status_code(200)

src/settings.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ use rouille::{input, Request};
33
pub const EMBED_IMAGES: &str = "embed_images";
44
pub const EMBED_EMBEDS: &str = "embed_embeds";
55
pub const PROXY_IMAGES: &str = "proxy_images";
6+
pub const FAST_REDIRECT: &str = "fast_redirect";
67
pub const REDIRECT_TIMER: &str = "redirect_timer";
78

89
pub struct Settings {
910
pub embed_images: bool,
1011
pub embed_embeds: bool,
1112
pub proxy_images: bool,
13+
pub fast_redirect: bool,
1214
pub redirect_timer: u32,
1315
}
1416

@@ -17,12 +19,14 @@ impl Settings {
1719
let mut embed_images = true;
1820
let mut embed_embeds = true;
1921
let mut proxy_images = false;
22+
let mut fast_redirect = false;
2023
let mut redirect_timer = 5;
2124
for (key, value) in input::cookies(request) {
2225
match key {
2326
EMBED_IMAGES => embed_images = value == "true",
2427
EMBED_EMBEDS => embed_embeds = value == "true",
2528
PROXY_IMAGES => proxy_images = value == "true",
29+
FAST_REDIRECT => fast_redirect = value == "true",
2630
REDIRECT_TIMER => redirect_timer = value.parse().unwrap_or(5),
2731
_ => {}
2832
}
@@ -32,6 +36,7 @@ impl Settings {
3236
embed_images,
3337
embed_embeds,
3438
proxy_images,
39+
fast_redirect,
3540
redirect_timer,
3641
}
3742
}

0 commit comments

Comments
 (0)