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

Fix charset parsing logic of Content-Type header #2543

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion integration/hurl/tests_ok/charset.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ Content-Type: text/html; charset=latin1
body == "<p>café</p>"


GET http://localhost:8000/charset/latin1/uppercase-key
HTTP 200
Content-Type: text/html; CHARSET=latin1
[Asserts]
body == "<p>café</p>"


GET http://localhost:8000/charset/gb2312
HTTP 200
Content-Type: text/html; charset=gb2312
Expand All @@ -25,8 +32,15 @@ Content-Type: text/html; charset=cp1256
body == "<p>مرحبا بالعالم</p>"


GET http://localhost:8000/charset/uppercase
GET http://localhost:8000/charset/uppercase-value
HTTP 200
Content-Type: text/html; charset=UTF-8
[Asserts]
body == "<p>Hello World!</p>"


GET http://localhost:8000/charset/many-keys
HTTP 200
Content-Type: text/plain; version=0.0.4; charset=utf-8; escaping=values
[Asserts]
body == "<p>Hello World!</p>"
20 changes: 18 additions & 2 deletions integration/hurl/tests_ok/charset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,36 @@ def charset_default():
return "<p>Hello World!</p>"


@app.route("/charset/uppercase")
def charset_uppercase():
@app.route("/charset/uppercase-value")
def charset_uppercase_value():
resp = make_response("<p>Hello World!</p>")
resp.headers["Content-Type"] = "text/html; charset=UTF-8"
return resp


@app.route("/charset/many-keys")
def charset_uppercase_many_keys():
resp = make_response("<p>Hello World!</p>")
resp.headers[
"Content-Type"
] = "text/plain; version=0.0.4; charset=utf-8; escaping=values"
return resp


@app.route("/charset/latin1")
def charset_latin1():
resp = make_response("<p>café</p>".encode("latin1"))
resp.headers["Content-Type"] = "text/html; charset=latin1"
return resp


@app.route("/charset/latin1/uppercase-key")
def charset_latin1_uppercase_key():
resp = make_response("<p>café</p>".encode("latin1"))
resp.headers["Content-Type"] = "text/html; CHARSET=latin1"
return resp


@app.route("/charset/gb2312")
def charset_gb2312():
resp = make_response("<p>你好世界</p>".encode("gb2312"))
Expand Down
23 changes: 20 additions & 3 deletions packages/hurl/src/http/mimetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ pub fn is_html(content_type: &str) -> bool {

/// Extracts charset from mime-type String
pub fn charset(mime_type: &str) -> Option<String> {
mime_type
.find("charset=")
.map(|index| mime_type[(index + 8)..].to_string())
let parts = mime_type.trim().split(';');
for part in parts {
let param = part.trim().split('=').collect::<Vec<_>>();
if param.len() == 2 && param[0].trim().eq_ignore_ascii_case("charset") {
return Some(param[1].trim().to_string());
}
}
None
}

#[cfg(test)]
Expand All @@ -51,10 +56,22 @@ pub mod tests {
charset("text/plain; charset=utf-8"),
Some("utf-8".to_string())
);

assert_eq!(
charset("text/plain; charset=ISO-8859-1"),
Some("ISO-8859-1".to_string())
);

assert_eq!(charset("text/plain;"), None);

assert_eq!(
charset("text/plain; CHARSET=ISO-8859-1"),
Some("ISO-8859-1".to_string())
);

assert_eq!(
charset("text/plain; version=0.0.4; charset=utf-8; escaping=values"),
Some("utf-8".to_string())
);
}
}