Skip to content

Commit 3427b98

Browse files
committed
Remove punycode dependency
punycode was used to convert non-ASCII hostnames in URLs. This can instead be done by the calling site. Remove the punycode function and dependence.
1 parent df613e5 commit 3427b98

File tree

5 files changed

+8
-64
lines changed

5 files changed

+8
-64
lines changed

bitreq/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ rust-version = "1.75.0"
1616
maintenance = { status = "experimental" }
1717

1818
[dependencies]
19-
# For the punycode feature:
20-
punycode = { version = "0.4.1", optional = true }
2119
# For the json-using-serde feature:
2220
serde = { version = "1.0.101", optional = true }
2321
serde_json = { version = "1.0.0", optional = true }
@@ -38,7 +36,7 @@ tiny_http = "0.12"
3836
chrono = "0.4.0"
3937

4038
[package.metadata.docs.rs]
41-
features = ["json-using-serde", "proxy", "https", "punycode"]
39+
features = ["json-using-serde", "proxy", "https"]
4240

4341
[features]
4442
default = ["std"]

bitreq/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ This crate is a fork for the very nice
77
rename it because I wanted to totally gut it and provide a crate with
88
different goals. Many thanks to the original author.
99

10-
Simple, minimal-dependency HTTP client. Optional features for
11-
unicode domains (`punycode`), http proxies (`proxy`), async support
12-
(`async`, `async-https`), and https with various TLS implementations
13-
(`https-rustls`, `https-rustls-probe`, and `https` which is an alias
14-
for `https-rustls`).
10+
Simple, minimal-dependency HTTP client. Optional features for http
11+
proxies (`proxy`), async support (`async`, `async-https`), and https
12+
with various TLS implementations (`https-rustls`, `https-rustls-probe`,
13+
and `https` which is an alias for `https-rustls`).
1514

1615
Without any optional features, my casual testing indicates about 100
1716
KB additional executable size for stripped release builds using this

bitreq/src/connection.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ impl Connection {
150150
/// Sends the [`Request`](struct.Request.html), consumes this
151151
/// connection, and returns a [`Response`](struct.Response.html).
152152
#[cfg(feature = "rustls")]
153-
pub(crate) fn send_https(mut self) -> Result<ResponseLazy, Error> {
153+
pub(crate) fn send_https(self) -> Result<ResponseLazy, Error> {
154154
enforce_timeout(self.timeout_at, move || {
155-
self.request.url.host = ensure_ascii_host(self.request.url.host)?;
156-
157155
let secured_stream = rustls_stream::create_secured_stream(&self)?;
158156

159157
#[cfg(feature = "log")]
@@ -170,9 +168,8 @@ impl Connection {
170168

171169
/// Sends the [`Request`](struct.Request.html), consumes this
172170
/// connection, and returns a [`Response`](struct.Response.html).
173-
pub(crate) fn send(mut self) -> Result<ResponseLazy, Error> {
171+
pub(crate) fn send(self) -> Result<ResponseLazy, Error> {
174172
enforce_timeout(self.timeout_at, move || {
175-
self.request.url.host = ensure_ascii_host(self.request.url.host)?;
176173
let bytes = self.request.as_bytes();
177174

178175
#[cfg(feature = "log")]
@@ -313,35 +310,6 @@ fn get_redirect(mut connection: Connection, status_code: i32, url: Option<&Strin
313310
}
314311
}
315312

316-
fn ensure_ascii_host(host: String) -> Result<String, Error> {
317-
if host.is_ascii() {
318-
Ok(host)
319-
} else {
320-
#[cfg(not(feature = "punycode"))]
321-
{
322-
Err(Error::PunycodeFeatureNotEnabled)
323-
}
324-
325-
#[cfg(feature = "punycode")]
326-
{
327-
let mut result = String::with_capacity(host.len() * 2);
328-
for s in host.split('.') {
329-
if s.is_ascii() {
330-
result += s;
331-
} else {
332-
match punycode::encode(s) {
333-
Ok(s) => result = result + "xn--" + &s,
334-
Err(_) => return Err(Error::PunycodeConversionFailed),
335-
}
336-
}
337-
result += ".";
338-
}
339-
result.truncate(result.len() - 1); // Remove the trailing dot
340-
Ok(result)
341-
}
342-
}
343-
}
344-
345313
/// Enforce the timeout by running the function in a new thread and
346314
/// parking the current one with a timeout.
347315
///

bitreq/src/error.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,10 @@ pub enum Error {
5252
/// The response contained invalid UTF-8 where it should be valid
5353
/// (eg. headers), so the response cannot interpreted correctly.
5454
InvalidUtf8InResponse,
55-
/// The provided url contained a domain that has non-ASCII
56-
/// characters, and could not be converted into punycode. It is
57-
/// probably not an actual domain.
58-
PunycodeConversionFailed,
5955
/// Tried to send a secure request (ie. the url started with
6056
/// `https://`), but the crate's `https` feature was not enabled,
6157
/// and as such, a connection cannot be made.
6258
HttpsFeatureNotEnabled,
63-
/// The provided url contained a domain that has non-ASCII
64-
/// characters, but it could not be converted into punycode
65-
/// because the `punycode` feature was not enabled.
66-
PunycodeFeatureNotEnabled,
6759
/// The provided proxy information was not properly formatted. See
6860
/// [Proxy::new](crate::Proxy::new) for the valid format.
6961
BadProxy,
@@ -110,8 +102,6 @@ impl fmt::Display for Error {
110102
TooManyRedirections => write!(f, "too many redirections (over the max)"),
111103
InvalidUtf8InResponse => write!(f, "response contained invalid utf-8 where valid utf-8 was expected"),
112104
HttpsFeatureNotEnabled => write!(f, "request url contains https:// but the https feature is not enabled"),
113-
PunycodeFeatureNotEnabled => write!(f, "non-ascii urls needs to be converted into punycode, and the feature is missing"),
114-
PunycodeConversionFailed => write!(f, "non-ascii url conversion to punycode failed"),
115105
BadProxy => write!(f, "the provided proxy information is malformed"),
116106
BadProxyCreds => write!(f, "the provided proxy credentials are malformed"),
117107
ProxyConnect => write!(f, "could not connect to the proxy server"),

bitreq/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//!
2222
//! ```toml
2323
//! [dependencies]
24-
//! bitreq = { version = "2.13.5-alpha", features = ["punycode"] }
24+
//! bitreq = { version = "2.13.5-alpha", features = ["https"] }
2525
//! ```
2626
//!
2727
//! Below is the list of all available features.
@@ -45,17 +45,6 @@
4545
//! crate to auto-detect root certificates installed in common
4646
//! locations.
4747
//!
48-
//! ## `punycode`
49-
//!
50-
//! This feature enables requests to non-ascii domains: the
51-
//! [`punycode`](https://crates.io/crates/punycode) crate is used to
52-
//! convert the non-ascii parts into their punycode representations
53-
//! before making the request. If you try to make a request to 㯙㯜㯙
54-
//! 㯟.net or i❤.ws for example, with this feature disabled (as it is
55-
//! by default), your request will fail with a
56-
//! [`PunycodeFeatureNotEnabled`](enum.Error.html#variant.PunycodeFeatureNotEnabled)
57-
//! error.
58-
//!
5948
//! ## `async`
6049
//!
6150
//! This feature enables asynchronous HTTP requests using tokio. It provides

0 commit comments

Comments
 (0)