Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oembedで指定されたURLが相対である時にパス解決する #8

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 46 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,18 @@ 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);
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
};
Expand Down Expand Up @@ -455,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<String>,proxy_filename:&str)->Option<String>{
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")
){
Expand Down Expand Up @@ -534,3 +500,42 @@ async fn load_all(resp: reqwest::Response,content_length_limit:u64)->Result<Vec<
}
Ok(response_bytes)
}
fn solve_url(icon:&str,base_url:&reqwest::Url,base_url_str:&str,media_proxy:&Option<String>,proxy_filename:&str)->Option<String>{
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())
}
}