Skip to content

Commit 073f3bb

Browse files
committed
fix content_type handling, add render_priority implementations
1 parent 2d21be3 commit 073f3bb

File tree

8 files changed

+56
-25
lines changed

8 files changed

+56
-25
lines changed

mitmproxy-contentviews/src/grpc.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ impl Prettify for GRPC {
4646
}
4747

4848
fn render_priority(&self, _data: &[u8], metadata: &dyn Metadata) -> f64 {
49-
let Some(ct) = metadata.content_type() else {
50-
return 0.0;
51-
};
52-
match ct.as_str() {
53-
"application/grpc" => 2.0,
54-
"application/grpc+proto" => 2.0,
55-
"application/prpc" => 2.0,
49+
match metadata.content_type() {
50+
Some("application/grpc") => 1.0,
51+
Some("application/grpc+proto") => 1.0,
52+
Some("application/prpc") => 1.0,
5653
_ => 0.0,
5754
}
5855
}

mitmproxy-contentviews/src/hex_dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Prettify for HexDump {
2626

2727
fn render_priority(&self, data: &[u8], _metadata: &dyn Metadata) -> f64 {
2828
if is_binary(data) {
29-
0.95
29+
0.5
3030
} else {
3131
0.0
3232
}

mitmproxy-contentviews/src/hex_stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Prettify for HexStream {
2828

2929
fn render_priority(&self, data: &[u8], _metadata: &dyn Metadata) -> f64 {
3030
if is_binary(data) {
31-
0.95
31+
0.4
3232
} else {
3333
0.0
3434
}

mitmproxy-contentviews/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ mod hex_stream;
44
mod msgpack;
55
mod protobuf;
66

7-
use anyhow::Result;
8-
97
pub use grpc::GRPC;
108
pub use hex_dump::HexDump;
119
pub use hex_stream::HexStream;
12-
use mitmproxy_highlight::Language;
1310
pub use msgpack::MsgPack;
1411
pub use protobuf::Protobuf;
1512

13+
use anyhow::Result;
14+
use mitmproxy_highlight::Language;
15+
1616
pub trait Metadata {
17-
fn content_type(&self) -> Option<String>;
17+
fn content_type(&self) -> Option<&str>;
1818
}
1919

2020
pub trait Prettify: Send + Sync {
@@ -30,7 +30,8 @@ pub trait Prettify: Send + Sync {
3030

3131
fn prettify(&self, data: &[u8], metadata: &dyn Metadata) -> Result<String>;
3232

33-
fn render_priority(&self, _data: &[u8], _metadata: &dyn Metadata) -> f64 {
33+
#[allow(unused_variables)]
34+
fn render_priority(&self, data: &[u8], metadata: &dyn Metadata) -> f64 {
3435
0.0
3536
}
3637
}
@@ -45,7 +46,7 @@ pub struct TestMetadata {
4546
}
4647

4748
impl Metadata for TestMetadata {
48-
fn content_type(&self) -> Option<String> {
49-
self.content_type.clone()
49+
fn content_type(&self) -> Option<&str> {
50+
self.content_type.as_deref()
5051
}
5152
}

mitmproxy-contentviews/src/msgpack.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ impl Prettify for MsgPack {
2222
// Convert the Value to prettified YAML
2323
serde_yaml::to_string(&value).context("Failed to convert to YAML")
2424
}
25+
26+
fn render_priority(&self, _data: &[u8], metadata: &dyn Metadata) -> f64 {
27+
match metadata.content_type() {
28+
Some("application/msgpack") => 1.0,
29+
Some("application/x-msgpack") => 1.0,
30+
_ => 0.0,
31+
}
32+
}
2533
}
2634

2735
impl Reencode for MsgPack {

mitmproxy-contentviews/src/protobuf.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ impl Prettify for Protobuf {
7676
let yaml_str = serde_yaml::to_string(&yaml_value).context("Failed to convert to YAML")?;
7777
yaml_to_pretty::apply_replacements(&yaml_str)
7878
}
79+
80+
fn render_priority(&self, _data: &[u8], metadata: &dyn Metadata) -> f64 {
81+
match metadata.content_type() {
82+
Some("application/x-protobuf") => 1.0,
83+
Some("application/x-protobuffer") => 1.0,
84+
_ => 0.0,
85+
}
86+
}
7987
}
8088

8189
impl Reencode for Protobuf {

mitmproxy-rs/src/contentview.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
use mitmproxy_contentviews::{Metadata, Prettify, Reencode};
22
use pyo3::{exceptions::PyValueError, prelude::*};
3+
use std::cell::OnceCell;
34

4-
pub struct PythonMetadata<'py>(Bound<'py, PyAny>);
5+
pub struct PythonMetadata<'py> {
6+
inner: Bound<'py, PyAny>,
7+
content_type: OnceCell<Option<String>>,
8+
}
9+
10+
impl<'py> PythonMetadata<'py> {
11+
pub fn new(inner: Bound<'py, PyAny>) -> Self {
12+
PythonMetadata {
13+
inner,
14+
content_type: OnceCell::new(),
15+
}
16+
}
17+
}
518

619
impl Metadata for PythonMetadata<'_> {
7-
fn content_type(&self) -> Option<String> {
8-
self.0
9-
.getattr("content_type")
10-
.ok()?
11-
.extract::<String>()
12-
.ok()
20+
fn content_type(&self) -> Option<&str> {
21+
self.content_type
22+
.get_or_init(|| {
23+
self.inner
24+
.getattr("content_type")
25+
.ok()?
26+
.extract::<String>()
27+
.ok()
28+
})
29+
.as_ref()
30+
.map(|ct| ct.as_str())
1331
}
1432
}
1533

1634
impl<'py> FromPyObject<'py> for PythonMetadata<'py> {
1735
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
18-
Ok(PythonMetadata(ob.clone()))
36+
Ok(PythonMetadata::new(ob.clone()))
1937
}
2038
}
2139

src/processes/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub use image;
22
use std::path::PathBuf;
3-
use std::sync::LazyLock;
43

54
#[cfg(any(target_os = "linux", target_os = "macos"))]
65
mod nix_list;

0 commit comments

Comments
 (0)