【问题标题】:rustls HTTP request response 301rustls HTTP 请求响应 301
【发布时间】:2020-09-05 14:03:49
【问题描述】:

使用example rustls client code发送HTTP请求

let mut config = rustls::ClientConfig::new();
config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);

let dns_name = webpki::DNSNameRef::try_from_ascii_str("google.com").unwrap();
let mut sess = rustls::ClientSession::new(&Arc::new(config), dns_name);
let mut sock = TcpStream::connect("google.com:443").unwrap();
let mut tls = rustls::Stream::new(&mut sess, &mut sock);
tls.write(concat!("GET / HTTP/1.1\r\n",
                  "Host: google.com\r\n",
                  "Connection: close\r\n",
                  "Accept-Encoding: identity\r\n",
                  "\r\n")
          .as_bytes())
    .unwrap();
let ciphersuite = tls.sess.get_negotiated_ciphersuite().unwrap();
writeln!(&mut std::io::stderr(), "Current ciphersuite: {:?}", ciphersuite.suite).unwrap();
let mut plaintext = Vec::new();
tls.read_to_end(&mut plaintext).unwrap();
stdout().write_all(&plaintext).unwrap();

导致重定向消息:

Current ciphersuite: TLS13_CHACHA20_POLY1305_SHA256
HTTP/1.1 301 Moved Permanently
Location: https://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Sat, 05 Sep 2020 13:56:25 GMT
Expires: Mon, 05 Oct 2020 13:56:25 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 220
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Alt-Svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Connection: close

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

此示例代码的行为是否正确?我应该更改哪些内容才能收到 OK 响应?

【问题讨论】:

    标签: http ssl https rust


    【解决方案1】:

    此示例代码的行为是否正确?

    是的。这也可以在使用其他工具尝试相同类型的请求时进行检查:

    $ curl -v https://google.com
    ...
    < HTTP/1.1 301 Moved Permanently
    < Location: https://www.google.com/
    

    我应该更改哪些内容才能收到 OK 响应?

    只需使用重定向中给出的 URL,即 https://www.google.com 而不是 https://google.com

    $ curl -v https://www.google.com
    ...
    < HTTP/1.1 200 OK
    

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 1970-01-01
      • 2021-08-28
      • 2020-11-17
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多