Skip to content

Commit 098fa5c

Browse files
committed
Fully remove http dependency from spacetimedb_lib
1 parent d48919f commit 098fa5c

6 files changed

Lines changed: 116 additions & 128 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings/src/http.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,61 @@ fn convert_request(parts: http::request::Parts) -> st_http::Request {
146146
log::warn!("Converting HTTP `Request` with unrecognized extensions");
147147
}
148148
st_http::Request {
149-
method: method.into(),
150-
headers: headers.into(),
149+
method: match method {
150+
http::Method::GET => st_http::Method::Get,
151+
http::Method::HEAD => st_http::Method::Head,
152+
http::Method::POST => st_http::Method::Post,
153+
http::Method::PUT => st_http::Method::Put,
154+
http::Method::DELETE => st_http::Method::Delete,
155+
http::Method::CONNECT => st_http::Method::Connect,
156+
http::Method::OPTIONS => st_http::Method::Options,
157+
http::Method::TRACE => st_http::Method::Trace,
158+
http::Method::PATCH => st_http::Method::Patch,
159+
_ => st_http::Method::Extension(method.to_string()),
160+
},
161+
headers: headers
162+
.into_iter()
163+
.map(|(k, v)| {
164+
let v = st_http::HeaderValue {
165+
is_sensitive: v.is_sensitive(),
166+
bytes: v.as_bytes().into(),
167+
};
168+
(k.map(|k| k.to_string()), v)
169+
})
170+
.collect(),
151171
timeout: timeout.map(Into::into),
152172
uri: uri.to_string(),
153-
version: version.into(),
173+
version: match version {
174+
http::Version::HTTP_09 => st_http::Version::Http09,
175+
http::Version::HTTP_10 => st_http::Version::Http10,
176+
http::Version::HTTP_11 => st_http::Version::Http11,
177+
http::Version::HTTP_2 => st_http::Version::Http2,
178+
http::Version::HTTP_3 => st_http::Version::Http3,
179+
_ => unreachable!("Unknown HTTP version: {version:?}"),
180+
},
154181
}
155182
}
156183

157184
fn convert_response(response: st_http::Response) -> http::Result<http::response::Parts> {
158185
let st_http::Response { headers, version, code } = response;
159186

160187
let (mut response, ()) = http::Response::new(()).into_parts();
161-
response.version = version.into();
188+
response.version = match version {
189+
st_http::Version::Http09 => http::Version::HTTP_09,
190+
st_http::Version::Http10 => http::Version::HTTP_10,
191+
st_http::Version::Http11 => http::Version::HTTP_11,
192+
st_http::Version::Http2 => http::Version::HTTP_2,
193+
st_http::Version::Http3 => http::Version::HTTP_3,
194+
};
162195
response.status = http::StatusCode::from_u16(code)?;
163-
response.headers = headers.try_into()?;
196+
response.headers = headers
197+
.into_iter()
198+
.map(|(k, v)| {
199+
let mut value = http::HeaderValue::try_from(v.bytes.into_vec())?;
200+
value.set_sensitive(v.is_sensitive);
201+
Ok((k.try_into()?, value))
202+
})
203+
.collect::<http::Result<_>>()?;
164204
Ok(response)
165205
}
166206

crates/bindings/tests/snapshots/deps__spacetimedb_bindings_dependencies.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ spacetimedb
8383
│ ├── derive_more (*)
8484
│ ├── enum_as_inner (*)
8585
│ ├── hex
86-
│ ├── http (*)
8786
│ ├── itertools (*)
8887
│ ├── log
8988
│ ├── spacetimedb_bindings_macro (*)

crates/core/src/host/instance_env.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,34 @@ fn convert_http_request(request: st_http::Request) -> http::Result<(http::reques
679679
} = request;
680680

681681
let (mut request, ()) = http::Request::new(()).into_parts();
682-
request.method = method.into();
682+
request.method = match method {
683+
st_http::Method::Get => http::Method::GET,
684+
st_http::Method::Head => http::Method::HEAD,
685+
st_http::Method::Post => http::Method::POST,
686+
st_http::Method::Put => http::Method::PUT,
687+
st_http::Method::Delete => http::Method::DELETE,
688+
st_http::Method::Connect => http::Method::CONNECT,
689+
st_http::Method::Options => http::Method::OPTIONS,
690+
st_http::Method::Trace => http::Method::TRACE,
691+
st_http::Method::Patch => http::Method::PATCH,
692+
st_http::Method::Extension(method) => http::Method::from_bytes(method.as_bytes()).expect("Invalid HTTP method"),
693+
};
683694
request.uri = uri.try_into()?;
684-
request.version = version.into();
685-
request.headers = headers.try_into()?;
695+
request.version = match version {
696+
st_http::Version::Http09 => http::Version::HTTP_09,
697+
st_http::Version::Http10 => http::Version::HTTP_10,
698+
st_http::Version::Http11 => http::Version::HTTP_11,
699+
st_http::Version::Http2 => http::Version::HTTP_2,
700+
st_http::Version::Http3 => http::Version::HTTP_3,
701+
};
702+
request.headers = headers
703+
.into_iter()
704+
.map(|(k, v)| {
705+
let mut value = http::HeaderValue::try_from(v.bytes.into_vec())?;
706+
value.set_sensitive(v.is_sensitive);
707+
Ok((k.try_into()?, value))
708+
})
709+
.collect::<http::Result<_>>()?;
686710

