0

I'm using code from How do I make a TLS connection using the rustls library? to make a socket with TLS and send a GET request to Google.

When I run cargo run, the program prints nothing to the screen.

use rustls;
use rustls::{ClientConfig, ClientSession, Session, TLSError};
use std::env;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::net::TcpStream;
use std::process;
use std::str;
use std::sync::Arc;
use webpki;
use webpki_roots;

fn main() {
    let mut socket = std::net::TcpStream::connect("www.google.com:443").unwrap();
    let mut config = rustls::ClientConfig::new();
    config
        .root_store
        .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
    let arc = std::sync::Arc::new(config);
    let dns_name = webpki::DNSNameRef::try_from_ascii_str("www.google.com").unwrap();
    let mut client = rustls::ClientSession::new(&arc, dns_name);
    let mut stream = rustls::Stream::new(&mut client, &mut socket); // Create stream
                                                                    // Instead of writing to the client, you write to the stream
    stream
        .write(b"GET / HTTP/1.1\r\nConnection: close\r\n\r\n")
        .unwrap();
    let mut plaintext = Vec::new();
    stream.read_to_end(&mut plaintext).unwrap();
    io::stdout().write_all(&plaintext).unwrap();
}

And in my Cargo.toml file:

[package]
name = "X"
version = "0.1.0"
authors = ["Behdad"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rustls = "0.17.0"
webpki = "0.21.2"
webpki-roots = "0.19.0"
serde_derive = "1.0.105"
serde = "1.0.105"
Behdad
  • 1,459
  • 3
  • 24
  • 36
  • 1
    It's hard to answer multiple questions made in one post. Please separate them into multiple questions so that we can help you better and so that your questions will help others in the future that have one of the same questions as you! [I've removed](https://stackoverflow.com/posts/60833492/revisions) the separate question. – Shepmaster Mar 24 '20 at 15:00
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Mar 24 '20 at 15:01
  • It looks like your question might be answered by the answers of [Why is a raw HTTP request extremely slow?](https://stackoverflow.com/q/28795609/155423); [Get Request to a Website with vanilla Rust](https://stackoverflow.com/q/50312240/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Mar 24 '20 at 15:01
  • An important part of a [MRE] example is that it's **minimal**. You have included crates that don't appear to be used, which is a good sign that you haven't fully minimized the problem. You might even be able to remove the TLS aspect and see the same problem. – Shepmaster Mar 24 '20 at 15:09
  • 1
    @Shepmaster Second question which you removed it wasn't separated question, just needs a very quick change in the code. This is the code I found on Stackoverflow, and it was marked as answer (and question was also same as mine) but it didn't work. Anyway I included `Cargo.toml` file also. And for your third comment, I should say this is different from the question you mentioned, because here I am using a TLS library and persistent connection here is not a deal. Thank you anyway. – Behdad Mar 24 '20 at 15:11

1 Answers1

2
.write(b"GET / HTTP/1.1\r\nConnection: close\r\n\r\n")

This is not a valid HTTP/1.1 request. It is missing the Host header.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • 1
    Thank you so much!! I just copied the code from another question without checking carefully!! I tried `rustls` example code but got no result for entire day! And just one more question : How can I use IP address instead of url? – Behdad Mar 24 '20 at 15:14
  • @Behdad: *"And just one more question"* - a comment is not a place to ask new questions. Also, you don't use a URL in the first place. If you refer to `www.google.com` - that's a domain name. A URL would be `https://www.google.com`. – Steffen Ullrich Mar 24 '20 at 15:18
  • danke schön! Sie helfen mir sehr gut! – Behdad Mar 24 '20 at 15:21