barehttp 0.0.1

A minimal, explicit HTTP client for Rust with no_std support and blocking I/O
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
use crate::client::HttpClient;
use crate::config::Config;
use crate::dns::DnsResolver;
use crate::error::Error;
use crate::headers::{HeaderName, Headers};
use crate::method::Method;
use crate::parser::Response;
use crate::parser::version::Version;
use crate::socket::BlockingSocket;
use crate::util::percent_encode;
use alloc::string::String;
use alloc::vec::Vec;
use core::marker::PhantomData;

/// Trait for types that can be converted into an HTTP body
pub trait IntoBody {
  /// Convert this type into a byte vector
  fn into_body(self) -> Vec<u8>;
}

impl IntoBody for Vec<u8> {
  fn into_body(self) -> Vec<u8> {
    self
  }
}

impl IntoBody for String {
  fn into_body(self) -> Vec<u8> {
    self.into_bytes()
  }
}

impl IntoBody for &str {
  fn into_body(self) -> Vec<u8> {
    self.as_bytes().to_vec()
  }
}

impl IntoBody for &[u8] {
  fn into_body(self) -> Vec<u8> {
    self.to_vec()
  }
}

/// Typestate marker indicating a request without a body
///
/// Used for HTTP methods like GET, HEAD, DELETE, OPTIONS.
pub struct WithoutBody;

/// Typestate marker indicating a request with a body
///
/// Used for HTTP methods like POST, PUT, PATCH.
pub struct WithBody;

/// Builder for constructing HTTP requests with compile-time body safety
///
/// Uses the typestate pattern to enforce body semantics at compile time.
/// Methods that require a body (POST, PUT, PATCH) return `ClientRequestBuilder<WithBody>`,
/// while methods without a body (GET, HEAD, etc.) return `ClientRequestBuilder<WithoutBody>`.
pub struct ClientRequestBuilder<S, D, B = WithoutBody> {
  client: HttpClient<S, D>,
  method: Method,
  url: String,
  headers: Headers,
  query_params: Vec<(String, String)>,
  form_data: Vec<(String, String)>,
  body: Option<Vec<u8>>,
  version: Version,
  request_config: Option<Config>,
  _phantom: PhantomData<B>,
}

impl<S, D, B> ClientRequestBuilder<S, D, B>
where
  S: BlockingSocket,
  D: DnsResolver,
{
  /// Add a header to the request
  #[must_use]
  pub fn header(
    mut self,
    name: impl Into<String>,
    value: impl Into<String>,
  ) -> Self {
    self.headers.insert(name, value);
    self
  }

  /// Add a URL-encoded query parameter
  #[must_use]
  pub fn query(
    mut self,
    key: impl Into<String>,
    value: impl Into<String>,
  ) -> Self {
    self.query_params.push((key.into(), value.into()));
    self
  }

  /// Add multiple URL-encoded query parameters from an iterator
  #[must_use]
  pub fn query_pairs<I, K, V>(
    mut self,
    iter: I,
  ) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
    K: Into<String>,
    V: Into<String>,
  {
    self
      .query_params
      .extend(iter.into_iter().map(|(k, v)| (k.into(), v.into())));
    self
  }

  /// Add a raw (non-encoded) query parameter
  #[must_use]
  pub fn query_raw(
    mut self,
    key: impl Into<String>,
    value: impl Into<String>,
  ) -> Self {
    let key_str = key.into();
    let value_str = value.into();
    self.query_params.push((key_str, value_str));
    self
  }

  /// Add multiple raw (non-encoded) query parameters from an iterator
  #[must_use]
  pub fn query_pairs_raw<I, K, V>(
    mut self,
    iter: I,
  ) -> Self
  where
    I: IntoIterator<Item = (K, V)>,
    K: Into<String>,
    V: Into<String>,
  {
    self
      .query_params
      .extend(iter.into_iter().map(|(k, v)| (k.into(), v.into())));
    self
  }

  /// Add a form data field (application/x-www-form-urlencoded)
  #[must_use]
  pub fn form(
    mut self,
    key: impl Into<String>,
    value: impl Into<String>,
  ) -> Self {
    self.form_data.push((key.into(), value.into()));
    self
  }

  /// Set the Content-Type header
  #[must_use]
  pub fn content_type(
    self,
    content_type: impl Into<String>,
  ) -> Self {
    self.header(HeaderName::CONTENT_TYPE, content_type)
  }

  /// Add a cookie to the request
  ///
  /// Cookies are automatically combined into a single Cookie header with semicolon separators.
  /// Multiple calls to this method will append cookies.
  ///
  /// # Example
  /// ```no_run
  /// # use barehttp::HttpClient;
  /// let mut client = HttpClient::new()?;
  /// client.get("http://example.com")
  ///     .cookie("session", "abc123")
  ///     .cookie("user", "john")
  ///     .call()?;
  /// # Ok::<(), barehttp::Error>(())
  /// ```
  #[must_use]
  pub fn cookie(
    mut self,
    name: impl Into<String>,
    value: impl Into<String>,
  ) -> Self {
    use alloc::format;
    let name_str = name.into();
    let value_str = value.into();
    let cookie_value = format!("{name_str}={value_str}");

    // Check if Cookie header already exists
    if let Some(existing) = self.headers.get(HeaderName::COOKIE) {
      // Append to existing cookies with semicolon separator
      let combined = format!("{existing}; {cookie_value}");
      self.headers.remove(HeaderName::COOKIE);
      self.headers.insert(HeaderName::COOKIE, combined);
    } else {
      // Create new Cookie header
      self.headers.insert(HeaderName::COOKIE, cookie_value);
    }

    self
  }

  /// Override the request URL
  #[must_use]
  pub fn uri(
    mut self,
    url: impl Into<String>,
  ) -> Self {
    self.url = url.into();
    self
  }

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

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

  /// Get immutable reference to request headers
  #[must_use]
  pub const fn headers_ref(&self) -> &Headers {
    &self.headers
  }

  /// Get mutable reference to request headers
  #[must_use]
  pub const fn headers_mut(&mut self) -> &mut Headers {
    &mut self.headers
  }

  /// Set the HTTP protocol version
  #[must_use]
  pub const fn version(
    mut self,
    version: Version,
  ) -> Self {
    self.version = version;
    self
  }

  /// Get the HTTP protocol version
  #[must_use]
  pub const fn version_ref(&self) -> Version {
    self.version
  }

  /// Override the client configuration for this request
  #[must_use]
  pub fn with_config(
    mut self,
    config: Config,
  ) -> Self {
    self.request_config = Some(config);
    self
  }

  /// Get the request-specific configuration if set
  #[must_use]
  pub const fn config_ref(&self) -> Option<&Config> {
    self.request_config.as_ref()
  }

  fn build_url(&self) -> String {
    if self.query_params.is_empty() {
      return self.url.clone();
    }

    let mut url = self.url.clone();
    let separator = if url.contains('?') {
      '&'
    } else {
      '?'
    };
    url.push(separator);

    for (i, (key, value)) in self.query_params.iter().enumerate() {
      if i > 0 {
        url.push('&');
      }
      url.push_str(&percent_encode(key));
      url.push('=');
      url.push_str(&percent_encode(value));
    }

    url
  }

  fn build_form_url_encoded<I, K, V>(iter: I) -> Vec<u8>
  where
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<str>,
    V: AsRef<str>,
  {
    let mut body = String::new();
    for (i, (key, value)) in iter.into_iter().enumerate() {
      if i > 0 {
        body.push('&');
      }
      body.push_str(&percent_encode(key.as_ref()));
      body.push('=');
      body.push_str(&percent_encode(value.as_ref()));
    }
    body.into_bytes()
  }

  fn build_form_body(&self) -> Vec<u8> {
    let mut body = String::new();
    for (i, (key, value)) in self.form_data.iter().enumerate() {
      if i > 0 {
        body.push('&');
      }
      body.push_str(&percent_encode(key));
      body.push('=');
      body.push_str(&percent_encode(value));
    }
    body.into_bytes()
  }
}

