20
20
//! ```rust
21
21
//! use http_io::client::HttpRequestBuilder;
22
22
//! use http_io::error::Result;
23
- //! use http_io::url::Url ;
23
+ //! use http_io::url::HttpUrl ;
24
24
//! use std::io;
25
25
//! use std::net::TcpStream;
26
26
//!
27
27
//! fn main() -> Result<()> {
28
- //! let url: Url = "http://www.google.com".parse()?;
29
- //! let s = TcpStream::connect((url.authority.as_ref (), url .port()? ))?;
30
- //! let mut response = HttpRequestBuilder::get(url )?.send(s)?.finish()?;
28
+ //! let http_url: HttpUrl = "http://www.google.com".parse()?;
29
+ //! let s = TcpStream::connect((http_url.host (), http_url .port()))?;
30
+ //! let mut response = HttpRequestBuilder::get(http_url )?.send(s)?.finish()?;
31
31
//! println!("{:#?}", response.headers);
32
32
//! io::copy(&mut response.body, &mut io::stdout())?;
33
33
//! Ok(())
41
41
//! use std::io;
42
42
//!
43
43
//! fn main() -> Result<()> {
44
- //! let url: Url = "http://www.google.com".parse( )?;
44
+ //! let url = Url::parse( "http://www.google.com")?;
45
45
//! let mut client = HttpClient::<std::net::TcpStream>::new();
46
46
//! for path in &["/", "/favicon.ico", "/robots.txt"] {
47
47
//! let mut url = url.clone();
48
- //! url.path = path.parse()? ;
48
+ //! url.set_path(&path) ;
49
49
//! io::copy(&mut client.get(url)?.finish()?.body, &mut io::stdout())?;
50
50
//! }
51
51
//! Ok(())
@@ -59,7 +59,7 @@ use crate::protocol::{HttpBody, HttpStatus};
59
59
use crate :: protocol:: { HttpMethod , HttpRequest , OutgoingRequest } ;
60
60
#[ cfg( feature = "std" ) ]
61
61
use crate :: url:: Scheme ;
62
- use crate :: url:: Url ;
62
+ use crate :: url:: { HttpUrl , Url } ;
63
63
#[ cfg( not( feature = "std" ) ) ]
64
64
use alloc:: string:: { String , ToString as _} ;
65
65
use core:: convert:: TryInto ;
@@ -74,63 +74,63 @@ pub struct HttpRequestBuilder {
74
74
75
75
impl HttpRequestBuilder {
76
76
/// Create a `HttpRequestBuilder` to build a DELETE request
77
- pub fn delete < U : TryInto < Url > > ( url : U ) -> Result < Self >
77
+ pub fn delete < U : TryInto < HttpUrl > > ( url : U ) -> Result < Self >
78
78
where
79
- <U as TryInto < Url > >:: Error : Display ,
79
+ <U as TryInto < HttpUrl > >:: Error : Display ,
80
80
{
81
81
HttpRequestBuilder :: new ( url, HttpMethod :: Delete )
82
82
}
83
83
84
84
/// Create a `HttpRequestBuilder` to build a GET request
85
- pub fn get < U : TryInto < Url > > ( url : U ) -> Result < Self >
85
+ pub fn get < U : TryInto < HttpUrl > > ( url : U ) -> Result < Self >
86
86
where
87
- <U as TryInto < Url > >:: Error : Display ,
87
+ <U as TryInto < HttpUrl > >:: Error : Display ,
88
88
{
89
89
HttpRequestBuilder :: new ( url, HttpMethod :: Get )
90
90
}
91
91
92
92
/// Create a `HttpRequestBuilder` to build a HEAD request
93
- pub fn head < U : TryInto < Url > > ( url : U ) -> Result < Self >
93
+ pub fn head < U : TryInto < HttpUrl > > ( url : U ) -> Result < Self >
94
94
where
95
- <U as TryInto < Url > >:: Error : Display ,
95
+ <U as TryInto < HttpUrl > >:: Error : Display ,
96
96
{
97
97
HttpRequestBuilder :: new ( url, HttpMethod :: Head )
98
98
}
99
99
100
100
/// Create a `HttpRequestBuilder` to build an OPTIONS request
101
- pub fn options < U : TryInto < Url > > ( url : U ) -> Result < Self >
101
+ pub fn options < U : TryInto < HttpUrl > > ( url : U ) -> Result < Self >
102
102
where
103
- <U as TryInto < Url > >:: Error : Display ,
103
+ <U as TryInto < HttpUrl > >:: Error : Display ,
104
104
{
105
105
HttpRequestBuilder :: new ( url, HttpMethod :: Options )
106
106
}
107
107
108
108
/// Create a `HttpRequestBuilder` to build a POST request
109
- pub fn post < U : TryInto < Url > > ( url : U ) -> Result < Self >
109
+ pub fn post < U : TryInto < HttpUrl > > ( url : U ) -> Result < Self >
110
110
where
111
- <U as TryInto < Url > >:: Error : Display ,
111
+ <U as TryInto < HttpUrl > >:: Error : Display ,
112
112
{
113
113
HttpRequestBuilder :: new ( url, HttpMethod :: Post )
114
114
}
115
115
116
116
/// Create a `HttpRequestBuilder` to build a PUT request
117
- pub fn put < U : TryInto < Url > > ( url : U ) -> Result < Self >
117
+ pub fn put < U : TryInto < HttpUrl > > ( url : U ) -> Result < Self >
118
118
where
119
- <U as TryInto < Url > >:: Error : Display ,
119
+ <U as TryInto < HttpUrl > >:: Error : Display ,
120
120
{
121
121
HttpRequestBuilder :: new ( url, HttpMethod :: Put )
122
122
}
123
123
124
124
/// Create a `HttpRequestBuilder`. May fail if the given url does not parse.
125
- pub fn new < U : TryInto < Url > > ( url : U , method : HttpMethod ) -> Result < Self >
125
+ pub fn new < U : TryInto < HttpUrl > > ( url : U , method : HttpMethod ) -> Result < Self >
126
126
where
127
- <U as TryInto < Url > >:: Error : Display ,
127
+ <U as TryInto < HttpUrl > >:: Error : Display ,
128
128
{
129
- let url = url
129
+ let url: HttpUrl = url
130
130
. try_into ( )
131
131
. map_err ( |e| Error :: ParseError ( e. to_string ( ) ) ) ?;
132
- let mut request = HttpRequest :: new ( method, url. path ( ) ) ;
133
- request. add_header ( "Host" , url. authority . clone ( ) ) ;
132
+ let mut request = HttpRequest :: new ( method, url. url ( ) . path ( ) ) ;
133
+ request. add_header ( "Host" , url. host ( ) . to_string ( ) ) ;
134
134
request. add_header ( "User-Agent" , "http_io" ) ;
135
135
request. add_header ( "Accept" , "*/*" ) ;
136
136
if method. has_body ( ) {
@@ -226,19 +226,23 @@ impl StreamConnector for std::net::TcpStream {
226
226
}
227
227
228
228
fn to_stream_addr ( url : Url ) -> Result < Self :: StreamAddr > {
229
+ use core:: convert:: TryFrom ;
230
+
231
+ let http_url = HttpUrl :: try_from ( url) ?;
229
232
let err = || {
230
233
std:: io:: Error :: new (
231
234
std:: io:: ErrorKind :: AddrNotAvailable ,
232
- format ! ( "Failed to lookup {}" , & url . authority ) ,
235
+ format ! ( "Failed to lookup {}" , http_url . host ( ) ) ,
233
236
)
234
237
} ;
238
+
235
239
Ok ( StreamId {
236
- addr : std:: net:: ToSocketAddrs :: to_socket_addrs ( & ( url . authority . as_ref ( ) , url . port ( ) ? ) )
240
+ addr : std:: net:: ToSocketAddrs :: to_socket_addrs ( & ( http_url . host ( ) , http_url . port ( ) ) )
237
241
. map_err ( |_| err ( ) ) ?
238
242
. next ( )
239
243
. ok_or_else ( err) ?,
240
- host : url . authority ,
241
- secure : url . scheme == Scheme :: Https ,
244
+ host : String :: from ( http_url . host ( ) ) ,
245
+ secure : Scheme :: Https . eq ( & http_url . scheme ( ) ) ,
242
246
} )
243
247
}
244
248
}
0 commit comments