0%

HTTP 协议

超文本传输协议(HTTP协议)

特点:

  • 简单
  • 易解析
  • 可读性好

RFC 2616 Link (HTTP 1.1)

HTTP 版本

  • HTTP/1.1 : 1999年 RFC 2616,还有RFC7230。目前少于三分之一的网站使用(包括其和其早期版本)。
    • 1.1相比于1.0:使用一个TCP连接可以传多个HTTP request/response(引入keep-alive-mechanism,HTTP长连接), client不需要在发送第一个请求后重新协商 TCP 3-Way-Handshake 连接。
    • 1.1相比于1.0:带宽优化:使用了分块传输编码(Chunked transfer encoding)的流式传输机制,即把内容分块(Chunk)进行流式传输
  • HTTP/2.0 : 2015年,超过一半的网站使用,以谷歌的SPDY为基础开始指定的新版本HTTP协议,RFC 7540.
  • HTTP/3.0 : HTTP over QUIC,18%网站使用,使用UDP作为底层传输协议

HTTP Request

HTTP Request 是client端向server端做的一个请求。

包括:

  • 一个请求行: GET /images/logo.png HTTP/1.1
  • 请求头:Accept-Language: en Link
    • Accept-Language: en
    • Accept-Ranges: type:指定bytes可以作为一个单位去定义一个range,如果是none则表示不支持partial request。
  • 空行
  • HTTP 消息正文( message body) : 可选

请求行和其他头字段必须以 <CR><LF>结尾,空行只包括 <CR><LF>且无空格

HTTP 方法

  • GET (1.0)
  • HEAD(1.0) :除了没有响应体外(Response body)与 GET 请求相同,可以用于检索
  • POST (1.0) :请求服务器接收包括的entity
  • OPTIONS(1.1) : 返回某个URL支持的HTTP 方法
  • PUT (1.1): 请求服务器将entity的保存在URI下(如果存在则修改,不存在则新建)
  • DELETE(1.1) :删除
  • TRACE (1.1) : 回复收到的请求,client知道server做了什么改动,由于XST攻击建议关闭
  • CONNECT (1.1): 用于proxy较多
HTTP method RFC Request has Body Response has Body Safe Idempotent Cacheable
GET RFC 7231 No Yes Yes Yes Yes
HEAD RFC 7231 Optional No Yes Yes Yes
POST RFC 7231 Yes Yes No No Yes
PUT RFC 7231 Yes Yes No Yes No
DELETE RFC 7231 Optional Yes No Yes No
CONNECT RFC 7231 Optional Yes No No No
OPTIONS RFC 7231 Optional Yes Yes Yes No
TRACE RFC 7231 No Yes Yes Yes No
PATCH RFC 5789 Yes Yes No No No

表格来自[1]

HTTP Response

  • 状态行 :HTTP/1.1 200 OK
  • 响应头: Content-Type: text/html
  • 空行
  • 消息体

状态码(status code)

  • 信息 1XX
  • 成功 2XX 200 OK
  • 重定向Redirection 3XX
  • Client Error 4XX
  • Server Error 5XX

几个常见的状态码:

  • 206 : Partial Content
  • 200: OK
  • 404: Not Found
  • 500: Internal Server error

例子

1
2
GET / HTTP/1.1
Host: www.example.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 155
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
ETag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: bytes
Connection: close

<html>
<head>
<title>An Example Page</title>
</head>
<body>
<p>Hello World, this is a very simple HTML document.</p>
</body>
</html>

Reference

[1] https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

[2] https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

[3] https://www.w3.org/Protocols/rfc2616/rfc2616.html