Skip to content

Commit c97c3e5

Browse files
committed
fix image api
1 parent a10a2da commit c97c3e5

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ on:
44
push:
55
pull_request:
66
branches: ['main']
7-
7+
88
jobs:
9-
deploy:
9+
ci:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4

src/proxy/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub async fn image_handler(req: Request) -> Result<Response> {
5252
if let Ok(url) = format!("https://{}{}", domain, req.url()?.path()).parse::<Uri>() {
5353
return handler(req, url).await;
5454
}
55-
return Response::error("Invalid URL",400);
55+
return Response::error( "Not Found",404);
5656
}
5757

5858
pub async fn handler(mut req: Request, uri: Uri) -> Result<Response> {

src/proxy/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ async fn get_bufsize(cx: &RouteContext<()>) -> usize {
4949
}
5050

5151
pub async fn handler(req: Request, cx: RouteContext<()>) -> Result<Response> {
52-
console_debug!("Request url: {:?}", req.url().unwrap());
53-
5452
let pre = PREFIXTJ.get_or_init(|| async {
5553
get_prefix_trojan(&cx).await
5654
}).await;
@@ -61,16 +59,25 @@ pub async fn handler(req: Request, cx: RouteContext<()>) -> Result<Response> {
6159
let reg = APIREGEX.get_or_init(|| async {
6260
get_regex().await
6361
}).await;
64-
62+
63+
let query = match req.url() {
64+
Ok(url) => url.query().unwrap_or("").to_string(),
65+
Err(_) => "".to_string(),
66+
};
6567
if let Some(captures) = reg.captures(req.path().as_str()) {
6668
let domain = captures.name("domain").map_or("", |x| x.as_str());
6769
let path = captures.name("path").map_or("", |x| x.as_str());
68-
let query = captures.name("query").map_or("", |x| x.as_str());
6970

7071
if !domain.contains('.') {
7172
return Response::error("Not Found", 404);
7273
}
73-
if let Ok(url) = format!("https://{}{}{}", domain, path, query).parse::<Uri>() {
74+
let mut full_url = format!("https://{}{}", domain, path);
75+
if !query.is_empty() {
76+
full_url.push('?');
77+
full_url.push_str(&query);
78+
}
79+
80+
if let Ok(url) = full_url.parse::<Uri>() {
7481
return api::handler(req, url).await;
7582
}
7683
}

0 commit comments

Comments
 (0)