HTTP 協議報文解析

說明

本篇主要是爲了記錄HTTP中報文的格式,以便針對報文進行解析。首先會介紹基礎的HTTP報文,之後會介紹
文件上傳時的數據報文格式.

HTTP基礎報文格式

按照HTTP報文類型進行介紹,HTTP報文類型分爲請求報文(客戶端主動請求報文)和響應報文(服務端響應客戶端的回傳報文)。其基本的格式爲:

GET / HTTP/1.1
Host: www.enjoytoday.cn
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://www.enjoytoday.cn/posts/326
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: bdshare_firstime=1466032270994; UM_distinctid=15c4ef2ac4e2e4-0d13269271b947-1b2a120b-1fa400-15c4ef2ac4f7b5; un=aGZjYWk=; comment_author=aGZjYWk=; [email protected]; comment_author_url=http://www.enjoytoday.cn; c_id=dUhIaTlndmc4MVVYbjRQTGxMRTotMTpFODg3QjgzQjg1NjgxQjQxRUYxNjg2QzJFRkMyQjI2QQ==; JSESSIONID=ADBC8C3DADF6C815D778450C193C6637.ajp13_worker; Hm_lvt_ce55bfda158556585a8b7b246346c8ba=1498560244,1498739070,1498833193,1498917432; Hm_lpvt_ce55bfda158556585a8b7b246346c8ba=1498917597; CNZZDATA1262047894=1598545996-1495973145-%7C1498917578

username=hfcai&sex=man

1) 請求報文

請求報文數據格式由三個部分組成:

  • 請求方法、URI、HTTP協議版本

    該部分位於數據首行,基本格式爲:

    GET /index.html HTTP/1.1

    該部分給出了請求類型和請求的資源位置(/index.html),其中HTTP中的請求類型包括:GET、POST、HEAD、PUT、DELETE.一般常用的爲POST和GET方式。

  • 請求頭部

    該部分緊跟着上一部分(方法、協議版本行),該部分主要是用於描述請求正文,其基本格式如下:

    Host: www.enjoytoday.cn
    Connection: keep-alive
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
    ......

    主要是用於說明請求源、連接類型、以及一些Cookie信息等。

  • 請求正文

    請求正文和請求頭部通過一個空行進行隔開,一般用於存放POST請求類型的請求正文,如上的請求體爲:

    username=hfcai&sex=man

2)響應報文

響應報文和請求報文格式類型,同樣也是分爲三個部分,響應頭部和響應體同樣也是通過一個空行進行隔開,如下爲一個簡單的示例:

HTTP/1.1 200 OK
  Date: Sat, 01 Jul 2017 14:51:26 GMT
  Server: Apache/2.4.7 (Ubuntu)
  Set-Cookie: JSESSIONID=84C993F5E433C4DE9BFBA57150FFC065.ajp13_worker;path=/;HttpOnly
  Content-Language: zh-CN
  Vary: Accept-Encoding
  Content-Encoding: gzip
  Content-Length: 7333
  Keep-Alive: timeout=5, max=100
  Connection: Keep-Alive
  Content-Type: text/html;charset=UTF-8

  <html>
  <head>
  <title>title of html.</html>
  </head>
  <body>
  <h1>Hello world!</h1>
  </body>
  </html>
  • 狀態碼部分

    該部分主要給出響應HTTP協議版本號、響應返回狀態碼、響應描述,同樣是單行顯示.格式爲:

    HTTP/1.1 200 OK

    響應返回一般由一個三位數組成,分別以整數1~5開頭組成。各個類型響應對應含義:

    • 1XX:信息提示,臨時的響應
    • 2XX:請求成功,服務端已經接受到請求信息
    • 3XX:重定位
    • 4XX:客戶端異常
    • 5XX:服務端異常
      常見的響應有:200(響應成功),400(請求異常,一般爲參數異常),404(請求資源不存在),405(請求方式不支持),500(服務器內部異常)
  • 響應頭部

    響應頭部主要是返回一些服務器的基本信息,以及一些Cookie值等。如上的響應頭爲:

    Date: Sat, 01 Jul 2017 14:51:26 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Set-Cookie: JSESSIONID=84C993F5E433C4DE9BFBA57150FFC065.ajp13_worker;path=/;HttpOnly
    Content-Language: zh-CN
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Content-Length: 7333
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: text/html;charset=UTF-8
  • 響應體

    該部分爲請求需要得到的具體數據,可以爲任何類型數據,一般網頁瀏覽返回的爲html文件內容.如上,內容不再列出。同樣,和響應頭部通過一個空行進行隔開。

文件上傳報文格式

如下,介紹通過瀏覽器上傳文件時它的報文格式和一般的報文的異同。文件上傳主要在於文件上傳時的格式差異,如下給出一個示例的格式:

POST /servlet/UploadServlet HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 349
Cache-Control: max-age=0
Origin: http://localhost:8080
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy4ceMstxB6tXIWBT
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost:8080/hello.html
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie: comment_author="aGZjYWk="; comment_author_email="[email protected]"; comment_author_url="http://localhost:8080"; UM_distinctid=15c4ef587620-0ad25ac342d545-1b2a120b-1fa400-15c4ef58763717; CNZZDATA1262047894=628334530-1495973332-%7C1495973678; Hm_lvt_ce55bfda158556585a8b7b246346c8ba=1495973333

------WebKitFormBoundaryy4ceMstxB6tXIWBT
Content-Disposition: form-data; name="filedata"; filename="test.txt"
Content-Type: text/plain

This is the upload file
this is a test file
this is a text file

------WebKitFormBoundaryy4ceMstxB6tXIWBT
Content-Disposition: form-data; name="submit"

upload
------WebKitFormBoundaryy4ceMstxB6tXIWBT--

如上,我上傳了一個測試文件(test.txt),其文件內容爲:

This is the upload file
this is a test file
this is a text file

其基本格式還是遵循HTTP的請求格式,但對於正文部分添加了一些對於上傳文件的說明,格式如下:

POST /servlet/UploadServlet HTTP/1.1    #請求uri、方法
Host: localhost:8080Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy4ceMstxB6tXIWBT    #邊界值
......
Accept-Language: zh-CN,zh;q=0.8

------WebKitFormBoundaryy4ceMstxB6tXIWBT                   #邊界
Content-Disposition: form-data;name="filedata";filename="test.txt" #文件基本信息
Content-Type: text/plain                              #文件基本信息           

This is the upload file                               #文件內容    
this is a test file
this is a text file

------WebKitFormBoundaryy4ceMstxB6tXIWBT       #提交按鈕屬性信息部分
Content-Disposition: form-data; name="submit"

upload                                        #提交按鈕value值                
------WebKitFormBoundaryy4ceMstxB6tXIWBT--

其他部分和一般的HTTP數據格式一樣(包括響應數據的格式)。

Enjoytoday,Enjoycoding

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章