The moon under the sea is the last month, the person in front of me is my sweetheart – Shagil Shuo
Start:
The content and progress of this chapter are based on my reading of the Illustrated HTTP Turing series, supplemented by my own Internet search.
About client and server
-
The end that requests access to a resource such as text or images is called the client, and the end that provides the response to the resource is called the server.
-
When the HTTP protocol is used for communication, one end of the communication line must be the client and the other end the server.
-
Sometimes, the identities of the two may be interchangeable, and the HTTP protocol can clearly distinguish the identities of the two sides.
-
The request must be the client and the corresponding must be the server
, as shown below: -
Corresponding message composition
Stateless protocol — HTTP
- HTTP is a stateless protocol, that is, it does not save the communication state between request and response.
- The protocol does not persist requests or responses that have been sent.
- For the HTTP protocol, when a new request is sent, a new response is generated.
- The protocol itself does not retain all previous request and response information.
- Reason: To process a large number of transactions faster.
- Hot potato
- With the development of business, statelessness brings more and more problems in practical application
- In order to preserve the state information and solve the problems caused by stateless protocols, the introduction
Cookie
technology - This is described later in state management
Cookie
The HTTP method for communicating with the server
GET: Obtains resources
-
The GET method is used to request the resource identified by the URI
-
The response returned by the server after the specified resource is parsed
POST: indicates the transfer entity body
-
The GET method can also do entity transfers, but generally only POST transfers are used
-
The POST method is also not primarily for getting the body of the response
PUT: transfers files
PUT
Method, which contains the file contents in the body of the request packet and saves them to the location specified by the request URI- As a result of the HTTP / 1.1
PUT
The method has no authentication mechanism, so it has security problems - so
PUT
Methods are generally not open
HEAD: indicates the packet header
HEAD
Method is used to verify the validity of the URI and the date and time of resource update- The HEAD method is the same as the GET method, except that the body of the packet is not returned
State management using cookies
-
HTTP is a stateless protocol and does not save the status of previously sent requests and responses. Therefore, the current request cannot be processed according to the previous status
-
This leads to the inefficient event of having to log in again every time you jump to a new page
-
Solution:
-
Additional parameters in each request packet manage login status
-
But managing all the client state is a huge burden
-
-
It is based on the above contradictory considerations, so the introduction of Cookie technology
- It does this by writing in request and response packets
Cookie
Information to control client state - By sending the response packet
Set-Cookie
Field information, notificationClient SaveCookie
- The next time the client sends a request to the server, the request packet is automatically added
Cookie
值 - The server receives the packet sent by the client
Cookie
Then, the system compares the status information with the server to find the previous status information
- The figure above describes the theoretical interaction scenario. The specific packets sent are as follows
Request packet (without cookie information) */ GET /reader/ HTTP/1.1 Host: hackr.jp /* 2. Response packet (the server generates Cookie information) */ 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:20GMT > the content-type: text/plain; charset=UTF-8 /*3. Request packet (automatically send the saved Cookie information) */ GET /image/ HTTP/1.1 Host: hackr.jp Cookie: sid=1342077140226724 Copy the code
- It does this by writing in request and response packets
HTTP information in the packet
The HTTP message
-
The information exchanged through THE HTTP protocol is called HTTP packets
- Client: A request packet
- Server: called corresponding packet
-
HTTP packet content is divided into header and body (not required).
-
The structure of request packets and response packets is as follows
- Request line: Contains the request method, request URI, and HTTP version.
- Status line: Contains the status code, cause phrase, and HTTP version of the response result.
- Header field: a header that contains the various conditions and attributes of the request and response.
- There are generally: general, request, response, entity four headers.
The HTTP status code
- Status code indicates the result of the HTTP request from the client, whether the processing is normal on the server, and error notification.
-
Category of status codes
category The reason the phrase 1XX Informational status code The received request is being processed 2XX Success status code The request is processed normally 3XX Redirection (Redirection status code) Additional operations are required to complete the request 4XX Client Error status code The server could not process the request 5XX Server Error status code The server failed to process the request -
The common status code is resolved as
- 200 OK: The client request is processed properly on the server.
- 204 No Content:
- The table server successfully processed the request, but did not contain the body of the entity in the response packet returned
- Used when only the client needs to send information to the server and no new information is needed to the client.
- 206 Partial Content
- The status code indicates that the client has made a range request
- The response packet contains the entity Content in the content-range Range
- 3XX redirection: Indicates that the browser needs to perform some special processing to process the request correctly.
- 301 Moved Permanently (permanent redirection)
- Indicates that the requested resource has been assigned a new URI
- If you saved the URI of the resource as a bookmark, you should save it again as prompted by the URI in the Location header field
- 302 Found (temporary redirection)
- Similar to 301, except that it represents a temporary redirect
- You don’t update your bookmarks like you did in 301
- 303 See Other
- Indicates that the requested resource has another URI and should be directed to obtain the requested resource using the GET method.
When the response status codes 301, 302, and 303 are returned, almost all browsers change POST to GET and delete the body in the request packet. After that, the request is automatically sent again.Copy the code
-
304 Not Modified
-
When a client sends a conditional request, the server allows the request to access the resource, but the conditional request is not met.
-
-
4XX Client error
The response from 4XX indicates that the client is the cause of the error.
-
400 Bad Request: Indicates that a syntax error exists in the Request packet
-
401 Unauthorized (Unauthorized)
- The request to be sent must have the AUTHENTICATION information that passes THE HTTP authentication (BASIC authentication and DIGEST authentication)
- If the request has been made once before, the user authentication fails
-
403 Forbidden
- Indicates that the server denied access to the requested resource
-
404 Not Found
- Indicates that the requested resource cannot be found on the server
-
5XX Server error
The response from 5XX indicates that the server itself has an error.
-
500 Internal Server Error
- Indicates that an error occurred on the server side while executing the request
- It could be a bug or some temporary glitch in the Web application
-
503 Service Unavailable
-
Indicates that the server is temporarily under load or down for maintenance and is now unable to process requests
-
HTTP header field
There are four TYPES of HTTP header fields
- General Header Fields
- Header used by both request and response packets.
- Request Header Fields
- Header used by the client to send request packets to the server. Add additional content to the request, response priority, etc.
- Response Header Fields
- Header used by the server to return a response packet to the client. The additional content of the supplementary response requires additional information from the client.
- Entity Header Fields
- Headers used for the entity part of request and response packets. Supplementary Information such as the update time of resources.
Header field table
-
Generic header field table
Header field name instructions Cache-Control Controls the behavior of the cache Connection Hop by hop header and connection management Date Date and time when the packet was created Pragma Packet instructions Trailer The header of the packet end Transfer-Encoding Specifies the transmission encoding mode of the message body Via Information about the proxy server Warning Error notification -
Request header field (part)
Header field name instructions Accept The type of media that the user agent can handle Accept-Charset The preferred character set Accept-Language Preferred language (natural language) Authorization Web Authentication information Expect Expect specific behavior from the server From The email address of the user Host The requested resource is located on the server If-Match Compare entity Tags (ETags) If-Modified-Since Compare the update times of resources If-None-Match Compare entity tags (as opposed to if-match) If-Range A range request for entity bytes is sent when the resource is not updated Proxy-Authorization The proxy server requires authentication information on the client Referer The original fetching party of the URI in the request User-Agent HTTP Information about the client program -
Response Header field (part)
Header field name instructions Accept-Ranges Whether to accept byte range requests Age Calculate the elapsed time of resource creation ETag Matching information about the resource Location Redirects the client to the specified URI Proxy-Authenticate The proxy server authenticates the client Retry-After Timing requirements for making the request again Server HTTP Server installation information vary Management information cached by the proxy server WWW-Authenticate The server authenticates the client
Cache - the cache-control HTTP
-
The operation mechanism of the Cache is manipulated by specifying the header field cache-control.
-
The parameters of directives are optional and are separated by commas (,).
-
Cache request instruction table
instruction parameter instructions no-cache There is no Force re-authentication to the source server no-store There is no Nothing in the request or response is cached Max-age = [seconds] necessary The maximum Age value of the response Stale (= [seconds]) Can be omitted Received an expired response Min-fresh = [seconds] necessary The response is expected to remain valid within the specified time only-if-cached There is no Get resources from the cache -
Cache response instruction
instruction parameter instructions public There is no A cache of responses can be provided to any party private Can be omitted Only a response is returned to a specific user no-cache Can be omitted You must verify its validity before caching it no-store There is no Nothing in the request or response is cached no-transform There is no Agents cannot change media types must-revalidate There is no Cacheable but must be confirmed with the source server -
When the public directive is used, it makes it clear that other users can also use the cache.
Cache-Control: public
-
When responding with a private directive, only the specific user is treated as an object, as opposed to the public directive.
Conclusion:
HTTP request and response packets must contain HTTP headers. Although ordinary people cannot feel the HEADERS in actual use, developers can learn various configurations when sending request packets and improve the efficiency of debugging in actual production.
You may also be interested in the following
- A graphic HTTP
- Three graphic HTTP
- How are some of the apis implemented in # VUE3.0?
- JavaScript reads local file configuration (compatible with lower IE versions)
- # What do you ask about the front end of one year’s work experience?