[英]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上传一个二进制文件时,在文件末尾添加了两个额外的字符0D
和0A
。
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体中提取二进制文件内容?
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.