HTTP is an application-layer protocol for transporting content such as HTML, images, and video. Web frameworks understand HTTP, so knowing how it works can help you improve performance and diagnose problems. HTTP sits above TCP, with TLS between them when using HTTPS. section 8.1
A basic HTTP request depends on details at both the protocol and transport levels:
- A GET request includes a request line and headers; the Host header supplies the hostname even though the TCP connection uses an IP address. section 8.4
- The example uses HTTP/1.0 so the server closes the connection after responding; its code would wait indefinitely if changed to HTTP/1.1 without sending another request. section 8.4
- A manual client can combine HTTP request handling with TCP, Ethernet, and DNS components. section 8.10
A minimal HTTP GET request
Over a Rust TCP stream, write `GET / HTTP/1.0\r\nHost: www.rustinaction.com\r\n\r\n`, then copy the response to standard output. section 8.4
Figure 8.2 Several layers of networking protocols involved with delivering content over the internet. The figure compares some common models, including the seven-layer OSI model and the four-layer TCP/IP model.
Compare the OSI and TCP/IP layer models to see how HTTP fits into internet content delivery. section 8.1
Listing 8.6 Constructing an HTTP GET request
1 use std::io::prelude::*;
2 use std::net::TcpStream;
3
4 fn main() -> std::io::Result<()> {
5 let host = "www.rustinaction.com:80"; #1
6
7 let mut conn =
8 TcpStream::connect(host)?;
9
10 conn.write_all(b"GET / HTTP/1.0")?;
11 conn.write_all(b"\r\n")?; #2
12
13 conn.write_all(b"Host: www.rustinaction.com")?;
14 conn.write_all(b"\r\n\r\n")?; #3
15
16 std::io::copy( #4
17 &mut conn, #4
18 &mut std::io::stdout() #4
19 )?; #4
20
21 Ok(())
22 }
See a Rust standard-library example that opens a TCP connection, writes an HTTP GET request, and prints the response. section 8.4







