Table of contents
2.1. The difference between fixed-length structure and non-fixed-length structure when sending
2.2. So why can't the structure with discontinuous memory be sent directly using send?
2.2. Then how do we receive data of variable length?
2.3. How do we receive discontinuous memory?
2.4 Serialization and deserialization
3.2 URL interpretation of HTTP
3.3 Data flow of HTTP protocol
Interview question: Who is more secure, GET method or POST
3.6 Analyze the content of the request body:
3.7.1 Why do I need a status code?
Mainly talking about the application layer and the transport layer
1. Custom protocol
It is the programmer 's agreement on the application layer format at the application layer
Let's first implement a web version of the calculator
Ideas:
accomplish:
What exactly is a custom protocol?
2. TCP sticky packet problem
The byte stream-oriented characteristics of tcp lead to the problem of tcp sticky packets
For the receiver , there is no way to distinguish whether the data has been sent several times, and the data sent before and after have no spacer and are glued together
2.1. The difference between fixed-length structure and non-fixed-length structure when sending
2.2. So why can't the structure with discontinuous memory be sent directly using send?
Let's first take a look at the sending principle of the memory continuous structure:
Let's look at the structure of variable length
2.2. Then how do we receive data of variable length?
For example, if data is sent twice, one time of 10 bytes and one time of 100 bytes, how to receive it?
1.tcp can add size when transmitting data
First receive 4 bytes, analyze the length of the data packet, and then receive the following content
This solves the sticking problem
2. You can also add a custom separator (\r\n)
2.3. How do we receive discontinuous memory?
For example, this kind of data stores the address of the first character
This requires serialization and deserialization
2.4 Serialization and deserialization
Serialization: convert the data structure in the program into a binary sequence;
Data structure:
If the memory is continuous , it can be sent directly.
For example: If the memory of the structure is discontinuous , serialization is to combine different memories , put them in a continuous space, and send them out. Example: multiple nodes in linked list
Deserialization : convert binary into data structure.
3. HTTP protocol
3.1 Overview of http protocol
Hypertext Transfer Protocol
3.2 URL interpretation of HTTP
Let's take a look at the full URL
File path with hierarchy:
query string:
A summary of each part of the URL :
3.3 Data flow of HTTP protocol
First of all, you need to know: The HTTP protocol is an application layer protocol, and the lower layer (transport layer) protocol it uses in the network protocol stack is the TCP protocol
To put it bluntly: the data generated by the HTTP protocol is transmitted by the TCP protocol at the transport layer.
3.4 Format of HTTP protocol
3.4.1. Request format
Use tcp to simulate the HTTP server to accept the request sent by the browser to the server
The code is similar to the previous one, let's look at the function of the worker thread:
We mimic the URL to search on the site:
At this time, it shows that the webpage is not working properly because we did not send any messages to the webpage
But we can find at this time that we have received the request sent to us by the webpage: the following is the content of the request
We can analyze what this is, and analyze what the HTTP request format is:
The first line of the request : request method, path, protocol version
3.4.2. Response format
We now simulate the server sending a response to the browser:
We send a request to the server in the browser and observe what the received response looks like
3.5 HTTP request method:
The request method focuses on GET and POST
GET: Ask the server to erase some resources
PST: Submit data to the server
The data submitted by POST is in the body:
For example: username=xxx&&passwd=xxx
eg: Assuming we want to log in on a certain web page , using a POST request will put our account and password in the request body for submission
Interview question: Who is more secure, GET method or POST
path:
It is the path we want to request resources
Protocol version:
HTTP 0.9, HTTP 1.0, HTTP 1.1 , HTTP 2.0 (still under development)
Currently the most used is HTTP1.1
3.6 Analyze the content of the request body:
The request body can have many key: value
3.7 HTTP response status code
3.7.1 Why do I need a status code?
The browser initiates an HTTP request, and the web server needs to tell the browser a status code (whether the request just initiated was processed successfully or failed)
Common test codes:
3.8 Common HTTP headers
- Content-Length: the length of the text
- Content-Type : the type of the body
- location : Redirect address, usually used with 3 xx
- Set-Cookie: Set Http Cookie
Cookies and Sessions
Cookie: The information saved by the browser is generally some insensitive information of the client. The source of the cookie data comes from the server .
Save in browser. When requesting a resource on the server next time, it will carry
Session: session data is saved on the server side, and generally describes the current session information (for example: browser information, which page the browser visits, etc.)
Cookie:
Application scenario:
Why: When visiting Baidu, you need to log in for the first time , but you don’t need to log in again for the second time
It is because when you log in for the first time, when the login is successful, the Baidu server will return a successful login response to the browser, and there are some cookie information in the response.
When you visit next time, you will carry these cookie information, and the server can know who is logged in after parsing these cookie information.
We can also use code to experiment with the role of cookies:
Our server returns a cookie information to the browser
Of course: the cookie information setting here is very random, and the design of cookies on the website is very complicated