boost read_until不在分隔符处停止 [英] boost read_until does not stop at delimiter

查看:169
本文介绍了boost read_until不在分隔符处停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用boost read_until函数来方便通过套接字接收和解析HTTP消息。所以我要尝试要做的是read_until从套接字直到 \r\\\
,我认为应该给我一行HTTP头。 (每个HTTP标题行以 \r\\\
结尾)。但是,我实际上从read_line获取的是整个标题,几行长。 (标题以 \r\\\
\r\\\
结尾,换句话说,空白行),根据HTTP标准。)这是一个代码段。 sock 是套接字文件描述符。

I'm using the boost read_until function to facilitate receiving and parsing HTTP messages over a socket. So what I'm trying to do is read_until from the socket until \r\n, which I think should give me one line of the HTTP header. (Each HTTP header line ends in \r\n, per the standard.) However, what I'm actually getting from read_line instead is the entire header, several lines long. (The header ends in \r\n\r\n, or in other words, a blank line. Also, per the HTTP standard.) Here's a code snippet. sock is the socket file descriptor.

boost::system::error_code err;
io::streambuf request_buff;

io::read_until(sock, request_buff, "\r\n", err); // read request line
if (err)
  throw Exception(string("Failed to read HTTP header request line from socket: ") + err.message());
cerr << "Read " << request_buff.size() << " bytes." << endl;

istream request(&request_buff);
try {
  request >> m_strMethod >> m_strPath >> m_strHttpVersion;

} catch (std::exception& e) {
  throw Exception(string("Failed to parse HTTP header: ") + e.what(), e);
}

if (!request)
  throw Exception("Failed to read HTTP header");
if (!alg::istarts_with(m_strHttpVersion, "HTTP/"))
  throw Exception(string("Malformed HTTP header: expected HTTP version but got: ") + m_strHttpVersion);

string strTemp;
while (std::getline(request, strTemp))
{
  cerr << "Extra line size = " << strTemp.size() << endl;
  cerr << "Extra line: '" << strTemp << '\'' << endl;
}

我期望看到的是输出指示它读取的字节数HTTP消息的第一行,没有Extra输出。我得到的是整个HTTP头中的字节数,和一个空白的额外行(这可能是因为>>操作没有消耗换行在第一行的结尾),后面的每隔一行标题,以及另一空白行(其指示标题的结尾,如上所述)。为什么read_until从套接字读取比标题的第一行更多,并将其放入request_buff?

What I expect to see is output indicating it read the number of bytes in the first line of the HTTP message and no "Extra" output. What I get instead is the number of bytes in the entire HTTP header, and a blank extra line (which maybe is because the >> operations didn't consume the newline at the end of the first line) followed by every other line in the header, and another blank line (which indicates the end of the header, as noted above). Why is read_until reading more from the socket than the first line of the header and putting it into request_buff?

注意,我使用netcat接收请求, 。因此,HTTP消息本身似乎格式正确。

Note, I used netcat to receive the request and it's coming through okay. So the HTTP message itself appears to be correctly formatted.

推荐答案

文档可能似乎意味着:


此函数用于将数据读入指定的streambuf
,直到 streambuf的get区域包含指定的分隔符。

"This function is used to read data into the specified streambuf until the streambuf's get area contains the specified delimiter."

但看起来更近:


streambuf的获取区域包含 ...

因此,它不承诺停止。它只是承诺在读取包含您的分隔符的块时立即返回给您。

So, it doesn't promise to stop there. It just promises to return to you as soon as it read the block that contains your delimiter.

这篇关于boost read_until不在分隔符处停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