wafrift-types 0.2.16

Core types for WAF Rift — Request, Technique, EvasionResult, Config, Error.
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
//! HTTP method and request types — the foundation layer all wafrift crates depend on.
//!
//! Intentionally simple — no dependency on any HTTP library. The transport
//! layer converts to/from `reqwest::Request` or raw bytes as needed.

use std::fmt;

use serde::{Deserialize, Serialize};

/// HTTP method — enforced at the type level instead of a bare `String`.
///
/// Using an enum prevents typos like `"POSTT"` and makes exhaustive
/// matching possible. The `Custom` variant preserves extensibility.
///
/// # Examples
///
/// Parse from a header line and route on the variant:
///
/// ```
/// use wafrift_types::request::Method;
///
/// let m: Method = "POST".parse().unwrap();
/// assert_eq!(m, Method::Post);
///
/// // Lowercase parses fine — case-insensitive.
/// let m: Method = "delete".parse().unwrap();
/// assert_eq!(m, Method::Delete);
///
/// // Anything not in the standard set becomes `Custom`.
/// let m: Method = "PURGE".parse().unwrap();
/// assert_eq!(m, Method::Custom("PURGE".to_string()));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Method {
    /// HTTP GET.
    Get,
    /// HTTP POST.
    Post,
    /// HTTP PUT.
    Put,
    /// HTTP DELETE.
    Delete,
    /// HTTP PATCH.
    Patch,
    /// HTTP HEAD.
    Head,
    /// HTTP OPTIONS.
    Options,
    /// Non-standard or extension method.
    Custom(String),
}

impl std::str::FromStr for Method {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s.to_ascii_uppercase().as_str() {
            "GET" => Self::Get,
            "POST" => Self::Post,
            "PUT" => Self::Put,
            "DELETE" => Self::Delete,
            "PATCH" => Self::Patch,
            "HEAD" => Self::Head,
            "OPTIONS" => Self::Options,
            other => Self::Custom(other.to_string()),
        })
    }
}

impl Method {
    /// Return the method as an uppercase string slice.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Get => "GET",
            Self::Post => "POST",
            Self::Put => "PUT",
            Self::Delete => "DELETE",
            Self::Patch => "PATCH",
            Self::Head => "HEAD",
            Self::Options => "OPTIONS",
            Self::Custom(s) => s.as_str(),
        }
    }

    /// Check if this method typically carries a request body.
    #[must_use]
    pub fn has_body(&self) -> bool {
        matches!(self, Self::Post | Self::Put | Self::Patch | Self::Custom(_))
    }
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl From<&str> for Method {
    fn from(s: &str) -> Self {
        s.parse().unwrap_or_else(|_| Method::Custom(s.to_string()))
    }
}

impl From<String> for Method {
    fn from(s: String) -> Self {
        s.parse().unwrap_or(Method::Custom(s))
    }
}

/// A request that wafrift can transform.
///
/// Intentionally simple — no HTTP library dependency. The transport
/// layer converts to/from `reqwest::Request` or raw bytes as needed.
///
/// # Construction
///
/// Use the provided constructors ([`Request::get`], [`Request::post`], etc.)
/// and builder methods ([`Request::header`], [`Request::with_body`]).
/// Direct struct construction is prevented by `#[non_exhaustive]`.
///
/// # Examples
///
/// Build a GET request, attach a header, then a JSON POST body:
///
/// ```
/// use wafrift_types::request::{Method, Request};
///
/// let r = Request::get("https://example.com/api")
///     .header("Accept", "application/json");
/// assert_eq!(r.method(), &Method::Get);
/// assert_eq!(r.url(), "https://example.com/api");
/// assert_eq!(r.headers().len(), 1);
/// assert_eq!(r.get_header("Accept"), Some("application/json"));
/// assert!(!r.has_body());
///
/// let r = Request::post("https://example.com/login", b"u=admin&p=admin".to_vec());
/// assert_eq!(r.method(), &Method::Post);
/// assert_eq!(r.body_bytes().unwrap(), b"u=admin&p=admin");
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Request {
    /// HTTP method.
    pub method: Method,
    /// Full request URL.
    pub url: String,
    /// Request headers as `(name, value)` pairs.
    pub headers: Vec<(String, String)>,
    /// Optional request body.
    pub body: Option<Vec<u8>>,
}

