Skip to content

Commit

Permalink
feat: parse content-type with \"mime\"
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jun 8, 2024
1 parent 1e68e2c commit d07ea6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ local-fake-dns = ["local", "shadowsocks-service/local-fake-dns"]
local-online-config = [
"local",
"reqwest",
"mime",
"shadowsocks-service/local-online-config",
]

Expand Down Expand Up @@ -224,6 +225,7 @@ serde = { version = "1.0", features = ["derive"] }
json5 = "0.4"
thiserror = "1.0"
base64 = "0.22"
mime = { version = "0.3", optional = true }

clap = { version = "4.5", features = ["wrap_help", "suggestions"] }
cfg-if = "1"
Expand Down
37 changes: 24 additions & 13 deletions src/service/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,16 +1097,12 @@ async fn get_online_config_servers(
online_config_url: &str,
) -> Result<Vec<ServerInstanceConfig>, Box<dyn std::error::Error>> {
use log::warn;
use mime::Mime;
use reqwest::{redirect::Policy, Client};

#[inline]
async fn get_online_config(online_config_url: &str) -> reqwest::Result<String> {
static SHADOWSOCKS_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
);

static SHADOWSOCKS_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

let client = Client::builder()
.user_agent(SHADOWSOCKS_USER_AGENT)
Expand All @@ -1131,14 +1127,29 @@ async fn get_online_config_servers(
// Content-Type: application/json; charset=utf-8
// mandatory in standard SIP008
match response.headers().get("Content-Type") {
Some(h) => {
if h != "application/json; charset=utf-8" {
warn!(
"SIP008 Content-Type must be \"application/json; charset=utf-8\", but found {}",
h.to_str().unwrap_or("[non-utf8-value]")
);
Some(h) => match h.to_str() {
Ok(hstr) => match hstr.parse::<Mime>() {
Ok(content_type) => {
if content_type.type_() == mime::APPLICATION
&& content_type.subtype() == mime::JSON
&& content_type.get_param("charset") == Some(mime::UTF_8)
{
trace!("checked Content-Type: {:?}", h);
} else {
warn!(
"Content-Type is not \"application/json; charset=utf-8\", which is mandatory in standard SIP008. found {:?}",
h
);
}
}
Err(err) => {
warn!("Content-Type parse failed, value: {:?}, error: {}", h, err);
}
},
Err(..) => {
warn!("Content-Type is not a UTF-8 string: {:?}", h);
}
}
},
None => {
warn!("missing Content-Type in SIP008 response from {}", online_config_url);
}
Expand Down

0 comments on commit d07ea6f

Please sign in to comment.