为什么我们需要多部分数据格式的边界?

Why do we need boundaries in multipart data format?

标题说明了一切。 我的意思是假设我们正在尝试上传多张图片, 对于每个多部分部分,我们将有 sub-headers 喜欢

Content-Disposition: form-data; name="file"; filename="mia.jpeg"
Content-Type: image/jpeg
Content-Length: 5379

Content-Length 足以告诉解析器此内容部分何时结束 并开始另一部分。 但我很可能遗漏了一些东西,你能帮忙吗?

Content-Length 不是多部分内容的要求。这个使用长度的问题在 the old RFC:

部分得到解决

5.2 Other data encodings rather than multipart

Various people have suggested using new mime top-level type "aggregate", e.g., aggregate/mixed or a content-transfer-encoding of "packet" to express indeterminate-length binary data, rather than relying on the multipart-style boundaries. While this would be useful, the "multipart" mechanisms are well established, simple to implement on both the sending client and receiving server, and as efficient as other methods of dealing with multiple combinations of binary data.

不过 current one 中没有该文本; length根本没有出现。

如果您认为发送方将流的结果作为多部分 post 的一部分发送,这就特别有意义,因为它可能事先不知道该流数据的长度。如果需要长度,则需要缓存或读取两次。

Why do we need boundaries in multipart data format?

边界是分隔符,旨在允许服务器将消息拆分为块或 body 部分。多部分请求可以包含任意数量的 body 部分。 multipart/form-data 请求当前在 RFC 7578.

中定义

每个部分都包含自己的内容 header(零个或多个 Content- header 字段)和一个 body。同样重要的是,边界定界符不得出现在任何封装部分内。

另一个相关文档是 RFC 2046,它定义了多部分 MIME 数据流:

The body must then contain one or more body parts, each preceded by a boundary delimiter line, and the last one followed by a closing boundary delimiter line. After its boundary delimiter line, each body part then consists of a header area, a blank line, and a body area.