Xiaobian is an atypical interviewer. For the first question about HTTP protocol, most people will ask what status codes are commonly used. What is the full name of HTTP?
What is the full name of HTTP?
HyperText Transfer Protocol, don’t mispronounce those words. Hypertext is tagged text, originally HTML. HTTP can now transfer more than HTML. It can transfer forms, JSON, XML, files.
What are the common HTTP status codes?
Most of you know the 200, 404, 500, 302 status codes. If you don’t know 404, you will be despised by xiaobian. Why are 500 errors so common? Because there are always bugs in development, a big exception is thrown, and the browser is 500. 500 stands for InternalServerError, which is an InternalServerError, and if it’s not a bug, it’s usually a database failure.
Ask a few more status codes and you won’t know, because most companies don’t use standard HTTP status codes for their software services. Many status codes will never appear and students won’t know either.
400 Bad Request Is used for parameter verification. One parameter is missing or the parameter type is incorrect.
Error 502 Bad Gateway error 502 occurs when the backend service is down or under too much pressure and the requests received by Nginx cannot be sent to the backend service for processing. This is also a very common mistake that occurs when people are distracted on Zhihu Douban.
Very few people know about this status code, because most back-end developers have very little experience with front-end Javascript development. When you open a frequently visited website in Chrome, you can see a lot of 304 status codes by looking at static resources transmitted by Network. This means that the resource is cached by the browser and does not need to be rerequested by the server.
401 Unauthorized. This is easy to understand: a resource exists but is not accessible to you.
403 Forbidden Forbidden access to resources. This error occurs if your IP address is blacklisted.
In fact, there are many status codes, xiaobian did not go to a good study, because it will not be used in the work. If you’re interested, keep reading wikipedia
What methods are available for HTTP?
GET does not explain, if readers do not know, advise not to work in the IT industry.
POST is used to create or modify resources. In RESTFUL specifications, POST is only used to create resources. The 201 Created status code is returned indicating that a resource is Created successfully. However, most sites do not follow strict RESTFUL specifications, and it is quite common for POST to be used to modify resources.
PUT corresponds to POST to create resources. PUT is used to modify resources. PUT parameters must be all attributes of the object.
PATCH parameters corresponding to PUT are all attributes of the object. PATCH parameters are partial attributes. Modification is partial field modification.
DELETE Deletes a resource.
HEAD is not commonly used, just like GET, except that it does not return the Body content, only the HTTP header. It is used to obtain meta information about the resource, such as length and modification time
OPTIONS are cross-domain related, more on that later.
I haven’t used TRACE.
CONNECT xiaobian has not been used.
Read the RPC specification for the next three interested parties. Xiaobian roughly read, said not how to understand, you go up you challenge.
What is the HTTP protocol format?
The HTTP request and response message protocols are the same and are divided into three parts, the start line, the header, and the body. These three parts are delimited by CRLF. The last header has two CRLFS to indicate the end of the header.
The starting line of an HTTP request is called the request line, which looks like GET /index.html HTTP/1.1
The starting line of the HTTP response is called the status line and is 200 OK
The message header consists of many key-value pairs, separated by CRLF, or no key-value pairs at all. Like the Content – Encoding: gzip
The message body is a string, the Length of which is specified by the Content-Length key in the message header. If there is no content-Length field, there is no body. For example, a GET request does not have a body. The body of a POST request is usually used to hold form data. The page content returned by the response to the GET request is also placed in the message body. We usually return JSON content from API calls in the body of the message.
What is chunking?
When the browser requests a resource from the server, it is a dynamic resource, and the server cannot predict the size of the resource in advance.
Mr. Server becomes a thunk, sends this chunk, regenerates another chunk, sends another chunk, until all resources are transferred.
Chunking requires a special key-value pair transfer-encoding: thunked in the request header, so the contents of the message body are chunked.
What is the mechanism for persistent connections?
In the early versions of HTTP, each request would initiate a connection, and a web page would have many static resources and many API calls in addition to the HTML of the page. If each request had a connection, it would be bound to create multiple connections with the server once the page was loaded, which was very wasteful of server resources. It also slows down client access. Keep-alive persistent connections were introduced after HTTP1.0 and became the default option in HTTP1.1. It enables one HTTP connection to serve multiple requests consecutively, effectively saving resources and increasing the loading speed of client pages.
Persistent connections should not be maintained all the time. After all, each connection takes up server resources. If too many people open the page, the server resources will be strained. Therefore, servers typically configure a KeepAlive Timeout parameter and KeepAlive Requests parameter to limit the duration of a single connection and the maximum number of Requests that can be served.
If the server sets timeout to 0, it degrades to a non-persistent connection. Non-persistent connections add a header to the response header Connection: Close notifies the client that the Connection needs to be closed immediately after receiving the current response.
Also, browsers don’t keep the connection going just because the server set KeepAlive Timeout to an infinite length. Each browser has its own built-in restrictions, which vary from browser vendor to browser vendor.
What is Pipeline pipelining?
HTTP1.0 does not support pipelinization, the same connection processing request sequence is one by one reply mode, processing a request requires a TTL, that is, the client to server round-trip time, processing N requests is N TTL. When a page has a lot of requests, the page loads very slowly.
Starting with HTTP1.1, the server is required to support pipelining, where multiple requests can be sent to the server at the same time and the responses can be read one by one. This pipelinization is the same as the pipelinization principle of Redis; the order of responses must be the same as the order of requests.
How to understand the statelessness of HTTP protocol?
The statelessness of HTTP protocol means that the protocol layer of the server does not need to establish any correlation between different requests. It specifically refers to the statelessness of the protocol layer. But this does not mean that applications built on top of HTTP cannot maintain state. The application layer can track the correlation between user requests through Session. The server will bind a unique Session ID to each Session object. The browser can record the Session ID in the local cache LocalStorage or Cookie, and carry this Session ID in subsequent requests. The server can then find the corresponding session state for each request.