Skip to content

Commit 7f56025

Browse files
committed
feat(filter): add html filter only on a specific content-type
1 parent e01f9d2 commit 7f56025

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/filter/filter_body.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@ impl FilterBodyAction {
2525
pub fn new(filters: Vec<BodyFilter>, headers: &[Header]) -> Self {
2626
let mut chain = Vec::new();
2727
let mut has_gzip = false;
28+
let mut content_type = None;
2829

2930
for header in headers {
3031
if header.name.to_lowercase() == "content-encoding" && header.value.to_lowercase() == "gzip" {
3132
has_gzip = true;
3233
break;
3334
}
35+
36+
if header.name.to_lowercase() == "content-type" {
37+
content_type = Some(header.value.to_lowercase());
38+
}
3439
}
3540

3641
if has_gzip {
3742
chain.push(FilterBodyActionItem::UnGzip(GzDecodeFilterBody::new()));
3843
}
3944

4045
for filter in filters {
41-
if let Some(item) = FilterBodyActionItem::new(filter) {
46+
if let Some(item) = FilterBodyActionItem::new(filter, content_type.clone()) {
4247
chain.push(item);
4348
}
4449
}
@@ -120,11 +125,26 @@ impl FilterBodyAction {
120125
}
121126

122127
impl FilterBodyActionItem {
123-
pub fn new(filter: BodyFilter) -> Option<Self> {
128+
pub fn new(filter: BodyFilter, content_type: Option<String>) -> Option<Self> {
124129
match filter {
125-
BodyFilter::HTML(html_body_filter) => {
126-
HtmlBodyVisitor::new(html_body_filter).map(|visitor| Self::Html(HtmlFilterBodyAction::new(visitor)))
127-
}
130+
BodyFilter::HTML(html_body_filter) => match content_type {
131+
Some(content_type) if content_type.contains("text/html") => {
132+
// @TODO Support charset
133+
HtmlBodyVisitor::new(html_body_filter).map(|visitor| Self::Html(HtmlFilterBodyAction::new(visitor)))
134+
}
135+
None => {
136+
// Assume HTML if no content type
137+
HtmlBodyVisitor::new(html_body_filter).map(|visitor| Self::Html(HtmlFilterBodyAction::new(visitor)))
138+
}
139+
_ => {
140+
log::error!(
141+
"html filtering is only supported for text/html content type, {} received",
142+
content_type.unwrap_or_default()
143+
);
144+
145+
None
146+
}
147+
},
128148
BodyFilter::Text(text_body_filter) => Some(Self::Text(TextFilterBodyAction::new(
129149
text_body_filter.id,
130150
match text_body_filter.action {

0 commit comments

Comments
 (0)