The HTTP 1.1 protocol extends the capabilities of the previous version and adds virtual hosts. Now we'll see what we need it for and how it works.
The previous version requires only the request line. In it, we describe which path on the site we want to look at(query string). But there's no mention of a site as such. We connect via telnet to a specific IP address. From this, we can conclude that the concept of a domain name is not important when using HTTP 1.0. Indeed, this version was created at a time when it was believed that one IP would correspond to one site. Naturally, this could not last long, because the internet grew rapidly. And HTTP 1.1 introduced virtual hosts. From the point of view of implementation in the HTTP protocol, just one small detail appeared. In addition to the request line, a header called host also became mandatory. It determines which domain should be returned from this IP address.
HEAD / HTTP/1.1 host: hexlet.io
In fact, sometimes there are some default elements in the behavior of HTTP and servers, which can, among other things, correct user errors. I.e., you can make an HTTP 1.1 request without specifying a host and get something in response. Ideally, such a request should not pass at all, but often web servers give the site given in the default settings. For example, Nginx has a default site. But this isn't really how requests should be made, it doesn't comply with the standard. You must always specify the host, otherwise there'll be no guaranteed response. It might be one response today, and a different response tomorrow. Some libraries may not work like this at all. Virtual hosts are an integral attribute of HTTP 1.1. They cannot be ignored.
Now we can make a GET request using version 1.1 and see what it will return to us:
telnet google.com 80 GET / HTTP/1.1 host: google.com HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Fri, 28 Feb 2020 06:06:40 GMT Expires: Sun, 29 Mar 2020 06:06:40 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN <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="http://www.google.com/">here</A>. </BODY></HTML>
As we already know, there are many different codes. As a result, we received an answer that's different from 200. There's another interesting detail here, that was introduced in HTTP 1.1, and that we should pay attention to. After executing the request, we didn't come out of telnet and end up in bash. This means that the connection hasn't been closed, and we can continue to enter data. Let's make a HEAD request for the same domain:
HEAD / HTTP/1.1 host: google.com HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Fri, 28 Feb 2020 06:11:31 GMT Expires: Sun, 29 Mar 2020 06:11:31 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN
We got a response again, but without the body, because we used the HEAD, not the GET request.
HTTP 1.1 introduces another default concept called keep-alive. keep-alive means that the TCP connection over that HTTP is running through isn't closed. Moreover, by default, all web servers should behave this way. The main purpose of introducing this feature is to reduce resource usage and CPU load, open fewer TCP connections (establishing each TCP connection takes time), and reduce latency. When we open a website, several resources are usually loaded from one domain. keep-alive allows you to open and use a single connection that won't be closed until explicitly specified, or if there's a timeout. The timeout depends on the browser and web server.
We can also state that we want to close the connection. To do this, after establishing a connection and passing standard headers, you need to pass another header. It's called
connection: close. Then keep-alive will be disabled, and after receiving a response, we'll see a message saying that the host has closed the connection:
Connection closed by foreign host.
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.