ゴミ箱
basic_parser.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-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 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_HTTP_BASIC_PARSER_HPP
11 #define BOOST_BEAST_HTTP_BASIC_PARSER_HPP
12 
13 #include <boost/beast/config.hpp>
19 #include <boost/asio/buffer.hpp>
20 #include <boost/optional.hpp>
21 #include <boost/assert.hpp>
22 #include <limits>
23 #include <memory>
24 #include <type_traits>
25 #include <utility>
26 
27 namespace boost {
28 namespace beast {
29 namespace http {
30 
77 
86 
94 
102 
107 
122 
128 
145 
154 
171 template<bool isRequest, class Derived>
173  : private detail::basic_parser_base
174 {
175  template<bool OtherIsRequest, class OtherDerived>
176  friend class basic_parser;
177 
178  // limit on the size of the stack flat buffer
179  static std::size_t constexpr max_stack_buffer = 8192;
180 
181  // Message will be complete after reading header
182  static unsigned constexpr flagSkipBody = 1<< 0;
183 
184  // Consume input buffers across semantic boundaries
185  static unsigned constexpr flagEager = 1<< 1;
186 
187  // The parser has read at least one byte
188  static unsigned constexpr flagGotSome = 1<< 2;
189 
190  // Message semantics indicate a body is expected.
191  // cleared if flagSkipBody set
192  //
193  static unsigned constexpr flagHasBody = 1<< 3;
194 
195  static unsigned constexpr flagHTTP11 = 1<< 4;
196  static unsigned constexpr flagNeedEOF = 1<< 5;
197  static unsigned constexpr flagExpectCRLF = 1<< 6;
198  static unsigned constexpr flagConnectionClose = 1<< 7;
199  static unsigned constexpr flagConnectionUpgrade = 1<< 8;
200  static unsigned constexpr flagConnectionKeepAlive = 1<< 9;
201  static unsigned constexpr flagContentLength = 1<< 10;
202  static unsigned constexpr flagChunked = 1<< 11;
203  static unsigned constexpr flagUpgrade = 1<< 12;
204  static unsigned constexpr flagFinalChunk = 1<< 13;
205 
206  static
207  std::uint64_t
208  default_body_limit(std::true_type)
209  {
210  // limit for requests
211  return 1 * 1024 * 1024; // 1MB
212  }
213 
214  static
215  std::uint64_t
216  default_body_limit(std::false_type)
217  {
218  // limit for responses
219  return 8 * 1024 * 1024; // 8MB
220  }
221 
222  std::uint64_t body_limit_; // max payload body
223  std::uint64_t len_; // size of chunk or body
224  std::unique_ptr<char[]> buf_; // temp storage
225  std::size_t buf_len_ = 0; // size of buf_
226  std::size_t skip_ = 0; // resume search here
227  std::uint32_t
228  header_limit_ = 8192; // max header size
229  unsigned short status_; // response status
230  state state_ = // initial state
232  unsigned f_ = 0; // flags
233 
234 public:
236  using is_request =
237  std::integral_constant<bool, isRequest>;
238 
240  ~basic_parser();
241 
243  basic_parser(basic_parser const&) = delete;
244 
246  basic_parser& operator=(basic_parser const&) = delete;
247 
249  basic_parser();
250 
256  template<class OtherDerived>
258 
265  basic_parser&
267  {
268  return *this;
269  }
270 
277  basic_parser const&
278  base() const
279  {
280  return *this;
281  }
282 
284  bool
285  got_some() const
286  {
287  return state_ != state::nothing_yet;
288  }
289 
302  bool
303  is_done() const
304  {
305  return state_ == state::complete;
306  }
307 
310  bool
312  {
313  return state_ > state::fields;
314  }
315 
321  bool
322  is_upgrade() const
323  {
324  return (f_ & flagConnectionUpgrade) != 0;
325  }
326 
332  bool
333  is_chunked() const
334  {
335  return (f_ & flagChunked) != 0;
336  }
337 
343  bool
344  is_keep_alive() const;
345 
351  boost::optional<std::uint64_t>
352  content_length() const;
353 
362  bool
363  need_eof() const
364  {
365  return (f_ & flagNeedEOF) != 0;
366  }
367 
399  void
400  body_limit(std::uint64_t v)
401  {
402  body_limit_ = v;
403  }
404 
417  void
418  header_limit(std::uint32_t v)
419  {
420  header_limit_ = v;
421  }
422 
424  bool
425  eager() const
426  {
427  return (f_ & flagEager) != 0;
428  }
429 
446  void
447  eager(bool v)
448  {
449  if(v)
450  f_ |= flagEager;
451  else
452  f_ &= ~flagEager;
453  }
454 
456  bool
458  {
459  return (f_ & flagSkipBody) != 0;
460  }
461 
477  void
478  skip(bool v);
479 
512  template<class ConstBufferSequence>
513  std::size_t
515 
516 #if ! BOOST_BEAST_DOXYGEN
517  std::size_t
518  put(boost::asio::const_buffers_1 const& buffer,
519  error_code& ec);
520 #endif
521 
538  void
539  put_eof(error_code& ec);
540 
541 private:
542  inline
543  Derived&
544  impl()
545  {
546  return *static_cast<Derived*>(this);
547  }
548 
549  template<class ConstBufferSequence>
550  std::size_t
551  put_from_stack(std::size_t size,
552  ConstBufferSequence const& buffers,
553  error_code& ec);
554 
555  void
556  maybe_need_more(
557  char const* p, std::size_t n,
558  error_code& ec);
559 
560  void
561  parse_start_line(
562  char const*& p, char const* last,
563  error_code& ec, std::true_type);
564 
565  void
566  parse_start_line(
567  char const*& p, char const* last,
568  error_code& ec, std::false_type);
569 
570  void
571  parse_fields(
572  char const*& p, char const* last,
573  error_code& ec);
574 
575  void
576  finish_header(
577  error_code& ec, std::true_type);
578 
579  void
580  finish_header(
581  error_code& ec, std::false_type);
582 
583  void
584  parse_body(char const*& p,
585  std::size_t n, error_code& ec);
586 
587  void
588  parse_body_to_eof(char const*& p,
589  std::size_t n, error_code& ec);
590 
591  void
592  parse_chunk_header(char const*& p,
593  std::size_t n, error_code& ec);
594 
595  void
596  parse_chunk_body(char const*& p,
597  std::size_t n, error_code& ec);
598 
599  void
600  do_field(field f,
601  string_view value, error_code& ec);
602 };
603 
604 } // http
605 } // beast
606 } // boost
607 
609 
610 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
void body_limit(std::uint64_t v)
Definition: basic_parser.hpp:400
std::size_t put(ConstBufferSequence const &buffers, error_code &ec)
Definition: basic_parser.ipp:103
Definition: async_result.hpp:20
field
Definition: field.hpp:21
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
basic_parser const & base() const
Definition: basic_parser.hpp:278
bool skip()
Returns true if the skip parse option is set.
Definition: basic_parser.hpp:457
bool is_header_done() const
Definition: basic_parser.hpp:311
bool is_upgrade() const
Definition: basic_parser.hpp:322
Definition: beast_common.hpp:6
void header_limit(std::uint32_t v)
Definition: basic_parser.hpp:418
bool need_eof() const
Definition: basic_parser.hpp:363
bool got_some() const
Returns true if the parser has received at least one byte of input.
Definition: basic_parser.hpp:285
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
state
Definition: basic_parser.hpp:38
bool eager() const
Returns true if the eager parse option is set.
Definition: basic_parser.hpp:425
std::integral_constant< bool, isRequest > is_request
true if this parser parses requests, false for responses.
Definition: basic_parser.hpp:237
basic_parser()
Constructor.
Definition: basic_parser.ipp:36
void eager(bool v)
Definition: basic_parser.hpp:447
basic_parser & operator=(basic_parser const &)=delete
Constructor.
boost::optional< std::uint64_t > content_length() const
Definition: basic_parser.ipp:79
~basic_parser()
Destructor.
Definition: basic_parser.ipp:30
bool is_keep_alive() const
Definition: basic_parser.ipp:60
boost::string_ref string_view
The type of string view used by the library.
Definition: string.hpp:36
bool is_done() const
Definition: basic_parser.hpp:303
bool is_chunked() const
Definition: basic_parser.hpp:333
void put_eof(error_code &ec)
Definition: basic_parser.ipp:290
Definition: basic_parser.hpp:29
basic_parser & base()
Definition: basic_parser.hpp:266
Definition: basic_parser.hpp:172