TCP / IP summarizing learning (2)

Simple HTTP protocol

.HTTP a protocol for communication between the client and the server

  • Custom client and server: one end of a request to access text or images, etc. referred to as client resources, while providing an end referred to as a server-side resource response. When using the HTTP protocol for communication between two computers, over a communication line must end with a client, the server side is the other end.

Second, by exchanging request and response reached:

  • HTTP protocol that a request sent from the client, the server responds to the last request and returns. In other words, definitely start start establishing communications client, the server does not send a response until a request is received.
  • Examples of the communication request and response:

clipboard.png
1. Request packet content (meaning: /index.htm page requesting access to resources on an HTTP server):
GET /index.htm
HTTP / 1.1 Host: hackr.jp

clipboard.png.
(The GET request represents the beginning of the start line access server type, referred to as method (Method); /index.htm string indicates the object request access to the resource, also called a request URI (request-URI); HTTP / 1.1, HTTP version number i.e., to prompt the HTTP protocol used by the client function. request packet is a request method, the URI request, protocol version, an optional header field and content requesting entity configuration.)

2. content of the response telegram:

clipboard.png

clipboard.png

(Initial beginning of the line in the HTTP / 1.1 represents a corresponding HTTP Server version next processing result represents a 200 OK status code of the request (status code) and a reason phrase (reason-phrase). The next line shows the creation response date and time, is an attribute in the header field (header field). followed by a blank line separated, after the content the body resource entity (entity body). response packet consists essentially of a protocol version, status code (represented by request success or failure numeric code), to explain the reason phrase of the status code, an optional header field, and a response to the entity body configuration.)


Three .HTTP agreement is not saved state:

  • HTTP 是一种不保存状态,即无状态(stateless)协议。HTTP 协议自 身不对请求和响应之间的通信状态进行保存。

作用:为了更快地处理大量事务,确保协议的可伸缩性,而特意把 HTTP 协议设计成如此简单的。
局限:随着 Web 的不断发展,因无状态而导致业务处理变得棘手的情况增多了。比如,用户登录到一家购物网站,即使他跳转到该站的其他页面后,也需要能继续保持登录状态。
解决方案:HTTP/1.1 虽然是无状态协议,但为了实现期望的保持状态功能,于是引入了 Cookie 技术(后面文章会详谈)。有了 Cookie 再用 HTTP 协议通信,就可以管理状态了。


四.请求 URI 定位资源:

  • HTTP 协议使用 URI 定位互联网上的资源。正是因为 URI 的特定功能,在互联网上任意位置的资源都能访问到。

clipboard.png

  • 图:以 http://hackr .jp/index.htm 作为请求的例子

clipboard.png

  • 除此之外,如果不是访问特定资源而是对服务器本身发起请求,可以 用一个 * 来代替请求 URI。下面这个例子是查询 HTTP 服务器端支持 的 HTTP 方法种类。

clipboard.png


五.告知服务器意图的 HTTP 方法

1.GET :获取资源

  • GET 方法用来请求访问已被 URI 识别的资源。指定的资源经服务器端解析后返回响应内容。也就是说,如果请求的资源是文本,那就保持原样返回;如果是像 CGI(Common Gateway Interface,通用网关接口)那样的程序,则返回经过执行后的输出结果。
  • 使用 GET 方法的请求-响应的例子:

clipboard.png

2.POST:传输实体主体

  • 虽然用 GET 方法也可以传输实体的主体,但一般不用 GET 方法进行传输,而是用 POST 方法。虽说 POST 的功能与 GET 很相似,但 POST 的主要目的并不是获取响应的主体内容,而是告知其响应结果。

clipboard.png

  • 使用 POST 方法的请求·响应的例子

clipboard.png

3.PUT:传输文件

  • 就像 FTP 协议的文件上传一样,要求在请求报文的主体中包含文件内容,然后保存到请求 URI 指定的位置。但是,鉴于 HTTP/1.1 的 PUT 方法自身不带验证机制,任何人都可以 上传文件 , 存在安全性问题,因此一般的 Web 网站不使用该方法。若 配合 Web 应用程序的验证机制,或架构设计采用 REST(REpresentational State Transfer,表征状态转移)标准的同类 Web 网站,就可能会开放使用 PUT 方法。

