動機
記錄用
http 1.0
一個http req對到一個tcp socket。 以每個檔案為單位,去開http request,所有網頁有關的檔案都會各自開一個http。 等拿到一個檔案再開下一個http request。
header
- 內容 協商,類型,編碼- 名詞
- request- Accept- 資料的類型,要看MIME- 像 application/json,*/*
 
- 像 
- 權重: application/xml;q=0.9,application/json, text/*;q=0.8- application/json最高(1),- application/xml次高(0.9)
 
 
- 資料的類型,要看MIME
- Accept-Charset- 就是charset,像utf8
 
- Accept-Encoding- 怎麼壓縮資料
 
- Accept-Language- 就是語言
 
 
- Accept
- response- Content-Type- 對到Accept & Accept-Charset- application/x-www-form-urlencoded; charset=utf-8- 這是form表單
- payload會像name=tom&password=9487
 
 
 
- 對到Accept & Accept-Charset
- Content-Encoding- 對到Accept-Encoding
 
- Content-Language- 對到Accept-Language
 
- Content-Location- 資料的實際位置
 
 
- Content-Type
 
- cache- Cache-Control- no-store: 每次都重拿
- no-cache: 每次都發req驗證
- max-age: max-age=10,十秒後才會發req
- stale-while-revalidate: 先拿原本的cache,同時在背景向伺服器發送請求
- public & private:
 
 
- Last-Modified & Etag- 都是cache的stamp- 一個是 時間
- 一個是 hash(md5)
 
 
- 都是cache的stamp
- If-Modified-Since & If-None-Match- 就是驗證時把stamp丟給server看的
 
 
- Cache-Control
- Referer & Refresh- request- Referer: 上一個網址
 
- response- Refresh: 重新導向跳轉 (5秒後跳到w3)- Refresh: 5; url=http://www.w3.org
 
 
- Refresh: 重新導向跳轉 (5秒後跳到w3)
 
- request
- 同源- request- Origin: 我的domain是
- Access-Control-Request-Origin: 向server發問這個origin會不會過同源- 後面的Origin還可以換成 Methods, Headers
 
 
- response- Access-Control-Allow-Origin: 能吃的domian- 後面的Origin還可以換成 Methods, Headers
 
 
- Access-Control-Allow-Origin: 能吃的domian
 
- request
http 1.1
http1會開很多tcp socket,tcp socket也是要$,所以有了keep-alive
keep-alive
在header放,Connection: Keep-Alive,表示這個socket傳完不用關掉
只要server也有一樣的response,就是使用keep-alive
但只限制在該網頁,所以現在http1.1是以每個網頁為單位,只要不同tab就是新的tcp socket
pipeline
現在有keep-alive,但還是只能等拿到一個檔案再開下一個http request。 所以有pipeline,可以先給多個http request,但是
- server還是一個一個回且照順序 (head-of-line blocking)
- 只能用於GET, HEAD之類,Idempotent的verb
websocket
有了keep-alive,但server沒辦法主動推資料給client
如果client要更新資料只能自己做poll,雖說有數個技術,但就只是差在poll後會保留多久這個用來查詢更新的socket
所以有了websocket,與http同樣用80,443 port,但是包自己的pkt header,就可以把資料切小(frame),讓接收端重組資料(少了flow-control、congestion-control的tcp)
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len |    Extended payload length    |
|I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
|N|V|V|V|       |S|             |   (if payload len==126/127)   |
| |1|2|3|       |K|             |                               |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
|     Extended payload length continued, if payload len == 127  |
+ - - - - - - - - - - - - - - - +-------------------------------+
|                               |Masking-key, if MASK set to 1  |
+-------------------------------+-------------------------------+
| Masking-key (continued)       |          Payload Data         |
+-------------------------------- - - - - - - - - - - - - - - - +
:                     Payload Data continued ...                :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                     Payload Data continued ...                |
使用方法是
- 先發http req,但裡面放- Upgrade: websocket
- Connection: Upgrade
 
- 如果ok,server的res也會有一樣的東西,之後就是websocket了
Server-sent events(sse)
但是這是新protocol,有沒有方法在http上做改進? Server-sent events就是在傳回資料時,標這是streaming,讓client不要關連線。
這樣能完成server push,但是只有單向!!
用法是
- 放下面的header
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
- 開始丟資料 (資料有另外的格式,但這裡跳過)
http2
一次大改版,可以想成在tcp與http1.1之間引入一層轉換層,來達成下面4點
- 都用binary- http原本都是text
- websocket可以是text也可以是binary
 
- 會壓縮header
- 所有東西都分割成frame傳輸,- multiplexing: 不用等前面的檔案傳好!! 直接傳各自組合
- Prioritization: 可以列優先序
 
- server push- server可以把資料先推到client的cache
- websocket與sse都是與應用程式溝通,這裡是cache!!
 

websocket是重新設計自己的protocol,使用http的port http2是使用http的header與語意,但是改變資料的表現方式與處理方式
websocket是新protocol,http2是改進的http
http2與websocket都可以在同一條路上同時傳輸(full-deplex)!! 但http2的方向主要是client->server之後server->server,server push沒有真正的雙向,websocket才是真正的雙向
Ref
HTTP persistent connection What is the difference between HTTP/1.1 pipelining and HTTP/2 multiplexing? HTTP 內容協商 HTTP 內容類型 HTTP Cache 快取 WebSocket协议入门介绍 Server-Sent Events 教程 HTTP/1.0/1.1/2.0的区别以及http和https的区别
