HTTP request and response packets must contain the HTTP header, but usually we do not feel it in the process of using the Web, it is not a lot of attention, recently read the “Diagram HTTP”, learn the structure of the HTTP header, has been the use of each field in the header.

HTTP header

HTTP request and response packets must contain the HTTP header, which provides information for the client and server to process the request and response respectively. Lists a roughly structured brain map of request and response messages

Request Method: GET
Copy the code

Field values can have multiple values for a single HTTP header field:

cache-control: public, max-age=0
Copy the code

Header field type

HTTP header fields are classified into four types based on actual usage.

Common header field: the header used by both the request and response packets.

Request header field: the header used for sending request packets from the client to the server. The header contains additional information about the request, client information, and priority of the response content.

Response header field: the header used when the response packet is returned from the server to the client. It supplements the additional content in the response and requires the client to attach additional content information.

Entity header field: The header used in the entity part of the request message and response message is supplemented with entity-related information such as update time of resource content.

Generic header field

Header field name instructions
Cache-Control Controlling cache behavior
Connection Hop – by – hop header and connection management
Date Date and time when the packet was created
Pragma Packet instructions
Transfer-Encoding Specifies the encoding mode of the packet transmission body
Upgrade Upgrade to another protocol
Via Proxy server information
Warning Error notification

Request header field

Header field name instructions
Accept The types of media that the user agent can handle
Accept-Charset Preferred character set
Accept-Encoding Priority content encoding
Authorization Web Authentication Information
Except Expect specific behavior from the server
Host The server where the resource is requested
if-Match Compare Entity Tag (ETag)
if-Modified-Since Compares the update times of resources
Range Byte range request for the entity
Refer Byte range request for the entity
TE Priority of transmission encoding
User-Agent HTTP client program information

Response header field

Header field name instructions
Accept-Ranges Whether to accept byte range requests
Age Calculate the elapsed time of resource creation
ETag Matching information of resources
Location Redirects a client to the specified UPI
Proxy-Authenticate The proxy server authenticates the client
WWW-Authenticate Authentication information about the server to the client
Server HTTP server installation information
vary Management information about the proxy server

Entity head field

Header field name instructions
Allow HTTP methods supported by the resource
Content-Encoding The encoding method applicable to the entity body
Content-Language The natural language of entity subjects
Content-Length The size of the entity body
Content-Location Replace the URI of the corresponding resource
Content-MD5 The packet digest of the entity body
Content-Range The location range of the entity body
Content-Type The media type of the entity body
EXpires The date and time when the entity body expires
Last-Modified The last modified date and time of the resource

In detail

Cache-Control

Connection

  • You can control whether certain fields are no longer forwarded to the agent

  • Managing persistent Connections
Connection : Keep-Alive
Copy the code

Date Indicates the Date and time when the HTTP packet is created

date: Sun, 19 May 2019 06:05:00 GMT
Copy the code

PraGma was used for backward compatibility with Cache servers that only supported HTTP/1.0, before the advent of cache-Control in HTTP/1.1.

Pragma: no-cache

Trailer specifies which header fields are used after the body of the message, which can be used in HTTP/1.1 block transfer codes.

Transfer-encoding Specifies the Encoding used when transmitting the body of the message (valid only for block Transfer Encoding)

The Upgrade parameter can be used to specify a completely different communication protocol (only with clients and neighboring servers). The connection: Upgrade parameter is used to check whether HTTP and other protocols can be used to communicate with higher versions.

Via to track the transmission path of request and response packets between the client and the server. When a packet passes through a proxy or gateway, information about the server is appended to the header field Via and then forwarded

Warning usually notifies the user of a cache-related problem in the following format

Warning [Warning code] [Warning host: port number] “Warning content” ([date time])

Request header field

Field used in the request packet sent from the client to the server to supplement the additional information about the request, client information, and the priority of the response.

Accept notifies the server that the user agent can process the media types and their relative priority. You can use q=num to represent the priority value of the weight. The value of num ranges from 0 to 1, and can be accurate to three decimal places.

Accept: text/html,application/json; Q = 0.9, application/XML. Q = 0.8

The accept-charset header field is used to inform the server of the character set supported by the user agent and the relative priority of the character set. The weight is the same as q for the Accept field

The accept-encoding header field can be used to inform the server of the content Encoding supported by the user agent and the Encoding priority. Multiple content encodings can be specified at one time, using the weight Q to indicate the priority.

Accept-Encoding: gzip, deflate, cpmpress, identity
Copy the code

The accept-language header field is used to tell the server which languages the user agent can handle, using the weight Q to specify the priority

Authorization informs the server of the authentication information of the user agent and authenticates the user identity.

