Detailed HTTP message

Time:2019-10-21

abstract

As a web developer, he uses HTTP protocol every day, but he always knows nothing about it. In this paper, refer to the HTTP rfc7230 specification, and sort out the HTTP message part.

HTTP message composition

Start line: start line, which describes the basic information of the request or response
*(header field CRLF): header
CRLF
[message body]: message body, data actually transmitted

Start line

The format of the starting line is
Start line = request line (response start line) status line

在这里插入图片描述

Header head在这里插入图片描述

These formats are rules for parsing.

order
Theoretically, the key order of the header fields doesn’t matter, but the best practice is to put the control fields in front, such as the host when the request is made and the date of the response, so as to find out whether it needs to be processed as soon as possible.

repeat
exceptSet-CookieIf the sender sends a duplicate key, the receiver will merge it. The values are separated by commas.

Field restriction
The protocol itself has no restrictions on each header field, but in the engineering practice, some practices have been obtained, there is no general restrictions, which is related to the specific semantics of the field. There is no standard value defined for the overall header size limit, some 4K, some 8K. The server side checks that the header header exceeds the limit value and is under security consideration and will not be ignored. It will throw 4xx error.

onlyHostFields are required in the request header. It doesn’t matter.

field Request header Response head explain
Host 1 0 Tell the server which host should handle it
User-Agent 1 0 Although the browser type has been used poorly and is not trusted, it can be used to customize the type sometimes.
Accept 1 0 The body type MIME type that can be received, such as text / HTML
Accept-Charset 1 0 Character set that can be received
Accept-Encoding 1 0 Acceptable encoding format
Accept-Language 1 0 Multilingual reception
Content-Type 1 1 Sent body type MIME type
Content-Encoding 1 1 Code sent
Content-Language 1 1 Language sent

There are complete categories here
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers

body

There must be a header, but the body does not have to use it.

Body is the content of the transmission. Because HTTP is an application layer protocol, in addition to data transmission, you need to define the data format for transmission. These format definitions are specified in the header.Content-LengthThe body length of the request or response must be accompanied by this field so that the other party can easily distinguish the message boundary, that is, when the body data ends. If the body is too large, you need to calculate and transfer at the same time. You can’t know the size of the whole body until the end of the final calculation. At this time, you can use chunk transfer.Transfer-EncodingSpecifies that the two header keys are mutually exclusive, and only one can be specified. If two are specified, the receiver takes precedence.Transfer-EncodingField. Generally, when there is a large amount of body data, chunks are used for transmission, which is more efficient. Without length, how can we know that the data transmission is over? Through a chunk with a length of 0, the corresponding block data has no content, which means that the body content is over.
在这里插入图片描述

What did jetty do?

Jetty is a web container. It needs to parse HTTP request and send HTTP response. What did you do? Next analysis

Pay attention to the public account [Abbot’s temple], receive the update of the article in the first time, and start the road of technical cultivation together with the Abbot
在这里插入图片描述

Reference resources

https://tools.ietf.org/pdf/rfc7230.pdf
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers

Recommended Today

CSS margin overlap and its prevention

The vertical adjacent boundaries of two or more block level boxes coincide. The resulting boundary width is the largest of the adjacent boundary widths. If a negative boundary appears, the negative boundary with the largest absolute value is subtracted from the maximum positive boundary. If there is no positive boundary, the negative boundary with the […]