HTTP study notes 1-HTTP Daquan

HTTP protocol

HttpThe protocol is built on tcp/iptop of the protocol and is an application layer protocol. The default port is 80; HTTPno connection and no state.

HTTP message

HTTPIt is a ASCIIcode for transmission and tcp/ipan application layer specification based on the protocol. The specification divides httpthe request into three parts:状态行、头部、主体

Request request

The request is divided into three parts:状态行、头部、主题(可选)

<Method> <request-url> <version>
<Headers>

<entity-body>
  • Method: Request method
  1. GET: Get resources from the server
  2. PUT: Write document
  3. Head: Only get the first part
  4. POST: Send data, such as forms
  5. Delete:delete
  6. Options: View request methods supported by the server
  7. Trace: Message tracking
  • request-url: The address of the requested resource, which urlgenerally contains several parts:协议、主机、端口、URI
  • version: HTTPProtocol version
  • Headers: Request header
  • Body: Request body

Response returns data

The return is divided into three parts:状态行、头部、主题(可选)

<Version> <status> <reason-phrase>
<Headers>

<entity-body>
  • version: Server protocol version
  • status: Return status code. Each status code has a different meaning
  1. 200: The request was successful
  2. 301: Permanent redirect
  3. 302: Temporary redirect
  4. 304: The resource is not modified
  5. 400: Request error
  6. 401:not certified
  7. 403:Permission denied
  8. 404: No such resource
  9. 500: Internal server error
  10. 502: The gateway or proxy received an invalid response from the server
  11. 504: Gateway or proxy timeout
  • reason-phrase: Reason phrase. Such as: timeoutwait
  • headers: Return to the head
  • entity-body: Back to main body/content

note:

  • 4xxThe status code is generally a client error
  • 5xxThe status code is generally a server error

HTTP long link

HTTPWith 请求<——>应答mode, when using non- keep-alivemode, each request, the client and server must establish a new connection, and disconnect immediately after completion ( stateless ).

When using the keep-alivemode, keep-alivethe connection at both ends is valid for a long time. After the request is completed, the connection will not be disconnected immediately. When C has a subsequent request, the connection can be used directly without the need for three-way handshake. Reduce overhead.

HTTP version 1.0

  • In the http 1.0version, you need to display the specified header to create a long link.
  • Header: Connection: keep-alive(both client and server need to be specified)

HTTP 1.1 version

  • In the http 1.1version, the default is a long link, and no additional header is required. But if you don't want to keep a long link, you can specify the header to be closed.
  • head:Connection: close

note:

  • Most browsers currently are HTTP 1.1versions
  • HTTPThe keep-alivesimple way is to keep the current tcpconnection
  • It is impossible to maintain a long link all the time, such as:, keep-alive: timeout=5,max=100which means that this tcpconnection channel can be maintained 5sand can receive 100次requests at most . Then it will be disconnected
  • HTTPIt is a stateless protocol, and keep-alivethere is no guarantee that the connection between C and S is active, HTTP 1.1and so is the version. The only guarantee is that you will be notified when the current connection is closed. So, don’t let the program rely on keep-alivethe keep-alive feature, otherwise there will be unexpected hidden dangers
  • After using the long link, how does C or S know that this transmission is over?
  1. Determine whether the data has reached the Content-Lengthsize
  2. Use chunkedcodes to make judgments. The dynamically generated file has no Content-Lengthheader, it is transmitted in blocks, and the chunkedencoded data has an empty chunkedblock at the end , which indicates the end of this transmission. But HTTP 2.0this feature is not used in the version, it HTTP 2.0comes with streaming

Transfer-Encoding header

Transfer-EncodingIt is a HTTPheader used to mark the message transmission format. Although in theory there can be many values, there HTTP规范is actually only one value defined in the current one——chunked

If HTTPthe Transfer-Encodingvalue of a message is chunked, then the message body is composed of an undetermined number of blocks and ends with the last 0block of size

Each non-empty block starts with the number of bytes of data contained in the block (hexadecimal), followed by one CRLF(carriage return and line feed), and then the data itself.

note:

  • chunkedTransmission cannot know the length of the content in advance, and can only be judged by the last empty block, so for download requests, there is no way to see the download progress
  • chunkedThe advantage is that the server can send the content while generating
  • HTTP 2.0The version is not supported chunkedbecause it HTTP 2.0has its own streamingtransmission method

HTTP cross-domain

Cross-Origin Resource Sharing ( CORS) is a mechanism that uses extra http头部to tell the browser, allowing a run orginon webapplication be granted access without the specified resource from the source server.

When a resource 不同的域、协议、端口requests a resource from the server where the resource itself is located , the resource initiates a跨域HTTP请求

Simple cross-
domain requirements for simple cross-domain requests:

  • The request method is 3one of the following
  1. HEAD
  2. GET
  3. POST
  • HTTPThe header information does not exceed the following fields
  1. Accept
  2. Accept-Language
  3. Content-Language
  4. Last-Event-ID
  5. Content-Type: Limited to three values application/x-www-form-urlencoded、text/plain、mutiport/form-data

Complex cross-domain requests
Load cross-domain requests will generate two httprequests, one pre-check request ( options), one cross-domain request; the server needs to do cross-domain support for both requests

Pre-check request: mainly check in advance whether the server supports the request method and header

How to handle cross-domain requests?
Divided into the following three situations:

  • The server does not support cross-domain
  1. Server add cross-domain relatedHeader
  • The client brings a header beyond the cross-domain allowed
  1. Client removes redundant headers
  2. Server plus redundant header support
  • Cross-domain is not allowed due to caching, which is common in access to static resources
  1. Ignore the cache, add a timestamp to each file in the previous paragraph, and directly access the source
  2. If it is caused by the browser's local cache, check the front-end code (whether there is a 2request)
  3. All the caches outside the browser that caused the problem, only checked one by one

Cross-domain related Header

head Description
Access-Control-Allow-Origin: | * Allow requests for specified domains
Access-Control-Allow-Methods: Allowed request methods
Access-Control-Allow-Headers:
Allowed head
Access-Control-Allow-credentials: true Allow cookies

The difference between MTU and MSS

MTU: Maximum Transmit Unit, The maximum transmission unit, that is, the maximum size of data that the physical interface ( 数据链路层) provides to the upper layer ( IP层) at one time; taking the commonly used Ethernet as an example, by default MTU=1500Byte, this is IPthe restriction of the Ethernet interface on the layer.

If the IPlayer <= 1500 Byteof data needs to be sent, only one IP包can complete the task. If there is > 1500 Bytedata to be transmitted, to complete transmission to be fragmented, but all fragments having a characteristic -IP Header ID相同

MSS:Maximum Segment Size, The maximum segment size. Fabric comprises tcp headerand tcp options, MSSare tcpused to limit the maximum number of bytes sent by the application layer. If MTU = 1500thenMSS = 1500 - 20(tcp header) - 20(tcp options) = 1460

Insert picture description here


to sum up

This article introduces the HTTPrequest, response, header, encoding, cross-domain, long link, MSS, MTU, etc. of the protocol. Experienced developers should know that HTTPmany functions in the layer, such as long links, caching, coding, etc., are achieved through HEADER, so Headerthey HTTPhave assumed a very important role in it.

Header Header Header Header Header Header Header Header Header Header Header

Guess you like

Origin blog.csdn.net/Free_time_/article/details/107449776