For a little fun我想在Rust中创建一个简单的HTTP请求。我把它扔在一起,效果很好:
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);
}
回应是:
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...
但是,如果我将请求更改为/C++.html
而不是/
:
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);
}
套接字返回"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
为什么套接字返回"invalid input"
? TCP套接字不知道HTTP协议(我用telnet和netcat测试了我的请求:它是正确的),所以它不能抱怨HTTP请求/响应。
"invalid input"
在这里意味着什么?为什么这不起作用?
我的锈版(我在OS X 10.10.1上):
$ rustc --version
rustc 1.0.0-nightly (ea6f65c5f 2015-01-06 19:47:08 +0000)
答案 0 :(得分:9)
"invalid input"
错误不是来自套接字。它来自String
。如果read_to_string()
调用更改为read_to_end()
,则响应成功。显然,响应不是有效的UTF-8。
更明确地说,代码:
println!("Reading response...");
let response = stream.read_to_end().unwrap();
println!("Printing response:");
println!("{}", String::from_utf8(response));
返回:
Err(invalid utf-8: invalid byte at index 14787)
所以HTTP响应很糟糕。查看网页,错误就在这里(�
字符是问题):
Lang.Next'14 Keynote: What � if anything � have we learned from C++?
答案 1 :(得分:1)
违规字符为0x96,确实无效utf-8。它应该是U + 2013 - 该文档是iso-8859-1或Windows 1252.该HTML存在许多其他问题,例如未转义的&#; p。
。