Skip to content

Commit d07ea6f

Browse files
committed
feat: parse content-type with \"mime\"
1 parent 1e68e2c commit d07ea6f

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ local-fake-dns = ["local", "shadowsocks-service/local-fake-dns"]
167167
local-online-config = [
168168
"local",
169169
"reqwest",
170+
"mime",
170171
"shadowsocks-service/local-online-config",
171172
]
172173

@@ -224,6 +225,7 @@ serde = { version = "1.0", features = ["derive"] }
224225
json5 = "0.4"
225226
thiserror = "1.0"
226227
base64 = "0.22"
228+
mime = { version = "0.3", optional = true }
227229

228230
clap = { version = "4.5", features = ["wrap_help", "suggestions"] }
229231
cfg-if = "1"

src/service/local.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,16 +1097,12 @@ async fn get_online_config_servers(
10971097
online_config_url: &str,
10981098
) -> Result<Vec<ServerInstanceConfig>, Box<dyn std::error::Error>> {
10991099
use log::warn;
1100+
use mime::Mime;
11001101
use reqwest::{redirect::Policy, Client};
11011102

11021103
#[inline]
11031104
async fn get_online_config(online_config_url: &str) -> reqwest::Result<String> {
1104-
static SHADOWSOCKS_USER_AGENT: &str = concat!(
1105-
env!("CARGO_PKG_NAME"),
1106-
"/",
1107-
env!("CARGO_PKG_VERSION"),
1108-
);
1109-
1105+
static SHADOWSOCKS_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
11101106

11111107
let client = Client::builder()
11121108
.user_agent(SHADOWSOCKS_USER_AGENT)
@@ -1131,14 +1127,29 @@ async fn get_online_config_servers(
11311127
// Content-Type: application/json; charset=utf-8
11321128
// mandatory in standard SIP008
11331129
match response.headers().get("Content-Type") {
1134-
Some(h) => {
1135-
if h != "application/json; charset=utf-8" {
1136-
warn!(
1137-
"SIP008 Content-Type must be \"application/json; charset=utf-8\", but found {}",
1138-
h.to_str().unwrap_or("[non-utf8-value]")
1139-
);
1130+
Some(h) => match h.to_str() {
1131+
Ok(hstr) => match hstr.parse::<Mime>() {
1132+
Ok(content_type) => {
1133+
if content_type.type_() == mime::APPLICATION
1134+
&& content_type.subtype() == mime::JSON
1135+
&& content_type.get_param("charset") == Some(mime::UTF_8)
1136+
{
1137+
trace!("checked Content-Type: {:?}", h);
1138+
} else {
1139+
warn!(
1140+
"Content-Type is not \"application/json; charset=utf-8\", which is mandatory in standard SIP008. found {:?}",
1141+
h
1142+
);
1143+
}
1144+
}
1145+
Err(err) => {
1146+
warn!("Content-Type parse failed, value: {:?}, error: {}", h, err);
1147+
}
1148+
},
1149+
Err(..) => {
1150+
warn!("Content-Type is not a UTF-8 string: {:?}", h);
11401151
}
1141-
}
1152+
},
11421153
None => {
11431154
warn!("missing Content-Type in SIP008 response from {}", online_config_url);
11441155
}

0 commit comments

Comments
 (0)