clipboard.png

  • 使用 PUT 方法的请求·响应的例子:

clipboard.png
// 响应1的意思其实是请求执行成功了,但无数据返回

4.HEAD:获得报文首部

  • HEAD 方法和 GET 方法一样,只是不返回报文主体部分。用于确认 URI 的有效性及资源更新的日期时间等。

clipboard.png

  • 使用 HEAD 方法的请求·响应的例子:

    clipboard.png

5.DELETE:删除文件

  • DELETE 方法用来删除文件,是与 PUT 相反的方法。DELETE 方法按 请求 URI 删除指定的资源。 但是,HTTP/1.1 的 DELETE 方法本身和 PUT 方法一样不带验证机制,所以一般的 Web 网站也不使用 DELETE 方法。当配合 Web 应用 程序的验证机制,或遵守 REST 标准时还是有可能会开放使用的。

    clipboard.png

  • 使用 DELETE 方法的请求·响应的例子:

clipboard.png

6.OPTIONS:询问支持的方法

  • OPTIONS 方法用来查询针对请求 URI 指定的资源支持的方法。

    clipboard.png

  • 使用 OPTIONS 方法的请求·响应的例子:

    clipboard.png

7.TRACE:追踪路径

  • TRACE 方法是让 Web 服务器端将之前的请求通信环回给客户端的方法。发送请求时,在 Max-Forwards 首部字段中填入数值,每经过一个服务器端就将该数字减 1,当数值刚好减到 0 时,就停止继续传输,最后接收到请求的服务器端则返回状态码 200 OK 的响应。 客户端通过 TRACE 方法可以查询发送出去的请求是怎样被加工修改 / 篡改的。这是因为,请求想要连接到源目标服务器可能会通过代理 中转,TRACE 方法就是用来确认连接过程中发生的一系列操作。 但是,TRACE 方法本来就不怎么常用,再加上它容易引发 XST(Cross-Site Tracing,跨站追踪)攻击,通常就更不会用到了。

clipboard.png

  • 使用 TRACE 方法的请求·响应的例子:

clipboard.png
clipboard.png

8.CONNECT:要求用隧道协议连接代理

  • CONNECT 方法要求在与代理服务器通信时建立隧道,实现用隧道协议进行 TCP 通信。主要使用 SSL(Secure Sockets Layer,安全套接 层)和 TLS(Transport Layer Security,传输层安全)协议把通信内容加密后经网络隧道传输。
  • CONNECT 方法的格式如下所示

clipboard.png

  • 使用 CONNECT 方法的请求·响应的例子:

clipboard.png

总结:

clipboard.png


五.持久连接节省通信量

1.短连接:HTTP 协议的初始版本中,每进行一次 HTTP 通信就要断开一次 TCP 连接。

clipboard.png

2. Features a persistent connection is: as long as there is no clearly stated either end disconnected, leave the TCP connection status.
The benefits of persistent connections: to reduce duplication and build overhead caused by TCP connection is disconnected, reducing the load on the server side. In addition, to reduce the cost of that part of the time, the HTTP requests and responses can end earlier, this Web page display speed is correspondingly increased.

clipboard.png

3. Pipelining: persistent connection request pipelined so that most (PIPELINING) mode transmission becomes possible. After sending a request and a response is received from the front to wait before sending the next request. After pipelined technology appears, without waiting for a next response may send requests directly. The pipeline technology is faster than persistent connections. The more the number of requests, the time difference more obvious
clipboard.png

Six .Cookie techniques
1. the introduction of reason: HTTP protocol while retaining the features of a stateless protocol have to solve similar contradictions, so the introduction of Cookie technology.
2 works: Cookie techniques Cookie state information is written in the request and response packets to control the clients. Cookie will be based on the known header field information of a Set-Cookie in the response message sent by the end, notify the client save Cookie. When the next time the client sends a request to the server go down, clients automatically added Cookie value in the request message sent. When the server discovery Cookie is sent by the client, we will check whether it is sent to the client from which a connection request, the server then recorded on the comparison, the status information obtained before last.
3. Legend:
scenarios occur Cookie interaction:

clipboard.png

clipboard.png

HTTP request and response message content is as follows:
clipboard.png

Guess you like

Origin www.cnblogs.com/jlfw/p/11952948.html