如何从 std::string 构造 boost::beast::http::message?

安德烈 K。

是,可以构建boost::beast::http::message(具体我必须构造一个boost::beast::http::response<bb_http::string_body>)从std::stringstd::string_view或其它原始缓冲区?

也许有某种解析器?从我在 Boost.Beast 样本中看到的,我们可以:

  1. 接收boost::beast::read*函数的响应在这种情况下,第一个参数应该是 a SyncReadStream,它必须符合SyncReadStreamfrom 的合同boost/beast/core/type_traits.hpp
struct is_sync_read_stream<T, detail::void_t<decltype(
    std::declval<std::size_t&>() = std::declval<T>().read_some(
        std::declval<detail::MutableBufferSequence>()),
    std::declval<std::size_t&>() = std::declval<T>().read_some(
        std::declval<detail::MutableBufferSequence>(),
        std::declval<boost::system::error_code&>()),
            (void)0)>> : std::true_type {};
  1. 或者像这样手工构建它 http::request<http::string_body> req{http::verb::get, target, version};

您可以手动调用解析器,例如使用这个简单的骨架函数:

http::response<http::string_body> do_parse(std::string_view input)
{
    beast::error_code ec;
    http::response_parser<http::string_body> p;

    // read headers
    auto buf = boost::asio::buffer(sample);
    auto n = p.put(buf, ec);
    assert(p.is_header_done());

    // read body
    if (!ec) {
        buf += n;
        n = p.put(buf, ec);
        p.put_eof(ec);
    }
    if (ec)
        throw boost::system::system_error(ec);
    assert(p.is_done());

    return p.release();
}

这假设输入是一个单独的完整请求。

现场演示

住在 Coliru

#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <string_view>
#include <iostream>
#include <iomanip>
namespace beast = boost::beast;
namespace http = beast::http;

http::response<http::string_body> do_parse(std::string_view input)
{
    beast::error_code ec;
    http::response_parser<http::string_body> p;

    // read headers
    auto buf = boost::asio::buffer(input);
    auto n   = p.put(buf, ec);
    assert(p.is_header_done());

    // read body
    if (!ec) {
        buf += n;
        n = p.put(buf, ec);
        p.put_eof(ec);
    }
    if (ec)
        throw boost::system::system_error(ec);
    assert(p.is_done());

    return p.release();
}

int main() {
    auto res = do_parse(
        "HTTP/1.1 200 OK\r\n"
        "Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"
        "Server: Apache/2.2.8 (Ubuntu) mod_ssl/2.2.8 OpenSSL/0.9.8g\r\n"
        "Last-Modified: Sun, 26 Sep 2010 22:04:35 GMT\r\n"
        "ETag: 45b6-834-49130cc1182c0\r\n"
        "Accept-Ranges: bytes\r\n"
        "Content-Length: 12\r\n"
        "Connection: close\r\n"
        "Content-Type: text/html\r\n"
        "\r\n"
        "Hello world!");
    std::cout << res << '\n';
    std::cout << "====== body:\n" << std::quoted(res.body()) << "\n";
}

印刷

HTTP/1.1 200 OK
Date: Sun, 10 Oct 2010 23:26:07 GMT
Server: Apache/2.2.8 (Ubuntu) mod_ssl/2.2.8 OpenSSL/0.9.8g
Last-Modified: Sun, 26 Sep 2010 22:04:35 GMT
ETag: 45b6-834-49130cc1182c0
Accept-Ranges: bytes
Content-Length: 12
Connection: close
Content-Type: text/html
Hello world!
====== body:
"Hello world!"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在boost-beast http请求中设置http标头?

使用boost :: beast的流HTTP的异步处理

使用 boost::beast 处理大型 http 响应

如何从std :: vector <char>构造std :: string?

如何使用boost / beast从HTTP POST请求中解析和提取有效载荷?

Boost Python 2:使用`std::string &`的构造函数

无法从Boost.Spirit中的占位符构造std :: string

boost :: beast同步HTTP客户端超时

Boost.beast http :: read()返回“错误版本”

包含 boost/beast/http.hpp 时出现多个错误

boost.beast : HTTP/1.1 400 错误请求

为什么Boost Variant对于boost :: beast :: websocket :: stream使用模板构造函数而不是move构造函数?

如何从CMake访问Boost 1.66和1.67上的Beast

如何在Boost Beast WebSocket中传递模型类型

如何在boost :: beast中获得async_read()的结果

如何使用boost :: beast连续流式传输文件

std :: string构造函数如何处理固定大小的char []?

如何从对象的数据成员构造std :: set或Boost flat_set?

如何通过Boost Spirit提取std :: string对象

如何使用带有 Boost.Python 的 std::map 或 std::vector 参数的构造函数包装 C++ 类?

如何使用Beast C ++库从HTTP重定向到HTTPS?

从http流(在boost :: beast中)进行读写有什么要求?

通过SSL(HTTPS)使用Boost-Beast(Asio)http客户端

C ++ Boost 1.66使用Beast http请求解析器解析字符串

如何解决采用std :: string和std :: vector的构造函数之间的歧义

带Boost :: Beast的SSL隧道

如何使用带有std :: vector <token_type>而不是std :: string的boost :: spirit :: qi

如何boost :: serialize std / boost :: optional?

如何从(binary == true)boost :: beast :: websocket :: stream <tcp :: socket>读到缓冲区(boost :: beast :: flat_buffer?)中,这样它就不会转义?