Skip to content

Commit 0eb8738

Browse files
authored
fix metadata interop, add testing-only content view
1 parent b3562a7 commit 0eb8738

6 files changed

Lines changed: 68 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased: mitmproxy_rs next
22

3+
- Contentview bugfixes.
34

45
## 14 April 2025: mitmproxy_rs 0.12.0
56

mitmproxy-contentviews/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ mod hex_dump;
22
mod hex_stream;
33
mod msgpack;
44
mod protobuf;
5+
mod test_inspect_metadata;
56

67
pub use hex_dump::HexDump;
78
pub use hex_stream::HexStream;
89
pub use msgpack::MsgPack;
910
pub use protobuf::Protobuf;
1011
pub use protobuf::GRPC;
12+
pub use test_inspect_metadata::TestInspectMetadata;
1113

1214
use anyhow::Result;
1315
use mitmproxy_highlight::Language;
16+
17+
use serde::Serialize;
1418
use std::path::Path;
1519

1620
pub trait Metadata {
@@ -66,10 +70,9 @@ pub trait Reencode: Send + Sync {
6670

6771
// no cfg(test) gate because it's used in benchmarks as well
6872
pub mod test {
69-
use crate::Metadata;
70-
use std::path::Path;
73+
use super::*;
7174

72-
#[derive(Default)]
75+
#[derive(Default, Serialize)]
7376
pub struct TestMetadata {
7477
pub content_type: Option<String>,
7578
pub headers: std::collections::HashMap<String, String>,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::test::TestMetadata;
2+
use crate::{Metadata, Prettify};
3+
use anyhow::Context;
4+
use std::collections::HashMap;
5+
use std::path::Path;
6+
7+
/// Contentview used for internal testing to ensure that the
8+
/// Python accessors in mitmproxy-rs all work properly.
9+
pub struct TestInspectMetadata;
10+
11+
impl Prettify for TestInspectMetadata {
12+
fn name(&self) -> &'static str {
13+
"Inspect Metadata (test only)"
14+
}
15+
16+
fn instance_name(&self) -> String {
17+
"_test_inspect_metadata".to_string()
18+
}
19+
20+
fn prettify(&self, _data: &[u8], metadata: &dyn Metadata) -> anyhow::Result<String> {
21+
let mut headers = HashMap::new();
22+
if let Some(host) = metadata.get_header("host") {
23+
headers.insert("host".to_string(), host);
24+
}
25+
let meta = TestMetadata {
26+
content_type: metadata.content_type().map(str::to_string),
27+
headers,
28+
path: metadata.get_path().map(str::to_string),
29+
is_http_request: metadata.is_http_request(),
30+
protobuf_definitions: metadata.protobuf_definitions().map(Path::to_path_buf),
31+
};
32+
// JSON would be nicer to consume on the Python side,
33+
// but let's not add dependencies for this.
34+
serde_yaml::to_string(&meta).context("Failed to convert to YAML")
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn prettify_simple() {
44+
let result = TestInspectMetadata
45+
.prettify(b"", &TestMetadata::default())
46+
.unwrap();
47+
assert_eq!(
48+
result,
49+
"content_type: null\nheaders: {}\nprotobuf_definitions: null\npath: null\nis_http_request: false\n"
50+
);
51+
}
52+
}

mitmproxy-rs/mitmproxy_rs/contentviews.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class InteractiveContentview(Contentview):
1818
def reencode(self, data: str, metadata) -> bytes:
1919
pass
2020

21+
_test_inspect_metadata: Contentview
2122
hex_dump: Contentview
2223
hex_stream: InteractiveContentview
2324
msgpack: InteractiveContentview
@@ -32,4 +33,5 @@ __all__ = [
3233
"msgpack",
3334
"protobuf",
3435
"grpc",
36+
"_test_inspect_metadata",
3537
]

mitmproxy-rs/src/contentviews.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use mitmproxy_contentviews::{Metadata, Prettify, Reencode};
22
use pyo3::{exceptions::PyValueError, prelude::*};
33
use std::cell::OnceCell;
4-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
55

66
pub struct PythonMetadata<'py> {
77
inner: Bound<'py, PyAny>,
@@ -56,9 +56,8 @@ impl Metadata for PythonMetadata<'_> {
5656
self.inner
5757
.getattr("protobuf_definitions")
5858
.ok()?
59-
.extract::<String>()
59+
.extract::<PathBuf>()
6060
.ok()
61-
.map(std::path::PathBuf::from)
6261
})
6362
.as_deref()
6463
}
@@ -67,16 +66,13 @@ impl Metadata for PythonMetadata<'_> {
6766
let Ok(http_message) = self.inner.getattr("http_message") else {
6867
return false;
6968
};
70-
let Ok(flow) = self
69+
let Ok(request) = self
7170
.inner
7271
.getattr("flow")
7372
.and_then(|flow| flow.getattr("request"))
7473
else {
7574
return false;
7675
};
77-
let Ok(request) = flow.getattr("request") else {
78-
return false;
79-
};
8076
http_message.is(&request)
8177
}
8278
}

mitmproxy-rs/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ mod mitmproxy_rs {
9191
use crate::contentviews::Contentview;
9292
#[pymodule_export]
9393
use crate::contentviews::InteractiveContentview;
94-
use mitmproxy_contentviews::{HexDump, HexStream, MsgPack, Protobuf, GRPC};
94+
use mitmproxy_contentviews::{
95+
HexDump, HexStream, MsgPack, Protobuf, TestInspectMetadata, GRPC,
96+
};
9597

9698
#[pymodule_init]
9799
fn init(m: &Bound<'_, PyModule>) -> PyResult<()> {
@@ -100,6 +102,7 @@ mod mitmproxy_rs {
100102
m.add_interactive_contentview(&MsgPack)?;
101103
m.add_interactive_contentview(&Protobuf)?;
102104
m.add_interactive_contentview(&GRPC)?;
105+
m.add_contentview(&TestInspectMetadata)?;
103106
Ok(())
104107
}
105108
}

0 commit comments

Comments
 (0)