-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathlib.rs
More file actions
132 lines (110 loc) · 3.7 KB
/
lib.rs
File metadata and controls
132 lines (110 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
mod hex_dump;
mod hex_stream;
mod msgpack;
mod protobuf;
mod test_inspect_metadata;
pub use hex_dump::HexDump;
pub use hex_stream::HexStream;
pub use msgpack::MsgPack;
pub use protobuf::GRPC;
pub use protobuf::Protobuf;
pub use test_inspect_metadata::TestInspectMetadata;
use anyhow::Result;
use mitmproxy_highlight::Language;
use serde::Serialize;
use std::path::Path;
pub trait Metadata {
/// The HTTP `content-type` of this message.
fn content_type(&self) -> Option<&str>;
/// Get an HTTP header value by name.
/// `name` is case-insensitive.
fn get_header(&self, name: &str) -> Option<String>;
/// Get the path from the flow's request.
fn get_path(&self) -> Option<&str> {
None
}
/// Check if this is an HTTP request.
fn is_http_request(&self) -> bool {
false
}
/// Get the protobuf definitions for this message.
fn protobuf_definitions(&self) -> Option<&Path> {
None
}
}
/// See https://docs.mitmproxy.org/dev/api/mitmproxy/contentviews.html
/// for API details.
pub trait Prettify: Send + Sync {
/// The name for this contentview, e.g. `gRPC` or `Protobuf`.
/// Favor brevity.
fn name(&self) -> &str;
fn instance_name(&self) -> String {
self.name().to_lowercase().replace(" ", "_")
}
/// The syntax highlighting that should be applied to the prettified output.
/// This is useful for contentviews that prettify to JSON or YAML.
fn syntax_highlight(&self) -> Language {
Language::None
}
/// Pretty-print `data`.
fn prettify(&self, data: &[u8], metadata: &dyn Metadata) -> Result<String>;
/// Render priority - typically a float between 0 and 1 for builtin views.
#[allow(unused_variables)]
fn render_priority(&self, data: &[u8], metadata: &dyn Metadata) -> f32 {
0.0
}
}
pub trait Reencode: Send + Sync {
fn reencode(&self, data: &str, metadata: &dyn Metadata) -> Result<Vec<u8>>;
}
// no cfg(test) gate because it's used in benchmarks as well
pub mod test {
use super::*;
#[derive(Default, Serialize)]
pub struct TestMetadata {
pub content_type: Option<String>,
pub headers: std::collections::HashMap<String, String>,
pub protobuf_definitions: Option<std::path::PathBuf>,
pub path: Option<String>,
pub is_http_request: bool,
}
impl TestMetadata {
pub fn with_content_type(mut self, content_type: &str) -> Self {
self.content_type = Some(content_type.to_string());
self
}
pub fn with_header(mut self, name: &str, value: &str) -> Self {
self.headers.insert(name.to_lowercase(), value.to_string());
self
}
pub fn with_path(mut self, path: &str) -> Self {
self.path = Some(path.to_string());
self
}
pub fn with_protobuf_definitions<P: AsRef<Path>>(mut self, definitions: P) -> Self {
self.protobuf_definitions = Some(definitions.as_ref().to_path_buf());
self
}
pub fn with_is_http_request(mut self, is_http_request: bool) -> Self {
self.is_http_request = is_http_request;
self
}
}
impl Metadata for TestMetadata {
fn content_type(&self) -> Option<&str> {
self.content_type.as_deref()
}
fn get_header(&self, name: &str) -> Option<String> {
self.headers.get(name).cloned()
}
fn get_path(&self) -> Option<&str> {
self.path.as_deref()
}
fn protobuf_definitions(&self) -> Option<&Path> {
self.protobuf_definitions.as_deref()
}
fn is_http_request(&self) -> bool {
self.is_http_request
}
}
}