In-depth analysis of the working principle of the HTTP protocol


HTTP (Hypertext Transfer Protocol) is one of the most widely used protocols on the Internet, which is responsible for transferring data between clients and servers.

1. Basic concepts and working principles of HTTP

1.1 Request and Response Model

HTTP adopts a request-response model, the client sends an HTTP request, and the server returns an HTTP response. A request consists of a request line, a request header, and a request body, and a response consists of a response line, a response header, and a response body.

1.2 HTTP method

HTTP defines a variety of request methods, commonly used are GET, POST, PUT, DELETE, etc. Different methods correspond to different operations, such as obtaining resources, submitting data, updating resources, and so on.

1.3 URL sum URI

URL (Uniform Resource Locator) is used to locate resources on the Internet, including protocol, host name, port number, path and other information. URI (Uniform Resource Identifier) ​​is a unique identifier of a resource, including URL and URN (Uniform Resource Name).

1.4 HTTP status code

The HTTP response contains a status code, which is used to indicate the processing result of the request. Common status codes are 200 (success), 404 (not found), 500 (server error), etc.

Send HTTP request:

const http = require('http');

const options = {
    
    
  hostname: 'www.example.com',
  port: 80,
  path: '/api/users',
  method: 'GET',
};

const req = http.request(options, (res) => {
    
    
  console.log(`HTTP状态码:${
     
     res.statusCode}`);
  
  res.setEncoding('utf8');
  
  let responseData = '';
  res.on('data', (chunk) => {
    
    
    responseData += chunk;
  });
  
  res.on('end', () => {
    
    
    console.log(`接收到数据:${
     
     responseData}`);
  });
});

req.on('error', (error) => {
    
    
  console.error(`请求出错:${
     
     error.message}`);
});

req.end();

2. Detailed explanation of HTTP protocol

2.1 HTTP request

The structure of the HTTP request, including the request line, request header and request body. Common request header fields, such as Host, User-Agent, Content-Type, etc.

Parse the HTTP request:

const http = require('http');

const server = http.createServer((req, res) => {
  console.log(`HTTP请求方法:${req.method}`);
  console.log(`URL路径:${req.url}`);
  
  for (const [header, value] of Object.entries(req.headers)) {
    
    
    console.log(`请求头:${
     
     header}: ${
     
     value}`);
  }
  
  let body = '';
  req.on('data', (chunk) => {
    
    
    body += chunk;
  });
  
  req.on('end', () => {
    
    
    console.log(`请求体:${
     
     body}`);
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, World!');
  });
});

server.listen(8080, () => {
    
    
  console.log('服务器已启动');
});

2.2 HTTP response

The structure of the HTTP response, including the response line, response headers, and response body. Response header fields, such as Content-Length, Content-Type, Cache-Control, etc.

Send HTTP response:

const http = require('http');

const server = http.createServer((req, res) => {
    
    
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.write('Hello, World!');
  res.end();
});

server.listen(8080, () => {
    
    
  console.log('服务器已启动');
});

2.3 Cookie and Session

Cookie and Session are commonly used web development technologies for storing and managing user status information. Cookies are stored on the client and transmitted through the browser's Cookie storage area; Sessions are stored on the server and transmitted through the Session ID. Cookie is suitable for storing small and insensitive data, while Session is suitable for storing large and sensitive data. In practical applications, developers can choose an appropriate mechanism to manage user status and implement user authentication functions according to specific needs.

1. Cookies

  1. Concept: Cookie is a small text file stored in the client (browser) and used to transfer data between the server and the client. It can store information about the user such as user preferences, shopping cart contents, etc.
  2. working principle:
    • The server sends the Cookie to the client through the Set-Cookie field of the response header.
    • After the client receives the cookie, it will store it in the cookie storage area of ​​the browser.
    • In subsequent requests, the client sends the Cookie back to the server through the Cookie field of the request header.
    • The server can read and parse the data in the cookie, and process it as needed.
// 设置Cookie
res.setHeader('Set-Cookie', 'username=john; Path=/; Expires=Wed, 30 Jun 2023 12:00:00 GMT');

