Files
boost_beast/test/http/test_parser.hpp
T

180 lines
3.8 KiB
C++
Raw Normal View History

2016-11-20 07:32:41 -05:00
//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-11-20 07:32:41 -05:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2016-11-20 07:32:41 -05:00
2017-07-20 13:40:34 -07:00
#ifndef BOOST_BEAST_HTTP_TEST_PARSER_HPP
#define BOOST_BEAST_HTTP_TEST_PARSER_HPP
2016-11-20 07:32:41 -05:00
2017-07-20 13:40:34 -07:00
#include <boost/beast/http/basic_parser.hpp>
#include <boost/beast/test/fail_counter.hpp>
2017-07-02 06:38:10 -07:00
#include <string>
#include <unordered_map>
2016-11-20 07:32:41 -05:00
2017-07-20 13:40:34 -07:00
namespace boost {
2016-11-20 07:32:41 -05:00
namespace beast {
namespace http {
template<bool isRequest>
class test_parser
: public basic_parser<isRequest, test_parser<isRequest>>
2016-11-20 07:32:41 -05:00
{
test::fail_counter* fc_ = nullptr;
public:
using mutable_buffers_type =
boost::asio::mutable_buffers_1;
int status = 0;
int version = 0;
std::string method;
std::string path;
std::string reason;
std::string body;
2017-06-30 08:46:22 -07:00
int got_on_begin = 0;
int got_on_field = 0;
int got_on_header = 0;
int got_on_body = 0;
int got_content_length = 0;
int got_on_chunk = 0;
int got_on_complete = 0;
2017-07-02 06:38:10 -07:00
std::unordered_map<
std::string, std::string> fields;
2016-11-20 07:32:41 -05:00
test_parser() = default;
explicit
test_parser(test::fail_counter& fc)
: fc_(&fc)
{
}
void
2017-07-12 18:06:36 -07:00
on_request_impl(verb, string_view method_str_,
string_view path_, int version_, error_code& ec)
2016-11-20 07:32:41 -05:00
{
method = std::string(
method_str_.data(), method_str_.size());
2016-11-20 07:32:41 -05:00
path = std::string(
path_.data(), path_.size());
version = version_;
2017-06-30 08:46:22 -07:00
++got_on_begin;
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
void
2017-07-12 18:06:36 -07:00
on_response_impl(int code,
2017-06-04 10:52:28 -07:00
string_view reason_,
2016-11-20 07:32:41 -05:00
int version_, error_code& ec)
{
status = code;
2016-11-20 07:32:41 -05:00
reason = std::string(
reason_.data(), reason_.size());
version = version_;
2017-06-30 08:46:22 -07:00
++got_on_begin;
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
void
2017-07-12 18:06:36 -07:00
on_field_impl(field, string_view name,
2017-07-02 06:38:10 -07:00
string_view value, error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-30 08:46:22 -07:00
++got_on_field;
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2017-07-02 06:38:10 -07:00
fields[name.to_string()] = value.to_string();
2016-11-20 07:32:41 -05:00
}
void
2017-07-12 18:06:36 -07:00
on_header_impl(error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-30 08:46:22 -07:00
++got_on_header;
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
void
2017-07-12 18:06:36 -07:00
on_body_init_impl(
boost::optional<std::uint64_t> const& content_length_,
error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-30 08:46:22 -07:00
++got_on_body;
got_content_length =
static_cast<bool>(content_length_);
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
std::size_t
2017-07-12 18:06:36 -07:00
on_body_impl(string_view s,
2016-11-20 07:32:41 -05:00
error_code& ec)
{
body.append(s.data(), s.size());
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
return s.size();
2016-11-20 07:32:41 -05:00
}
void
2017-07-12 18:06:36 -07:00
on_chunk_header_impl(
std::uint64_t,
string_view,
error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-30 08:46:22 -07:00
++got_on_chunk;
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
2017-07-12 18:06:36 -07:00
std::size_t
on_chunk_body_impl(
std::uint64_t,
string_view s,
error_code& ec)
{
body.append(s.data(), s.size());
if(fc_)
fc_->fail(ec);
else
ec.assign(0, ec.category());
return s.size();
}
2016-11-20 07:32:41 -05:00
void
2017-07-12 18:06:36 -07:00
on_finish_impl(error_code& ec)
2016-11-20 07:32:41 -05:00
{
2017-06-30 08:46:22 -07:00
++got_on_complete;
2016-11-20 07:32:41 -05:00
if(fc_)
fc_->fail(ec);
2017-06-13 07:08:52 -07:00
else
2017-06-19 16:57:12 -07:00
ec.assign(0, ec.category());
2016-11-20 07:32:41 -05:00
}
};
} // http
} // beast
2017-07-20 13:40:34 -07:00
} // boost
2016-11-20 07:32:41 -05:00
#endif