impl Request {
    /// Create a GET request.
    pub fn get(url: impl Into<String>) -> Self {
        Self {
            method: Method::Get,
            url: url.into(),
            headers: Vec::new(),
            body: None,
        }
    }

    /// Create a POST request with a body.
    pub fn post(url: impl Into<String>, body: impl Into<Vec<u8>>) -> Self {
        Self {
            method: Method::Post,
            url: url.into(),
            headers: Vec::new(),
            body: Some(body.into()),
        }
    }

    /// Create a PUT request with a body.
    pub fn put(url: impl Into<String>, body: impl Into<Vec<u8>>) -> Self {
        Self {
            method: Method::Put,
            url: url.into(),
            headers: Vec::new(),
            body: Some(body.into()),
        }
    }

    /// Create a DELETE request.
    pub fn delete(url: impl Into<String>) -> Self {
        Self {
            method: Method::Delete,
            url: url.into(),
            headers: Vec::new(),
            body: None,
        }
    }

    /// Create a request with an arbitrary method.
    pub fn with_method(method: impl Into<Method>, url: impl Into<String>) -> Self {
        Self {
            method: method.into(),
            url: url.into(),
            headers: Vec::new(),
            body: None,
        }
    }

    // ── Accessors ────────────────────────────────────────────────

    /// Returns a reference to the HTTP method.
    #[must_use]
    pub fn method(&self) -> &Method {
        &self.method
    }

    /// Returns a mutable reference to the HTTP method.
    pub fn method_mut(&mut self) -> &mut Method {
        &mut self.method
    }

    /// Returns the URL as a string slice.
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Returns a mutable reference to the URL.
    pub fn url_mut(&mut self) -> &mut String {
        &mut self.url
    }

    /// Returns a slice of the header pairs.
    #[must_use]
    pub fn headers(&self) -> &[(String, String)] {
        &self.headers
    }

    /// Returns a mutable reference to the headers vec.
    pub fn headers_mut(&mut self) -> &mut Vec<(String, String)> {
        &mut self.headers
    }

    /// Returns a reference to the body, if present.
    #[must_use]
    pub fn body_bytes(&self) -> Option<&[u8]> {
        self.body.as_deref()
    }

    /// Sets the body, replacing any existing body.
    pub fn set_body(&mut self, body: impl Into<Vec<u8>>) {
        self.body = Some(body.into());
    }

    /// Clears the body.
    pub fn clear_body(&mut self) {
        self.body = None;
    }

    // ── Builder methods ──────────────────────────────────────────

    /// Add a header to the request (builder pattern).
    #[must_use]
    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }

    /// Add a header to the request (mutable reference version).
    pub fn add_header(&mut self, name: impl Into<String>, value: impl Into<String>) {
        self.headers.push((name.into(), value.into()));
    }

    /// Set the request body.
    #[must_use]
    pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = Some(body.into());
        self
    }

    // ── Query methods ────────────────────────────────────────────

    /// Get the value of a header by name (case-insensitive).
    #[must_use]
    pub fn get_header(&self, name: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(name))
            .map(|(_, v)| v.as_str())
    }

    /// Get all values for a header name (case-insensitive).
    #[must_use]
    pub fn get_headers(&self, name: &str) -> Vec<&str> {
        self.headers
            .iter()
            .filter(|(k, _)| k.eq_ignore_ascii_case(name))
            .map(|(_, v)| v.as_str())
            .collect()
    }

    /// Get the Content-Type header value.
    #[must_use]
    pub fn content_type(&self) -> Option<&str> {
        self.get_header("content-type")
    }

    /// Check if this request has a body.
    #[must_use]
    pub fn has_body(&self) -> bool {
        self.body.as_ref().is_some_and(|b| !b.is_empty())
    }

    /// Get the body as a UTF-8 string, if present and valid.
    #[must_use]
    pub fn body_str(&self) -> Option<&str> {
        self.body.as_ref().and_then(|b| std::str::from_utf8(b).ok())
    }
}

