File tree Expand file tree Collapse file tree 4 files changed +23
-0
lines changed
Expand file tree Collapse file tree 4 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ doctest = false
1919unstable = [" tendril/unstable" , " string_cache/unstable" ]
2020heap_size = [" heapsize" , " heapsize_plugin" ]
2121codegen = [" html5ever_macros" ]
22+ with-hyper = [" hyper" ]
2223
2324[dependencies ]
2425time = " 0"
@@ -29,6 +30,7 @@ mac = "0"
2930tendril = " 0.2"
3031heapsize = { version = " 0.1.1" , optional = true }
3132heapsize_plugin = { version = " 0.1.0" , optional = true }
33+ hyper = {version = " 0.7" , optional = true }
3234
3335[dev-dependencies ]
3436rustc-serialize = " 0.3.15"
Original file line number Diff line number Diff line change 1010
1111set -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.
1415cargo test --no-run
1516cargo test | ./scripts/shrink-test-output.py
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ use std::borrow::Cow;
1616use std:: mem;
1717
1818use encoding:: { self , EncodingRef } ;
19+ #[ cfg( feature = "with-hyper" ) ] use hyper:: client:: IntoUrl ;
1920use string_cache:: QualName ;
2021use tendril;
2122use 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
Original file line number Diff line number Diff line change 1818#[ cfg( feature = "heap_size" ) ]
1919extern crate heapsize;
2020
21+ #[ cfg( feature = "with-hyper" ) ] extern crate hyper;
22+
2123#[ macro_use]
2224extern crate log;
2325
You can’t perform that action at this time.
0 commit comments