3. Application layer (emphasis) 26http

Application layer: Data communication between applications .

Because the application program is written by the programmer himself, the agreement between the application programs is defined by the programmer himself.

Typical protocols in the tcp/ip protocol stack

1. Custom protocol (HTTP)

1. Definition

Agree on a data format between applications by yourself.

2. How to customize

Structural binary serialization: use structs to organize data formats (cross-platform-byte alignment)

Other typical serialization methods: json, protobuf (understand)

  • Serialization: According to the specified data format, multiple data objects are organized into binary data strings that can be stored persistently or transmitted
  • Deserialization: parse the binary data string according to the specified format to obtain multiple data objects

Network version computer:

The client sends the two numbers that need to be calculated and an operator to the server, and the server returns the result to the client after the calculation is completed.

Elements to consider:

  1. Organization & Analysis Efficiency
  2. transmission efficiency

2. Typical protocol: HTTP protocol

1. Definition

HTTP: It is actually a tcp server, but the upper layer data format uses the protocol format of http

  • Hypertext Transfer Protocol
  • The application layer protocol uses the TCP protocol at the transport layer
  • Clear text transmission protocol (organize data transmission in a specified format in the form of strings)
  • is a simple request-response protocol

3. Request format (HTTP protocol format)

  1. The first line (request line): request method URL protocol version\r\n
  2. Request header: composed of key-value pairs in the form of key: val\r\n
  3. Blank line: \r\n, is the space between the head and the body
  4. Body: Data submitted to the server

1. First line

Request method: Visual representation of the request type

GET: It is mainly used to obtain entity resources from the server. There is no body, and data can be submitted, but the submitted data is placed in the URL, which is not safe and has length restrictions.

POST: It is mainly used to submit data to the server, and the data is placed in the body.

HEAD: Similar to the GET function, but the response does not require entity data, only the header.

2. Specific format

URL: Uniform Resource Locator - collectively referred to as a web address.

Locate a resource on a host in the network and define how to request it.

Protocol scheme name://username:password@domain name:port/resource path? Query String # Fragment Identifier

http://user:pass@ip:port/part?query_string#id

http: protocol scheme name

user:pass: username and password

www.baidu.com: domain name, server alias (a name that is easy to remember)

port: port (the HTTP protocol uses port 80 by default)

/part: resource path /index.html - relative root directory

3. Query String

query_string: query string, composed of key-value pairs in the form of key = val & key = val, which is the data submitted by the client to the server. Once there are special characters in the submitted data, it will be ambiguous with the spacer in the http protocol, so there cannot be special characters in the query string. Therefore, if there are special strings in the submitted data, URL encoding is required.

  • urlencode: encoding - convert each byte of a special character into a hexadecimal character, such as '+' -> 2B, and the prefix % is used to indicate that this is the transcoded data '+' -> "%2B" .
  • urldecode: When a % character is encountered in the URL, it is considered that the following two characters need to be decoded, and the first hexadecimal character is converted into a number * 16 + the converted data of the second character: %2B ->2*16+11=43

4.id

Fragment identifier, which is a tag id in hypertext data

The location of the id can be located directly to the location of the id when accessing the page

3. Protocol version

0.9——1.0——1.1——2.0

  • 0.9: Not a complete version
  • 1.0: Improve the protocol format mainly for version 0.9, and establish various request methods
  • 1.1: Mainly made some performance optimizations for version 1.0
  • 2.0: Use binary transfer, with more performance optimization

1. Optimization of 1.1

  1. Cache control: Cache management of resources locally or on proxy servers greatly improves resource retrieval efficiency
  2. The connection mode is a long connection (the http protocol is a short connection at the beginning of version 1.0)
  • Short connection: establish a connection, send a request, receive a response, close the connection, and end the communication
  • Long connection: In one connection, multiple requests-responses can be made
  • Pipelining: Do not wait for the last request to receive a response before sending the next request, you can make continuous requests
  • Multiplexing & active push: multiple resources can be pushed in the response of one request, and continuous requests can be made, with corresponding request identifiers in the response, and no sequential response is required - solving queue head congestion and avoiding repeated headers transmission of fields

4. Header field

Request header: Accept, Referer, Cookie

Body header: Content-Length, Content-Type

Response header:

General header: Contention: keep-alive, close

5. Empty line

\r\n and \r\n at the end of the last header field form a continuous \r\n\r\n, forming the end of the header.

When the http client or server receives data, it takes this as a sign to first take out the complete header information and then parse the header to get each field, determine the length of the body according to the Content-Length in the header field, and then take out the body of the specified length , this request or response ends.

4. Response format

  1. The first line: protocol version, response status code, status code description, visual representation of processing results
  2. Header: key: val\r\n key-value pair, some descriptions of the response and body
  3. Empty line: \r\nSpace header and body
  4. Protocol version: 0.9/1.0/1.1/2.0
  5. Response status code: Intuitive representation of processing results

2. Header field: key: val\r\n

1. Cookie mechanism

Location: save the new link, and use 3xx to achieve redirection

Response header: Set-Cookie

Request header: Cookie

Together, they implement the HTTP cookie mechanism

  1. Initiate a user login request, and the server performs login verification. After successful login, the user name and status information are sent to the client as the value of the Set-Cookie field
  2. The client browser will save the data in Set-Cookie to the Cookie file
  3. When the client requests the specified server next time, the Cookie content is read from the Cookie file, and then sent to the server through the Cookie field

Cookie mechanism: the client saves some cookie information returned by the server and sends it to the server when it requests the server next time, so as to maintain the communication status of the client.

2. Hidden dangers of Cookie mechanism (session session management)

Because the client's private information is constantly being transmitted.

session session management :

After the client logs in successfully, the server will create a session for each client, which contains various information of the client, and then save the session session information in the server's database, and send the session ID as the cookie content to the client.

When the client requests the server next time, it will send the session ID to the server, and the server searches for session information through the session ID, and then obtains various status information of the client.

3. The difference between Cookie and session

  • Cookies store information locally on the client, and send the information to the server each time a request is made, which has certain hidden dangers.
  • The session is to save the client status information on the server, and the session_id is used as a cookie to transmit during communication, which has high security.

4. token (understand)

5. Response status code

1xx: Description of some protocol switching negotiations; 101-protocol switching

2xx: request successfully processed; 200-success

3xx: Redirection - when the URL of a resource is changed, but the original link is still saved and can still be used, it is used in conjunction with the Location header field to return the new URL link; 301-permanent & 302-temporary

4xx: Indicates client error; 404&400

5xx: indicates a server error; 500&502-failure&504-timeout

Status code description: A simple text description of the status code.

Guess you like

Origin blog.csdn.net/weixin_56316833/article/details/131745545