Skip to content

Commit 8e498b1

Browse files
committed
replace hyper v0.10 with reqwest
`hyper` v0.10 is extremely outdated, the current release is v1.6. this old version pulls in vulnerable dependencies (incl. `hyper` v0.10 itself). rather than upgrading to `hyper` v1.6 i opted to replace it with `reqwest` since this crate here is not `async` and `hyper` v1 is completely `async`. due to the very limited amount of features needed from `hyper` (just executing HTTP POST requests) it can instead be replaced with the much simpler `reqwest` crate. while it hasn't hit v1.0 yet it is very widely used (nearly 200 million downloads on crates.io). this is a breaking change for consumers since the URLs can now just be passed as strings rather than having to call `.parse()?` on the string. note that this is similar to, but not the same as, adnanademovic#8. fixes adnanademovic#9
1 parent 87bfef7 commit 8e498b1

File tree

6 files changed

+24
-29
lines changed

6 files changed

+24
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ version = "0.1.0"
1010
[dependencies]
1111
base64 = "0.22.1"
1212
error-chain = "0.12.4"
13-
hyper = "0.10.15"
13+
reqwest = { version = "0.12", features = ["blocking"] }
1414
lazy_static = "1.5.0"
1515
regex = "1.11.1"
1616
serde = { version = "1.0.217", features = ["derive"] }

src/bin/echo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn main() {
3939
bar: "baz".into(),
4040
};
4141
println!("Sending: {:?}", req);
42-
let uri = "http://localhost:8080/".parse().unwrap();
42+
let uri = "http://localhost:8080/";
4343
let res: Result<Result<TestStruct, _>, _> = client.call(&uri, "echo", req.clone());
4444
println!("Echo Received: {:?}", res);
4545
let res: Result<Result<TestStruct, _>, _> = client.call(&uri, "double", req.clone());

src/bin/threadcheck.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ fn main() {
3232
std::thread::sleep(Duration::from_secs(1));
3333

3434
let t1 = std::thread::spawn(|| {
35-
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "foo", ())
35+
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "foo", ())
3636
.unwrap()
3737
.unwrap();
3838
});
3939

4040
let t2 = std::thread::spawn(|| {
41-
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "bar", ())
41+
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "bar", ())
4242
.unwrap()
4343
.unwrap();
4444
});
4545

4646
let t3 = std::thread::spawn(|| {
47-
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "foo", ())
47+
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "foo", ())
4848
.unwrap()
4949
.unwrap();
5050
});
5151

5252
let t4 = std::thread::spawn(|| {
53-
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "bar", ())
53+
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "bar", ())
5454
.unwrap()
5555
.unwrap();
5656
});

src/client.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
use super::error::{Result, ResultExt};
22
use super::xmlfmt::{from_params, into_params, parse, Call, Fault, Params, Response};
3-
use hyper::{self, Client as HyperClient};
43
use serde::{Deserialize, Serialize};
54
use std;
6-
use Url;
5+
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
76

8-
use hyper::header::Headers;
9-
header! { (ContentType, "ContentType") => [String] }
10-
11-
pub fn call_value<Tkey>(uri: &Url, name: Tkey, params: Params) -> Result<Response>
7+
pub fn call_value<URL, Tkey>(uri: &URL, name: Tkey, params: Params) -> Result<Response>
128
where
9+
URL: reqwest::IntoUrl + Clone,
1310
Tkey: Into<String>,
1411
{
1512
Client::new()?.call_value(uri, name, params)
1613
}
1714

18-
pub fn call<'a, Tkey, Treq, Tres>(
19-
uri: &Url,
15+
pub fn call<'a, URL, Tkey, Treq, Tres>(
16+
uri: &URL,
2017
name: Tkey,
2118
req: Treq,
2219
) -> Result<std::result::Result<Tres, Fault>>
2320
where
21+
URL: reqwest::IntoUrl + Clone,
2422
Tkey: Into<String>,
2523
Treq: Serialize,
2624
Tres: Deserialize<'a>,
@@ -29,34 +27,33 @@ where
2927
}
3028

3129
pub struct Client {
32-
client: HyperClient,
30+
client: reqwest::blocking::Client,
3331
}
3432

3533
impl Client {
3634
pub fn new() -> Result<Client> {
37-
let client = HyperClient::new();
35+
let client = reqwest::blocking::Client::new();
3836
Ok(Client { client })
3937
}
4038

41-
pub fn call_value<Tkey>(&mut self, uri: &Url, name: Tkey, params: Params) -> Result<Response>
39+
pub fn call_value<URL, Tkey>(&mut self, uri: &URL, name: Tkey, params: Params) -> Result<Response>
4240
where
41+
URL: reqwest::IntoUrl + Clone,
4342
Tkey: Into<String>,
4443
{
4544
use super::xmlfmt::value::ToXml;
46-
let body_str = Call {
45+
let body = Call {
4746
name: name.into(),
4847
params,
4948
}
5049
.to_xml();
51-
let bytes: &[u8] = body_str.as_bytes();
52-
let body = hyper::client::Body::BufBody(bytes, bytes.len());
5350

54-
let mut headers = Headers::new();
55-
headers.set(ContentType("xml".to_owned()));
51+
let mut headers = HeaderMap::new();
52+
headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/xml"));
5653

5754
let response = self
5855
.client
59-
.post(uri.as_ref())
56+
.post(uri.clone())
6057
.headers(headers)
6158
.body(body)
6259
.send()
@@ -65,13 +62,14 @@ impl Client {
6562
parse::response(response).map_err(Into::into)
6663
}
6764

68-
pub fn call<'a, Tkey, Treq, Tres>(
65+
pub fn call<'a, URL, Tkey, Treq, Tres>(
6966
&mut self,
70-
uri: &Url,
67+
uri: &URL,
7168
name: Tkey,
7269
req: Treq,
7370
) -> Result<std::result::Result<Tres, Fault>>
7471
where
72+
URL: reqwest::IntoUrl + Clone,
7573
Tkey: Into<String>,
7674
Treq: Serialize,
7775
Tres: Deserialize<'a>,

src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ extern crate base64;
44
#[macro_use]
55
extern crate error_chain;
66
#[macro_use]
7-
extern crate hyper;
8-
#[macro_use]
97
extern crate lazy_static;
108
extern crate regex;
119
#[macro_use]
@@ -15,13 +13,13 @@ pub extern crate rouille;
1513
extern crate serde_bytes;
1614
extern crate serde_xml_rs;
1715
extern crate xml;
16+
extern crate reqwest;
1817

1918
pub mod client;
2019
pub mod error;
2120
pub mod server;
2221
mod xmlfmt;
2322

2423
pub use client::{call, call_value, Client};
25-
pub use hyper::Url;
2624
pub use server::Server;
2725
pub use xmlfmt::{from_params, into_params, Call, Fault, Params, Response, Value};

src/server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ where
135135
F: Send + Sync + 'static + Fn(&rouille::Request) -> rouille::Response,
136136
{
137137
server: rouille::Server<F>,
138-
// server: hyper::Server<NewService, hyper::Body>,
139138
}
140139

141140
impl<F> BoundServer<F>

0 commit comments

Comments
 (0)