braid_http_rs 0.1.5

Unified Braid Protocol implementation in Rust, including Braid-HTTP, Antimatter CRDT, and BraidFS.
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
//! Main Braid HTTP client implementation.

use crate::core::client::config::ClientConfig;
#[cfg(not(target_arch = "wasm32"))]
use crate::core::client::native_network::NativeNetwork;
#[cfg(target_arch = "wasm32")]
use crate::core::client::wasm_network::WasmNetwork;
use crate::core::error::{BraidError, Result};
use crate::core::traits::BraidNetwork;
use crate::core::types::{BraidRequest, BraidResponse};
use std::sync::Arc;

/// The main Braid HTTP client
#[derive(Clone)]
pub struct BraidClient {
    #[cfg(not(target_arch = "wasm32"))]
    pub network: Arc<NativeNetwork>,
    #[cfg(target_arch = "wasm32")]
    pub network: Arc<WasmNetwork>,
    pub config: Arc<ClientConfig>,
    /// Active multiplexers by origin.
    #[cfg(not(target_arch = "wasm32"))]
    pub multiplexers: Arc<
        tokio::sync::Mutex<
            std::collections::HashMap<String, Arc<crate::core::client::multiplex::Multiplexer>>,
        >,
    >,
}

impl BraidClient {
    #[cfg(not(target_arch = "wasm32"))]
    pub fn network(&self) -> &Arc<NativeNetwork> {
        &self.network
    }

