HTTP1.1学习笔记 -- RFC2616

本人跟web无缘,从来没有想去学http,现在看来,学学也是有益无害,总会要用着点滴。

RFC见这里: https://www.ietf.org/rfc/rfc2616.txt 

0. URI格式 

  http://host[:port][abs_path] 

1. Message Type  (Request/Response)

  两种类型格式统一,都包括三部分,外加一个空行,格式如下:
    start-line  (request-line/status-line)
    *(message-header CRLF)
    [ message-body ]

 2.Start-Line:

  (request)  Request-Line   = Method  Request-URI HTTP-Version
    GET 请求获取 Request-URI 所标识的资源
    POST 在 Request-URI 所标识的资源后附加新的数据(非幂等)
    HEAD 请求获取由 Request-URI 所标识的资源的响应消息报头
    PUT 请求服务器存储一个资源,并用 Request-URI 作为其标识 (幂等)
    DELETE 请求服务器删除 Request-URI 所标识的资源
    TRACE 请求服务器回送收到的请求信息,主要用于测试或诊断
    CONNECT 保留将来使用
    OPTIONS 请求查询服务器的性能,或者查询与资源相关的选项和需求,
          用来获取更多服务器端的信息,是一个不应该对服务器数据造成影响的方法。
    
    注:如果一个方法重复执行多次,产生的效果是一样的,那就是幂等(idempotent)的。
      请求数据不在GET方法中使用,而是在POST方法中使用。
      POST方法适用于需要客户填写表单的场合。
      与请求数据相关的最常使用的请求头是Content-Type和Content-Length。
 (response) Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF

  状态代码由三位数字组成,第一个数字定义了响应的类别,且有五种可能取值。
  1xx:指示信息--表示请求已接收,继续处理。
  2xx:成功--表示请求已被成功接收、理解、接受。
  3xx:重定向--要完成请求必须进行更进一步的操作。
  4xx:客户端错误--请求有语法错误或请求无法实现。
  5xx:服务器端错误--服务器未能实现合法的请求。

  常见状态代码、状态描述的说明如下。

  200 OK:客户端请求成功。
  400 Bad Request:客户端请求有语法错误,不能被服务器所理解。
  401 Unauthorized:请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用。
  403 Forbidden:服务器收到请求,但是拒绝提供服务。
  404 Not Found:请求资源不存在,举个例子:输入了错误的URL。
  500 Internal Server Error:服务器发生不可预期的错误。
  503 Server Unavailable:服务器当前不能处理客户端的请求,一段时间后可能恢复正常,

  举个例子:HTTP/1.1 200 OK(CRLF)。

  Status-Code =
  "100" ; Section 10.1.1: Continue
  | "101" ; Section 10.1.2: Switching Protocols
  | "200" ; Section 10.2.1: OK
  | "201" ; Section 10.2.2: Created
  | "202" ; Section 10.2.3: Accepted
  | "203" ; Section 10.2.4: Non-Authoritative Information
  | "204" ; Section 10.2.5: No Content
  | "205" ; Section 10.2.6: Reset Content
  | "206" ; Section 10.2.7: Partial Content
  | "300" ; Section 10.3.1: Multiple Choices
  | "301" ; Section 10.3.2: Moved Permanently
  | "302" ; Section 10.3.3: Found
  | "303" ; Section 10.3.4: See Other
  | "304" ; Section 10.3.5: Not Modified
  | "305" ; Section 10.3.6: Use Proxy
  | "307" ; Section 10.3.8: Temporary Redirect
  | "400" ; Section 10.4.1: Bad Request
  | "401" ; Section 10.4.2: Unauthorized
  | "402" ; Section 10.4.3: Payment Required
  | "403" ; Section 10.4.4: Forbidden
  | "404" ; Section 10.4.5: Not Found
  | "405" ; Section 10.4.6: Method Not Allowed
  | "406" ; Section 10.4.7: Not Acceptable
  | "407" ; Section 10.4.8: Proxy Authentication Required
  | "408" ; Section 10.4.9: Request Time-out
  | "409" ; Section 10.4.10: Conflict
  | "410" ; Section 10.4.11: Gone
  | "411" ; Section 10.4.12: Length Required
  | "412" ; Section 10.4.13: Precondition Failed
  | "413" ; Section 10.4.14: Request Entity Too Large
  | "414" ; Section 10.4.15: Request-URI Too Large
  | "415" ; Section 10.4.16: Unsupported Media Type
  | "416" ; Section 10.4.17: Requested range not satisfiable
  | "417" ; Section 10.4.18: Expectation Failed
  | "500" ; Section 10.5.1: Internal Server Error
  | "501" ; Section 10.5.2: Not Implemented
  | "502" ; Section 10.5.3: Bad Gateway
  | "503" ; Section 10.5.4: Service Unavailable
  | "504" ; Section 10.5.5: Gateway Time-out
  | "505" ; Section 10.5.6: HTTP Version not supported

3. Message-Header = field-name ":" [ field-value ] 

 general-header = Cache-Control            ; Section 14.9
| Connection ; Section 14.10
| Date ; Section 14.18
| Pragma ; Section 14.32
| Trailer ; Section 14.40
| Transfer-Encoding ; Section 14.41
| Upgrade ; Section 14.42
| Via ; Section 14.45
| Warning ; Section 14.46

request-header = Accept ; Section 14.1
| Accept-Charset ; Section 14.2
| Accept-Encoding ; Section 14.3
| Accept-Language ; Section 14.4
| Authorization ; Section 14.8
| Expect ; Section 14.20
| From ; Section 14.22
| Host ; Section 14.23
| If-Match ; Section 14.24
| If-Modified-Since ; Section 14.25
| If-None-Match ; Section 14.26
| If-Range ; Section 14.27
| If-Unmodified-Since ; Section 14.28
| Max-Forwards ; Section 14.31
| Proxy-Authorization ; Section 14.34
| Range ; Section 14.35
| Referer ; Section 14.36
| TE ; Section 14.39
| User-Agent ; Section 14.43

response-header = Accept-Ranges ; Section 14.5
| Age ; Section 14.6
| ETag ; Section 14.19
| Location ; Section 14.30
| Proxy-Authenticate ; Section 14.33
| Retry-After ; Section 14.37
| Server ; Section 14.38
| Vary ; Section 14.44
| WWW-Authenticate ; Section 14.47

entity-header = Allow ; Section 14.7
| Content-Encoding ; Section 14.11
| Content-Language ; Section 14.12
| Content-Length ; Section 14.13
| Content-Location ; Section 14.14
| Content-MD5 ; Section 14.15
| Content-Range ; Section 14.16
| Content-Type ; Section 14.17
| Expires ; Section 14.21
| Last-Modified ; Section 14.29
| extension-header

 

4.Message-Body (text, or encoded text)

  entity-body = *OCTET
  entity-body := Content-Encoding( Content-Type( data ) )
  Example: parameter=value&also=another

5. Example

1) Get
地址中”?”之后的部分就是通过GET发送的请求数据,我们可以在地址栏中清楚的看到,各个数据之间用”&”符号隔开。显然,这种方式不适合传送私密数据。另外,由于不同的浏览器对地址的字符限制也有所不同,一般最多只能识别1024个字符,所以如果需要传送大量数据的时候,也不适合使用GET方式。
GET /search?hl=zh-CN&source=hp&q=domety&aq=f&oq= HTTP/1.1  
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, 
application/msword, application/x-silverlight, application/x-shockwave-flash, */*  
Referer: <href="http://www.google.cn/">http://www.google.cn/</a>  
Accept-Language: zh-cn  
Accept-Encoding: gzip, deflate  
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; TheWorld)  
Host: <href="http://www.google.cn">www.google.cn</a>  
Connection: Keep-Alive  
Cookie: PREF=ID=80a06da87be9ae3c:U=f7167333e2c3b714:NW=1:TM=1261551909:LM=1261551917:S=ybYcq2wpfefs4V9g; 
NID=31=ojj8d-IygaEtSxLgaJmqSjVhCspkviJrB6omjamNrSm8lZhKy_yMfO2M4QMRKcH1g0iQv9u-2hfBW7bUFwVh7pGaRUb0RnHcJU37y-
FxlRugatx63JLv7CWMD6UB_O_r
 2) Post
对于上面提到的不适合使用GET方式的情况,可以考虑使用POST方式,
因为使用POST方法可以允许客户端给服务器提供信息较多。
POST方法将请求参数封装在HTTP请求数据中,以名称/值的形式出现,可以传输大量数据,
这样POST方式对传送的数据大小没有限制,而且也不会显示在URL中。
还以上面的搜索domety为例,如果使用POST方式的话,格式如下:
POST /search HTTP/1.1  
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, 
application/msword, application/x-silverlight, application/x-shockwave-flash, */*  
Referer: <href="http://www.google.cn/">http://www.google.cn/</a>  
Accept-Language: zh-cn  
Accept-Encoding: gzip, deflate  
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; TheWorld)  
Host: <href="http://www.google.cn">www.google.cn</a>  
Connection: Keep-Alive  
Cookie: PREF=ID=80a06da87be9ae3c:U=f7167333e2c3b714:NW=1:TM=1261551909:LM=1261551917:S=ybYcq2wpfefs4V9g; 
NID=31=ojj8d-IygaEtSxLgaJmqSjVhCspkviJrB6omjamNrSm8lZhKy_yMfO2M4QMRKcH1g0iQv9u-2hfBW7bUFwVh7pGaRUb0RnHcJU37y-
FxlRugatx63JLv7CWMD6UB_O_r  

hl=zh-CN&source=hp&q=domety
可以看到,POST方式请求行中不包含数据字符串,这些数据保存在”请求内容”部分,各数据之间也是使用”&”符号隔开。
POST方式大多用于页面的表单中。因为POST也能完成GET的功能,因此多数人在设计表单的时候一律都使用POST方式,
其实这是一个误区。GET方式也有自己的特点和优势,我们应该根据不同的情况来选择是使用GET还是使用POST。
 
  3) 下面给出一个HTTP响应报文例子:

HTTP/1.1 200 OK

Date: Sat, 31 Dec 2005 23:59:59 GMT
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 122

<html>
<head>
<title>Wrox Homepage</title>
</head>
<body>
<!-- body goes here -->
</body>
</html>

  

HTTP2.0的改进之处:

HTTP2.0就是在应用层(HTTP/2)和传输层(TCP or UDP)之间增加一个二进制分帧层。

在二进制分帧层中, HTTP/2 会将所有传输的信息分割为更小的消息和帧(frame),并对它们采用二进制格式的编码 ,
其中 HTTP1.x 的首部信息会被封装到 HEADER frame,而相应的 Request Body 则封装到 DATA frame 里面。

最关键的一个字段是Stream Identifier

posted @ 2016-05-23 16:24  AndyHu518  阅读(1055)  评论(0编辑  收藏  举报