687711
let timeout = timeout.map(|d| d.to_duration_saturating());
688712

@@ -703,8 +727,24 @@ fn convert_http_response(response: http::response::Parts) -> st_http::Response {
703727
let _ = extensions;
704728

705729
st_http::Response {
706-
headers: headers.into(),
707-
version: version.into(),
730+
headers: headers
731+
.into_iter()
732+
.map(|(k, v)| {
733+
let v = st_http::HeaderValue {
734+
is_sensitive: v.is_sensitive(),
735+
bytes: v.as_bytes().into(),
736+
};
737+
(k.map(|k| k.to_string()), v)
738+
})
739+
.collect(),
740+
version: match version {
741+
http::Version::HTTP_09 => st_http::Version::Http09,
742+
http::Version::HTTP_10 => st_http::Version::Http10,
743+
http::Version::HTTP_11 => st_http::Version::Http11,
744+
http::Version::HTTP_2 => st_http::Version::Http2,
745+
http::Version::HTTP_3 => st_http::Version::Http3,
746+
_ => unreachable!("Unknown HTTP version: {version:?}"),
747+
},
708748
code: status.as_u16(),
709749
}
710750
}

crates/lib/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ chrono.workspace = true
4343
derive_more.workspace = true
4444
enum-as-inner.workspace = true
4545
hex.workspace = true
46-
http.workspace = true
4746
itertools.workspace = true
4847
log.workspace = true
4948
serde = { workspace = true, optional = true }

crates/lib/src/http.rs

Lines changed: 26 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,6 @@ pub enum Method {
5252
Extension(String),
5353
}
5454

55-
impl From<http::Method> for Method {
56-
fn from(method: http::Method) -> Method {
57-
match method {
58-
http::Method::GET => Method::Get,
59-
http::Method::HEAD => Method::Head,
60-
http::Method::POST => Method::Post,
61-
http::Method::PUT => Method::Put,
62-
http::Method::DELETE => Method::Delete,
63-
http::Method::CONNECT => Method::Connect,
64-
http::Method::OPTIONS => Method::Options,
65-
http::Method::TRACE => Method::Trace,
66-
http::Method::PATCH => Method::Patch,
67-
_ => Method::Extension(method.to_string()),
68-
}
69-
}
70-
}
71-
72-
impl From<Method> for http::Method {
73-
fn from(method: Method) -> http::Method {
74-
match method {
75-
Method::Get => http::Method::GET,
76-
Method::Head => http::Method::HEAD,
77-
Method::Post => http::Method::POST,
78-
Method::Put => http::Method::PUT,
79-
Method::Delete => http::Method::DELETE,
80-
Method::Connect => http::Method::CONNECT,
81-
Method::Options => http::Method::OPTIONS,
82-
Method::Trace => http::Method::TRACE,
83-
Method::Patch => http::Method::PATCH,
84-
Method::Extension(method) => http::Method::from_bytes(method.as_bytes()).expect("Invalid HTTP method"),
85-
}
86-
}
87-
}
88-
8955
/// An HTTP version.
9056
#[derive(Clone, SpacetimeType, PartialEq, Eq)]
9157
#[sats(crate = crate, name = "HttpVersion")]
@@ -97,31 +63,6 @@ pub enum Version {
9763
Http3,
9864
}
9965

