Skip to content

Commit 95ee7b6

Browse files
committed
Add optional hyper support.
1 parent 2f4f64b commit 95ee7b6

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mac = "0"
2929
tendril = "0.2"
3030
heapsize = { version = "0.1.1", optional = true }
3131
heapsize_plugin = { version = "0.1.0", optional = true }
32+
hyper = {version = "0.7", optional = true}
3233

3334
[dev-dependencies]
3435
rustc-serialize = "0.3.15"

scripts/travis-build.sh

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
set -ex
1212

13+
cargo build --features 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

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

119137
/// Options for choosing a character encoding

src/lib.rs

+2
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 = "hyper")] extern crate hyper;
22+
2123
#[macro_use]
2224
extern crate log;
2325

0 commit comments

Comments
 (0)