HTTP protocol
HTTP是應用層協議(傳輸層采用TCP因此是面向連接的),是WWW所使用的協議。當前使用的是HTTP/1.1(HTTP/1.0對每個request/response使用獨立的connection,每次請求都需要重新建立到server的HTTP連接;HTTP/1.1引入了HTTP persistent connection,重用一個connection多次(通過在client和server的HTTP header中添加Connection: Keep-Alive
),參考)。
HTTP-base的C/S通信: client(常見的為瀏覽器)發出request, server(常見的為http server)返回針對該request的response。(通常采用80端口)。
【request】
一個request通常包含如下內容:
request line
header fields (key-value pairs)
empty line
optional message body
- 每行必須使用
\r\n
結尾; - request line的部分包含請求文件的路徑部分,例如
GET /index.html HTTP/1.1
; - request header fields部分只有
Host
是不可省略的,其他都是可選的; - 空行部分不能包含任何空格字符;
- message body部分通常用于放置POST方法的數據,是可選的
request支持的方法由如下幾種(request line的部分指定):
HEAD //和GET方法類似,但是只獲取response的HTTP header
GET //獲取指定路徑的內容(reponse包含header和message body)
POST //將數據POST到server
PUT //uploads a representation of the specified resource
DELETE //delete the specified resource
TRACE //返回server得到的request,以了解在中間server進行了哪些修改
OPTIONS
CONNECT
PATCH
一般HTTP Server需要實現GET,HEAD和OPTIONS方法。
Host: www.google.com //server的domain name,不能省略
Accpet: text/plain //接受的Content-Type
Accept-Charset: utf-8 //接受的character set
Accept-Language: en-US //接受的語言
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Connection: Close //客戶端在獲取response之后會斷開連接,如設置為Keep-Alive,則該連接可以多次使用
Cookie: username=my-username; password=my-password; //指定client端攜帶的cookies
Content-Length: 348 //數據包內容的長度(之后message body部分)
Content-Type: application/x-www-form-urlencoded //數據報內容的類型
【response】
一個response通常包含如下內容:
response line
header fields (key-value pairs)
empty line
message body
- 每行必須使用
\r\n
結尾; - reponse line的部分包含status code和status reason phrase,如
HTTP/1.1 200 OK
,HTTP/1.1 404 Not Found
; - 空行部分不能包含任何空格字符;
- message body部分即獲取的URI的資源的內容,如HTML頁面代碼
response的status code,比較常見到的:
200 OK
302 Found //用于redirection
400 Bad Request //the request has bad syntax
403 Forbidden //the server is refusing to respond to it
404 Not Found //could not be found
502 Bad Gateway //the server was acting as proxy and received an invalid response from the upstream server(一般為app server),如nginx + PHP-FPM
更多參考:http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Server: nginx/0.7.67 //http server信息
Connection: close //或Keep-Alive
Content-Length: 35 //response body部分內容長度
Content-Type: text/html;charset=gb2312 //reponse body部分的類型和編碼
Cache-Control: max-age=3600 //指定client可以cache
Content-Encoding: gzip //指定response內容是壓縮的
Set-Cookie: userId=2; username=min; //在client端種下cookies
更多的Http headers fields: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
【HTTP request和response實例】
如下介紹HEAD
, GET
, POST
共3種方法的HTTP request/response實例。
HEAD:
HEAD
和GET
類似但是只獲取response的response line和header fields
部分,不包括message body
。
request:
HEAD /request.php?q=keywords&status=2 HTTP/1.1
HOST: example.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
response
HTTP/1.1 200 OK
Server nginx/0.7.67
Date Mon, 18 Jul 2010 02:51:09 GMT
Content-Type text/html
Transfer-Encoding chunked
Connection keep-alive
X-Powered-By PHP/5.3.6-12
Content-Encoding gzip
GET:
request:
GET /index.php?q=keywords HTTP/1.1
Host test.54min.com
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Connection keep-alive
response:
HTTP/1.1 200 OK
Server nginx/0.7.67
Date Mon, 18 Jul 2010 02:54:02 GMT
Content-Type text/html
Last-Modified Fri, 24 Jun 2010 07:39:28 GMT
Transfer-Encoding chunked
Connection keep-alive
Content-Encoding gzip
<html>
this is c
sfsdfsdfs
</html>
POST:
POST
方法的request需要指定Content-Length
和Content-Type
request:
POST /login.php HTTP/1.1
Host: www.example.com
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded
userid=joe&password=guessme
response:
HTTP/1.1 200 OK
Server nginx/0.7.67
Date Mon, 18 Jul 2010 02:54:02 GMT
Content-Type text/html
Last-Modified Fri, 24 Jun 2010 07:39:28 GMT
Transfer-Encoding chunked
Connection keep-alive
Content-Encoding gzip
<html>
this is c
sfsdfsdfs
</html>
參考:http://developers.sun.com/mobility/midp/ttips/HTTPPost/
【HTTP是statless protocol】
HTTP是無狀態的協議,server端不保留client端的任何狀態信息,因此要實現在多次連接下能夠獲取之前的狀態,可以通過cookies(client端)或session(server端)的方法。
【HTTP協議的應用】
綜上,HTTP協議即是WWW互聯網基于TCP socket定義的一組通訊規范,使用該協議即可實現http client和http server的通信。它的優點就是簡單,最常用,大部分語言都支持client端實現。
實際中借助libevent
庫可輕松實現高并發高性能的HTTP server,將自己的應用封裝一個HTTP接口,方便各種客戶端進行數據通信。
【推薦的HTTP(header和raw message)分析工具】
參考:http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol