2015-01-08 16 views
7

For a little fun Rust'ta basit bir HTTP isteği yapmak istedim. Ben birlikte bu attı ve o inşaat büyük:Rust'un TcpSocket :: write() "geçersiz girdiyi" döndürmesine neden olabilir?

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

Tepki geçerli:

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

Ancak, ben /C++.html yerine / olmak isteğini değiştirirseniz:

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

soket döner "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 

Soket neden "invalid input"? TCP soketi HTTP protokolünün farkında değil (ve isteğimi telnet ve netcat ile test ettim: doğru), bu yüzden HTTP isteği/yanıtından şikayetçi olamaz.

"invalid input" ne anlama geliyor? Bu neden çalışmıyor?

My pas sürümü (Ben OS X 10.10.1 kulüpler): "invalid input" hata soketinden gelmiyor

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

cevap

9

. String'dan geliyor. read_to_string() çağrısı read_to_end() olarak değiştirilirse, yanıt başarılı olur. Görünüşe göre yanıt geçerli değil UTF-8. Daha açıkça

, kod:

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

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

döner:

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

Yani HTTP yanıtı kötüdür. Web sayfasının baktığımızda, hata burada ( karakter sorunu vardır):

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

kusurlu karakterler aslında utf-8 geçersiz 0x96 vardır. U + 2013 – olmalıdır. Belge iso-8859-1 veya windows 1252'dir. Önizlenmemiş & 'ler gibi HTML ile ilgili başka birçok sorun vardır.

İlgili konular