mik-sdk 0.1.2

Ergonomic macros for WASI HTTP handlers - ok!, error!, json!
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
//! HTTP request builder for outbound requests.

use super::error::{Error, Result};
use super::response::Response;
use super::ssrf::{is_private_address, validate_authority, validate_percent_encoding};

// Re-export Method from request module (single source of truth)
pub use crate::request::Method;

/// URL scheme.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Scheme {
    /// HTTP (unencrypted).
    Http,
    /// HTTPS (TLS encrypted).
    Https,
}

impl std::fmt::Display for Scheme {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Http => f.write_str("http"),
            Self::Https => f.write_str("https"),
        }
    }
}

/// HTTP request builder for outbound requests.
///
/// Build a request and then send it using `send_with()` with your WASI bindings.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::get("https://api.example.com/data")
///     .header("Accept", "application/json")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
#[must_use = "request must be sent with .send_with()"]
#[non_exhaustive]
pub struct ClientRequest {
    method: Method,
    url: String,
    headers: Vec<(String, String)>,
    body: Option<Vec<u8>>,
    timeout_ns: Option<u64>,
    deny_private_ips: bool,
}

impl ClientRequest {
    /// Create a new request with the given method and URL.
    #[must_use]
    pub fn new(method: Method, url: &str) -> Self {
        Self {
            method,
            url: url.to_string(),
            headers: Vec::new(),
            body: None,
            timeout_ns: None,
            deny_private_ips: false,
        }
    }

    /// Add a header to the request.
    ///
    /// # Panics
    ///
    /// Panics if the header value contains CR (`\r`) or LF (`\n`) characters,
    /// which could enable header injection attacks.
    #[must_use]
    pub fn header(mut self, name: &str, value: &str) -> Self {
        assert!(
            !value.contains('\r') && !value.contains('\n'),
            "Header value must not contain CR or LF characters (header injection)"
        );
        self.headers.push((name.to_string(), value.to_string()));
        self
    }

