1use std::collections::HashSet;
7
8use http::HeaderName;
9use tower_layer::Layer;
10
11use crate::service::DebugService;
12
13#[derive(Debug, Clone)]
15pub struct DebugLayer {
16 config: DebugConfig,
17}
18
19#[derive(Debug, Clone)]
21pub struct DebugConfig {
22 pub log_headers: bool,
24 pub log_bodies: bool,
26 pub log_response_frames: bool,
28 pub max_body_bytes: usize,
30 pub hex_dump: bool,
32 pub sensitive_headers: HashSet<HeaderName>,
34 pub reveal_sensitive_headers: bool,
36}
37
38impl Default for DebugConfig {
39 fn default() -> Self {
40 Self {
41 log_headers: true,
42 log_bodies: true,
43 log_response_frames: true,
44 max_body_bytes: 4096,
45 hex_dump: false,
46 sensitive_headers: HashSet::from([HeaderName::from_static("authorization")]),
47 reveal_sensitive_headers: false,
48 }
49 }
50}
51
52impl DebugLayer {
53 pub fn new() -> Self {
57 Self {
58 config: DebugConfig::default(),
59 }
60 }
61
62 pub fn with_config(config: DebugConfig) -> Self {
64 Self { config }
65 }
66
67 pub fn sensitive_headers(mut self, sensitive_headers: HashSet<HeaderName>) -> Self {
69 self.config.sensitive_headers = sensitive_headers;
70 self
71 }
72
73 pub fn reveal_sensitive_headers(mut self, enabled: bool) -> Self {
75 self.config.reveal_sensitive_headers = enabled;
76 self
77 }
78
79 pub fn log_headers(mut self, enabled: bool) -> Self {
81 self.config.log_headers = enabled;
82 self
83 }
84
85 pub fn log_bodies(mut self, enabled: bool) -> Self {
87 self.config.log_bodies = enabled;
88 self
89 }
90
91 pub fn log_response_frames(mut self, enabled: bool) -> Self {
93 self.config.log_response_frames = enabled;
94 self
95 }
96
97 pub fn max_body_bytes(mut self, max: usize) -> Self {
99 self.config.max_body_bytes = max;
100 self
101 }
102
103 pub fn hex_dump(mut self, enabled: bool) -> Self {
105 self.config.hex_dump = enabled;
106 self
107 }
108}
109
110impl Default for DebugLayer {
111 fn default() -> Self {
112 Self::new()
113 }
114}
115
116impl<S> Layer<S> for DebugLayer {
117 type Service = DebugService<S>;
118
119 fn layer(&self, inner: S) -> Self::Service {
120 DebugService::new(inner, self.config.clone())
121 }
122}