preface
At the moment, HTTP is essential in the knowledge that the front end must understand, and I am also learning, I mainly read the diagram of HTTP to learn if there is an error please point out.
HTTP basic network
To understand HTTP, it is necessary to understand the TCP/IP protocol family in advance. Commonly used networks, including the Internet, operate on the basis of the TCP/IP protocol family. HTTP is a subset of that.
Application layer: provides required services for different network applications.
Transport layer: Provides end-to-end communication/transmission functions for application layer entities to ensure sequential transmission of packets and data integrity.
Network layer: It deals with packets flowing over the network and contains protocols that involve the logical transmission of packets across the network.
Link layer: Monitors data exchange and handles the hardware part of the network connection.
- When the TCP/IP protocol family is used to communicate on the network, the TCP/IP protocol family communicates with each other in a hierarchical order. The sender goes down from the application layer, and the receiver goes up from the application layer.
- Let’s use HTTP as an example. First, the sending client makes an HTTP request at the application layer (HTTP protocol) to view a Web page. Then, for the convenience of transmission, the data received from the application layer (HTTP request packets) is segmented at the transport layer (TCP), and each packet is marked with a serial number and port number and forwarded to the network layer.
- At the network layer (IP protocol), add the MAC address as the communication destination and forward it to the link layer. This way, the communication request to the network is ready.
- The server on the receiving end receives data at the link layer and sends the data to the upper layer in sequence, all the way to the application layer. HTTP requests sent by clients are received only when they are transmitted to the application layer.
Protocols/services closely related to HTTP: IP, TCP, DNS
The IP protocol is responsible for the transmission of data packets. Of course, this needs to cooperate with IP and MAC addresses. The communication between IP addresses depends on MAC addresses, which involves the ARP protocol used to resolve addresses.
TCP provides a reliable byte stream service that splits large chunks of data into smaller packets for easy transmission and ensures that packets are delivered to their destination.
The DNS service resolves domain names
Uris (Uniform Resource Identifiers) and urls (Uniform Resource Locators)
URI: A string that identifies the name of an Internet resource. Composition: Host name (including port number)+ relative path + identifier
Differences: URIs indicate the location of the requested resource on the Internet, while URLS indicate the location of the requested resource and how to access the resource. URL is a subset of URIs.
Simple HTTP protocol
The HTTP protocol is used for communication between the client and server
Request message: consists of request method, request URI, protocol version, optional request header field, and content entity.
Response message: Basically consists of protocol version, status code (numeric code indicating success or failure of the request), reason phrase used to explain the status code, optional response head field, and entity body. We’ll explain more about this later.
HTTP stateless
HTTP is a stateless protocol that does not save state. The HTTP protocol does not store the state of communication between requests and responses. That is, at the HTTP level, the protocol does not persist requests or responses that have been sent.
- While preserving the feature of stateless protocol, Cookie technology is introduced to solve similar contradictory problems. Cookie technology controls client state by writing Cookie information in request and response messages.
- The Cookie notifies the client to save the Cookie based on the set-cookie header field in the response packet sent from the server. When the client sends a request to the server next time, the client automatically adds the Cookie value to the request packet and sends the request packet.
- After discovering the Cookie sent by the client, the server will check which client sent the connection request, and then compare the records on the server to obtain the previous status information.
Get and POST
- Get requests can be cached, but Post requests cannot
- Post is a little more secure than Get, because Get requests are contained in the URL and will be saved by the browser. Post is not, but in the case of packet capture, it is the same.
- Post can use the Request Body to transfer more data than Get, which doesn’t have this technology
- Urls have a length limit that affects Get requests, but this length limit is browser-specific, not RFC specific
- Post supports more encoding types and does not restrict data types
The semantics of the CODE for each interval
-
100-199 The received request is being processed
-
200-299 The operation succeeded
- 200 OK: indicates that the request from the client is processed correctly on the server
- 204 No content: Indicates that the request is successful, but the response packet does not contain the body of the entity
- 206 Partial Content for a range request
-
300-399 operations need to be redirected and get data in another way
- 301 Moved permanently, permanently redirects: indicates that the resource has been assigned a new URL
- 302 Found, temporary redirection, indicating that the resource was temporarily assigned a new URL
- 303 See Other: indicates that another URL exists for the resource. Use the GET method to obtain the resource
- 304 Not Modified: indicates that the server allows access to the resource but the request condition is not met
- 307 Temporary redirect Is the same as 302
-
There is a problem with the request sent from 400-499
- 400 Bad Request: Syntax errors exist in the request packet
- 401 Unauthorized: The request to be sent requires authentication information that is authenticated through HTTP
- 403 Forbidden: Access to requested resources is denied by the server
- 404 not found: No requested resource was found on the server
-
500-599 server has a problem
The cache
The cache is a copy of resources stored on the local disk of the proxy server or client. The cache is used to reduce the access to the source server so as to save traffic and communication time and achieve better interaction experience.
The requested resource is returned directly from the cache server to the client if it has already been cached, or the client can read it directly from the local disk. The cache can be set to expire. When the cache expires, the client/cache server can request new resources from the source server.
The common header field can be used in both request and response packets
- Cache-Control
- No-cache Indicates that the client does not cache expired resources
- No-store indicates that no cache is performed
- Max-age indicates that the cache duration of the resource is smaller than the specified value. The client accepts the cache resource and the cache server does not confirm the validity of the resource
- Connection controls the header field (hop-by-hop) that is no longer forwarded to the agent and manages persistent connections
- Close indicates that the server is explicitly disconnected
- Keep-alive indicates to save the persistent connection. Before HTTP/1.1, the default connection is non-persistent. You need to add this field if you want to save the persistent connection
- Upgrade can be used to specify a completely different communication protocol, for which the server can return a 101 status code
Request header field
- Accept refers to the media types that the user agent can handle and their relative priority
- Accept-encoding is used to inform the server of the content Encoding supported by the user agent and the priority order of the content Encoding
- Authorization refers to the authentication information used to inform the server of the user agent
- Host If multiple domain names exist under an IP address, the help server knows the specific Host to be requested
- The user-agent communicates information to the server, such as the browser that created the request and the name of the User Agent