420skun
420skun

Reputation: 139

rustls HTTP request response 301

Sending an HTTP request using the example rustls client code

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

results in a redirection message:

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>

Is this the correct behavior for this example code? What am I supposed to change to receive an OK response?

Upvotes: 1

Views: 374

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123531

Is this the correct behavior for this example code?

Yes. This can also be checked when trying the same kind of request using other tools:

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

What am I supposed to change to receive an OK response?

Just use the URL given in the redirect instead, i.e. https://www.google.com and not https://google.com.

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

Upvotes: 1

Related Questions