impl fmt::Display for Request {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.method, self.url)?;
        if let Some(ct) = self.content_type() {
            write!(f, " [{ct}]")?;
        }
        if let Some(body) = &self.body {
            write!(f, " ({} bytes)", body.len())?;
        }
        Ok(())
    }
}

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

    #[test]
    fn method_from_str() {
        assert_eq!("GET".parse::<Method>().unwrap(), Method::Get);
        assert_eq!("post".parse::<Method>().unwrap(), Method::Post);
        assert_eq!(
            "PURGE".parse::<Method>().unwrap(),
            Method::Custom("PURGE".into())
        );
    }

    #[test]
    fn method_as_str_roundtrip() {
        for method in &[
            Method::Get,
            Method::Post,
            Method::Put,
            Method::Delete,
            Method::Patch,
            Method::Head,
            Method::Options,
        ] {
            assert_eq!(method.as_str().parse::<Method>().unwrap(), *method);
        }
    }

    #[test]
    fn method_has_body() {
        assert!(!Method::Get.has_body());
        assert!(Method::Post.has_body());
        assert!(Method::Put.has_body());
        assert!(!Method::Head.has_body());
    }

    #[test]
    fn method_display() {
        assert_eq!(Method::Get.to_string(), "GET");
        assert_eq!(Method::Custom("PURGE".into()).to_string(), "PURGE");
    }

    #[test]
    fn request_builder() {
        let req = Request::get("https://example.com")
            .header("X-Test", "value")
            .header("Content-Type", "text/html");
        assert_eq!(req.get_header("x-test"), Some("value"));
        assert_eq!(req.content_type(), Some("text/html"));
    }

    #[test]
    fn request_get_headers_multiple() {
        let mut req = Request::get("https://example.com");
        req.add_header("Cookie", "a=1");
        req.add_header("Cookie", "b=2");
        assert_eq!(req.get_headers("cookie").len(), 2);
    }

    #[test]
    fn request_body_str() {
        let req = Request::post("https://example.com", b"hello".to_vec());
        assert_eq!(req.body_str(), Some("hello"));
        assert!(req.has_body());
    }

    #[test]
    fn request_display() {
        let req = Request::post("https://example.com/api", b"data".to_vec())
            .header("Content-Type", "application/json");
        let display = req.to_string();
        assert!(display.contains("POST"));
        assert!(display.contains("example.com"));
        assert!(display.contains("4 bytes"));
    }

    #[test]
    fn request_equality() {
        let a = Request::get("https://example.com");
        let b = Request::get("https://example.com");
        assert_eq!(a, b);
    }

    #[test]
    fn request_with_method() {
        let req = Request::with_method("PURGE", "https://example.com/cache");
        assert_eq!(req.method, Method::Custom("PURGE".into()));
    }

    #[test]
    fn request_put_and_delete() {
        let put = Request::put("https://example.com/api", b"data".to_vec());
        assert_eq!(put.method, Method::Put);
        assert!(put.has_body());

        let del = Request::delete("https://example.com/api/1");
        assert_eq!(del.method, Method::Delete);
        assert!(!del.has_body());
    }

    #[test]
    fn request_serde_roundtrip() {
        let req = Request::post("https://example.com", b"body".to_vec()).header("X-Test", "value");
        let json = serde_json::to_string(&req).expect("serialize");
        let deserialized: Request = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(req, deserialized);
    }

    #[test]
    fn request_accessors() {
        let req =
            Request::post("https://example.com", b"test".to_vec()).header("Host", "example.com");
        assert_eq!(req.url(), "https://example.com");
        assert_eq!(*req.method(), Method::Post);
        assert_eq!(req.headers().len(), 1);
        assert_eq!(req.body_bytes(), Some(b"test".as_slice()));
    }

    #[test]
    fn request_mutators() {
        let mut req = Request::get("https://example.com");
        req.set_body(b"new body");
        assert!(req.has_body());
        assert_eq!(req.body_str(), Some("new body"));
        req.clear_body();
        assert!(!req.has_body());
        *req.url_mut() = "https://other.com".to_string();
        assert_eq!(req.url(), "https://other.com");
    }
}