123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699 |
- #ifndef BOOST_BEAST_HTTP_BASIC_PARSER_HPP
- #define BOOST_BEAST_HTTP_BASIC_PARSER_HPP
- #include <boost/beast/core/detail/config.hpp>
- #include <boost/beast/core/error.hpp>
- #include <boost/beast/core/string.hpp>
- #include <boost/beast/http/field.hpp>
- #include <boost/beast/http/verb.hpp>
- #include <boost/beast/http/detail/basic_parser.hpp>
- #include <boost/asio/buffer.hpp>
- #include <boost/optional.hpp>
- #include <boost/assert.hpp>
- #include <limits>
- #include <memory>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace beast {
- namespace http {
- template<bool isRequest>
- class basic_parser
- : private detail::basic_parser_base
- {
- boost::optional<std::uint64_t>
- body_limit_ =
- boost::optional<std::uint64_t>(
- default_body_limit(is_request{}));
- std::uint64_t len_ = 0;
- std::uint64_t len0_ = 0;
- std::unique_ptr<char[]> buf_;
- std::size_t buf_len_ = 0;
- std::size_t skip_ = 0;
- std::uint32_t header_limit_ = 8192;
- unsigned short status_ = 0;
- state state_ = state::nothing_yet;
- unsigned f_ = 0;
-
- static std::size_t constexpr max_stack_buffer = 8192;
-
- static unsigned constexpr flagSkipBody = 1<< 0;
-
- static unsigned constexpr flagEager = 1<< 1;
-
- static unsigned constexpr flagGotSome = 1<< 2;
-
-
-
- static unsigned constexpr flagHasBody = 1<< 3;
- static unsigned constexpr flagHTTP11 = 1<< 4;
- static unsigned constexpr flagNeedEOF = 1<< 5;
- static unsigned constexpr flagExpectCRLF = 1<< 6;
- static unsigned constexpr flagConnectionClose = 1<< 7;
- static unsigned constexpr flagConnectionUpgrade = 1<< 8;
- static unsigned constexpr flagConnectionKeepAlive = 1<< 9;
- static unsigned constexpr flagContentLength = 1<< 10;
- static unsigned constexpr flagChunked = 1<< 11;
- static unsigned constexpr flagUpgrade = 1<< 12;
- static unsigned constexpr flagFinalChunk = 1<< 13;
- static constexpr
- std::uint64_t
- default_body_limit(std::true_type)
- {
-
- return 1 * 1024 * 1024;
- }
- static constexpr
- std::uint64_t
- default_body_limit(std::false_type)
- {
-
- return 8 * 1024 * 1024;
- }
- template<bool OtherIsRequest>
- friend class basic_parser;
- friend class basic_parser_test;
- protected:
-
- basic_parser() = default;
-
- basic_parser(basic_parser &&) = default;
-
- basic_parser& operator=(basic_parser &&) = default;
- public:
-
- using is_request =
- std::integral_constant<bool, isRequest>;
-
- virtual ~basic_parser() = default;
-
- basic_parser(basic_parser const&) = delete;
-
- basic_parser& operator=(basic_parser const&) = delete;
-
- bool
- got_some() const
- {
- return state_ != state::nothing_yet;
- }
-
- bool
- is_done() const
- {
- return state_ == state::complete;
- }
-
- bool
- is_header_done() const
- {
- return state_ > state::fields;
- }
-
- bool
- upgrade() const
- {
- return (f_ & flagConnectionUpgrade) != 0;
- }
-
- bool
- chunked() const
- {
- return (f_ & flagChunked) != 0;
- }
-
- bool
- keep_alive() const;
-
- boost::optional<std::uint64_t>
- content_length() const;
-
- boost::optional<std::uint64_t>
- content_length_remaining() const;
-
- bool
- need_eof() const
- {
- return (f_ & flagNeedEOF) != 0;
- }
-
- void
- body_limit(boost::optional<std::uint64_t> v)
- {
- body_limit_ = v;
- }
-
- void
- header_limit(std::uint32_t v)
- {
- header_limit_ = v;
- }
-
- bool
- eager() const
- {
- return (f_ & flagEager) != 0;
- }
-
- void
- eager(bool v)
- {
- if(v)
- f_ |= flagEager;
- else
- f_ &= ~flagEager;
- }
-
- bool
- skip() const
- {
- return (f_ & flagSkipBody) != 0;
- }
-
- void
- skip(bool v);
-
- template<class ConstBufferSequence>
- std::size_t
- put(ConstBufferSequence const& buffers, error_code& ec);
- #if ! BOOST_BEAST_DOXYGEN
- std::size_t
- put(net::const_buffer buffer,
- error_code& ec);
- #endif
-
- void
- put_eof(error_code& ec);
- protected:
-
- virtual
- void
- on_request_impl(
- verb method,
- string_view method_str,
- string_view target,
- int version,
- error_code& ec) = 0;
-
- virtual
- void
- on_response_impl(
- int code,
- string_view reason,
- int version,
- error_code& ec) = 0;
-
- virtual
- void
- on_field_impl(
- field name,
- string_view name_string,
- string_view value,
- error_code& ec) = 0;
-
- virtual
- void
- on_header_impl(error_code& ec) = 0;
-
- virtual
- void
- on_body_init_impl(
- boost::optional<std::uint64_t> const& content_length,
- error_code& ec) = 0;
-
- virtual
- std::size_t
- on_body_impl(
- string_view body,
- error_code& ec) = 0;
-
- virtual
- void
- on_chunk_header_impl(
- std::uint64_t size,
- string_view extensions,
- error_code& ec) = 0;
-
- virtual
- std::size_t
- on_chunk_body_impl(
- std::uint64_t remain,
- string_view body,
- error_code& ec) = 0;
-
- virtual
- void
- on_finish_impl(error_code& ec) = 0;
- private:
- boost::optional<std::uint64_t>
- content_length_unchecked() const;
- template<class ConstBufferSequence>
- std::size_t
- put_from_stack(
- std::size_t size,
- ConstBufferSequence const& buffers,
- error_code& ec);
- void
- maybe_need_more(
- char const* p, std::size_t n,
- error_code& ec);
- void
- parse_start_line(
- char const*& p, char const* last,
- error_code& ec, std::true_type);
- void
- parse_start_line(
- char const*& p, char const* last,
- error_code& ec, std::false_type);
- void
- parse_fields(
- char const*& p, char const* last,
- error_code& ec);
- void
- finish_header(
- error_code& ec, std::true_type);
- void
- finish_header(
- error_code& ec, std::false_type);
- void
- parse_body(char const*& p,
- std::size_t n, error_code& ec);
- void
- parse_body_to_eof(char const*& p,
- std::size_t n, error_code& ec);
- void
- parse_chunk_header(char const*& p,
- std::size_t n, error_code& ec);
- void
- parse_chunk_body(char const*& p,
- std::size_t n, error_code& ec);
- void
- do_field(field f,
- string_view value, error_code& ec);
- };
- }
- }
- }
- #include <boost/beast/http/impl/basic_parser.hpp>
- #ifdef BOOST_BEAST_HEADER_ONLY
- #include <boost/beast/http/impl/basic_parser.ipp>
- #endif
- #endif
|