| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | #ifndef BOOST_BEAST_HTTP_TEST_PARSER_HPP |
| 11 | #define BOOST_BEAST_HTTP_TEST_PARSER_HPP |
| 12 | |
| 13 | #include <boost/beast/http/basic_parser.hpp> |
| 14 | #include <boost/beast/_experimental/test/fail_count.hpp> |
| 15 | #include <string> |
| 16 | #include <unordered_map> |
| 17 | |
| 18 | namespace boost { |
| 19 | namespace beast { |
| 20 | namespace http { |
| 21 | |
| 22 | template<bool isRequest> |
| 23 | class test_parser |
| 24 | : public basic_parser<isRequest> |
| 25 | { |
| 26 | test::fail_count* fc_ = nullptr; |
| 27 | |
| 28 | public: |
| 29 | using mutable_buffers_type = |
| 30 | net::mutable_buffer; |
| 31 | |
| 32 | int status = 0; |
| 33 | int version = 0; |
| 34 | std::string method; |
| 35 | std::string path; |
| 36 | std::string reason; |
| 37 | std::string body; |
| 38 | int got_on_begin = 0; |
| 39 | int got_on_field = 0; |
| 40 | int = 0; |
| 41 | int got_on_body = 0; |
| 42 | int got_content_length = 0; |
| 43 | int got_on_chunk = 0; |
| 44 | int got_on_complete = 0; |
| 45 | std::unordered_map< |
| 46 | std::string, std::string> fields; |
| 47 | |
| 48 | test_parser() = default; |
| 49 | |
| 50 | explicit |
| 51 | test_parser(test::fail_count& fc) |
| 52 | : fc_(&fc) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | on_request_impl(verb, string_view method_str_, |
| 58 | string_view path_, int version_, error_code& ec) |
| 59 | { |
| 60 | method = std::string( |
| 61 | method_str_.data(), method_str_.size()); |
| 62 | path = std::string( |
| 63 | path_.data(), path_.size()); |
| 64 | version = version_; |
| 65 | ++got_on_begin; |
| 66 | if(fc_) |
| 67 | fc_->fail(ec); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | on_response_impl(int code, |
| 72 | string_view reason_, |
| 73 | int version_, error_code& ec) |
| 74 | { |
| 75 | status = code; |
| 76 | reason = std::string( |
| 77 | reason_.data(), reason_.size()); |
| 78 | version = version_; |
| 79 | ++got_on_begin; |
| 80 | if(fc_) |
| 81 | fc_->fail(ec); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | on_field_impl(field, string_view name, |
| 86 | string_view value, error_code& ec) |
| 87 | { |
| 88 | ++got_on_field; |
| 89 | if(fc_) |
| 90 | fc_->fail(ec); |
| 91 | fields[std::string(name)] = std::string(value); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | (error_code& ec) |
| 96 | { |
| 97 | ++got_on_header; |
| 98 | if(fc_) |
| 99 | fc_->fail(ec); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | on_body_init_impl( |
| 104 | boost::optional<std::uint64_t> const& content_length_, |
| 105 | error_code& ec) |
| 106 | { |
| 107 | // The real implementation clears out the error code in basic_string_body::reader::init |
| 108 | ec = {}; |
| 109 | ++got_on_body; |
| 110 | got_content_length = |
| 111 | static_cast<bool>(content_length_); |
| 112 | if(fc_) |
| 113 | fc_->fail(ec); |
| 114 | } |
| 115 | |
| 116 | std::size_t |
| 117 | on_body_impl(string_view s, |
| 118 | error_code& ec) |
| 119 | { |
| 120 | body.append(s: s.data(), n: s.size()); |
| 121 | if(fc_) |
| 122 | fc_->fail(ec); |
| 123 | return s.size(); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | ( |
| 128 | std::uint64_t, |
| 129 | string_view, |
| 130 | error_code& ec) |
| 131 | { |
| 132 | ++got_on_chunk; |
| 133 | if(fc_) |
| 134 | fc_->fail(ec); |
| 135 | } |
| 136 | |
| 137 | std::size_t |
| 138 | on_chunk_body_impl( |
| 139 | std::uint64_t, |
| 140 | string_view s, |
| 141 | error_code& ec) |
| 142 | { |
| 143 | body.append(s: s.data(), n: s.size()); |
| 144 | if(fc_) |
| 145 | fc_->fail(ec); |
| 146 | return s.size(); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | void |
| 151 | on_finish_impl(error_code& ec) |
| 152 | { |
| 153 | ++got_on_complete; |
| 154 | if(fc_) |
| 155 | fc_->fail(ec); |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | } // http |
| 160 | } // beast |
| 161 | } // boost |
| 162 | |
| 163 | #endif |
| 164 | |