A Za, A Za, Fighting...

坚信:勤能补拙

2011知识点 - HTTP协议

HTTP protocol

© Min的技术分享 – 54min.com (RSS订阅) | 原文链接:http://54min.com/post/the-http-protocol.html

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方法。

request的HTTP header fields参考。常见的:

    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 OKHTTP/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

response的HTTP header fields参考。常见的:

    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实例】

如下介绍HEADGETPOST共3种方法的HTTP request/response实例。

HEAD:

HEADGET类似但是只获取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-LengthContent-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

posted on 2011-09-04 22:19 simplyzhao 阅读(374) 评论(0)  编辑 收藏 引用 所属分类: R_找工复习2011


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2011年6月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