1#![no_std]
2
3#[cfg(feature = "alloc")]
4extern crate alloc;
5
6mod framing;
7pub use framing::*;
8mod request;
9mod response;
10mod url;
11
12#[cfg(feature = "alloc")]
13use alloc::string::String;
14pub use request::*;
15pub use response::*;
16pub use url::*;
17
18pub const LINE_ENDING: &str = "\r\n";
19pub const HEADER_SEPARATOR: &str = ": ";
20
21pub fn add_line(buffer: &mut [u8], position: usize, line: &[u8]) -> Option<usize> {
22 let destination_buffer = buffer.get_mut(position..position + line.len())?;
23 destination_buffer.copy_from_slice(line);
24 let mut new_position = position + line.len();
25
26 let line_ending = LINE_ENDING.as_bytes();
28 let destination_buffer = buffer.get_mut(new_position..new_position + line_ending.len())?;
29 destination_buffer.copy_from_slice(line_ending);
30 new_position += line_ending.len();
31
32 Some(new_position)
33}
34
35pub fn add_line_iterator<'a, I>(buffer: &mut [u8], mut position: usize, iter: I) -> Option<usize>
36where
37 I: Iterator<Item = &'a [u8]>,
38{
39 for part in iter {
40 let part_length = part.len();
41
42 let destination_buffer = buffer.get_mut(position..position + part_length)?;
43 destination_buffer.copy_from_slice(part);
44 position += part_length;
45 }
46
47 let line_ending = LINE_ENDING.as_bytes();
49 let destination_buffer = buffer.get_mut(position..position + line_ending.len())?;
50 destination_buffer.copy_from_slice(line_ending);
51 position += line_ending.len();
52
53 Some(position)
54}
55
56pub fn parse_header(buffer: &[u8]) -> Option<(&str, &str)> {
57 let mut parts = buffer.splitn(2, |&b| b == b':');
58
59 let name = parts.next()?;
60 let value = parts.next()?;
61
62 let name = core::str::from_utf8(name).ok()?.trim();
63 let value = core::str::from_utf8(value).ok()?.trim();
64
65 Some((name, value))
66}
67
68pub fn split_lines(buffer: &[u8]) -> impl Iterator<Item = &[u8]> + '_ + Clone {
69 buffer.split(|&b| b == b'\n').map(|line| {
70 if line.ends_with(b"\r") {
71 &line[..line.len() - 1]
72 } else {
73 line
74 }
75 })
76}
77
78pub fn get_body(buffer: &[u8]) -> Option<&[u8]> {
79 let mut position = 0;
80 for line in buffer.split_inclusive(|b| *b == b'\n') {
81 position += line.len();
82 if line == b"\r\n" || line == b"\n" {
83 return buffer.get(position..);
84 }
85 }
86 None
87}
88
89#[cfg(feature = "alloc")]
90pub fn format_url<'a>(
91 scheme: &str,
92 host: &str,
93 query_parameters: impl Iterator<Item = (&'a str, &'a str)> + Clone,
94) -> String {
95 let total_query_length: usize = query_parameters
96 .clone()
97 .map(|(key, value)| key.len() + value.len() + 2) .sum();
99
100 let mut url = String::with_capacity(
101 scheme.len() + 3 + host.len() + 1 + total_query_length.saturating_sub(1),
102 ); url.push_str(scheme);
105 url.push_str("://");
106 url.push_str(host);
107 if total_query_length > 0 {
108 url.push('?');
109
110 for (i, (key, value)) in query_parameters.enumerate() {
111 if i > 0 {
112 url.push('&');
113 }
114 url.push_str(key);
115 url.push('=');
116 url.push_str(value);
117 }
118 }
119
120 url
121}
122
123fn number(mut value: usize, radix: usize, storage: &mut [u8]) -> &[u8] {
124 let mut pos = storage.len();
125 loop {
126 pos -= 1;
127 storage[pos] = b"0123456789abcdef"[value % radix];
128 value /= radix;
129 if value == 0 {
130 return &storage[pos..];
131 }
132 }
133}