]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 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 | ||
8 | #ifndef BEAST_HTTP_TEST_PARSER_HPP | |
9 | #define BEAST_HTTP_TEST_PARSER_HPP | |
10 | ||
11 | #include <beast/http/basic_parser.hpp> | |
12 | #include <beast/test/fail_counter.hpp> | |
13 | ||
14 | namespace beast { | |
15 | namespace http { | |
16 | ||
17 | template<bool isRequest> | |
18 | class test_parser | |
19 | : public basic_parser<isRequest, false, | |
20 | test_parser<isRequest>> | |
21 | { | |
22 | test::fail_counter* fc_ = nullptr; | |
23 | ||
24 | public: | |
25 | using mutable_buffers_type = | |
26 | boost::asio::mutable_buffers_1; | |
27 | ||
28 | int status = 0; | |
29 | int version = 0; | |
30 | std::string method; | |
31 | std::string path; | |
32 | std::string reason; | |
33 | std::string body; | |
34 | bool got_on_begin = false; | |
35 | bool got_on_field = false; | |
36 | bool got_on_header = false; | |
37 | bool got_on_body = false; | |
38 | bool got_content_length = false; | |
39 | bool got_on_prepare = false; | |
40 | bool got_on_commit = false; | |
41 | bool got_on_chunk = false; | |
42 | bool got_on_complete = false; | |
43 | ||
44 | test_parser() = default; | |
45 | ||
46 | explicit | |
47 | test_parser(test::fail_counter& fc) | |
48 | : fc_(&fc) | |
49 | { | |
50 | } | |
51 | ||
52 | void | |
53 | on_request( | |
54 | boost::string_ref const& method_, | |
55 | boost::string_ref const& path_, | |
56 | int version_, error_code& ec) | |
57 | { | |
58 | method = std::string( | |
59 | method_.data(), method_.size()); | |
60 | path = std::string( | |
61 | path_.data(), path_.size()); | |
62 | version = version_; | |
63 | got_on_begin = true; | |
64 | if(fc_) | |
65 | fc_->fail(ec); | |
66 | } | |
67 | ||
68 | void | |
69 | on_response(int status_, | |
70 | boost::string_ref const& reason_, | |
71 | int version_, error_code& ec) | |
72 | { | |
73 | status = status_; | |
74 | reason = std::string( | |
75 | reason_.data(), reason_.size()); | |
76 | version = version_; | |
77 | got_on_begin = true; | |
78 | if(fc_) | |
79 | fc_->fail(ec); | |
80 | } | |
81 | ||
82 | void | |
83 | on_field(boost::string_ref const&, | |
84 | boost::string_ref const&, | |
85 | error_code& ec) | |
86 | { | |
87 | got_on_field = true; | |
88 | if(fc_) | |
89 | fc_->fail(ec); | |
90 | } | |
91 | ||
92 | void | |
93 | on_header(error_code& ec) | |
94 | { | |
95 | got_on_header = true; | |
96 | if(fc_) | |
97 | fc_->fail(ec); | |
98 | } | |
99 | ||
100 | void | |
101 | on_body(error_code& ec) | |
102 | { | |
103 | got_on_body = true; | |
104 | if(fc_) | |
105 | fc_->fail(ec); | |
106 | } | |
107 | ||
108 | void | |
109 | on_body(std::uint64_t content_length, | |
110 | error_code& ec) | |
111 | { | |
112 | got_on_body = true; | |
113 | got_content_length = true; | |
114 | if(fc_) | |
115 | fc_->fail(ec); | |
116 | } | |
117 | ||
118 | void | |
119 | on_data(boost::string_ref const& s, | |
120 | error_code& ec) | |
121 | { | |
122 | body.append(s.data(), s.size()); | |
123 | } | |
124 | ||
125 | void | |
126 | on_chunk(std::uint64_t, | |
127 | boost::string_ref const&, | |
128 | error_code& ec) | |
129 | { | |
130 | got_on_chunk = true; | |
131 | if(fc_) | |
132 | fc_->fail(ec); | |
133 | } | |
134 | ||
135 | void | |
136 | on_complete(error_code& ec) | |
137 | { | |
138 | got_on_complete = true; | |
139 | if(fc_) | |
140 | fc_->fail(ec); | |
141 | } | |
142 | }; | |
143 | ||
144 | } // http | |
145 | } // beast | |
146 | ||
147 | #endif |