Skip to content

Commit c0b3ef8

Browse files
committed
swap out url::Url for http::uri::Uri
same authors, slightly different interfaces
1 parent bfa3b51 commit c0b3ef8

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ authors = [
1616
[features]
1717

1818
[dependencies]
19+
http.workspace = true
1920
slab.workspace = true
20-
url.workspace = true
2121
wasi.workspace = true
2222
wstd-macro.workspace = true
2323

@@ -46,13 +46,13 @@ license = "MIT OR Apache-2.0 OR Apache-2.0 WITH LLVM-exception"
4646
anyhow = "1"
4747
cargo_metadata = "0.18.1"
4848
heck = "0.5"
49+
http = "1.1"
4950
quote = "1.0"
5051
serde_json = "1"
5152
slab = "0.4.9"
5253
syn = "2.0"
5354
test-programs = { path = "test-programs" }
5455
test-programs-artifacts = { path = "test-programs/artifacts" }
55-
url = "2.5.0"
5656
wasi = "0.13.1"
5757
wasmtime = "26"
5858
wasmtime-wasi = "26"

examples/http_get.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::error::Error;
2-
use wstd::http::{Client, Method, Request, Url};
2+
use wstd::http::{Client, Method, Request};
33
use wstd::io::AsyncRead;
44

55
#[wstd::main]
66
async fn main() -> Result<(), Box<dyn Error>> {
7-
let request = Request::new(Method::Get, Url::parse("https://postman-echo.com/get")?);
7+
let request = Request::new(Method::Get, "https://postman-echo.com/get".parse()?);
88
let mut response = Client::new().send(request).await?;
99

1010
let content_type = response

src/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! HTTP networking support
22
3-
pub use url::Url;
3+
pub use http::uri::Uri;
44

55
#[doc(inline)]
66
pub use body::{Body, IntoBody};

src/http/request.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
use crate::io::{empty, Empty};
22

33
use super::{Body, IntoBody, Method};
4-
use url::Url;
4+
use http::uri::Uri;
55
use wasi::http::outgoing_handler::OutgoingRequest;
66
use wasi::http::types::{Headers as WasiHeaders, Scheme};
77

88
/// An HTTP request
99
#[derive(Debug)]
1010
pub struct Request<B: Body> {
1111
method: Method,
12-
url: Url,
12+
uri: Uri,
1313
headers: WasiHeaders,
1414
body: B,
1515
}
1616

1717
impl Request<Empty> {
1818
/// Create a new HTTP request to send off to the client.
19-
pub fn new(method: Method, url: Url) -> Self {
19+
pub fn new(method: Method, uri: Uri) -> Self {
2020
Self {
2121
body: empty(),
2222
method,
23-
url,
23+
uri,
2424
headers: WasiHeaders::new(),
2525
}
2626
}
@@ -31,42 +31,47 @@ impl<B: Body> Request<B> {
3131
pub fn set_body<C: IntoBody>(self, body: C) -> Request<C::IntoBody> {
3232
let Self {
3333
method,
34-
url,
34+
uri,
3535
headers,
3636
..
3737
} = self;
3838
Request {
3939
method,
40-
url,
40+
uri,
4141
headers,
4242
body: body.into_body(),
4343
}
4444
}
4545

46-
pub fn into_outgoing(self) -> (OutgoingRequest, B) {
46+
pub(crate) fn into_outgoing(self) -> (OutgoingRequest, B) {
4747
let wasi_req = OutgoingRequest::new(self.headers);
4848

4949
// Set the HTTP method
50-
wasi_req.set_method(&self.method.into()).unwrap();
50+
wasi_req
51+
.set_method(&self.method.into())
52+
.expect("method accepted by wasi-http implementation");
5153

5254
// Set the url scheme
53-
let scheme = match self.url.scheme() {
54-
"http" => Scheme::Http,
55-
"https" => Scheme::Https,
56-
other => Scheme::Other(other.to_owned()),
55+
let scheme = match self.uri.scheme().map(|s| s.as_str()) {
56+
Some("http") => Scheme::Http,
57+
Some("https") | None => Scheme::Https,
58+
Some(other) => Scheme::Other(other.to_owned()),
5759
};
58-
wasi_req.set_scheme(Some(&scheme)).unwrap();
60+
wasi_req
61+
.set_scheme(Some(&scheme))
62+
.expect("scheme accepted by wasi-http implementation");
5963

60-
// Set the url path + query string
61-
let path = match self.url.query() {
62-
Some(query) => format!("{}?{query}", self.url.path()),
63-
None => self.url.path().to_owned(),
64-
};
65-
wasi_req.set_path_with_query(Some(&path)).unwrap();
64+
// Set authority
65+
wasi_req
66+
.set_authority(self.uri.authority().map(|a| a.as_str()))
67+
.expect("authority accepted by wasi-http implementation");
6668

67-
// Not sure why we also have to set the authority, but sure we can do
68-
// that too!
69-
wasi_req.set_authority(Some(self.url.authority())).unwrap();
69+
// Set the url path + query string
70+
if let Some(p_and_q) = self.uri.path_and_query() {
71+
wasi_req
72+
.set_path_with_query(Some(&p_and_q.to_string()))
73+
.expect("path with query accepted by wasi-http implementation")
74+
}
7075

7176
// All done; request is ready for send-off
7277
(wasi_req, self.body)

0 commit comments

Comments
 (0)