2015-01-08 5 views
7

For a little fun Ich wollte eine einfache HTTP-Anfrage in Rust machen. Ich warf dies zusammen und es funktioniert super:Was kann dazu führen, dass TcpSocket :: write() von Rust "ungültige Eingabe" zurückgibt?

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); 
} 

Antwort ist:

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... 

Wenn ich jedoch die Anforderung ändern /C++.html statt / zu sein:

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); 
} 

Die Buchse kehrt "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 

Warum gibt der Sockel "invalid input" zurück? Dem TCP-Socket ist das HTTP-Protokoll nicht bekannt (und ich habe meine Anfrage mit telnet und netcat getestet: es ist korrekt), sodass es sich nicht über HTTP-Anfragen/-Antworten beschweren kann.

Was bedeutet "invalid input" hier sogar? Warum funktioniert das nicht?

Mein Rost Version (ich bin auf OS X 10.10.1):

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

Antwort

9

Der "invalid input" Fehler nicht aus der Steckdose kommt. Es kommt von String. Wenn der Aufruf read_to_string() in read_to_end() geändert wird, ist die Antwort erfolgreich. Anscheinend ist die Antwort nicht UTF-8 gültig.

Deutlicher ausgedrückt, der Code:

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

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

kehrt:

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

So ist die HTTP-Antwort ist schlecht. Mit Blick auf die Web-Seite, der Fehler ist hier (die Zeichen sind das Problem):

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

Die anstößigen Zeichen sind 0x96, in der Tat ungültig utf-8. Es sollte U + 2013 sein – Das Dokument ist entweder ISO-8859-1 oder Windows 1252. Es gibt eine Reihe anderer Probleme mit diesem HTML, wie unescaped &.

Verwandte Themen