From 8fb41fe7731ce877713f29abc2ff56fb35f06111 Mon Sep 17 00:00:00 2001 From: kozakura913 <98575220+kozakura913@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:25:20 +0900 Subject: [PATCH 1/2] =?UTF-8?q?oembed=E3=83=AD=E3=82=B0=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 203edac..064f169 100644 --- a/src/main.rs +++ b/src/main.rs @@ -390,7 +390,9 @@ async fn get_file( let builder=builder.header(reqwest::header::USER_AGENT,user_agent); let timeout_ms=config.timeout.min(q.response_timeout.unwrap_or(u32::MAX) as u64); let builder=builder.timeout(std::time::Duration::from_millis(timeout_ms)); - builder.send().await.ok() + builder.send().await.map_err(|e|{ + println!("oembed {} {:?}",href,e); + }).ok() }else{ None }; From 84037f436c2203f80bebc500a1aeaa2620a75bcf Mon Sep 17 00:00:00 2001 From: kozakura913 <98575220+kozakura913@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:33:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?oembed=E3=81=A7=E6=8C=87=E5=AE=9A=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=81=9FURL=E3=81=8C=E7=9B=B8=E5=AF=BE=E3=81=A7?= =?UTF-8?q?=E3=81=82=E3=82=8B=E6=99=82=E3=81=AB=E3=83=91=E3=82=B9=E8=A7=A3?= =?UTF-8?q?=E6=B1=BA=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 83 +++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 064f169..3971daf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -384,7 +384,10 @@ async fn get_file( }, Some(("alternate",Some(href),Some("application/json+oembed"))) => { let href=html_escape::decode_html_entities(&href); - let embed_res=if let Ok(href)=urlencoding::decode(&href){ + let embed_res=if let Ok(mut href)=urlencoding::decode(&href){ + if let Some(s)=solve_url(&href,&base_url,&base_url_str,&None,""){ + href=Cow::Owned(s); + } let builder=client.get(href.as_ref()); let user_agent=q.user_agent.as_ref().unwrap_or_else(||&config.user_agent); let builder=builder.header(reqwest::header::USER_AGENT,user_agent); @@ -457,45 +460,6 @@ async fn get_file( if resp.icon.is_none(){ resp.icon=Some(format!("{}/favicon.ico",base_url_str)); } - fn solve_url(icon:&String,base_url:&reqwest::Url,base_url_str:&str,media_proxy:&Option,proxy_filename:&str)->Option{ - let icon=if icon.starts_with("//"){ - Cow::Owned(format!("{}:{}",base_url.scheme(),icon)) - }else if icon.starts_with("/"){ - Cow::Owned(format!("{}{}",base_url_str,icon)) - }else if !icon.starts_with("http"){ - let buf=std::path::PathBuf::from(base_url.path()); - let buf=buf.join(std::path::Path::new(icon)); - if let Some(s)=buf.to_str(){ - let mut path_list=vec![]; - for part in s.split("/"){ - if part.is_empty()||part=="."{ - - }else if part==".."{ - if path_list.len()>0{ - path_list.remove(path_list.len()-1); - } - }else{ - path_list.push(part); - } - } - let mut path_string=base_url_str.to_owned(); - for part in path_list{ - path_string+="/"; - path_string+=part; - } - Cow::Owned(path_string) - }else{ - return None; - } - }else{ - Cow::Borrowed(icon) - }; - if let Some(media_proxy)=&media_proxy{ - Some(format!("{}{}?url={}",media_proxy,proxy_filename,urlencoding::encode(icon.as_str()))) - }else{ - Some(icon.into_owned()) - } - } if let Some(Some(icon))=resp.icon.as_ref().map(|s| solve_url(s,&base_url,&base_url_str,&config.media_proxy,"icon.webp") ){ @@ -536,3 +500,42 @@ async fn load_all(resp: reqwest::Response,content_length_limit:u64)->Result,proxy_filename:&str)->Option{ + let icon=if icon.starts_with("//"){ + Cow::Owned(format!("{}:{}",base_url.scheme(),icon)) + }else if icon.starts_with("/"){ + Cow::Owned(format!("{}{}",base_url_str,icon)) + }else if !icon.starts_with("http"){ + let buf=std::path::PathBuf::from(base_url.path()); + let buf=buf.join(std::path::Path::new(icon)); + if let Some(s)=buf.to_str(){ + let mut path_list=vec![]; + for part in s.split("/"){ + if part.is_empty()||part=="."{ + + }else if part==".."{ + if path_list.len()>0{ + path_list.remove(path_list.len()-1); + } + }else{ + path_list.push(part); + } + } + let mut path_string=base_url_str.to_owned(); + for part in path_list{ + path_string+="/"; + path_string+=part; + } + Cow::Owned(path_string) + }else{ + return None; + } + }else{ + Cow::Borrowed(icon) + }; + if let Some(media_proxy)=&media_proxy{ + Some(format!("{}{}?url={}",media_proxy,proxy_filename,urlencoding::encode(&icon))) + }else{ + Some(icon.into_owned()) + } +}