-

HTTP - Messages

HTTP is based on the client-server architecture model and a stateless request/response protocol that operates by exchanging messages across a reliable TCP/IP connection.

An HTTP "client" is a program (Web browser or any other client) that establishes a connection to a server for the purpose of sending one or more HTTP request messages. An HTTP "server" is a program ( generally a web server like Apache Web Server or Internet Information Services IIS, etc. ) that accepts connections in order to serve HTTP requests by sending HTTP response messages.

HTTP使用统一资源标识符(URI)来标识给定的资源并建立连接。一旦建立连接,HTTP消息的传递格式与Internet邮件[RFC5322]和多用途Internet邮件扩展(MIME)[RFC2045]所使用的格式相似。这些消息包括请求从客户端到服务器和响应到客户端服务器从将具有以下格式:

 HTTP-message   = <Request> | <Response> ; HTTP/1.1 messages

HTTP请求和HTTP响应使用RFC 822的通用消息格式传输所需的数据。此通用消息格式由以下四个项目组成。

  • A Start-line
  • Zero or more header fields followed by CRLF
  • An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields
  • Optionally a message-body

在以下部分中,我们将介绍HTTP消息中使用的每个实体。

消息开始行

起始行将具有以下通用语法:

start-line = Request-Line | Status-Line

在探讨HTTP请求和HTTP响应消息时,我们将探讨请求行和状态行。现在来看看请求和响应的起始行的例子:

GET /hello.htm HTTP/1.1     (This is Request-Line sent by the client)

HTTP/1.1 200 OK             (This is Status-Line sent by the server)

标题字段

HTTP头域提供有关请求或响应的所需信息,或关于在消息正文中发送的对象。HTTP消息标头有四种类型:

上面提到的所有报头遵循相同的通用格式和每个所述报头字段的包含名称后跟冒号的()和字段值,如下所示:

message-header = field-name ":" [ field-value ]

以下是各种标题字段的示例:

User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Accept-Language: en, mi
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Accept-Ranges: bytes
Content-Length: 51
Vary: Accept-Encoding
Content-Type: text/plain

消息体

消息正文部分对于HTTP消息是可选的,但是如果它是可用的,则它用于携带与请求或响应相关联的实体。如果实体主体相关联,则通常Content-TypeContent-Length头部行指定与之关联的主体的性质。

消息体是携带实际的HTTP请求数据(包括表单数据和上传的信息)以及来自服务器的HTTP响应数据(包括文件,图像等)的消息体。下面显示的是消息体的简单内容:

<html>
   <body>
   
      <h1>Hello, World!</h1>
   
   </body>
</html>

接下来的两章将利用上述概念来准备HTTP请求和HTTP响应。