Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace hyper with reqwest #7

Merged
merged 26 commits into from
Oct 28, 2018
Merged

Replace hyper with reqwest #7

merged 26 commits into from
Oct 28, 2018

Conversation

iceiix
Copy link
Owner

@iceiix iceiix commented Oct 24, 2018

@iceiix
Copy link
Owner Author

iceiix commented Oct 24, 2018

   Compiling steven v0.0.1 (/Users/admin/games/rust/steven)                                                                                                                                                         
error[E0423]: expected function, found macro `log::set_boxed_logger`                                                                                                                                                
   --> src/main.rs:182:5                                                                                                                                                                                            
    |                                                                                                                                                                                                               
182 |     log::set_boxed_logger(Box::new(proxy)).unwrap();                                                                                                                                                          
    |     ^^^^^^^^^^^^^^^^^^^^^ did you mean `log::set_boxed_logger!(...)`?                                                                                                                                         
                                                                                                                                                                                                                    
error[E0107]: wrong number of type arguments: expected at least 1, found 0                                                                                                                                          
   --> src/render/mod.rs:859:29                                                                                                                                                                                     
    |                                                                                                                                                                                                               
859 |     fn obtain_skin(client: &::hyper::Client, hash: &str) -> Result<image::DynamicImage, ::std::io::Error> {                                                                                                   
    |                             ^^^^^^^^^^^^^^^ expected at least 1 type argument                                                                                                                                 
                                                                                                                                                                                                                    
error: aborting due to 2 previous errors                                                                                                                                                                            

@iceiix
Copy link
Owner Author

iceiix commented Oct 24, 2018

   Compiling steven v0.0.1 (/Users/admin/games/rust/steven)                                                                                                                                                         
error[E0423]: expected function, found macro `log::set_boxed_logger`                                                                                                                                                
   --> src/main.rs:182:5                                                                                                                                                                                            
    |                                                                                                                                                                                                               
182 |     log::set_boxed_logger(Box::new(proxy)).unwrap();                                                                                                                                                          
    |     ^^^^^^^^^^^^^^^^^^^^^ did you mean `log::set_boxed_logger!(...)`?                                                                                                                                         
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
  --> src/protocol/mojang.rs:44:31                                                                                                                                                                                  
   |                                                                                                                                                                                                                
44 |         let res = try!(client.post(LOGIN_URL)                                                                                                                                                                  
   |                               ^^^^                                                                                                                                                                             
                                                                                                                                                                                                                    
error[E0618]: expected function, found `hyper::header::HeaderName`                                                                                                                                                  
  --> src/protocol/mojang.rs:46:33                                                                                                                                                                                  
   |                                                                                                                                                                                                                
46 |                         .header(hyper::header::CONTENT_TYPE("application/json".parse().unwrap()))                                                                                                              
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a function                                                                                                
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
  --> src/protocol/mojang.rs:72:31                                                                                                                                                                                  
   |                                                                                                                                                                                                                
72 |         let res = try!(client.post(VALIDATE_URL)                                                                                                                                                               
   |                               ^^^^                                                                                                                                                                             
                                                                                                                                                                                                                    
error[E0618]: expected function, found `hyper::header::HeaderName`                                                                                                                                                  
  --> src/protocol/mojang.rs:74:33                                                                                                                                                                                  
   |                                                                                                                                                                                                                
74 |                         .header(hyper::header::CONTENT_TYPE("application/json".parse().unwrap()))                                                                                                              
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a function                                                                                                
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
  --> src/protocol/mojang.rs:79:35                                                                                                                                                                                  
   |                                                                                                                                                                                                                
79 |             let res = try!(client.post(REFRESH_URL)                                                                                                                                                            
   |                                   ^^^^                                                                                                                                                                         
                                                                                                                                                                                                                    
error[E0618]: expected function, found `hyper::header::HeaderName`                                                                                                                                                  
  --> src/protocol/mojang.rs:81:37                                                                                                                                                                                  
   |                                                                                                                                                                                                                
81 |                             .header(hyper::header::CONTENT_TYPE("application/json".parse().unwrap()))                                                                                                          
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a function                                                                                            
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
   --> src/protocol/mojang.rs:130:31                                                                                                                                                                                
    |                                                                                                                                                                                                               
130 |         let res = try!(client.post(JOIN_URL)                                                                                                                                                                  
    |                               ^^^^                                                                                                                                                                            
                                                                                                                                                                                                                    
error[E0618]: expected function, found `hyper::header::HeaderName`                                                                                                                                                  
   --> src/protocol/mojang.rs:132:33                                                                                                                                                                                
    |                                                                                                                                                                                                               
132 |                         .header(hyper::header::CONTENT_TYPE("application/json".parse().unwrap()))                                                                                                             
    |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a function                                                                                               
                                                                                                                                                                                                                    
error[E0308]: mismatched types                                                                                                                                                                                      
   --> src/resources.rs:291:38                                                                                                                                                                                      
    |                                                                                                                                                                                                               
291 |                 let res = client.get(ASSET_INDEX_URL)                                                                                                                                                         
    |                                      ^^^^^^^^^^^^^^^ expected struct `hyper::Uri`, found reference                                                                                                            
    |                                                                                                                                                                                                               
    = note: expected type `hyper::Uri`                                                                                                                                                                              
               found type `&'static str`                                                                                                                                                                            
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/resources.rs:292:34                                                                                                                                                                                      
    |                                                                                                                                                                                                               
292 |                                 .send()                                                                                                                                                                       
    |                                  ^^^^                                                                                                                                                                         
                                                                                                                                                                                                                    
error[E0308]: mismatched types                                                                                                                                                                                      
   --> src/resources.rs:321:42                                                                                                                                                                                      
    |                                                                                                                                                                                                               
321 |                     let res = client.get(&format!("http://resources.download.minecraft.net/{}", hash_path))                                                                                                   
    |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `hyper::Uri`, found reference                                                      
    |                                                                                                                                                                                                               
    = note: expected type `hyper::Uri`                                                                                                                                                                              
               found type `&std::string::String`                                                                                                                                                                    
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/resources.rs:322:38                                                                                                                                                                                      
    |                                                                                                                                                                                                               
322 |                                     .send()                                                                                                                                                                   
    |                                      ^^^^                                                                                                                                                                     
                                                                                                                                                                                                                    
error[E0308]: mismatched types                                                                                                                                                                                      
   --> src/resources.rs:358:34                                                                                                                                                                                      
    |                                                                                                                                                                                                               
358 |             let res = client.get(VANILLA_CLIENT_URL)                                                                                                                                                          
    |                                  ^^^^^^^^^^^^^^^^^^ expected struct `hyper::Uri`, found reference                                                                                                             
    |                                                                                                                                                                                                               
    = note: expected type `hyper::Uri`                                                                                                                                                                              
               found type `&'static str`                                                                                                                                                                            
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/resources.rs:359:30                                                                                                                                                                                      
    |                                                                                                                                                                                                               
359 |                             .send()                                                                                                                                                                           
    |                              ^^^^                                                                                                                                                                             
                                                                                                                                                                                                                    
error[E0308]: mismatched types                                                                                                                                                                                      
   --> src/render/mod.rs:875:44                                                                                                                                                                                     
    |                                                                                                                                                                                                               
875 |             let mut res = match client.get(&url).send() {                                                                                                                                                     
    |                                            ^^^^ expected struct `hyper::Uri`, found reference                                                                                                                 
    |                                                                                                                                                                                                               
    = note: expected type `hyper::Uri`                                                                                                                                                                              
               found type `&std::string::String`                                                                                                                                                                    
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/render/mod.rs:875:50                                                                                                                                                                                     
    |                                                                                                                                                                                                               
875 |             let mut res = match client.get(&url).send() {                                                                                                                                                     
    |                                                  ^^^^                                                                                                                                                         
                                                                                                                                                                                                                    
error: aborting due to 17 previous errors                           

@iceiix
Copy link
Owner Author

iceiix commented Oct 25, 2018

error[E0423]: expected function, found macro `log::set_boxed_logger`                                                                                                                                                
   --> src/main.rs:182:5                                                                                                                                                                                            
    |                                                                                                                                                                                                               
182 |     log::set_boxed_logger(Box::new(proxy)).unwrap();                                                                                                                                                          
    |     ^^^^^^^^^^^^^^^^^^^^^ did you mean `log::set_boxed_logger!(...)`?                                                                                                                                         
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
  --> src/protocol/mojang.rs:44:31                                                                                                                                                                                  
   |                                                                                                                                                                                                                
44 |         let res = try!(client.post(LOGIN_URL.parse::<hyper::Uri>().unwrap())                                                                                                                                   
   |                               ^^^^                                                                                                                                                                             
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
  --> src/protocol/mojang.rs:72:31                                                                                                                                                                                  
   |                                                                                                                                                                                                                
72 |         let res = try!(client.post(VALIDATE_URL.parse::<hyper::Uri>().unwrap())                                                                                                                                
   |                               ^^^^                                                                                                                                                                             
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
  --> src/protocol/mojang.rs:79:35                                                                                                                                                                                  
   |                                                                                                                                                                                                                
79 |             let res = try!(client.post(REFRESH_URL.parse::<hyper::Uri>().unwrap())                                                                                                                             
   |                                   ^^^^                                                                                                                                                                         
                                                                                                                                                                                                                    
error[E0599]: no method named `post` found for type `hyper::Client<hyper::client::HttpConnector>` in the current scope                                                                                              
   --> src/protocol/mojang.rs:130:31                                                                                                                                                                                
    |                                                                                                                                                                                                               
130 |         let res = try!(client.post(JOIN_URL.parse::<hyper::Uri>().unwrap())                                                                                                                                   
    |                               ^^^^                                                                                                                                                                            
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/resources.rs:292:34                                                                                                                                                                                      
    |                                                                                                                                                                                                               
292 |                                 .send()                                                                                                                                                                       
    |                                  ^^^^                                                                                                                                                                         
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/resources.rs:322:38                                                                                                                                                                                      
    |                                                                                                                                                                                                               
322 |                                     .send()                                                                                                                                                                   
    |                                      ^^^^                                                                                                                                                                     
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/resources.rs:359:30                                                                                                                                                                                      
    |                                                                                                                                                                                                               
359 |                             .send()                                                                                                                                                                           
    |                              ^^^^                                                                                                                                                                             
                                                                                                                                                                                                                    
error[E0599]: no method named `send` found for type `hyper::client::ResponseFuture` in the current scope                                                                                                            
   --> src/render/mod.rs:875:80                                                                                                                                                                                     
    |                                                                                                                                                                                                               
875 |             let mut res = match client.get(url.parse::<hyper::Uri>().unwrap()).send() {                                                                                                                       
    |                                                                                ^^^^                                                                                                                           
                                                                                                                                                                                                                    
error: aborting due to 9 previous errors                  

Down to 9 errors, the first about a boxed logger looks familiar, when updating log module: 43f6565#diff-639fbc4ef05b315af92b4d836c31b023. The others are all of two kinds, both missing methods: there is no longer a post(), even though there is get(), and no more send(), what replaces it?

@iceiix
Copy link
Owner Author

iceiix commented Oct 25, 2018

Old syntax:

        let res = try!(client.post(LOGIN_URL.parse::<hyper::Uri>().unwrap())
                         .body(&req)
                         .header(hyper::header::CONTENT_TYPE, "application/json")
                        .send());

New syntax from https://docs.rs/hyper/0.12.12/hyper/struct.Request.html, instead of client.post() pass this to client.request():

let request = Request::builder()
    .method("GET")
    .uri("https://www.rust-lang.org/")
    .header("X-Custom-Foo", "Bar")
    .body(())
    .unwrap();

@iceiix
Copy link
Owner Author

iceiix commented Oct 25, 2018

Converted but client.request(request) returns a ResponseFuture, may need to map() it or as in https://github.com/hyperium/hyper/blob/09156a70a63ce1740bc0d3ecff9db5861bffb389/examples/client_json.rs#L46

     client
        // Fetch the url...
        .get(url)
        // And then, if we get a response back...
        .and_then(|res| {
            // asynchronously concatenate chunks of the body
            res.into_body().concat2()
        })

This also explains the missing method errors in ResponseFuture:

error[E0599]: no method named send found for type hyper::client::ResponseFuture in the current scope

need to update to resolve the ResponseFuture

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

error[E0277]: the trait bound `hyper::Response<hyper::Body>: std::io::Read` is not satisfied                                                                                                                        
  --> src/protocol/mojang.rs:51:43                                                                                                                                                                                  
   |                                                                                                                                                                                                                
51 |         let ret: serde_json::Value = try!(serde_json::from_reader(res));                                                                                                                                       
   |                                           ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `hyper::Response<hyper::Body>`                                                              
   |                                                                                                                                                                                                                
   = note: required by `serde_json::from_reader`    

How to parse JSON from a response? Example uses futures: https://github.com/hyperium/hyper/blob/master/examples/client_json.rs#L39

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

It is convoluted, but this seems to compile:

        let ret: serde_json::Value = try!(serde_json::from_slice(&res.into_body().concat2().wait().unwrap().into_bytes()));

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

                    let mut progress = ProgressRead {
                        read: res,
                        progress: &progress_info,
                        task_name: "Downloading Asset Index".into(),
                        task_file: location.to_string_lossy().into_owned(),
                    };
                    io::copy(&mut progress, &mut file).unwrap();

Need to get the std::io::Read trait from hyper::Responsehyper::Body:

error[E0277]: the trait bound `hyper::Response<hyper::Body>: std::io::Read` is not satisfied                                                                                                                        
   --> src/resources.rs:307:21                                                                                                                                                                                      
    |                                                                                                                                                                                                               
307 |                     io::copy(&mut progress, &mut file).unwrap();                                                                                                                                              
    |                     ^^^^^^^^ the trait `std::io::Read` is not implemented for `hyper::Response<hyper::Body>`                                                                                                  
    |                                                                                                                                                                                                               
    = note: required because of the requirements on the impl of `std::io::Read` for `resources::ProgressRead<'_, hyper::Response<hyper::Body>>`                                                                     
    = note: required by `std::io::copy`         

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

Down to 1 mysterious compile error:

   Compiling steven v0.0.1 (/Users/admin/games/rust/steven)                                                                                                                                                         
error[E0423]: expected function, found macro `log::set_boxed_logger`                                                                                                                                                
   --> src/main.rs:188:5                                                                                                                                                                                            
    |                                                                                                                                                                                                               
188 |     log::set_boxed_logger(Box::new(proxy)).unwrap();                                                                                                                                                          
    |     ^^^^^^^^^^^^^^^^^^^^^ did you mean `log::set_boxed_logger!(...)`?                                                                                                                                         
                                                                                                                                                                                                                    
error: aborting due to previous error                                                                                                                                                                               
                                                                                                                                                                                                                    
For more information about this error, try `rustc --explain E0423`.                                                                                                                                                 
error: Could not compile `steven`.     

log 0.4.5 updated in 43f6565#diff-639fbc4ef05b315af92b4d836c31b023, this change:

-    log::set_logger(|max_log_level| {
-         max_log_level.set(log::LogLevelFilter::Trace);
-         Box::new(proxy)
-     }).unwrap();
+     log::set_boxed_logger(Box::new(proxy)).unwrap();
+     log::set_max_level(log::LevelFilter::Trace);

but it worked before the hyper update. The log 0.4.5 example https://docs.rs/log/0.4.5/log/#use-with-std uses set_boxed_logger:

pub fn init() -> Result<(), SetLoggerError> {
    log::set_boxed_logger(Box::new(SimpleLogger))
        .map(|()| log::set_max_level(LevelFilter::Info))
}

not set_boxed_logger!, why does it think it is a macro? If I change it then set_boxed_logger isn't found either.

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

Got to build, but fails because uses https URLs but must be http:

screen shot 2018-10-26 at 7 49 37 pm

"an error occurred trying to connect: invalid URL, scheme must be http" - need TLS support

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

https://hyper.rs/guides/client/configuration/

By default, a Client can only speak to HTTP addresses. This is because hyper doesn’t ship with a TLS implementation. You may have noticed that Client::new() returns a Client. We can plug in a different connector using the client config.

Since connecting to HTTPS addresses is so common, hyper provides a separate hyper-tls crate with a pluggable HttpsConnector. Here’s how you’d use it.

extern crate hyper;
extern crate hyper_tls;

use hyper::Client;
use hyper_tls::HttpsConnector;

let https = HttpsConnector::new(4).expect("TLS initialization failed");
let client = Client::builder()
    .build::<_, hyper::Body>(https);

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

You can plug in any kind of connector you need. This means that you could pick a different TLS implementation than the one chosen by hyper-tls, such as rustls

hyper-tls uses native-ssl which seems to use OpenSSL, something I want to avoid (https://github.com/iceiix/steven/issues/2). rustls is a Rust implementation of TLS. For why hyper-tls was split out see hyperium/hyper#985, long story short to avoid explicit OpenSSL dependency. Looks like hyper-rustls provides the integration: https://github.com/ctz/hyper-rustls

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

Added hyper-rustls, but now a different error:

screen shot 2018-10-27 at 9 00 11 am

executor failed to spawn task: tokio::spawn failed (is a tokio runtime running this future?)

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

Converting to reqwest is sounding more and more attractive. Hyper getting started https://users.rust-lang.org/t/hyper-client-getting-started/19260 points to reqwest even...

@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

https://docs.rs/reqwest/0.9.4/reqwest/

Additionally, reqwest's Response struct implements Rust's Read trait, so many useful standard library and third party crates will have convenience methods that take a Response anywhere T: Read is acceptable.

this is very nice, solves this problem #7 (comment) which I tried to workaround with the ugly:

            let ret: serde_json::Value = try!(serde_json::from_slice(&res.into_body().concat2().wait().unwrap().into_bytes()));

with reqwest, from_reader() can be used directly on the response since it implements std::io::Read!

            let ret: serde_json::Value = try!(serde_json::from_reader(res));

@iceiix iceiix changed the title Begin updating to hyper 0.12.11 Replace hyper with reqwest (was updating to hyper 0.12.11) Oct 27, 2018
@iceiix
Copy link
Owner Author

iceiix commented Oct 27, 2018

Removed hyper, converted to reqwest, I'm liking it a lot better. Able to login but then cannot auth with server:

screen shot 2018-10-27 at 4 37 51 pm

@iceiix iceiix changed the title Replace hyper with reqwest (was updating to hyper 0.12.11) Replace hyper with reqwest Oct 27, 2018
@iceiix
Copy link
Owner Author

iceiix commented Oct 28, 2018

This is basically done but the mysterious log::set_boxed_error error still occurs, first seen with hyper 0.12.11 but it persists with reqwest 0.9.4 (not seen with the original hyper 0.8.0). rust-lang/log#303

@iceiix iceiix merged commit b17f296 into updates Oct 28, 2018
@iceiix iceiix deleted the hyper branch October 28, 2018 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant