简体   繁体   English

如何从post请求的HTTP主体中提取二进制文件内容?

[英]How to extract binary file content from HTTP body of post request?

Currently, I try to implement one simple web server with C++ under windows. 目前,我尝试在Windows下使用C ++实现一个简单的Web服务器。 To support file upload, POST is implemented in my codes. 为了支持文件上传, POST在我的代码中实现。

Now, when one binary file is uploaded through IE, two extra characters 0D and 0A is added into the end of file. 现在,当通过IE上传一个二进制文件时,在文件末尾添加了两个额外的字符0D0A

The file content before uploading 上传前的文件内容

在此输入图像描述

The file content after uploading 上传后的文件内容

在此输入图像描述

Here are some codes pieces 这是一些代码片段

// receive data from http socket
char* orig_buf = buf;
while ( (cnt=recv(m_hSocket, buf, 1, 0)) > 0)
{
    if(*buf++ == '\n')
    {
        *buf = '\0';
        buflen = buf - orig_buf;
        return orig_buf;
    }
}

// save buffer to binary file
std::ofstream ofs(szFilename, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
ofs.write(buf, buflen);

Through Fiddler , we find the 0D 0A is added by web browser. 通过Fiddler ,我们发现0D 0A是由网络浏览器添加的。

在此输入图像描述

In this specs , we know the file line ending should be added into before Boundary . 在这个规范中 ,我们知道应该在Boundary之前添加文件行结尾。 However, the first question: what if there is no 0D 0A between file content and Boundary? 但是,第一个问题: 如果文件内容和边界之间没有0D 0A怎么办? Because if the content of binary file including 0D 0A , it is hard to distinguish whether 0D 0A is belong to the binary file or not. 因为如果二进制文件的内容包括0D 0A ,则很难区分0D 0A是否属于二进制文件。

Per @CodeCaster, add header of post message here 按@CodeCaster,在此处添加帖子消息的标题

在此输入图像描述

Question 2: How to extract binary file content from http body? 问题2: 如何从http体中提取二进制文件内容?

  • By Content-Length ? Content-Length or By Boundary ? 还是Boundary with eliminating 0D 0A . 消除0D 0A

what if there is no 0D 0A between file content and Boundary? 如果文件内容和边界之间没有0D 0A怎么办? Because if the content of binary file including 0D 0A, it is hard to distinguish whether 0D 0A is belong to the binary file or not. 因为如果二进制文件的内容包括0D 0A,则很难区分0D 0A是否属于二进制文件。

The user agent has to make sure to pick a boundary that isn't present in the payload. 用户代理必须确保选择有效负载中不存在的边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从POST请求中读取正文 - How to read body from POST request 您如何使用 boost/beast 从 HTTP POST 请求中解析和提取有效负载? - How do you use boost/beast to parse and extract the payload from an HTTP POST request? 如何使用 Arduino 从 http 响应正文中提取相关信息? - How to extract relevant info from the body of http response with Arduino? BOOST ASIO POST HTTP REQUEST — 标题和正文 - BOOST ASIO POST HTTP REQUEST — headers and body 如何从QNetworkReply中读取内容(http响应正文) - How can I read content (http response body) from a QNetworkReply 如何从http请求正文字符串获取文件名? - How to get filename from http request body string? 如何在mFC / C ++中通过HTTP post方法发送Zip(二进制)文件? - how to send Zip(binary) file Through HTTP post method in mFC/C++? 如何发出将发布查询和文件都上传到C ++ Qt中的服务器的http发布请求 - How to make an http post request that uploads both post queries & a file to a server in C++ Qt 在使用带有 SIM7000A 模块和 Arduino 的 HTTP AT 命令发送 POST 请求时,如何包含正文? - How can I include a body while sending a POST request using HTTP AT commands with a SIM7000A module and an Arduino? C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据) - C++: How can I Send binary data(protobuf data) using HTTP post request through a curl call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM