2015-01-08 14 views
7

For a little fun Volevo effettuare una semplice richiesta HTTP in Rust. Ho buttato questo insieme e funziona benissimo:Cosa può causare che Rust's TcpSocket :: write() restituisca "input non valido"?

use std::io::TcpStream; 

fn main() { 
    // This just does a "GET /" to www.stroustrup.com 
    println!("Establishing connection..."); 
    let mut stream = TcpStream::connect("www.stroustrup.com:80").unwrap(); 

    println!("Writing HTTP request..."); 
    // unwrap() the result to make sure it succeeded, at least 
    let _ = stream.write(b"GET/HTTP/1.1\r\n\ 
          Host: www.stroustrup.com\r\n\ 
          Accept: */*\r\n\ 
          Connection: close\r\n\r\n").unwrap(); 

    println!("Reading response..."); 
    let response = stream.read_to_string().unwrap(); 

    println!("Printing response:"); 
    println!("{}", response); 
} 

risposta è:

Establishing connection... 
Writing HTTP request... 
Reading response... 
Printing response: 
HTTP/1.1 200 OK 
...and the rest of the long HTTP response with all the HTML as I'd expect... 

Tuttavia, se cambio la richiesta di essere /C++.html invece di /:

use std::io::TcpStream; 

fn main() { 
    // The only change is to "GET /C++.html" instead of "GET /" 
    println!("Establishing connection..."); 
    let mut stream = TcpStream::connect("www.stroustrup.com:80").unwrap(); 

    println!("Writing HTTP request..."); 
    // unwrap() the result to make sure it succeeded, at least 
    let _ = stream.write(b"GET /C++.html HTTP/1.1\r\n\ 
          Host: www.stroustrup.com\r\n\ 
          Accept: */*\r\n\ 
          Connection: close\r\n\r\n").unwrap(); 

    println!("Reading response..."); 
    let response = stream.read_to_string().unwrap(); 

    println!("Printing response:"); 
    println!("{}", response); 
} 

La presa rendimenti "invalid input":

Establishing connection... 
Writing HTTP request... 
Reading response... 
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: invalid input', /Users/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-mac/build/src/libcore/result.rs:746 

Perché la presa restituisce "invalid input"? Il socket TCP non è a conoscenza del protocollo HTTP (e ho testato la mia richiesta con telnet e netcat: è corretto), quindi non può lamentarsi della richiesta/risposta HTTP.

Che cosa significa anche "invalid input" qui? Perché non funziona?

La mia versione di ruggine (io sono su OS X 10.10.1):

$ rustc --version 
rustc 1.0.0-nightly (ea6f65c5f 2015-01-06 19:47:08 +0000) 

risposta

9

L'errore "invalid input" non è venuta dalla presa. Proviene da String. Se la chiamata read_to_string() viene modificata in read_to_end(), la risposta ha esito positivo. Apparentemente la risposta non è valida UTF-8.

Più esplicitamente, il codice:

println!("Reading response..."); 
let response = stream.read_to_end().unwrap(); 

println!("Printing response:"); 
println!("{}", String::from_utf8(response)); 

rendimenti:

Err(invalid utf-8: invalid byte at index 14787) 

Quindi la risposta HTTP è male. Guardando la pagina web, l'errore è qui (i personaggi sono il problema):

Lang.Next'14 Keynote: What � if anything � have we learned from C++? 
1

I personaggi incriminati sono 0x96, anzi non valida utf-8. Dovrebbe essere U + 2013 – Il documento è iso-8859-1 o Windows 1252. Ci sono molti altri problemi con quell'HTML, come ad esempio lo & senza caratteri di escape.

Problemi correlati