Get the App
SLTechnology News&Howtos  ›  Servers  › 

How http requests determine boundaries

Shulou Source: shulou.com Published: 2022-05-31 12:58:07 10月05日 Update

This article introduces you how to determine the boundary of http request, the content is very detailed, interested friends can refer to it, I hope it can help you.

S3 upload has two methods PUT request: this upload request upload object protocol explicitly carries Content-length;POST request: this does not require a well-known Content-length, but through a streaming data transmission, but always want to know where the boundary is? There are several ways to let the server know the boundaries of the data; how does HTTP data determine boundaries?

S3 is an HTTP-based protocol, HTTP is an application layer protocol based on TCP, and TCP is a data stream-oriented protocol that has no boundaries. HTTP as an application layer protocol requires its own explicit definition of data boundaries.

HTTP boundary determination HTTP can be a keep-alive, can be a streaming protocol since the HTTP 1.1 protocol. So we need a way to identify body boundaries in three ways:

HTTP packet header explicitly sets Content-LengthHTTP transmission encoding method with Transfer-Encoding: chunked short connection (connection disconnection)(the third case, generally regarded as an abnormal scenario. So below we discuss the first two cases)

Both cases depend on whether the client's protocol is followed. Normally, if Content-Length is transmitted, it should be consistent with body. If the header does not have this field, then the client can also use Transfer-Encoding: chunked to transmit the body, which also allows the server to correctly identify the boundaries of the body.

Discussion in four cases

Case 1: What if only Content-Length is set, but body is not allowed?

Content-Length greater than body? Server implementations typically set a timeout. The server actively disconnects and reports timeout. This current online system is done by nginx, 1 minute timeout. The client itself ctrl+c, the client actively disconnects the service is generally the card owner, and the server waits to read the data declared by the client. There are two solutions: Content-Length is smaller than body? Request successful, data truncation

Case 2: If there is no Content-Length, Transfer-Encoding: Chunked is set.

At this time, the boundary can be correctly identified, subject to Chunked mode. Content-Length does not make a difference. Even if Content-Length is set, it does not depend on this value. It does not matter if it is not allowed. As long as it follows the chunked pattern, the actual body taken shall prevail.

Case 3: Content-Length and Transfer-Encoding are both set?

subject to chunked transmission.

Case 4: Content-Length and Transfer-Encoding are not set?

This is the same as Content-Length=0.

Screenshot example Chunked's package

Content-Length package

How to determine the boundary of http request is shared here. I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Tags: Boundaries situations data services customers methods transports clients servers initiative content application layer more patterns coding help applications good well-known consistent Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Huawei MariaDB OPPO Reno