Notes illustrates HTTP (b) - Simple HTTP protocol

This chapter HTTP 1.0 as an example to explain the basic structure of the HTTP protocol.

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.

The other end of the resource request to access the text or image to become a client, and providing an end becomes a server resource response.

HTTP protocol that a request sent from the client, the server responds to the last request and returns.

First, request and response packets composition

A request sent by the client as follows:

GET /index.htm HTTP/1.1
Host: hackr.jp

Wherein, the GET request represents the access server type, referred to as method (method). Followed by a string /index.htmresource object represents a request for access, also called the request URI. HTTP final version of HTTP / 1.1 represent used to prompt the HTTP version used by the client.

So head in the above paragraph requests using the GET method request index.htm hackr.jp resources on the server, the client is using HTTP version 1.1.

In conclusion: a request message is a request method, request URI, protocol version, optional request header fields and content entity formed .

requestcontent

FIG explains the composition of the request packet.

Receiving a request to the server, it returns the following:

HTTP/1.1 200 OK
Date: Tue, 10 Jul 2012 06:50:15 GMT
Content-Length: 362
Content-Type: text/html
<html>
......

Wherein, HTTP / 1.1 represents a corresponding version of HTTP server; later 200 OKprocessing result status code indicates a request (status code). The next line indicates the response time of creation, is the response header fields (header field) of a property. Blank line after the contents of the main resources represent an entity (entity body).

Summary: The response packet consists essentially of a protocol version, status code (indicating success or failure of the request numeric code), to explain the reason phrase of the status code, an optional header field, and a response to the entity constituting the main body .

responsecontent

The figure is a response message composition.

Two, HTTP is a stateless protocol

HTTP is a non-save state, i.e., no state (Stateless) protocol. HTTP communication state between the own right requests and responses stored. Every time a new request is established, there will be a corresponding response is generated, and before or after the request are not in any way.

HTTP is stateless characteristics, both benefits and disadvantages. Benefit is that the stateless HTTP features make not maintain client state, greatly simplifying the work agreement and server, ensuring scalability agreement. The downside is that as Web development, web applications become increasingly complex, stateless protocol need to maintain client state by other means (login information), such as means of Cookie technology, it is to maintain the status from the beginning of the introduction of HTTP 1.1.

Third, tell the server intention HTTP methods

HTTP 1.1 mainly contains the following methods can be used.

  • GET method is used to access the requested resource has been identified in the URI. After a specified resource server parses the returned response content.
  • POST method for transmitting entity body. Although the GET method can also be transmitted by the main entities, but generally do not GET method. Although the function of POST and GET method is similar, but the main purpose is not to obtain POST body content of the response .
  • PUT method is mainly used to transfer files like FTP file upload, it requires that contains the file contents in the body of the request message, and then save the URI specified location. Because of HTTP 1.1 PUT method with no authentication mechanism, anyone can upload files, it has a security risk, generally do not use the PUT method.
  • DELETE method is mainly used to delete files on the server, on the contrary, DELETE method to delete the file server specified by the URI and PUT. Also because of security risks, it is generally not used DELETE methods.
  • HEAD method is mainly used to confirm the validity of the date and time and resource URI updates, etc., will not return to the main content of the message.
  • OPTIONS method to query method supported by the resources of designated in the request URI.
  • TRACE method is used to track the path, when the transmission request is added in the header field of the request Max-Forwardsfield, a number, each through a server, the field value minus one, when reaching a server so that the field is 0, it will returns the status code 200 OK response. You can query request sent by the TRACE how the method is processing / tampering. TRACE method itself uses less scene, but there are hidden dangers track cross-site attacks, so use a little more scene.
  • CONNECT method of claim represented by tunneling proxy protocol connection. When the communication with the proxy server, to realize a TCP communication tunnel protocol mainly used SSL (Secure Sockets Layer, Secure Socket Layer) and TLS (Transport Layer Security, Transport Layer Security).

Note that the method names are case-sensitive, remember to use capital letters.

The following table shows some of the ways HTTP 1.0 and HTTP 1.1 support.

method Explanation Supported HTTP protocol version
GET Access to resources 1.0/1.1
POST Transmission entity body 1.0/1.1
PUT Transfer files 1.0/1.1
HEAD Get the message headers 1.0/1.1
DELETE Delete Files 1.0/1.1
OPTIONS Inquiry method Supported 1.1
TRACE Track path 1.1
CONNECT Requirements for Tunneling Protocol Connection Broker 1.1
LINK Establish the link between resources and 1.0
UNLINK Disconnect relations 1.0

Wherein the commonly used GETinclude: POST, , OPTIONSmethod.

Fourth, persistent connections

We know that HTTP protocol based on the TCP protocol, but before the TCP protocol will each establish a "three-way handshake", so if every time you send an HTTP request should establish a TCP connection, it will cause too many "handshake" a waste of server resources. The initial version of the HTTP protocol, HTTP traffic every time need to re-establish TCP request, which is a big disadvantage.

To solve the above problems, HTTP 1.1 persistent connections introduced (HTTP Persistent Connections, has become HTTP keep-alive or HTTP connection reuse). Features a persistent connection is not made as long as either the TCP connection is disconnected, it would have been to maintain the state of TCP connections.

The benefits of persistent connections to reduce duplication establish a TCP connection and disconnection caused by overhead, reducing the load on the server. In HTTP 1.1, the default for all connections are persistent connection .

Fifth, pipelining

Persistent connection request pipelined so that most (Pipeline) mode transmission becomes possible. After sending the request after waiting for the response before sending a request to the next, but after the emergence of pipeline technology, different waiting for a response can initiate the next request, this can be done to send multiple requests in parallel, greatly reducing the time the page is loaded.

Mentioned before HTTP is a stateless protocol, we maintain client state by Cookie.

Cookie will be based in a response message sent from the server is called the Set-Cookieheader field information, 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.

After the discovery server sent from the client over the cookies, 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.

Set-Cookie response message:

HTTP/1.1 200 OK
Date: Thu, 12 Jul 2012 07:12:20 GMT
Server: Apache
<Set-Cookie: sid=1342077140226724; path=/; expires=Wed,
10-Oct-12 07:12:20 GMT>
Content-Type: text/plain; charset=UTF-8

Cookie carrying information request packet:

GET /image/ HTTP/1.1
Host: hackr.jp
Cookie: sid=1342077140226724

Guess you like

Origin www.cnblogs.com/DM428/p/11203694.html