    #[cfg(target_arch = "wasm32")]
    pub fn network(&self) -> &Arc<WasmNetwork> {
        &self.network
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn client(&self) -> &reqwest::Client {
        self.network.client()
    }

    pub fn new() -> Result<Self> {
        Self::with_config(ClientConfig::default())
    }

    pub fn with_config(config: ClientConfig) -> Result<Self> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            let mut builder = reqwest::Client::builder()
                .timeout(std::time::Duration::from_millis(config.request_timeout_ms))
                .pool_idle_timeout(std::time::Duration::from_secs(90))
                .pool_max_idle_per_host(config.max_total_connections as usize);

            if !config.proxy_url.is_empty() {
                if let Ok(proxy) = reqwest::Proxy::all(&config.proxy_url) {
                    builder = builder.proxy(proxy);
                }
            }

            let client = builder
                .build()
                .map_err(|e| BraidError::Config(e.to_string()))?;
            let network = Arc::new(NativeNetwork::new(client));

            Ok(BraidClient {
                network,
                config: Arc::new(config),
                multiplexers: Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new())),
            })
        }

        #[cfg(target_arch = "wasm32")]
        {
            let network = Arc::new(WasmNetwork);
            Ok(BraidClient {
                network,
                config: Arc::new(config),
            })
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub fn with_client(client: reqwest::Client) -> Result<Self> {
        Ok(BraidClient {
            network: Arc::new(NativeNetwork::new(client)),
            config: Arc::new(ClientConfig::default()),
            multiplexers: Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new())),
        })
    }

    pub async fn get(&self, url: &str) -> Result<BraidResponse> {
        self.fetch(url, BraidRequest::new()).await
    }

    pub async fn put(
        &self,
        url: &str,
        body: &str,
        mut request: BraidRequest,
    ) -> Result<BraidResponse> {
        request = request.with_method("PUT").with_body(body.to_string());

        if request.content_type.is_none() {
            request = request.with_content_type("application/json");
        }

        if request.version.is_none() {
            let random_version = uuid::Uuid::new_v4().to_string();
            request.version = Some(vec![crate::core::types::Version::new(&random_version)]);
        }

        self.fetch(url, request).await
    }

    pub async fn post(
        &self,
        url: &str,
        body: &str,
        mut request: BraidRequest,
    ) -> Result<BraidResponse> {
        request = request.with_method("POST").with_body(body.to_string());
        self.fetch(url, request).await
    }

    pub async fn poke(&self, recipient_endpoint: &str, post_url: &str) -> Result<BraidResponse> {
        let request = BraidRequest::new()
            .with_method("POST")
            .with_body(post_url.to_string())
            .with_content_type("text/plain");

        self.fetch(recipient_endpoint, request).await
    }

    pub async fn fetch(&self, url: &str, request: BraidRequest) -> Result<BraidResponse> {
        self.fetch_with_retries(url, request).await
    }

    pub async fn subscribe(
        &self,
        url: &str,
        request: BraidRequest,
    ) -> Result<crate::core::client::Subscription> {
        self.log_request(url, &request);
        let rx = self.network.subscribe(url, request).await?;
        Ok(crate::core::client::Subscription::new(rx))
    }

    async fn fetch_with_retries(&self, url: &str, request: BraidRequest) -> Result<BraidResponse> {
        let retry_config = request.retry.clone().unwrap_or_else(|| {
            if self.config.max_retries == 0 {
                crate::core::client::retry::RetryConfig::no_retry()
            } else {
                crate::core::client::retry::RetryConfig::default()
                    .with_max_retries(self.config.max_retries)
                    .with_initial_backoff(std::time::Duration::from_millis(
                        self.config.retry_delay_ms,
                    ))
            }
        });

        let mut retry_state = crate::core::client::retry::RetryState::new(retry_config);

        loop {
            self.log_request(url, &request);

            match self.fetch_internal(url, &request).await {
                Ok(response) => {
                    self.log_response(url, &response);

                    let status = response.status;
                    if (400..600).contains(&status) {
                        let retry_after = response
                            .headers
                            .get("retry-after")
                            .and_then(|v| crate::core::client::retry::parse_retry_after(v));

                        match retry_state.should_retry_status(status, retry_after) {
                            crate::core::client::retry::RetryDecision::Retry(delay) => {
                                if self.config.enable_logging {
                                    tracing::warn!(
                                        "Request status {} (attempt {}), retrying in {:?}",
                                        status,
                                        retry_state.attempts,
                                        delay
                                    );
                                }
                                crate::core::client::utils::sleep(delay).await;
                                continue;
                            }
                            crate::core::client::retry::RetryDecision::DontRetry => {
                                return Ok(response);
                            }
                        }
                    }
                    retry_state.reset();
                    return Ok(response);
                }
                Err(e) => {
                    let is_abort = matches!(&e, BraidError::Aborted);

                    match retry_state.should_retry_error(is_abort) {
                        crate::core::client::retry::RetryDecision::Retry(delay) => {
                            if self.config.enable_logging {
                                tracing::warn!(
                                    "Request failed (attempt {}), retrying in {:?}: {}",
                                    retry_state.attempts,
                                    delay,
                                    e
                                );
                            }
                            crate::core::client::utils::sleep(delay).await;
                            continue;
                        }
                        crate::core::client::retry::RetryDecision::DontRetry => {
                            return Err(e);
                        }
                    }
                }
            }
        }
    }

    async fn fetch_internal(&self, url: &str, request: &BraidRequest) -> Result<BraidResponse> {
        self.network.fetch(url, request.clone()).await
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn fetch_multiplexed(
        &self,
        url: &str,
        mut request: BraidRequest,
    ) -> Result<BraidResponse> {
        let origin = self.origin_from_url(url)?;

        let mut multiplexers = self.multiplexers.lock().await;
        let multiplexer = if let Some(m) = multiplexers.get(&origin) {
            m.clone()
        } else {
            let multiplex_url = format!("{}/.multiplex", origin);
            let m_id = format!("{:x}", rand::random::<u64>());
            let m = Arc::new(crate::core::client::multiplex::Multiplexer::new(
                origin.clone(),
                m_id,
            ));

            let client = self.clone();
            let m_inner = m.clone();
            let origin_task = origin.clone();
            crate::core::client::utils::spawn_task(async move {
                let run_multiplex = async {
                    let multiplex_method =
                        reqwest::Method::from_bytes(b"MULTIPLEX").map_err(|e| {
                            BraidError::Protocol(format!("Invalid multiplex method: {}", e))
                        })?;
                    let multiplex_header_name = reqwest::header::HeaderName::from_bytes(
                        crate::core::protocol::constants::headers::MULTIPLEX_VERSION
                            .as_str()
                            .as_bytes(),
                    )
                    .map_err(|e| {
                        BraidError::Protocol(format!("Invalid multiplex header: {}", e))
                    })?;

                    let resp = client
                        .network
                        .client()
                        .request(multiplex_method, &multiplex_url)
                        .header(multiplex_header_name, "1.0")
                        .send()
                        .await
                        .map_err(|e| {
                            BraidError::Http(format!(
                                "Failed to establish multiplexed connection to {}: {}",
                                multiplex_url, e
                            ))
                        })?;

                    m_inner.run_stream(resp).await
                };

                if let Err(e) = run_multiplex.await {
                    tracing::error!("Multiplexer task failed for {}: {}", origin_task, e);
                }
            });

            multiplexers.insert(origin.clone(), m.clone());
            m
        };
        drop(multiplexers);

        let r_id = format!("{:x}", rand::random::<u32>());
        let (tx, rx) = async_channel::bounded(100);
        multiplexer.add_request(r_id.clone(), tx).await;

        request.extra_headers.insert(
            crate::core::protocol::constants::headers::MULTIPLEX_THROUGH.to_string(),
            format!("/.well-known/multiplexer/{}/{}", multiplexer.id, r_id),
        );

        self.log_request(url, &request);
        let initial_response = self.fetch_internal(url, &request).await?;
        self.log_response(url, &initial_response);

        if initial_response.status == 293 {
            let mut response_buffer = Vec::new();
            let mut headers_parsed = None;

            while let Ok(chunk) = rx.recv().await {
                response_buffer.extend_from_slice(&chunk);

                if headers_parsed.is_none() {
                    if let Ok((status, headers, body_start)) =
                        crate::core::protocol::parse_tunneled_response(&response_buffer)
                    {
                        headers_parsed = Some((status, headers, body_start));
                    }
                }
            }

            if let Some((status, headers, body_start)) = headers_parsed {
                let body = bytes::Bytes::copy_from_slice(&response_buffer[body_start..]);
                return Ok(BraidResponse {
                    status,
                    headers,
                    body,
                    is_subscription: false,
                });
            } else {
                return Err(crate::core::error::BraidError::Protocol(
                    "Multiplexed response ended before headers received".to_string(),
                ));
            }
        }

        Ok(initial_response)
    }

    pub fn config(&self) -> &ClientConfig {
        &self.config
    }

    fn log_request(&self, _url: &str, _request: &BraidRequest) {}

    fn log_response(&self, _url: &str, _response: &BraidResponse) {}

    fn origin_from_url(&self, url: &str) -> Result<String> {
        let parsed_url = url::Url::parse(url).map_err(|e| BraidError::Config(e.to_string()))?;
        Ok(format!(
            "{}://{}",
            parsed_url.scheme(),
            parsed_url.host_str().unwrap_or("")
        ))
    }
}

