HTTP protocol
Http
The protocol is built on tcp/ip
top of the protocol and is an application layer protocol. The default port is 80
; HTTP
no connection and no state.
HTTP message
HTTP
It is a ASCII
code for transmission and tcp/ip
an application layer specification based on the protocol. The specification divides http
the request into three parts:状态行、头部、主体
Request request
The request is divided into three parts:状态行、头部、主题(可选)
<Method> <request-url> <version>
<Headers>
<entity-body>
Method
: Request method
GET
: Get resources from the serverPUT
: Write documentHead
: Only get the first partPOST
: Send data, such as formsDelete
:deleteOptions
: View request methods supported by the serverTrace
: Message tracking
request-url
: The address of the requested resource, whichurl
generally contains several parts:协议、主机、端口、URI
version
:HTTP
Protocol versionHeaders
: Request headerBody
: Request body
Response returns data
The return is divided into three parts:状态行、头部、主题(可选)
<Version> <status> <reason-phrase>
<Headers>
<entity-body>
version
: Server protocol versionstatus
: Return status code. Each status code has a different meaning
200
: The request was successful301
: Permanent redirect302
: Temporary redirect304
: The resource is not modified400
: Request error401
:not certified403
:Permission denied404
: No such resource500
: Internal server error502
: The gateway or proxy received an invalid response from the server504:
Gateway or proxy timeout
reason-phrase
: Reason phrase. Such as:timeout
waitheaders
: Return to the headentity-body
: Back to main body/content
note:
4xx
The status code is generally a client error5xx
The status code is generally a server error
HTTP long link
HTTP
With 请求<——>应答
mode, when using non- keep-alive
mode, each request, the client and server must establish a new connection, and disconnect immediately after completion ( stateless ).
When using the keep-alive
mode, keep-alive
the 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.0
version, 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.1
version, 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.1
versions HTTP
Thekeep-alive
simple way is to keep the currenttcp
connection- It is impossible to maintain a long link all the time, such as:,
keep-alive: timeout=5,max=100
which means that thistcp
connection channel can be maintained5s
and can receive100次
requests at most . Then it will be disconnected HTTP
It is a stateless protocol, andkeep-alive
there is no guarantee that the connection between C and S is active,HTTP 1.1
and 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 onkeep-alive
the 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?
- Determine whether the data has reached the
Content-Length
size - Use
chunked
codes to make judgments. The dynamically generated file has noContent-Length
header, it is transmitted in blocks, and thechunked
encoded data has an emptychunked
block at the end , which indicates the end of this transmission. ButHTTP 2.0
this feature is not used in the version, itHTTP 2.0
comes with streaming
Transfer-Encoding header
Transfer-Encoding
It is a HTTP
header 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 HTTP
the Transfer-Encoding
value of a message is chunked
, then the message body is composed of an undetermined number of blocks and ends with the last 0
block 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:
chunked
Transmission 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 progresschunked
The advantage is that the server can send the content while generatingHTTP 2.0
The version is not supportedchunked
because itHTTP 2.0
has its ownstreaming
transmission method
HTTP cross-domain
Cross-Origin Resource Sharing ( CORS
) is a mechanism that uses extra http头部
to tell the browser, allowing a run orgin
on web
application 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
3
one of the following
HEAD
GET
POST
HTTP
The header information does not exceed the following fields
Accept
Accept-Language
Content-Language
Last-Event-ID
Content-Type
: Limited to three valuesapplication/x-www-form-urlencoded、text/plain、mutiport/form-data
Complex cross-domain requests
Load cross-domain requests will generate two http
requests, 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
- Server add cross-domain related
Header
- The client brings a header beyond the cross-domain allowed
- Client removes redundant headers
- Server plus redundant header support
- Cross-domain is not allowed due to caching, which is common in access to static resources
- Ignore the cache, add a timestamp to each file in the previous paragraph, and directly access the source
- If it is caused by the browser's local cache, check the front-end code (whether there is a
2
request) - 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 IP
the restriction of the Ethernet interface on the layer.
If the IP
layer <= 1500 Byte
of data needs to be sent, only one IP包
can complete the task. If there is > 1500 Byte
data 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 header
and tcp options
, MSS
are tcp
used to limit the maximum number of bytes sent by the application layer. If MTU = 1500
thenMSS = 1500 - 20(tcp header) - 20(tcp options) = 1460
to sum up
This article introduces the HTTP
request, response, header, encoding, cross-domain, long link, MSS, MTU, etc. of the protocol. Experienced developers should know that HTTP
many functions in the layer, such as long links, caching, coding, etc., are achieved through HEADER
, so Header
they HTTP
have assumed a very important role in it.
Header Header Header Header Header Header Header Header Header Header Header