使用boost :: beast解码Ntrip 1.0协议的最佳方法是什么?

时间:2018-06-07 01:29:53

标签: httpclient boost-beast

我目前正在使用boost::beast来实现ntrip 1.0客户端。它有如下要求:

  

GET / BUCU1 HTTP / 1.0用户代理:NTRIP GNSSInternetRadio / 1.2.0   授权:基本aHVnb2JlbjpodWdvYmVuMTIz

响应如:

  

ICY 200 OK

它有非标准的http响应。

我作为野兽http客户端示例代码,它在缓冲区中获得此响应。但它在read函数中生成异常。错误是"读取,错误版本"。我想知道处理非标准http响应的最佳方法是什么。

void
    on_write(
        boost::system::error_code ec,
        std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    if (ec)
        return fail(ec, "write");


    // Receive the HTTP response
    // boost::beast::flat_buffer buffer_; // (Must persist between reads)
    // http::response <http::string_body> res_;

    http::async_read(socket_, buffer_, res_,
        std::bind(
            &session::on_read,
            shared_from_this(),
            std::placeholders::_1,
            std::placeholders::_2));
}

void
    on_read(
        boost::system::error_code ec,
        std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    if (ec)
        return fail(ec, "read");         // ex
}

1 个答案:

答案 0 :(得分:0)

这可以使用位于master中的实验目录中的icy_stream类或开发分支(https://github.com/boostorg/beast)来完成:

#include <boost/beast/experimental/http/icy_stream.hpp>
...
using http = boost::beast::http;
using tcp = boost::asio::ip::tcp;
...
boost::asio::io_context ioc;
http::icy_stream<tcp::socket> stream(ioc);
http::response<http::string_body> res;
...
http::read(stream, res);