100-
impl From<http::Version> for Version {
101-
fn from(version: http::Version) -> Version {
102-
match version {
103-
http::Version::HTTP_09 => Version::Http09,
104-
http::Version::HTTP_10 => Version::Http10,
105-
http::Version::HTTP_11 => Version::Http11,
106-
http::Version::HTTP_2 => Version::Http2,
107-
http::Version::HTTP_3 => Version::Http3,
108-
_ => unreachable!("Unknown HTTP version: {version:?}"),
109-
}
110-
}
111-
}
112-
113-
impl From<Version> for http::Version {
114-
fn from(version: Version) -> http::Version {
115-
match version {
116-
Version::Http09 => http::Version::HTTP_09,
117-
Version::Http10 => http::Version::HTTP_10,
118-
Version::Http11 => http::Version::HTTP_11,
119-
Version::Http2 => http::Version::HTTP_2,
120-
Version::Http3 => http::Version::HTTP_3,
121-
}
122-
}
123-
}
124-
12566
/// A set of HTTP headers.
12667
///
12768
/// Construct this by converting from a [`http::HeaderMap`].
@@ -134,26 +75,22 @@ pub struct Headers {
13475

13576
// `http::header::IntoIter` only returns the `HeaderName` for the first
13677
// `HeaderValue` with that name, so we have to manually assign the names.
137-
struct HeaderMapIntoIter {
138-
prev: Option<(http::HeaderName, http::HeaderValue)>,
139-
inner: http::header::IntoIter<http::HeaderValue>,
78+
struct HeaderIter<I, T> {
79+
prev: Option<(String, T)>,
80+
inner: I,
14081
}
14182

142-
impl From<http::header::HeaderMap> for HeaderMapIntoIter {
143-
fn from(map: http::header::HeaderMap) -> Self {
144-
let mut inner = map.into_iter();
145-
Self {
146-
prev: inner.next().map(|(k, v)| (k.unwrap(), v)),
147-
inner,
148-
}
149-
}
150-
}
151-
152-
impl Iterator for HeaderMapIntoIter {
153-
type Item = (http::HeaderName, http::HeaderValue);
83+
impl<I, T> Iterator for HeaderIter<I, T>
84+
where
85+
I: Iterator<Item = (Option<String>, T)>,
86+
{
87+
type Item = (String, T);
15488

15589
fn next(&mut self) -> Option<Self::Item> {
156-
let (prev_k, prev_v) = self.prev.take()?;
90+
let (prev_k, prev_v) = self
91+
.prev
92+
.take()
93+
.or_else(|| self.inner.next().map(|(k, v)| (k.unwrap(), v)))?;
15794
self.prev = self
15895
.inner
15996
.next()
@@ -166,28 +103,20 @@ impl Iterator for HeaderMapIntoIter {
166103
}
167104
}
168105

169-
impl From<http::HeaderMap> for Headers {
170-
fn from(value: http::HeaderMap) -> Headers {
171-
Headers {
172-
entries: HeaderMapIntoIter::from(value)
173-
.map(|(name, value)| HttpHeaderPair {
174-
name: name.to_string(),
175-
value: value.into(),
176-
})
177-
.collect(),
178-
}
106+
impl FromIterator<(Option<String>, HeaderValue)> for Headers {
107+
fn from_iter<T: IntoIterator<Item = (Option<String>, HeaderValue)>>(iter: T) -> Self {
108+
let inner = iter.into_iter();
109+
let entries = HeaderIter { prev: None, inner }
110+
.map(|(name, value)| HttpHeaderPair { name, value })
111+
.collect();
112+
Self { entries }
179113
}
180114
}
181115

182-
impl TryFrom<Headers> for http::HeaderMap {
183-
type Error = http::Error;
184-
fn try_from(headers: Headers) -> http::Result<Self> {
185-
let Headers { entries } = headers;
186-
let mut new_headers = http::HeaderMap::with_capacity(entries.len() / 2);
187-
for HttpHeaderPair { name, value } in entries {
188-
new_headers.insert(http::HeaderName::try_from(name)?, value.try_into()?);
189-
}
190-
Ok(new_headers)
116+
impl Headers {
117+
#[allow(clippy::should_implement_trait)]
118+
pub fn into_iter(self) -> impl Iterator<Item = (String, HeaderValue)> {
119+
IntoIterator::into_iter(self.entries).map(|HttpHeaderPair { name, value }| (name, value))
191120
}
192121
}
193122

@@ -202,27 +131,9 @@ struct HttpHeaderPair {
202131
/// A valid HTTP header value, sourced from an already-validated [`http::HeaderValue`].
203132
#[derive(Clone, SpacetimeType)]
204133
#[sats(crate = crate, name = "HttpHeaderValue")]
205-
struct HeaderValue {
206-
bytes: Box<[u8]>,
207-
is_sensitive: bool,
208-
}
209-
210-
impl From<http::HeaderValue> for HeaderValue {
211-
fn from(value: http::HeaderValue) -> HeaderValue {
212-
HeaderValue {
213-
is_sensitive: value.is_sensitive(),
214-
bytes: value.as_bytes().into(),
215-
}
216-
}
217-
}
218-
219-
impl TryFrom<HeaderValue> for http::HeaderValue {
220-
type Error = http::Error;
221-
fn try_from(value: HeaderValue) -> http::Result<http::HeaderValue> {
222-
let mut new_value = http::HeaderValue::from_bytes(&value.bytes)?;
223-
new_value.set_sensitive(value.is_sensitive);
224-
Ok(new_value)
225-
}
134+
pub struct HeaderValue {
135+
pub bytes: Box<[u8]>,
136+
pub is_sensitive: bool,
226137
}
227138

228139
#[derive(Clone, SpacetimeType)]

0 commit comments

Comments
 (0)