feedparser_rs/http/
response.rs

1use std::collections::HashMap;
2
3/// HTTP response from feed fetch
4#[derive(Debug, Clone)]
5pub struct FeedHttpResponse {
6    /// HTTP status code
7    pub status: u16,
8    /// Final URL after redirects
9    pub url: String,
10    /// Response headers
11    pub headers: HashMap<String, String>,
12    /// Response body
13    pub body: Vec<u8>,
14    /// `ETag` header value
15    pub etag: Option<String>,
16    /// Last-Modified header value
17    pub last_modified: Option<String>,
18    /// Content-Type header value
19    pub content_type: Option<String>,
20    /// Encoding extracted from Content-Type
21    pub encoding: Option<String>,
22}
23
24impl FeedHttpResponse {
25    /// Extract charset from Content-Type header
26    ///
27    /// Parses header like "text/xml; charset=utf-8" and returns "utf-8"
28    pub fn extract_charset_from_content_type(content_type: &str) -> Option<String> {
29        content_type.split(';').find_map(|part| {
30            part.trim()
31                .strip_prefix("charset=")
32                .map(|s| s.trim_matches('"').to_string())
33        })
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_extract_charset_simple() {
43        let ct = "text/xml; charset=utf-8";
44        assert_eq!(
45            FeedHttpResponse::extract_charset_from_content_type(ct),
46            Some("utf-8".to_string())
47        );
48    }
49
50    #[test]
51    fn test_extract_charset_quoted() {
52        let ct = "application/xml; charset=\"ISO-8859-1\"";
53        assert_eq!(
54            FeedHttpResponse::extract_charset_from_content_type(ct),
55            Some("ISO-8859-1".to_string())
56        );
57    }
58
59    #[test]
60    fn test_extract_charset_no_charset() {
61        let ct = "application/xml";
62        assert_eq!(
63            FeedHttpResponse::extract_charset_from_content_type(ct),
64            None
65        );
66    }
67
68    #[test]
69    fn test_extract_charset_multiple_params() {
70        let ct = "text/html; boundary=something; charset=utf-8";
71        assert_eq!(
72            FeedHttpResponse::extract_charset_from_content_type(ct),
73            Some("utf-8".to_string())
74        );
75    }
76}