// 读取Cookie
const cookies = req.headers.cookie;
console.log(cookies); // 输出:username=john; sessionId=abc123

2. Session

  1. Concept: Session is a data structure on the server that stores user state information and is used to track the user's session state. It stores session data on the server, not on the client.
  2. working principle:
    • When a user accesses the server, the server will create a unique session ID (Session ID) for the user.
    • The server stores the Session ID in its own storage medium (such as memory, database, etc.).
    • The server sends the Session ID to the client, usually through a cookie.
    • The client sends the Session ID back to the server through the cookie in subsequent requests.
    • The server searches for the corresponding session data according to the Session ID, and realizes state tracking and management.
// 在Express框架中使用Session
const session = require('express-session');
const app = express();

app.use(session({
    
    
  secret: 'mySecretKey',
  resave: false,
  saveUninitialized: false
}));

// 设置Session数据
req.session.username = 'john';

// 读取Session数据
console.log(req.session.username); // 输出:john

2.4 Caching mechanism

The caching mechanism is an important means to improve the performance and response speed of web applications. By properly setting client-side cache and server-side cache policies, unnecessary network requests can be reduced, server load can be reduced, and user experience can be improved. Developers need to choose an appropriate cache solution according to specific business needs, and pay attention to the update and refresh mechanism of the cache to ensure data consistency and real-time performance.

1. The concept of caching

Caching is a technology for storing data. In web development, it is mainly used to store acquired or calculated data, so that it can be quickly acquired and returned to the client in subsequent requests, avoiding repeated data processing and resource consumption.

Second, the principle of caching

  1. Client-side caching: Client-side caching refers to the browser's local storage of copies of resources (such as HTML, CSS, JavaScript, images, etc.). When the browser requests the same resource again, it will first check whether there is a copy of the resource in the cache. If it exists and is valid, it will be obtained directly from the cache to avoid requesting the server again.
  2. Server-side caching: Server-side caching refers to storing the response returned by the server in the server's cache, and using the cached response directly in subsequent requests without regenerating it. Common server-side caching mechanisms include page caching, database query caching, and object caching.

3. Common caching strategies

  1. Strong cache: Inform the client of the validity period of the resource by setting the Expires or Cache-Control field in the response header. When the client requests the resource again, it first checks the validity period of the local cache, and if it has not expired, directly uses the local cache without initiating a request.
  2. Negotiation cache: By setting the ETag or Last-Modified field in the response header, the server can tell the client how to verify whether the resource has changed. When the client requests the resource again, it will carry the ETag or Last-Modified value of the last request. The server judges whether the resource has changed by comparing these values. If there is no change, it returns 304 Not Modified, and the client continues to use it. local cache.
  3. CDN cache: CDN (Content Delivery Network) is a distributed cache server network that caches static resources on servers closer to users. When a user requests a static resource, the CDN will select the nearest cache server to return the resource according to the user's geographical location, thereby improving resource access speed and user experience.
  4. Database query cache: cache the database query results in memory to avoid repeated query of the database. Common caching solutions include Redis, Memcached, etc.
  5. Page caching: cache the rendering results of the entire page, and return the cached page directly when the same page is requested next time, avoiding re-rendering the page.

4. Precautions for caching mechanism

  1. It is necessary to select an appropriate caching strategy according to different resource types and business scenarios to avoid data inconsistency caused by cache expiration.
  2. For dynamic content or frequently updated data, a shorter cache time should be used, or a mechanism for actively updating the cache should be adopted.
  3. When using CDN cache, pay attention to the cache refresh mechanism to ensure that cached resources are updated in a timely manner.
  4. Avoid over-reliance on caching, and do not force the use of caching mechanisms for some data or requests that are not suitable for caching.

As one of the most basic and commonly used protocols on the Internet, HTTP plays an important role. Understanding the working principle and common applications of the HTTP protocol will help developers better apply and tune web applications, and improve user experience and network security.

Guess you like

Origin blog.csdn.net/weixin_43749805/article/details/131412072