Skip to content

Commit

Permalink
Fix multibyte request failed (#24)
Browse files Browse the repository at this point in the history
* fix multibyte request failed

* remove unused variable

* add test
  • Loading branch information
takurinton authored Nov 13, 2024
1 parent 9829954 commit a58828f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
48 changes: 48 additions & 0 deletions src/utils/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pub fn encode(input: &str) -> String {
let mut encoded = String::new();

for byte in input.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
encoded.push(byte as char);
}
// 他の文字は %XX の形式でエンコード
_ => encoded.push_str(&format!("%{:02X}", byte)),
}
}

encoded
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_encode() {
assert_eq!(encode("abc"), "abc");
assert_eq!(encode("あいう"), "%E3%81%82%E3%81%84%E3%81%86");
assert_eq!(encode("a b c"), "a%20b%20c");
assert_eq!(encode("a+b+c"), "a%2Bb%2Bc");
assert_eq!(encode("a=b=c"), "a%3Db%3Dc");
assert_eq!(encode("a:b:c"), "a%3Ab%3Ac");
assert_eq!(encode("a~b~c"), "a~b~c");
assert_eq!(encode("a!b!c"), "a%21b%21c");
assert_eq!(encode("a*b*c"), "a%2Ab%2Ac");
assert_eq!(encode("a'b'c"), "a%27b%27c");
assert_eq!(encode("a(c)c"), "a%28c%29c");
assert_eq!(encode("a)c)c"), "a%29c%29c");
assert_eq!(encode("a;c;c"), "a%3Bc%3Bc");
assert_eq!(encode("a:c:c"), "a%3Ac%3Ac");
assert_eq!(encode("a,d,c"), "a%2Cd%2Cc");
assert_eq!(encode("a/d/c"), "a%2Fd%2Fc");
assert_eq!(encode("a\\d\\c"), "a%5Cd%5Cc");
assert_eq!(encode("a?d?c"), "a%3Fd%3Fc");
assert_eq!(encode("a#d#c"), "a%23d%23c");
assert_eq!(encode("a&d&c"), "a%26d%26c");
assert_eq!(encode("a=d=c"), "a%3Dd%3Dc");
assert_eq!(encode("a@d@c"), "a%40d%40c");
assert_eq!(encode("a$d$c"), "a%24d%24c");
assert_eq!(encode("a`d`c"), "a%60d%60c");
}
}
15 changes: 12 additions & 3 deletions src/utils/google_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use serde::Deserialize;
use std::env;
use tracing::error;

use crate::http::client::{HttpClient, StatusCode};
use crate::{
http::client::{HttpClient, StatusCode},
utils::encode::encode,
};

#[derive(Deserialize, Debug)]
pub struct GoogleItem {
Expand Down Expand Up @@ -48,20 +51,26 @@ pub async fn google_search(
};

let url = format!(
"https://www.googleapis.com/customsearch/v1?cx={search_engine_id}&key={api_key}&hl=ja{search_type}&q={q}{site}");
"https://www.googleapis.com/customsearch/v1?cx={search_engine_id}&key={api_key}&hl=ja{search_type}&q={}{site}", encode(q));

let client = HttpClient::new();
let result = match client.get(&url).await {
Ok(result) => match result.status_code {
StatusCode::OK => result,
StatusCode::BadRequest => return Err("リクエストが不正です。".to_string()),
StatusCode::Unauthorized => return Err("認証に失敗しました。".to_string()),
StatusCode::Forbidden => return Err("アクセス権限がありません。".to_string()),
StatusCode::NotFound => return Err("リソースが見つかりませんでした。".to_string()),
StatusCode::TooManyRequests => return Err(
"Google Search API へのリクエスト超過です。しばらくしてからやり直してください。"
.to_string(),
),
_ => return Err("予期しないエラーが発生しました。".to_string()),
status => {
return Err(
format!("予期しないエラーが発生しました。status: {}", status as u16)
.to_string(),
);
}
},
Err(_) => return Err("Google 検索でエラーが発生しました。".to_string()),
};
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod encode;
pub mod fetch_atproto;
pub mod fetch_chatgpt;
pub mod fetch_rss_feed;
Expand Down

0 comments on commit a58828f

Please sign in to comment.