Skip to content

Commit 137e3ff

Browse files
authored
feat: Make loading from an external URL be gated behind a feature. (#149)
* feat: Make loading from an external URL be gated behind a feature. `attohttpc` and its transitive dependencies add a large amount of size to the crate (on the order of about ~400 kB). For applications which don't need external URL loading and are concerned with code size (for example, a WASM binary), this is wasted space. Placing this stuff behind a feature gate allows people to opt-out and reduce code size if they wish. * fix: Add `http` feature by default for python and wasm bindings. I'm assumining that these bindings expect `http` to be active. Having `default_features` be false meant that these bindings weren't getting the `http` gated code.
1 parent 0d5311c commit 137e3ff

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

bindings/python/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ built = { version = "0.5", features = ["chrono"] }
1717
path = "../../css-inline"
1818
version = "*"
1919
default-features = false
20+
features = ["http"]
2021

2122
[dependencies]
2223
url = "2"

bindings/wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ crate-type = ["cdylib"]
2020
path = "../../css-inline"
2121
version = "*"
2222
default-features = false
23+
features = ["http"]
2324

2425
[dependencies]
2526
url = "2"

css-inline/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ categories = ["web-programming"]
1515
name = "css-inline"
1616

1717
[features]
18-
default = ["cli"]
18+
default = ["cli", "http"]
1919
cli = ["pico-args", "rayon"]
20+
http = ["attohttpc"]
2021

2122
[dependencies]
2223
cssparser = "0.29"
2324
kuchiki = "0.8"
2425
memchr = "2.4"
25-
attohttpc = { version = "0", default-features = false, features = ["compress", "tls-rustls"] }
26+
attohttpc = { version = "0", default-features = false, features = ["compress", "tls-rustls"], optional = true }
2627
url = "2"
2728
smallvec = "1"
2829
indexmap = "1.8.1"

css-inline/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub enum InlineError {
1919
/// Input-output error. May happen during writing the resulting HTML.
2020
IO(io::Error),
2121
/// Network-related problem. E.g. resource is not available.
22+
#[cfg(feature = "http")]
2223
Network(attohttpc::Error),
2324
/// Syntax errors or unsupported selectors.
2425
ParseError(Cow<'static, str>),
@@ -29,6 +30,8 @@ impl From<io::Error> for InlineError {
2930
Self::IO(error)
3031
}
3132
}
33+
34+
#[cfg(feature = "http")]
3235
impl From<attohttpc::Error> for InlineError {
3336
fn from(error: attohttpc::Error) -> Self {
3437
Self::Network(error)
@@ -41,6 +44,7 @@ impl Display for InlineError {
4144
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
4245
match self {
4346
Self::IO(error) => error.fmt(f),
47+
#[cfg(feature = "http")]
4448
Self::Network(error) => error.fmt(f),
4549
Self::ParseError(error) => f.write_str(error),
4650
Self::MissingStyleSheet { path } => {

css-inline/src/lib.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
rust_2018_idioms,
2525
rust_2018_compatibility
2626
)]
27+
28+
#[cfg(feature = "http")]
2729
use attohttpc::{Method, RequestBuilder};
30+
2831
use kuchiki::{
2932
parse_html, traits::TendrilSink, ElementData, Node, NodeDataRef, NodeRef, Specificity,
3033
};
@@ -40,6 +43,7 @@ use std::{
4043
fs,
4144
io::{ErrorKind, Write},
4245
};
46+
4347
pub use url::{ParseError, Url};
4448

4549
/// Replace double quotes in property values.
@@ -267,6 +271,7 @@ impl<'a> CSSInliner<'a> {
267271
style_tag.as_node().detach();
268272
}
269273
}
274+
270275
if self.options.load_remote_stylesheets {
271276
let mut links = document
272277
.select("link[rel~=stylesheet]")
@@ -338,9 +343,20 @@ impl<'a> CSSInliner<'a> {
338343

339344
fn load_external(location: &str) -> Result<String> {
340345
if location.starts_with("https") | location.starts_with("http") {
341-
let request = RequestBuilder::try_new(Method::GET, location)?;
342-
let response = request.send()?;
343-
Ok(response.text()?)
346+
#[cfg(feature = "http")]
347+
{
348+
let request = RequestBuilder::try_new(Method::GET, location)?;
349+
let response = request.send()?;
350+
Ok(response.text()?)
351+
}
352+
353+
#[cfg(not(feature = "http"))]
354+
{
355+
Err(InlineError::IO(std::io::Error::new(
356+
ErrorKind::Unsupported,
357+
"Loading external URLs requires the `http` feature",
358+
)))
359+
}
344360
} else {
345361
fs::read_to_string(location).map_err(|error| match error.kind() {
346362
ErrorKind::NotFound => InlineError::MissingStyleSheet {

0 commit comments

Comments
 (0)