-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Detect encoding and decode text response #256
Changes from 1 commit
25378d6
f5d00b6
2337017
19c57f6
8c08daa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ use encoding_rs::{Encoding, UTF_8}; | |
use futures::{Async, Poll, Stream}; | ||
use serde::de::DeserializeOwned; | ||
use serde_json; | ||
use uchardet; | ||
|
||
use client::KeepCoreThreadAlive; | ||
use header::Headers; | ||
|
@@ -189,10 +188,7 @@ impl Response { | |
content_type.get_param("charset") | ||
.map(|charset| charset.as_str().to_string()) | ||
}) | ||
.unwrap_or_else(|| { | ||
uchardet::detect_encoding_name(&content) | ||
.unwrap_or_else(|_| "utf-8".to_string()) | ||
}); | ||
.unwrap_or_else(|| "utf-8".to_string()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With no copy of the string above, this can just be |
||
let encoding = Encoding::for_label(encoding_name.as_bytes()).unwrap_or(UTF_8); | ||
let (text, _, _) = encoding.decode(&content); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like So, seems this could be handled like so: // a block because of borrow checker
{
let (text, _, _) = encoding.decode(&content);
match text {
Cow::Owned(s) => return Ok(s),
_ => (),
}
}
unsafe {
// decoding returned Cow::Borrowed, meaning these bytes
// are already valid utf8
Ok(String::from_utf8_unchecked(content))
} |
||
Ok(text.to_string()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs to copy the string, and so could just be
charset.as_str()
.