Skip to content

Commit f470e3e

Browse files
committed
Add optional hyper support.
1 parent 1ee59c1 commit f470e3e

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ doctest = false
1919
unstable = ["tendril/unstable", "string_cache/unstable"]
2020
heap_size = ["heapsize", "heapsize_plugin"]
2121
codegen = ["html5ever_macros"]
22+
with-hyper = ["hyper"]
2223

2324
[dependencies]
2425
time = "0"
@@ -29,6 +30,7 @@ mac = "0"
2930
tendril = "0.2"
3031
heapsize = { version = "0.1.1", optional = true }
3132
heapsize_plugin = { version = "0.1.0", optional = true }
33+
hyper = {version = "0.7", optional = true}
3234

3335
[dev-dependencies]
3436
rustc-serialize = "0.3.15"

scripts/travis-build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
set -ex
1212

13+
cargo build --features with-hyper
1314
# Test without unstable first, to make sure src/tree_builder/rules.expanded.rs is up-to-date.
1415
cargo test --no-run
1516
cargo test | ./scripts/shrink-test-output.py

src/driver.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::borrow::Cow;
1616
use std::mem;
1717

1818
use encoding::{self, EncodingRef};
19+
#[cfg(feature = "with-hyper")] use hyper::client::IntoUrl;
1920
use string_cache::QualName;
2021
use tendril;
2122
use tendril::{StrTendril, ByteTendril};
@@ -113,6 +114,23 @@ impl<Sink: TreeSink> Parser<Sink> {
113114
opts: opts,
114115
}
115116
}
117+
118+
/// Fetch an HTTP or HTTPS URL with Hyper and parse.
119+
#[cfg(feature = "with-hyper")]
120+
pub fn from_http<U: IntoUrl>(self, url: U) -> Result<Sink::Output, ::hyper::Error> {
121+
use hyper::Client;
122+
use hyper::header::ContentType;
123+
use hyper::mime::Attr::Charset;
124+
use encoding::label::encoding_from_whatwg_label;
125+
126+
let mut response = try!(Client::new().get(url).send());
127+
let opts = BytesOpts {
128+
transport_layer_encoding: response.headers.get::<ContentType>()
129+
.and_then(|content_type| content_type.get_param(Charset))
130+
.and_then(|charset| encoding_from_whatwg_label(charset))
131+
};
132+
Ok(try!(self.from_bytes(opts).read_from(&mut response)))
133+
}
116134
}
117135

118136
/// Options for choosing a character encoding

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#[cfg(feature = "heap_size")]
1919
extern crate heapsize;
2020

21+
#[cfg(feature = "with-hyper")] extern crate hyper;
22+
2123
#[macro_use]
2224
extern crate log;
2325

0 commit comments

Comments
 (0)