impl Default for BraidClient {
    fn default() -> Self {
        Self::new().unwrap_or_else(|_| {
            let network = Arc::new(NativeNetwork::new(reqwest::Client::new()));
            BraidClient {
                network,
                config: Arc::new(ClientConfig::default()),
                multiplexers: Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new())),
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::types::BraidRequest;

    #[test]
    fn test_client_init() {
        let client = BraidClient::new().unwrap();
        assert_eq!(client.config().max_retries, 3);
    }

    #[test]
    fn test_origin_extraction() {
        let client = BraidClient::new().unwrap();
        assert_eq!(
            client.origin_from_url("http://example.com/foo").unwrap(),
            "http://example.com"
        );
    }

    #[test]
    fn test_put_request_prep() {
        let mut req = BraidRequest::new();
        req = req.with_method("PUT").with_body("test".to_string());
        if req.content_type.is_none() {
            req = req.with_content_type("application/json");
        }
        if req.version.is_none() {
            req.version = Some(vec![crate::core::types::Version::new("test-version")]);
        }
        assert_eq!(req.method, "PUT");
        assert_eq!(req.version.unwrap()[0].to_string(), "test-version");
    }

    #[test]
    fn test_poke_request_prep() {
        let req = BraidRequest::new()
            .with_method("POST")
            .with_body("http://example.com/post")
            .with_content_type("text/plain");
        assert_eq!(req.method, "POST");
        assert_eq!(
            String::from_utf8_lossy(&req.body),
            "http://example.com/post"
        );
    }
}