Git Product home page Git Product logo

Comments (6)

vinniefalco avatar vinniefalco commented on May 2, 2024

I'm not sure I understand the question. Are you saying that you have a boost::asio::streambuf (or beast::streambuf) and you want to parse the HTTP message? Yes, Beast can do this:

using namespace beast;

template<class Body, class ConstBufferSequence>
response_v1<Body, headers>
do_parse(ConstBufferSequence const& cb, error_code &ec)
{
    error_code ec;
    parser_v1<false, Body, headers> p;
    p.write(cb, ec);
    return p;
}

void parse_streambuf()
{
    error_code ec;
    boost::asio::streambuf sb;
    ...
    std::cout << do_parse(sb.data(), ec);
}

from beast.

11nk avatar 11nk commented on May 2, 2024

Sorry, my english is poor . I want to say from the example i don't known how to get the response headers and body directly .

from beast.

11nk avatar 11nk commented on May 2, 2024

Is beast::streambuf compatible with boost::asio::streambuf
I cannot use boost::asio::buffer_cast<const char *>() to convert a streambuf to a raw pointer

from beast.

vinniefalco avatar vinniefalco commented on May 2, 2024

Given

beast::http::response_v1<beast::http::string_body, beast::http::headers> res;

You can access the headers using res.headers. For example res.headers["Server"] will return a string.

The body can be accessed with res.body, which in this example will be of type std::string.

beast::streambuf has the same interface as boost::asio::streambuf. You cannot use buffer_cast on a streambuf or on streambuf::data. If you want to convert the entire streambuf to a pointer you need to loop over all the buffers and append them together. The function beast::to_string does this, you can use that function or you can write your own. Example:

beast::streambuf sb;
...
std::cout << beast::to_string(sb.data());

This is the implementation of to_string:
https://github.com/vinniefalco/Beast/blob/master/include/beast/core/to_string.hpp#L38

Hope this helps!

from beast.

11nk avatar 11nk commented on May 2, 2024

Thanks .

    boost::asio::ip::tcp::socket sock(ios);
    beast::http::request_v1<beast::http::empty_body> req;
    req.method = "GET";
    req.url = "/";
    req.version = 11;
    req.headers.replace("Host", "www.boost.org");
    req.headers.replace("User-Agent", "Beast");
    beast::http::prepare(req);
    beast::http::write(sock, req);

    std::string sb;
    beast::http::response_v1<beast::http::string_body> resp;
    beast::http::read(sock, sb, resp);

The code use VC2015 UP3 compile failed with
error C2338: DynamicBuffer requirements not met

from beast.

vinniefalco avatar vinniefalco commented on May 2, 2024

The second argument to http::read must meet the requirements of DynamicBuffer (http://vinniefalco.github.io/beast/beast/types/DynamicBuffer.html). Both boost::asio::streambuf and beast::streambuf meet these requirements. This should fix your code:

    beast::streambuf sb;
    beast::http::response_v1<beast::http::string_body> resp;
    beast::http::read(sock, sb, resp);

The body of the message will be in resp.body, of type std::string.

Note that if you want to read another response on the same connected socket, you have to re-use the same streambuf.

from beast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.