impl<S, D> ClientRequestBuilder<S, D, WithoutBody>
where
  S: BlockingSocket,
  D: DnsResolver,
{
  /// Create a new request builder for methods without a body
  pub fn new(
    client: HttpClient<S, D>,
    method: Method,
    url: impl Into<String>,
  ) -> Self {
    Self {
      client,
      method,
      url: url.into(),
      headers: Headers::new(),
      query_params: Vec::new(),
      form_data: Vec::new(),
      body: None,
      version: Version::HTTP_11,
      request_config: None,
      _phantom: PhantomData,
    }
  }

  /// # Errors
  /// Returns an error if the request fails
  pub fn call(self) -> Result<Response, Error> {
    let url = self.build_url();

    let body = if self.form_data.is_empty() {
      self.body
    } else {
      Some(self.build_form_body())
    };

    self
      .client
      .request(self.method, &url, &self.headers, body, self.request_config.as_ref())
  }

  /// Force this request to allow a body (e.g., for DELETE with body)
  #[must_use]
  pub fn force_send_body(self) -> ClientRequestBuilder<S, D, WithBody> {
    ClientRequestBuilder {
      client: self.client,
      method: self.method,
      url: self.url,
      headers: self.headers,
      query_params: self.query_params,
      form_data: self.form_data,
      body: self.body,
      version: self.version,
      request_config: self.request_config,
      _phantom: PhantomData,
    }
  }
}

impl<S, D> ClientRequestBuilder<S, D, WithBody>
where
  S: BlockingSocket,
  D: DnsResolver,
{
  /// Create a new request builder for methods with a body
  pub fn new(
    client: HttpClient<S, D>,
    method: Method,
    url: impl Into<String>,
  ) -> Self {
    Self {
      client,
      method,
      url: url.into(),
      headers: Headers::new(),
      query_params: Vec::new(),
      form_data: Vec::new(),
      body: None,
      version: Version::HTTP_11,
      request_config: None,
      _phantom: PhantomData,
    }
  }

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

  /// # Errors
  /// Returns an error if the request fails
  pub fn call(self) -> Result<Response, Error> {
    let url = self.build_url();

    let body = if self.form_data.is_empty() {
      self.body
    } else {
      Some(self.build_form_body())
    };

    self
      .client
      .request(self.method, &url, &self.headers, body, self.request_config.as_ref())
  }

  /// # Errors
  /// Returns an error if the request fails
  pub fn send_string(
    mut self,
    content: impl Into<String>,
  ) -> Result<Response, Error> {
    self.body = Some(content.into().into_bytes());
    self.call()
  }

  /// # Errors
  /// Returns an error if the request fails
  pub fn send_bytes(
    mut self,
    bytes: Vec<u8>,
  ) -> Result<Response, Error> {
    self.body = Some(bytes);
    self.call()
  }

  /// # Errors
  /// Returns an error if the request fails
  pub fn send(
    mut self,
    body: impl IntoBody,
  ) -> Result<Response, Error> {
    self.body = Some(body.into_body());
    self.call()
  }

  /// # Errors
  /// Returns an error if the request fails
  pub fn send_form<I, K, V>(
    mut self,
    iter: I,
  ) -> Result<Response, Error>
  where
    I: IntoIterator<Item = (K, V)>,
    K: AsRef<str>,
    V: AsRef<str>,
  {
    let form_body = Self::build_form_url_encoded(iter);
    self
      .headers
      .insert(HeaderName::CONTENT_TYPE, "application/x-www-form-urlencoded");
    self.body = Some(form_body);
    self.call()
  }

  /// # Errors
  /// Returns an error if the request fails
  pub fn send_empty(self) -> Result<Response, Error> {
    self.call()
  }
}