HTTP Packet Composition

reference

HTTP packet: consists of request packets and response packets

Request message: consists of four parts: request line, request header, blank line and request body

Response message: consists of status line, response header, blank line, and response body

  • Request line: contains HTTP method, request address, HTTP protocol, and version
  • Request header/response header: is a set of key: values that tell the server what I want, what type to pay attention to, etc
  • Empty line: used to distinguish headers from entities, because the request headers are of the key:value format. When a blank line is parsed, the server knows that the next line is no longer the request header and should be parsed as the request body
  • Request body: Request parameters
  • Status line: contains HTTP protocol and version, digital status code, and English name of status code
  • Response body: data returned by the server

HTTP request methods (9)

HTTP1.0: GET, POST, HEAD

HTTP1.1: PUT, PATCH, DELETE, OPTIONS, TRACE, CONNECT

methods describe
GET Access to resources
POST Transferring resources usually results in changes to the server resources
HEAD Get message header
PUT Update the resource
PATCH Supplement to PUT, partial update to known resourcesrookie
DELETE Delete the resource
OPTIONS Lists the request methods supported by the request resource for cross-domain requests
TRACE Trace request/response paths for testing or diagnostics
CONNECT Pipe the connection to the proxy server (Tunnel proxyMore on that.)

GET and POST

  • GETIs harmless when the browser falls back, whilePOSTThe request will be made again
  • GETRequests are actively cached by the browser, whilePOSTNo, unless you set it manually
  • GETRequest parameters are retained in browser history, whilePOSTParameters in are not retained
  • GETRequest inURLThe parameters passed by the browser have length limits (browser limits vary in size), whilePOSTThere is no limit to
  • GETParameters throughURLPass,POSTOn theRequest bodyIn the
  • GETThere is noPOSTSafe becauseGETRequest parameters are directly exposed inURLSo it can’t be used to transmit sensitive information
  • GETThe request can only proceedURLCoding, andPOSTSupports multiple encoding modes
  • For the data type of the parameter,GETWe only acceptASCIICharacters, andPOSTThere is no limit to
  • GETGenerates a TCP packet,POSTGenerates two packets (Firefox only sends them once). GET The browser sends the HTTP header and data with a response of 200 successful, POST sends the header with a response of 100 continue, and data with a response of 200 successful

What is a persistent/long connection

The http1.0 protocol uses the “request-reply” mode, each request/reply client and server to create a new connection, immediately after completion of the connection (HTTP protocol is connection-free protocol).

Http1.1 supports long connections, i.e., adding a Connection to the request header: Keep-alive: After a TCP connection is established in keep-alive mode, the connection between the client and the server remains valid. Multiple HTTP requests and responses can be sent or received. When a subsequent request to the server occurs, The keep-alive feature avoids establishing or re-establishing connections

Advantages and disadvantages of long connection

advantages

  • Reduce CPU and memory usageBecause you don’t have to make and close connections as often
  • Support pipeliningRequest and response patterns for
  • Reduce network congestionBecause TCP requests are reduced
  • Reduced response time for subsequent requestsBecause there is no need to wait to establish TCP, shake hands, wave, and close TCP
  • When an error occurs, alsoError prompt can be made without closing the connection

disadvantages

After a long connection is established, it is a waste of resources for the server to keep the connection, and the length of the long connection time directly affects the number of concurrent servers

There is also the possibility of queue congestion (explained below), resulting in information delays

How to avoid long connection resource waste?

  • Client request header declaration:Connection: closeAfter this communication, the connection will be closed
  • Server Configuration: Such as Nginx, Settingskeepalive_timeoutSet the long connection timeout period.keepalive_requestsSet the maximum number of persistent connection requests

What is pipelining

Http1.1 in the case of long connections, after establishing a connection channel, the message passing over the connection is similar to

Request 1 -> Response 1 -> Request 2 -> Response 2 -> Request 3 -> Response 3

The message for managing the connection becomes something like this

Request 1 -> Request 2 -> Request 3 -> Response 1 -> Response 2 -> Response 3

Pipelines are in the same TCP connection after sending a request can not need to wait for it to continue to send out the request, it can reduce the response time of the whole, but the server will respond to the request in accordance with the order of the request, so if there are many requests, but in front of the request and response is slow, is a well-known problem team head congestion solution (below)

Resolve HTTP queue header blocking

Http1.0 protocol USES a request/reply mode, the message must be sent a, is formed a serial fifo queue, no priorities, the priority in the order of only a team of the first row in front of the request processing, the result is if the team’s first request takes too long, the back of the request is only in the blocking state, This is known as the queue head blocking problem. The solution is as follows:

Concurrent connections

Domain name subdivision

Forward and reverse proxies

Forward agent

The agent working on the client is the forward agent.

To use forward proxies, you need to configure a proxy server on the client. Forward proxies are transparent to the server.

Packet capture tools such as Fiddler, Charles, and proxy tools for accessing some Internet sites are all forward proxies

Forward proxies are commonly used for

  • The cache
  • Block certain unhealthy websites
  • Access an otherwise inaccessible web site by proxy
  • Internet access authentication: authorizes user access

The reverse proxy

The proxy that works on the server side is called a reverse proxy. You do not need to configure a reverse proxy on the client. The reverse proxy is transparent to the client. Nginx, for example, is a reverse proxy

Reverse proxies are used for load balancing, server caching, traffic isolation, and logging