    /// Forward trace ID header to outgoing request.
    ///
    /// If `trace_id` is `None`, no header is added.
    /// Use with `Request::trace_id()` to propagate trace context.
    ///
    /// ```no_run
    /// # use mik_sdk::http_client::{self, Response, Error};
    /// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
    /// #     Ok(Response::new(200, vec![], vec![]))
    /// # }
    /// # fn main() -> Result<(), Error> {
    /// # let trace_id: Option<&str> = None;
    /// let response = http_client::get("https://api.example.com/data")
    ///     .with_trace_id(trace_id)
    ///     .send_with(send)?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn with_trace_id(self, trace_id: Option<&str>) -> Self {
        use crate::constants::HEADER_TRACE_ID_TITLE;
        match trace_id {
            Some(id) => self.header(HEADER_TRACE_ID_TITLE, id),
            None => self,
        }
    }

    /// Set the request body.
    #[must_use]
    pub fn body(mut self, body: &[u8]) -> Self {
        self.body = Some(body.to_vec());
        self
    }

    /// Set JSON body (also sets Content-Type header).
    #[must_use]
    pub fn json(mut self, body: &[u8]) -> Self {
        use crate::constants::{HEADER_CONTENT_TYPE_TITLE, MIME_JSON};
        self.headers
            .push((HEADER_CONTENT_TYPE_TITLE.to_string(), MIME_JSON.to_string()));
        self.body = Some(body.to_vec());
        self
    }

    /// Set request timeout in milliseconds.
    ///
    /// Values over ~18 trillion ms are clamped to `u64::MAX` nanoseconds.
    #[must_use]
    pub const fn timeout_ms(mut self, ms: u64) -> Self {
        self.timeout_ns = Some(ms.saturating_mul(1_000_000));
        self
    }

    /// Set request timeout in nanoseconds.
    #[must_use]
    pub const fn timeout_ns(mut self, ns: u64) -> Self {
        self.timeout_ns = Some(ns);
        self
    }

    /// Deny requests to private/internal IP addresses (SSRF protection).
    ///
    /// When enabled, requests to the following will be rejected:
    /// - `localhost`, `127.x.x.x` (loopback)
    /// - `10.x.x.x` (private class A)
    /// - `172.16.x.x` - `172.31.x.x` (private class B)
    /// - `192.168.x.x` (private class C)
    /// - `169.254.x.x` (link-local)
    /// - `::1`, `fe80::` (IPv6 loopback/link-local)
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use mik_sdk::http_client::{self, Response, Error};
    /// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
    /// #     Ok(Response::new(200, vec![], vec![]))
    /// # }
    /// # fn main() -> Result<(), Error> {
    /// # let user_provided_url = "https://api.example.com/data";
    /// // Protect against SSRF when URL comes from user input
    /// let response = http_client::get(user_provided_url)
    ///     .deny_private_ips()
    ///     .send_with(send)?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub const fn deny_private_ips(mut self) -> Self {
        self.deny_private_ips = true;
        self
    }

    /// Check if private IP denial is enabled.
    #[must_use]
    pub const fn denies_private_ips(&self) -> bool {
        self.deny_private_ips
    }

    /// Get the HTTP method.
    #[must_use]
    pub const fn method(&self) -> Method {
        self.method
    }

    /// Get the URL.
    #[inline]
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Get the headers.
    #[inline]
    #[must_use]
    pub fn headers(&self) -> &[(String, String)] {
        &self.headers
    }

    /// Get the request body bytes.
    #[inline]
    #[must_use]
    pub fn body_bytes(&self) -> Option<&[u8]> {
        self.body.as_deref()
    }

    /// Get the timeout in nanoseconds.
    #[must_use]
    pub const fn timeout(&self) -> Option<u64> {
        self.timeout_ns
    }

    /// Check if private IPs are denied.
    #[must_use]
    pub const fn is_private_ips_denied(&self) -> bool {
        self.deny_private_ips
    }

    // =========================================================================
    // Sending
    // =========================================================================

    /// Send the request using a custom sender function.
    ///
    /// This method allows you to integrate with any HTTP client by providing
    /// a sender function that takes the request data and returns a response.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A function that sends the request and returns a `Result<Response, Error>`
    ///
    /// # Errors
    ///
    /// Returns an error if URL validation fails or the sender function fails.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use mik_sdk::http_client::{self, Error, Response};
    /// // Define a sender that uses WASI HTTP
    /// fn wasi_send(req: &http_client::ClientRequest) -> Result<Response, Error> {
    ///     // Convert to WASI types and send...
    ///     todo!("Implement WASI HTTP sending")
    /// }
    ///
    /// # fn main() -> Result<(), Error> {
    /// let response = http_client::get("https://api.example.com/users")
    ///     .send_with(wasi_send)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # For WASI HTTP
    ///
    /// When using with `wasi:http/outgoing-handler`, you need to implement
    /// the conversion between `ClientRequest` and WASI HTTP types.
    /// See the external-api example for a complete implementation.
    pub fn send_with<F>(self, sender: F) -> Result<Response>
    where
        F: FnOnce(&Self) -> Result<Response>,
    {
        // Validate URL before sending
        let _ = self.parse_url()?;
        sender(&self)
    }

    /// Parse the URL into scheme, authority, and path components.
    ///
    /// Returns `(scheme, authority, path_with_query)` tuple.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidUrl`] if:
    /// - URL doesn't start with `http://` or `https://`
    /// - Host is missing
    /// - Authority contains invalid characters
    /// - Port number is invalid (non-numeric or out of range)
    /// - SSRF protection is enabled and URL points to a private/internal address
    pub fn parse_url(&self) -> Result<(Scheme, String, String)> {
        // Parse scheme
        let (scheme, rest) = if self.url.starts_with("https://") {
            (Scheme::Https, &self.url[8..])
        } else if self.url.starts_with("http://") {
            (Scheme::Http, &self.url[7..])
        } else {
            return Err(Error::InvalidUrl(format!(
                "URL must start with `http://` or `https://`: `{}`",
                self.url
            )));
        };

        // Split authority and path
        let (authority, path) = rest
            .find('/')
            .map_or((rest, "/"), |idx| (&rest[..idx], &rest[idx..]));

        if authority.is_empty() {
            return Err(Error::InvalidUrl("missing host in URL".to_string()));
        }

        // Validate authority (host and optional port)
        validate_authority(authority)?;

        // Check for private IPs if SSRF protection is enabled
        if self.deny_private_ips && is_private_address(authority) {
            return Err(Error::InvalidUrl(format!(
                "request to private/internal address denied: `{authority}`"
            )));
        }

        // Validate percent-encoding in path
        validate_percent_encoding(path)?;

        Ok((scheme, authority.to_string(), path.to_string()))
    }
}

// ============================================================================
// Convenience constructors
// ============================================================================

/// Create a GET request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::get("https://api.example.com/users")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn get(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Get, url)
}

/// Create a POST request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(201, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::post("https://api.example.com/users")
///     .json(b"{\"name\":\"Alice\"}")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn post(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Post, url)
}

/// Create a PUT request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::put("https://api.example.com/users/123")
///     .json(b"{\"name\":\"Alice Updated\"}")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn put(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Put, url)
}

/// Create a DELETE request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(204, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::delete("https://api.example.com/users/123")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn delete(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Delete, url)
}

/// Create a PATCH request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::patch("https://api.example.com/users/123")
///     .json(b"{\"name\":\"Updated Name\"}")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn patch(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Patch, url)
}

/// Create a HEAD request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![("content-length".into(), "1024".into())], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::head("https://api.example.com/large-file")
///     .send_with(send)?;
///
/// let content_length = response.header("content-length");
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn head(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Head, url)
}

/// Create an OPTIONS request.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![("allow".into(), "GET, POST, PUT, DELETE".into())], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::options("https://api.example.com/users")
///     .send_with(send)?;
///
/// let allowed = response.header("allow");
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn options(url: &str) -> ClientRequest {
    ClientRequest::new(Method::Options, url)
}

/// Create a request with a custom method.
///
/// # Example
///
/// ```no_run
/// # use mik_sdk::http_client::{self, Response, Error};
/// # fn send(_req: &http_client::ClientRequest) -> Result<Response, Error> {
/// #     Ok(Response::new(200, vec![], vec![]))
/// # }
/// # fn main() -> Result<(), Error> {
/// let response = http_client::request(http_client::Method::Post, "https://api.example.com/data")
///     .header("Content-Type", "text/plain")
///     .body(b"Hello, World!")
///     .send_with(send)?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn request(method: Method, url: &str) -> ClientRequest {
    ClientRequest::new(method, url)
}