ゴミ箱
chunk_encode.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_CHUNK_ENCODE_HPP
11 #define BOOST_BEAST_HTTP_CHUNK_ENCODE_HPP
12 
13 #include <boost/beast/config.hpp>
18 #include <boost/asio/buffer.hpp>
19 #include <memory>
20 #include <type_traits>
21 
22 namespace boost {
23 namespace beast {
24 namespace http {
25 
40 struct chunk_crlf
41 {
43  chunk_crlf() = default;
44 
45  //-----
46 
48 #if BOOST_BEAST_DOXYGEN
49  using value_type = implementation_defined;
50 #else
52 #endif
53 
55  using const_iterator = value_type const*;
56 
58  chunk_crlf(chunk_crlf const&) = default;
59 
62  begin() const
63  {
65  }
66 
69  end() const
70  {
71  return begin() + 1;
72  }
73 };
74 
75 //------------------------------------------------------------------------------
76 
106 {
107  using view_type = buffer_cat_view<
108  detail::chunk_size, // chunk-size
109  boost::asio::const_buffers_1, // chunk-extensions
110  chunk_crlf>; // CRLF
111 
112  std::shared_ptr<
114  view_type view_;
115 
116 public:
128  explicit
129  chunk_header(std::size_t size);
130 
154  chunk_header(
155  std::size_t size,
156  string_view extensions);
157 
180  template<class ChunkExtensions
181 #if ! BOOST_BEAST_DOXYGEN
182  , class = typename std::enable_if<
184  ChunkExtensions>::value>::type
185 #endif
186  >
187  chunk_header(
188  std::size_t size,
189  ChunkExtensions&& extensions);
190 
216  template<class ChunkExtensions, class Allocator
217 #if ! BOOST_BEAST_DOXYGEN
218  , class = typename std::enable_if<
220  ChunkExtensions>::value>::type
221 #endif
222  >
223  chunk_header(
224  std::size_t size,
225  ChunkExtensions&& extensions,
226  Allocator const& allocator);
227 
228  //-----
229 
231 #if BOOST_BEAST_DOXYGEN
232  using value_type = implementation_defined;
233 #else
235 #endif
236 
238 #if BOOST_BEAST_DOXYGEN
239  using const_iterator = implementation_defined;
240 #else
241  using const_iterator = typename view_type::const_iterator;
242 #endif
243 
245  chunk_header(chunk_header const&) = default;
246 
249  begin() const
250  {
251  return view_.begin();
252  }
253 
256  end() const
257  {
258  return view_.end();
259  }
260 };
261 
262 //------------------------------------------------------------------------------
263 
283 template<class ConstBufferSequence>
285 {
286  using view_type = buffer_cat_view<
287  detail::chunk_size, // chunk-size
288  boost::asio::const_buffers_1, // chunk-extensions
289  chunk_crlf, // CRLF
290  ConstBufferSequence, // chunk-body
291  chunk_crlf>; // CRLF
292 
293  std::shared_ptr<
295  view_type view_;
296 
297 public:
312  explicit
313  chunk_body(
314  ConstBufferSequence const& buffers);
315 
342  chunk_body(
343  ConstBufferSequence const& buffers,
344  string_view extensions);
345 
371  template<class ChunkExtensions
372 #if ! BOOST_BEAST_DOXYGEN
373  , class = typename std::enable_if<
374  ! std::is_convertible<typename std::decay<
375  ChunkExtensions>::type, string_view>::value>::type
376 #endif
377  >
378  chunk_body(
379  ConstBufferSequence const& buffers,
380  ChunkExtensions&& extensions);
381 
410  template<class ChunkExtensions, class Allocator
411 #if ! BOOST_BEAST_DOXYGEN
412  , class = typename std::enable_if<
413  ! std::is_convertible<typename std::decay<
414  ChunkExtensions>::type, string_view>::value>::type
415 #endif
416  >
417  chunk_body(
418  ConstBufferSequence const& buffers,
419  ChunkExtensions&& extensions,
420  Allocator const& allocator);
421 
422  //-----
423 
425 #if BOOST_BEAST_DOXYGEN
426  using value_type = implementation_defined;
427 #else
429 #endif
430 
432 #if BOOST_BEAST_DOXYGEN
433  using const_iterator = implementation_defined;
434 #else
435  using const_iterator = typename view_type::const_iterator;
436 #endif
437 
440  begin() const
441  {
442  return view_.begin();
443  }
444 
447  end() const
448  {
449  return view_.end();
450  }
451 };
452 
453 //------------------------------------------------------------------------------
454 
457 template<class Trailer = chunk_crlf>
459 {
460  static_assert(
463  "Trailer requirements not met");
464 
465  using buffers_type = typename
467 
468  using view_type =
470  detail::chunk_size0, // "0\r\n"
471  buffers_type>; // Trailer (includes CRLF)
472 
473  template<class Allocator>
474  buffers_type
475  prepare(Trailer const& trailer, Allocator const& alloc);
476 
477  buffers_type
478  prepare(Trailer const& trailer, std::true_type);
479 
480  buffers_type
481  prepare(Trailer const& trailer, std::false_type);
482 
483  std::shared_ptr<void> sp_;
484  view_type view_;
485 
486 public:
491  chunk_last();
492 
502  explicit
503  chunk_last(Trailer const& trailer);
504 
513 #if BOOST_BEAST_DOXYGEN
514  template<class Allocator>
515  chunk_last(Trailer const& trailer, Allocator const& allocator);
516 #else
517  template<class DeducedTrailer, class Allocator,
518  class = typename std::enable_if<
520  chunk_last(
521  DeducedTrailer const& trailer, Allocator const& allocator);
522 #endif
523 
524  //-----
525 
527  chunk_last(chunk_last const&) = default;
528 
530 #if BOOST_BEAST_DOXYGEN
531  using value_type = implementation_defined;
532 #else
533  using value_type =
535 #endif
536 
538 #if BOOST_BEAST_DOXYGEN
539  using const_iterator = implementation_defined;
540 #else
541  using const_iterator =
542  typename view_type::const_iterator;
543 #endif
544 
547  begin() const
548  {
549  return view_.begin();
550  }
551 
554  end() const
555  {
556  return view_.end();
557  }
558 };
559 
560 //------------------------------------------------------------------------------
561 
570 template<class Allocator>
572 {
573  std::basic_string<char,
574  std::char_traits<char>, Allocator> s_;
575 
576  std::basic_string<char,
577  std::char_traits<char>, Allocator> range_;
578 
579  template<class FwdIt>
580  FwdIt
581  do_parse(FwdIt it, FwdIt last, error_code& ec);
582 
583  void
584  do_insert(string_view name, string_view value);
585 
586 public:
593  using value_type = std::pair<string_view, string_view>;
594 
595  class const_iterator;
596 
598  basic_chunk_extensions() = default;
599 
602 
605 
610  explicit
611  basic_chunk_extensions(Allocator const& allocator)
612  : s_(allocator)
613  {
614  }
615 
621  void
623  {
624  s_.clear();
625  }
626 
631  void
632  parse(string_view s, error_code& ec);
633 
638  void
639  insert(string_view name);
640 
648  void
649  insert(string_view name, string_view value);
650 
653  str() const
654  {
655  return s_;
656  }
657 
659  begin() const;
660 
662  end() const;
663 };
664 
665 //------------------------------------------------------------------------------
666 
668 using chunk_extensions =
670 
684 template<class ConstBufferSequence, class... Args>
685 auto
688  Args&&... args) ->
690 {
692  buffers, std::forward<Args>(args)...);
693 }
694 
700 inline
703 {
704  return chunk_last<chunk_crlf>{};
705 }
706 
720 template<class Trailer, class... Args>
723  Trailer const& trailer,
724  Args&&... args)
725 {
726  return chunk_last<Trailer>{
727  trailer, std::forward<Args>(args)...};
728 }
729 
730 } // http
731 } // beast
732 } // boost
733 
735 
736 #endif
BufferSequence< boost::asio::const_buffer > ConstBufferSequence
Definition: type_traits.hpp:280
const_iterator end() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:69
const_iterator end() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:447
chunk_last< chunk_crlf > make_chunk_last()
Definition: chunk_encode.hpp:702
const_iterator end() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:256
Definition: async_result.hpp:20
typename view_type::value_type value_type
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:234
const_iterator begin() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:249
typename view_type::value_type value_type
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:534
detail::buffers_helper< ConstBufferSequence > buffers(ConstBufferSequence const &b)
Definition: ostream.hpp:50
typename view_type::const_iterator const_iterator
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:542
basic_chunk_extensions(Allocator const &allocator)
Definition: chunk_encode.hpp:611
chunk_crlf()=default
Constructor.
const_iterator begin() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:547
Definition: beast_common.hpp:6
Definition: chunk_encode.hpp:222
void clear()
Definition: chunk_encode.hpp:622
Definition: chunk_encode.hpp:105
boost::system::error_code error_code
The type of error code used by the library.
Definition: error.hpp:21
typename detail::common_buffers_type< Buffers... >::type value_type
Definition: buffer_cat.hpp:40
const_iterator end() const
Required for BufferSequence.
Definition: buffer_cat.ipp:486
const_iterator begin() const
Required for BufferSequence.
Definition: buffer_cat.ipp:477
const_iterator begin() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:62
Definition: chunk_encode.hpp:25
Definition: type_traits.hpp:59
typename view_type::const_iterator const_iterator
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:435
boost::asio::const_buffers_1 chunk_last()
Returns a buffer sequence holding a final chunk header.
Definition: chunk_encode.hpp:163
typename view_type::value_type value_type
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:428
Definition: chunk_encode.hpp:40
boost::string_ref string_view
The type of string view used by the library.
Definition: string.hpp:36
const_iterator end() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:554
typename detail::is_fields_helper< T >::type is_fields
Definition: type_traits.hpp:178
value_type const * const_iterator
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:55
Definition: chunk_encode.hpp:571
static value_type value
Definition: chunk_encode.hpp:186
std::pair< string_view, string_view > value_type
Definition: chunk_encode.hpp:593
typename view_type::const_iterator const_iterator
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:241
Definition: chunk_encode.hpp:458
string_view str() const
Return the serialized representation of the chunk extension.
Definition: chunk_encode.hpp:653
typename T::reader::const_buffers_type type
Definition: chunk_encode.hpp:248
auto make_chunk(ConstBufferSequence const &buffers, Args &&...args) -> chunk_body< ConstBufferSequence >
Definition: chunk_encode.hpp:686
Definition: chunk_encode.hpp:284
Definition: chunk_encode.hpp:68
const_iterator begin() const
Required for ConstBufferSequence.
Definition: chunk_encode.hpp:440