Expect is used to tell the server that a particular behavior is expected, and is not currently used by browsers

The mailbox of the actual operator of the mailbox of the user agent that sent the request

Host Specifies the domain name of the requesting server and the Tcp port number monitored by the server. If no port is specified, the default port of the requested service is automatically used to inform the server of the domain name and port number of the requested resource.

Conditions of the request

The request header field in the form of if-xxxx is a conditional request. After receiving the attached request condition, the server executes the request only when the condition is met

If the if-match request method is GET, the server returns resources only when the ETag value of the requested resources is one of the values in the first field of the IF-match request. If the request method is Put, resources are allowed to be uploaded. ETag is the unique entity tag of a resource. After the resource is updated, the entity tag value ETag is also updated.

If-modified-since Normally this field is only used in GET requests. If the resource is updated after the date of the if-Modified-since field value, the server accepts the request, otherwise a 304 (Not Modified) with no response body is returned. Used to verify the validity of a proxy or client local resource

If-none-match The server returns the requested resource if and only if there is no resource on the server whose entity ETag value matches the value listed in the header field, otherwise 304 is returned

If-range Range request; If the condition in the if-range field is met, the server returns 206 Partial Content. If the condition in the if-range field is not met, the server returns 206 Partial Content. All resources of 200 OK are returned as normal processing.

Proxy-authorization is used for user proxies to send authentication credentials to Proxy servers

Proxy-Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
Copy the code

Range is used for Range requests that fetch only part of the resource, and the field value indicates the specified Range of the server resource

Range: <unit>=<range-start>-<range-end>, <range-start>-<range-end>
Copy the code

Referer can use the Referer to see from which page a request for a resource originated

The TE tells the server the transport encoding and relative priority of the response that the client can handle

User-agent passes names such as the browser and User Agent information that created the request to the server

User-Agent: <product> /<product-version> <comment> //<product> Product identifier //<product-version> Product version number //comment 0 or more comments about the information that constitutes the productCopy the code

Response header field

The response header field is the field used in the response packet returned by the server to the client, which is used to supplement the additional information of the response, server information, and additional requirements for the client.

Accept-ranges is used to tell clients whether the server can handle range requests

Accept-Ranges: none | bytes  //
Copy the code

Age tells the client how long ago the source server created the response, with field values in seconds.

ETag A resource is uniquely identified as a string. The server assigns an ETag value to each resource. When the resource is updated, the ETag value is also updated. There is no specific generation algorithm, and resources are usually used to hash last-modified timestamps, or hash or version numbers. Use: 1. Prevent the mutual coverage caused by the updating of resources at the same time (air collision); 2. Cache unchanged resources

Location specifies the address to redirect the page to

Proxy-authenticate sends authentication information required by the Proxy server to the client. Specifies the mode of authentication to use to obtain access to resources on a proxy server. The proxy server validates the request so that it can further deliver the request

Retry-after tells the client how long it should be able to Retry the request. The main response is the status 503 (Service Unavailable).

The Server header field Server tells the client about the HTTP Server application installed on the current Server.

Vary When a proxy server receives a request for a resource specified in the Vary header field, if the accept-Language field values are the same, it returns a response directly from the source server; otherwise, it needs to fetch the resource from the source server before returning it as a response.

Www-authenticate Authenticate HTTP access. It tells the client which authentication scheme (Basic or Digest) is appropriate for accessing the resource specified by the requested URI; Defines what authentication methods are used to obtain connections to resources

Entity head field

The entity header field is the header used by the entity part contained in the request message and the response message, and is used to supplement the update time and entity-related information of the content. The entity-related header contained in both request and response messages

ALlow Indicates the set of HTTP methods supported by the enumeration resource. When the server receives an unsupported HTTP Method, it returns 405 (Method Not Allowed) as a response and writes all supported HTTP methods to the header field ALlow.

Allow: GET, POST, HEAD
Copy the code

Content-encoding Tells the client which Content Encoding method the server chooses for the body of the entity. (Content encoding refers to compression without losing entity information.) Commonly used ones are gzip, Compress, Deflate, and identify.

Content-language tells the client the natural Language used by the entity body

Content-Language: zh-CN
Copy the code

Content-length Specifies the size of the main part of the entity.

Content-location Specifies the URI corresponding to the body of the packet. Unlike the header field Location, content-location represents the URI of the resource returned by the packet body.

Content-MD5

Content-Range

Content-type specifies the media type for the body of the entity. As with Accept, the field value is assigned as type/subtype

After receiving a response containing the first field Expires, the cache server will respond to the request with the cache. The response copy will be kept until the specified time. When the specified time Expires, the cache server will send the request. Turns to the source server to request resources

Last-modified Specifies the time when the resource was Last Modified