1. Web Server (web server)
2. HTTP protocol ( application layer protocol )
2.1 Introduction
2.2 Overview
2.3 Working principle
1. Client connects to web server
An HTTP client, usually a browser, establishes a TCP socket connection with the Web server's HTTP port (default is 80) . For example, http://www.baidu.com(URL)
2.Send HTTP request
Through the TCP socket, the client sends a text request message to the Web server. A request message consists of
It consists of four parts: ① request line, ② request header, ③ blank line and ④ request data .
3. The server accepts the request and returns an HTTP response
The web server parses the request and locates the requested resource. The server writes a copy of the resource to the TCP socket, which is read by the client. A response consists of ① status line, ② response header, ③ blank line and ④ response data .
4. Release the TCP connection
- If the connection mode is close , the server actively closes the TCP connection, and the client passively closes the connection and releases the TCP connection;
- If the connection mode is keep-alive , the connection will be maintained for a period of time, during which time requests can continue to be received .
5. Client browser parses HTML content
- The client browser first parses the status line for a status code indicating whether the request was successful .
- Then each response header is parsed, and the response header informs the following number of bytes of the HTML document and the character set of the document .
- The client browser reads the response data HTML, formats it according to the syntax of HTML, and displays it in the browser window .
For example: type the URL in the browser address bar and press Enter, you will go through the following process:
- 1. The browser requests the DNS server to resolve the IP address corresponding to the domain name in the URL ;
- 2. After parsing the IP address, establish a TCP connection with the server based on the IP address and the default port 80 ;
- 3. The browser issues an HTTP request to read a file (the file corresponding to the part after the domain name in the URL), and the request message is
- The data of the third message of the TCP three-way handshake is sent to the server ;
- 4. The server responds to the browser request and sends the corresponding HTML text to the browser;
- 5. Release the TCP connection;
- 6. The browser displays the HTML document in the browser window.
3. HTPP request/response message format
3.1 HTTP request format
- GET
GET / HTTP/1.1
Host: www.baidu.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: BAIDUID=6729CB682DADC2CF738F533E35162D98:FG=1;
BIDUPSID=6729CB682DADC2CFE015A8099199557E; PSTM=1614320692; BD_UPN=13314752;
BDORZ=FFFB88E999055A3F8A630C64834BD6D0;
__yjs_duid=1_d05d52b14af4a339210722080a668ec21614320694782; BD_HOME=1;
H_PS_PSSID=33514_33257_33273_31660_33570_26350;
BA_HECTOR=8h2001alag0lag85nk1g3hcm60q
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
空行
请求数据为空
- POST (from the latest version of Web server project details - 04 http connection processing (Part 1) (qq.com) )
POST / HTTP1.1
Host:www.wrox.com
User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Content-Type:application/x-www-form-urlencoded
Content-Length:40
Connection: Keep-Alive
空行
name=Professional%20Ajax&publisher=Wiley
3.2 HTTP response message format
HTTP/1.1 200 OK
Bdpagetype: 1
Bdqid: 0xf3c9743300024ee4
Cache-Control: private
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html;charset=utf-8
Date: Fri, 26 Feb 2021 08:44:35 GMT
Expires: Fri, 26 Feb 2021 08:44:35 GMT
Server: BWS/1.1
Set-Cookie: BDSVRTM=13; path=/
Set-Cookie: BD_HOME=1; path=/
Set-Cookie: H_PS_PSSID=33514_33257_33273_31660_33570_26350; path=/; domain=.baidu.com
Strict-Transport-Security: max-age=172800
Traceid: 1614329075128412289017566699583927635684
X-Ua-Compatible: IE=Edge,chrome=1
Transfer-Encoding: chunked
4. HTTP request method:
The HTTP/1.1 protocol defines a total of eight methods (also called "actions") to operate specified resources in different ways:
- 1.GET : Make a "display" request to the specified resource. The GET method should only be used to read data and should not be used for operations that produce "side effects", such as in Web Applications. One reason is that GET may be accessed randomly by web spiders and the like.
- 2.HEAD : Like the GET method, it makes a request for the specified resource to the server. It's just that the server will not return the text part of the resource. The advantage is that using this method can obtain the "information about the resource" (metainformation or metadata) without having to transmit the entire content.
- 3.POST : Submit data to the specified resource and request the server for processing (such as submitting a form or uploading a file). The data is included in the request article. This request may create a new resource or modify an existing resource, or both.
- 4.PUT : Upload the latest content to the specified resource location.
- 5.DELETE : Request the server to delete the resource identified by Request-URI.
- 6.TRACE : Echo the request received by the server, mainly used for testing or diagnosis.
- 7.OPTIONS : This method allows the server to return all HTTP request methods supported by the resource. Use '*' to replace the resource name and send an OPTIONS request to the web server to test whether the server function is functioning properly.
- 8.CONNECT : The HTTP/1.1 protocol is reserved for proxy servers that can change the connection to a pipeline. Typically used for links to SSL encrypted servers (via non-encrypted HTTP proxy servers)
5. HTTP status code
The first line of all HTTP responses is the status line , which consists of the current HTTP version number , a 3-digit status code , and a phrase describing the status separated by spaces . The first digit of the status code represents the type of current response:
- 1xx message————The request has been received by the server and continues to be processed.
- 2xx Success——The request has been successfully received, understood, and accepted by the server
- 3xx redirect--subsequent operations are required to complete this request
- 4xx request error - the request contains a lexical error or cannot be executed
- 5xx Server Error——The server encountered an error while processing a correct request.
Although RFC 2616 has recommended phrases to describe status, such as "200 0K", "404 Not Found", WEB developers can still decide which phrases to use to display localized status descriptions or custom information.
recommended article:
Detailed explanation of HTTP messages_http messages_Hardworking666's blog-CSDN blog https://blog.csdn.net/Hardworking666/article/details/123833192 [Writing webserver from scratch·Basics #02] The core of the server --- I/O processing units and task classes - dayceng - Blog Park (cnblogs.com) https://www.cnblogs.com/DAYceng/p/17418584.html#%E5%A4%84%E7%90%86%E6 %96%B0%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%9A%84%E8%BF%9E%E6%8E%A5%E8%AF%B7%E6%B1 %82