Skip to content

Commit

Permalink
Optimize fn get_encoding_from_headers()
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Jul 25, 2024
1 parent cdd8ce9 commit f81c1c9
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@ use indexmap::IndexMap;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyDict};

// Get encoding from the "Content-Type" header
/// Get encoding from the "Content-Type" header
pub fn get_encoding_from_headers(
headers: &IndexMap<String, String, RandomState>,
) -> Option<String> {
// Extract and decode the Content-Type header
let content_type = headers
headers
.iter()
.find_map(|(key, value)| {
if key.to_lowercase() == "content-type" {
Some(value.clone())
.find(|(key, _)| key.to_ascii_lowercase() == "content-type")
.map(|(_, value)| value.as_str())
.and_then(|content_type| {
// Parse the Content-Type header to separate the media type and parameters
let mut parts = content_type.split(';');
let media_type = parts.next().unwrap_or("").trim();
let params = parts.next().unwrap_or("").trim();

// Check for specific conditions and return the appropriate encoding
if let Some(param) = params.to_ascii_lowercase().strip_prefix("charset=") {
Some(param.trim_matches('"').to_string())
} else if media_type == "application/json" {
Some("UTF-8".into())
} else {
None
}
})
.unwrap_or_default();

// Parse the Content-Type header to separate the media type and parameters
let mut parts = content_type.split(';');
let media_type = parts.next().unwrap_or("").trim();
let params = parts.next().unwrap_or("").trim();

// Check for specific conditions and return the appropriate encoding
if let Some(param) = params.to_lowercase().strip_prefix("charset=") {
Some(param.trim_matches('"').to_string())
} else if media_type == "application/json" {
Some("UTF-8".into())
} else {
None
}
}

/// Get encoding from the `<meta charset="...">` tag within the first 2048 bytes of HTML content.
Expand Down

0 comments on commit f81c1c9

Please sign in to comment.