klafs-api 0.1.7

Rust client library for the Klafs sauna control API
Documentation
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! Debug logging for HTTP traffic
//!
//! This module provides utilities for capturing and logging HTTP request/response
//! traffic for debugging purposes.

use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
use tracing::{debug, trace};

/// Configuration for debug logging
#[derive(Debug, Clone)]
pub struct DebugConfig {
    /// Whether debug logging is enabled
    pub enabled: bool,
    /// Optional file path to write debug logs
    pub log_file: Option<PathBuf>,
    /// Whether to log request/response bodies
    pub log_bodies: bool,
    /// Whether to redact sensitive information (passwords, tokens)
    pub redact_sensitive: bool,
}

impl Default for DebugConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            log_file: None,
            log_bodies: true,
            redact_sensitive: true,
        }
    }
}

impl DebugConfig {
    /// Create a new debug config with logging enabled
    pub fn enabled() -> Self {
        Self {
            enabled: true,
            ..Default::default()
        }
    }

    /// Enable logging to a file
    pub fn with_log_file(mut self, path: PathBuf) -> Self {
        self.log_file = Some(path);
        self
    }

    /// Disable body logging
    pub fn without_bodies(mut self) -> Self {
        self.log_bodies = false;
        self
    }

    /// Disable sensitive data redaction (use with caution!)
    pub fn without_redaction(mut self) -> Self {
        self.redact_sensitive = false;
        self
    }
}

/// HTTP traffic logger
#[derive(Debug)]
pub struct HttpDebugger {
    config: DebugConfig,
    enabled: AtomicBool,
    log_buffer: Arc<RwLock<Vec<TrafficEntry>>>,
}

/// A single HTTP traffic entry
#[derive(Debug, Clone)]
pub struct TrafficEntry {
    /// Unique request ID
    pub request_id: String,
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Request method
    pub method: String,
    /// Request URL
    pub url: String,
    /// Request headers (redacted if configured)
    pub request_headers: Vec<(String, String)>,
    /// Request body (redacted if configured)
    pub request_body: Option<String>,
    /// Response status code
    pub response_status: Option<u16>,
    /// Response headers
    pub response_headers: Vec<(String, String)>,
    /// Response body
    pub response_body: Option<String>,
    /// Request duration in milliseconds
    pub duration_ms: Option<u64>,
    /// Any error that occurred
    pub error: Option<String>,
}

impl TrafficEntry {
    /// Create a new traffic entry for a request
    pub fn new(method: &str, url: &str) -> Self {
        Self {
            request_id: uuid::Uuid::new_v4().to_string(),
            timestamp: chrono::Utc::now(),
            method: method.to_string(),
            url: url.to_string(),
            request_headers: Vec::new(),
            request_body: None,
            response_status: None,
            response_headers: Vec::new(),
            response_body: None,
            duration_ms: None,
            error: None,
        }
    }

    /// Format as a debug string
    pub fn format(&self) -> String {
        let mut output = String::new();

        output.push_str(&format!(
            "\n{}\n[{}] {} {}\nRequest ID: {}\n",
            "=".repeat(60),
            self.timestamp.format("%Y-%m-%d %H:%M:%S%.3f UTC"),
            self.method,
            self.url,
            self.request_id
        ));

        if !self.request_headers.is_empty() {
            output.push_str("\n--- Request Headers ---\n");
            for (key, value) in &self.request_headers {
                output.push_str(&format!("{}: {}\n", key, value));
            }
        }

        if let Some(ref body) = self.request_body {
            output.push_str("\n--- Request Body ---\n");
            output.push_str(body);
            output.push('\n');
        }

        if let Some(status) = self.response_status {
            output.push_str(&format!("\n--- Response Status: {} ---\n", status));
        }

        if !self.response_headers.is_empty() {
            output.push_str("\n--- Response Headers ---\n");
            for (key, value) in &self.response_headers {
                output.push_str(&format!("{}: {}\n", key, value));
            }
        }

        if let Some(ref body) = self.response_body {
            output.push_str("\n--- Response Body ---\n");
            // Try to pretty-print JSON
            if let Ok(json) = serde_json::from_str::<serde_json::Value>(body) {
                if let Ok(pretty) = serde_json::to_string_pretty(&json) {
                    output.push_str(&pretty);
                } else {
                    output.push_str(body);
                }
            } else {
                // Only truncate extremely long HTML responses (>500KB)
                if body.len() > 500000 {
                    output.push_str(&body[..500000]);
                    output.push_str(&format!("\n... [truncated, {} bytes total]", body.len()));
                } else {
                    output.push_str(body);
                }
            }
            output.push('\n');
        }

        if let Some(duration) = self.duration_ms {
            output.push_str(&format!("\nDuration: {}ms\n", duration));
        }

        if let Some(ref error) = self.error {
            output.push_str(&format!("\n!!! Error: {} !!!\n", error));
        }

        output.push_str(&"=".repeat(60));
        output.push('\n');

        output
    }
}

