HTTP protocol – HTTP common header (common)
Generic header field name
Generic header field name | instructions |
---|---|
Cache-Control | Controls the behavior of caching |
Connection | Allows client orders and servers to specify options related to request/response links |
Date | Date and time when the packet was created |
Cache-Control
Cache-control request instruction:
instruction | parameter | instructions |
---|---|---|
no-cache | There is no | Force revalidation to the source server |
no-store | There is no | Specifies that the cache cannot store any portion of the request locally |
Max – age = [s] | Must be | For example, max-age=31536000, cache for one year |
Cache-control Cache response instruction:
instruction | instructions |
---|---|
public | Caching of responses can be provided to any party |
private | Returns a response only to a specific user |
no-cache | The cache must be validated before being cached |
no-store | Specifies that the cache cannot store any part of the response locally |
Max – age = [s] | The maximum Age value of the response |
supplement
No-cache: does not mean no cache. This means that before a cached resource can be used, it must be checked by the server (revalidate can also do this). No-store: tells the browser not to cache it. Max – age: If the content of a resource is stable and does not change for a long time, we can declare that the browser /CDN can cache the resource for a long time (31536000 seconds, i.e., a year). As long as the user does not manually clear the browser cache, the internal server will no longer receive requests for the resource (current browser /CDN). Recommended: Front-end static resource caching optimality and max-age pitfalls
Connection
field | instructions |
---|---|
keep-alive | Maintain long links |
close | Close links |
Keep-alive indicates the supplement
Connection: keep-alive is the field used for HTTP persistent connections.
Comparison of close and keep-alive requests:
Keep – advantages and Alive
Advantages: The keep-alive mode is more efficient, because it avoids the overhead of connection establishment and release. Disadvantages: Long-time TCP connections tend to occupy invalid system resources, wasting system resources
HTTP protocol – HTTP request header (common)
Request header field name
Request header field name | instructions |
---|---|
Host | The host name and port number of the server receiving the request are given |
Referer | Provides the URL of the document containing the currently requested URL |
User-Agent | Tell the server the name of the application that initiated the request |
Accept | Types of content that the server can process (MIME_type) |
Accept-Encoding | Encoding mode (GZIP: LZ77 compression algorithm; Compress: LZW compression algorithm. My identity. |
If-Modified-Since | Last-modified implements negotiated cache |
If-None-Match | Caching is implemented with ETag |
Authorization | User credentials; Like Bearer XXXX. |
Cookie | The browser carries it every time it sends a request |
Referer
I have a www.baidu.com link in www.google.com, so click on www.baidu.com and its header will say:
Referer=http://www.google.com
Copy the code
Referer’s role:
- Preventing hotlinking
I only allow my own website to visit my own picture server, so my domain name is www.google.com, so the picture server takes the Referer every time to judge whether it is my own domain name www.google.com, if it is, it will continue to visit, not block.
- Prevent malicious requests.
Dynamic requests are at the time and must be Referer for my own site.
Accept
The request header is used to tell the (server) what type of content the client can process, which is represented by MIME type. The server can choose one of many alternatives to apply and notify the client of its choice using the Content-Type reply header.
The Accept field
The Accept field | information |
---|---|
<MIME_type>/<MIME_subtype> | A single exact MIME type, such as text/ HTML. |
<MIME_type>/* | A MIME type, but no subclass is specified. Image /* can be used to refer to image/ PNG, image/ SVG, image/ GIF, and any other image type. |
* / * | MIME type of any type |
; Q = (q factor weight) | The value represents the order of precedence and is expressed by relative quality value, also known as weight. |
Accept: text/html Accept: image/* Accept: text/html, application/xhtml+xml, application/xml; Q = 0.9 * / *; Q = 0.8Copy the code
HTTP protocol – HTTP response header (common)
Name of the response header field
field | information |
---|---|
Age | I don’t know what it is, either |
Server | The name and version of the server application software |
vary | Determines a request header for the future |
Set-Cookie | The server sends cookies to the client |
vary
Vary implements dynamic services
For example, if the content you provide to the mobile side is different, it can be used to prevent your client from misusing the cache used for the desktop side. And help Google and other search engines find your mobile version of the page while telling them they don’t need Cloaking.
Vary: accept-encoding Different clients may compress the Encoding differently. Some clients may not support compression, so the data returned by the server cannot be compressed, requiring the server to perform different data returns. The solution to this problem is to return specific data by adding Vary’s Accept-Encoding to tell the server what type is supported
HTTP protocol – HTTP entity header (common)
Entity header field name
Entity head field | information |
---|---|
Allow | Enumerates the collection of HTTP methods supported by the resource |
Content-Encoding | Arbitrary encoding performed on a subject |
Content-Length | The length or dimension of a body |
Content-Type | The object type of this principal |
ETag | The entity tag associated with this entity |
Last-Modified | The date and time this entity was last modified |
Allow
When receiving an unsupported HTTP Method, the server returns a response with the status code 405 Method Not Allowed. At the same time, all supported HTTP methods are written to the header field Allow and returned.
Expires
Cache expiration time, used to specify the expiration time of resources, is a specific point in time on the server.
Expires is a Web server response header field that tells the browser in response to an HTTP request that the browser can cache data directly from the browser before the expiration date without having to request it again.
Expires: Wed, 04 Jul 2012 08:26:05 GMT # Set expires and cache-control < META http-equiv=" expires "content="Wed," 04 Jul 2012 08:26:05 GMT"Copy the code
ps
If a “max-age” or “s-max-age” directive is set in the cache-control response header, the Expires header is ignored
Expires is a product of HTTP/1 and is limited to local time, which can invalidate a cache if you change it.
supplement
Content-encoding and Content-Type and server and client processing flow
# Response Headers(Response Headers) content-encoding: gzip content-type: text/plain; charset=iso-8859-1Copy the code
Take returning hello message as an example. The server sends a message to the browser: Hello. First, the server needs to tell the browser the Type of the data I sent to you. Charset = ISO-8859-1 tells the browser what to do;
Since the computer only knows 0 and 1, the browser should receive: 01101000(h) 01100101(e) 01101100(L) 01101100(L) 01101111(O)
If we compress ‘hello’ with gzip; So the binary string has changed; So we also need to tell the browser content-encoding: gzip;
Server --> content-type :text/plain and content-encoding :gzip--> browser --> First parse compression algorithm content-encoding :gzip--> Content-encoding :gzip--> Content-typeCopy the code
The relation between last-modified and if-modified-since
If the server returns the resource after the browser accesses the resource for the first time, add last-Modified to Response Headers. The value is the Last time the resource was Modified on the server:
Last-Modified: Fri, 23 Oct 2020 07:33:48 GMT
Copy the code
If the browser requests the resource again, Request Headers is added
If-Modified-Since: Fri, 23 Oct 2020 07:33:48 GMT
Copy the code
The server receives the resource request again and compares the if-modified-since value with the last modification time of the resource in the server. If there is no change, return 304 and empty response body, read directly from cache; If the time of if-modified-since is less than the time of the last modification of the resource on the server, the file has been updated, so the new resource file and 200 are returned.
The ETag and If – None – Match
When the browser accesses a resource for the first time, the server returns the resource with a unique identifier of the current resource file. Add the ETag to the Response Headers file.
ETag: "5f92875c-6fa"
Copy the code
If the browser requests the resource again, Request Headers is added
If-None-Match: "5f92875c-6fa"
Copy the code
The server can determine If the resource has been modified relative to the client by comparing if-none-match with the ETag of the resource on the server
The MIME type
MDN MIME type
Related blog
HTTPS encryption, HTTP2.0, keep-alive
Accept
Content-encoding and Content-Type of HTTP and server and client processing flow
Browser caching mechanism