HTTP协议精讲

四层网络模型

+--------------------------+
|    Application (HTTP)    |
+--------------------------+
| Transport Protocol (TCP) |
+--------------------------+
|  Internet Protocol (IP)  |
+--------------------------+
|    Network Technology    |
+--------------------------+

Network Technology,各种硬件设备,比如:Wifi(无线网络)、Ethernet(有线网络)、Modem(猫) Internet Protocol,IPv4、IPv6,找到对应的机器 TCP/UDP,保证packet的正确性(reliable) HTTP/FTP,具体应用

URL(Uniform Resource Identifier)

http://guest:secret@www.ietf.org:80/html.charters/wg-dir.html?sess=1#Applications_Area

protocl:    http
username:   guest
password:   secret
host:       www.ietf.org
port:       80
path:       /html.charters
file:       wg-dir.html
query:      sess=1
fragment:   Applications_Area

Basic Operations

GET POST PUT DELETE OPTIONS HEAD TRACE CONNECT

HTTP/1.0,不支持Host。

$ telnet www.openbsd.cafe 80
GET /ping.html HTTP/1.0
Connection: close               <-- request

HTTP/1.0 200 OK                 <-- response
Connection: close
Content-Length: 36
Content-Type: text/html
Date: Thu, 11 Jun 2026 07:07:48 GMT
Last-Modified: Thu, 11 Jun 2026 06:41:39 GMT
Server: OpenBSD httpd

<html>
<body>
Pong!
</body>
</html>

HTTP/1.1,支持Host(Virtual Hosts)。 同一台物理机器,hosting了两个公司的website。不同hostname,指向同一个ip address,但希望返回不同的数据。通过Host头,来区分。 http://www.company1.com/news.html http://www.company2.com/news.html

$ telnet www.openbsd.cafe 80
Trying 185.52.176.93...
Connected to www.openbsd.cafe.
Escape character is '^]'.

GET /ping.html HTTP/1.1
Host: www.openbsd.cafe
Connection: close

HTTP/1.1 200 OK
Connection: close
Content-Length: 36
Content-Type: text/html
Date: Thu, 11 Jun 2026 06:46:52 GMT
Last-Modified: Thu, 11 Jun 2026 06:41:39 GMT
Server: OpenBSD httpd

<html>
<body>
Pong!
</body>
</html>

Connection closed by foreign host.

Host 访问哪个host "Connection: close" 提示httpd在发送数据结束时,直接close socket。

$ telnet www.openbsd.cafe 80
Trying 185.52.176.93...
Connected to www.openbsd.cafe.
Escape character is '^]'.

HEAD /ping.html HTTP/1.1
Host: www.openbsd.cafe
Connection: close

HTTP/1.1 200 OK
Connection: close
Content-Length: 36
Content-Type: text/html
Date: Thu, 11 Jun 2026 06:50:44 GMT
Last-Modified: Thu, 11 Jun 2026 06:41:39 GMT
Server: OpenBSD httpd

Connection closed by foreign host.

HEAD,确认uri指向的资源是否存在。httpd只返回Headers,不返回具体内容。

关于Redirection:lets a server redirect a client to another uri for an object. a way to support a single site to use multiple servers.

HttpProxy Gateway, 各种请求的入口 Tunnel,VPN Cache Server, vanish