impl Default for HttpDebugger {
    fn default() -> Self {
        Self::new(DebugConfig::default())
    }
}

impl HttpDebugger {
    /// Create a new HTTP debugger with the given configuration
    pub fn new(config: DebugConfig) -> Self {
        let enabled = config.enabled;
        Self {
            config,
            enabled: AtomicBool::new(enabled),
            log_buffer: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Check if debugging is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled.load(Ordering::Relaxed)
    }

    /// Enable debugging at runtime
    pub fn enable(&self) {
        self.enabled.store(true, Ordering::Relaxed);
    }

    /// Disable debugging at runtime
    pub fn disable(&self) {
        self.enabled.store(false, Ordering::Relaxed);
    }

    /// Log a request (before sending)
    pub async fn log_request(
        &self,
        method: &str,
        url: &str,
        headers: &reqwest::header::HeaderMap,
        body: Option<&str>,
    ) -> String {
        let mut entry = TrafficEntry::new(method, url);

        // Capture headers
        for (key, value) in headers.iter() {
            let value_str = value.to_str().unwrap_or("<binary>").to_string();
            let value_str = if self.config.redact_sensitive {
                self.redact_header(key.as_str(), &value_str)
            } else {
                value_str
            };
            entry.request_headers.push((key.to_string(), value_str));
        }

        // Capture body
        if self.config.log_bodies {
            if let Some(body) = body {
                entry.request_body = Some(if self.config.redact_sensitive {
                    self.redact_body(body)
                } else {
                    body.to_string()
                });
            }
        }

        let request_id = entry.request_id.clone();

        if self.is_enabled() {
            debug!(
                request_id = %entry.request_id,
                method = %entry.method,
                url = %entry.url,
                "HTTP Request"
            );
            trace!("Request details:\n{}", entry.format());
        }

        // Store in buffer
        self.log_buffer.write().await.push(entry);

        request_id
    }

    /// Log a response
    pub async fn log_response(
        &self,
        request_id: &str,
        status: u16,
        headers: &reqwest::header::HeaderMap,
        body: Option<&str>,
        duration_ms: u64,
    ) {
        let mut buffer = self.log_buffer.write().await;

        if let Some(entry) = buffer.iter_mut().find(|e| e.request_id == request_id) {
            entry.response_status = Some(status);
            entry.duration_ms = Some(duration_ms);

            // Capture response headers (with redaction if enabled)
            for (key, value) in headers.iter() {
                let value_str = value.to_str().unwrap_or("<binary>").to_string();
                let value_str = if self.config.redact_sensitive {
                    self.redact_header(key.as_str(), &value_str)
                } else {
                    value_str
                };
                entry.response_headers.push((key.to_string(), value_str));
            }

            // Capture response body (with redaction if enabled)
            if self.config.log_bodies {
                if let Some(body) = body {
                    entry.response_body = Some(if self.config.redact_sensitive {
                        self.redact_body(body)
                    } else {
                        body.to_string()
                    });
                }
            }

            if self.is_enabled() {
                debug!(
                    request_id = %request_id,
                    status = status,
                    duration_ms = duration_ms,
                    "HTTP Response"
                );
                trace!("Response details:\n{}", entry.format());
            }

            // Write to file if configured
            if let Some(ref path) = self.config.log_file {
                if let Err(e) = self.write_to_file(path, &entry.format()).await {
                    tracing::warn!("Failed to write debug log to file: {}", e);
                }
            }
        }
    }

    /// Log an error
    pub async fn log_error(&self, request_id: &str, error: &str) {
        let mut buffer = self.log_buffer.write().await;

        if let Some(entry) = buffer.iter_mut().find(|e| e.request_id == request_id) {
            entry.error = Some(error.to_string());

            if self.is_enabled() {
                debug!(
                    request_id = %request_id,
                    error = %error,
                    "HTTP Error"
                );
            }
        }
    }

    /// Get all captured traffic entries
    pub async fn get_traffic(&self) -> Vec<TrafficEntry> {
        self.log_buffer.read().await.clone()
    }

    /// Clear the traffic buffer
    pub async fn clear(&self) {
        self.log_buffer.write().await.clear();
    }

    /// Export all traffic to a string
    pub async fn export(&self) -> String {
        let buffer = self.log_buffer.read().await;
        buffer
            .iter()
            .map(|e| e.format())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Redact sensitive header values
    fn redact_header(&self, key: &str, value: &str) -> String {
        let key_lower = key.to_lowercase();
        if key_lower.contains("authorization")
            || key_lower.contains("cookie")
            || key_lower.contains("token")
            || key_lower.contains("auth")
        {
            if value.len() > 10 {
                format!("{}...REDACTED", &value[..5])
            } else {
                "REDACTED".to_string()
            }
        } else {
            value.to_string()
        }
    }

    /// Redact sensitive body content
    fn redact_body(&self, body: &str) -> String {
        // Try to parse as form data or JSON and redact password fields
        if body.contains("Password=") {
            // Form-encoded
            body.split('&')
                .map(|pair| {
                    if pair.to_lowercase().starts_with("password=") {
                        "Password=REDACTED"
                    } else {
                        pair
                    }
                })
                .collect::<Vec<_>>()
                .join("&")
        } else if let Ok(mut json) = serde_json::from_str::<serde_json::Value>(body) {
            // JSON
            Self::redact_json_passwords(&mut json);
            serde_json::to_string(&json).unwrap_or_else(|_| body.to_string())
        } else {
            body.to_string()
        }
    }

    /// Recursively redact password fields in JSON
    fn redact_json_passwords(value: &mut serde_json::Value) {
        match value {
            serde_json::Value::Object(map) => {
                for (key, val) in map.iter_mut() {
                    let key_lower = key.to_lowercase();
                    if key_lower.contains("password")
                        || key_lower.contains("secret")
                        || key_lower.contains("token")
                        || key_lower == "pin"
                    {
                        *val = serde_json::Value::String("REDACTED".to_string());
                    } else {
                        Self::redact_json_passwords(val);
                    }
                }
            }
            serde_json::Value::Array(arr) => {
                for item in arr.iter_mut() {
                    Self::redact_json_passwords(item);
                }
            }
            _ => {}
        }
    }

    /// Write to log file
    async fn write_to_file(&self, path: &PathBuf, content: &str) -> std::io::Result<()> {
        use tokio::io::AsyncWriteExt;
        let mut file = tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
            .await?;
        file.write_all(content.as_bytes()).await?;
        file.write_all(b"\n").await?;
        Ok(())
    }
}

/// Helper for timing operations
pub struct Timer {
    start: Instant,
}

impl Timer {
    /// Start a new timer
    pub fn start() -> Self {
        Self {
            start: Instant::now(),
        }
    }

    /// Get elapsed time in milliseconds
    pub fn elapsed_ms(&self) -> u64 {
        self.start.elapsed().as_millis() as u64
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_redact_body_form() {
        let debugger = HttpDebugger::new(DebugConfig::enabled());
        let body = "UserName=test@example.com&Password=secret123&Remember=true";
        let redacted = debugger.redact_body(body);
        assert!(redacted.contains("Password=REDACTED"));
        assert!(redacted.contains("UserName=test@example.com"));
    }

    #[test]
    fn test_redact_body_json() {
        let debugger = HttpDebugger::new(DebugConfig::enabled());
        let body = r#"{"username":"test","password":"secret","pin":"1234"}"#;
        let redacted = debugger.redact_body(body);
        assert!(redacted.contains("REDACTED"));
        assert!(!redacted.contains("secret"));
        assert!(!redacted.contains("1234"));
    }

    #[test]
    fn test_redact_header() {
        let debugger = HttpDebugger::new(DebugConfig::enabled());

        let cookie = debugger.redact_header("Cookie", ".ASPXAUTH=verylongtokenvalue");
        assert!(cookie.contains("REDACTED"));

        let content_type = debugger.redact_header("Content-Type", "application/json");
        assert_eq!(content_type, "application/json");
    }
}