ゴミ箱
message.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_MESSAGE_HPP
11 #define BOOST_BEAST_HTTP_MESSAGE_HPP
12 
13 #include <boost/beast/config.hpp>
20 #include <boost/optional.hpp>
21 #include <boost/throw_exception.hpp>
22 #include <memory>
23 #include <stdexcept>
24 #include <string>
25 #include <tuple>
26 #include <utility>
27 
28 namespace boost {
29 namespace beast {
30 namespace http {
31 
46 #if BOOST_BEAST_DOXYGEN
47 template<bool isRequest, class Fields = fields>
48 struct header : Fields
49 
50 #else
51 template<bool isRequest, class Fields = fields>
52 struct header;
53 
54 template<class Fields>
55 struct header<true, Fields> : Fields
56 #endif
57 {
58  static_assert(is_fields<Fields>::value,
59  "Fields requirements not met");
60 
62 #if BOOST_BEAST_DOXYGEN
63  using is_request = std::integral_constant<bool, isRequest>;
64 #else
65  using is_request = std::true_type;
66 #endif
67 
69  using fields_type = Fields;
70 
82  unsigned version = 11;
83 
85  header() = default;
86 
88  header(header&&) = default;
89 
91  header(header const&) = default;
92 
94  header& operator=(header&&) = default;
95 
97  header& operator=(header const&) = default;
98 
109  verb
110  method() const;
111 
123  void
124  method(verb v);
125 
133  method_string() const;
134 
145  void
146  method_string(string_view s);
147 
153  target() const;
154 
161  void
162  target(string_view s);
163 
164  // VFALCO Don't rearrange these declarations or
165  // ifdefs, or else the documentation will break.
166 
177 #if BOOST_BEAST_DOXYGEN
178  template<class... Args>
179  explicit
180  header(Args&&... args);
181 
182 #else
183  template<class Arg1, class... ArgN,
184  class = typename std::enable_if<
185  ! std::is_convertible<typename
186  std::decay<Arg1>::type, header>::value &&
187  ! std::is_convertible<typename
188  std::decay<Arg1>::type, verb>::value &&
189  ! std::is_convertible<typename
190  std::decay<Arg1>::type, header>::value
191  >::type>
192  explicit
193  header(Arg1&& arg1, ArgN&&... argn);
194 
195 private:
196  template<bool, class, class>
197  friend struct message;
198 
199  template<class T>
200  friend
201  void
203 
204  template<class... FieldsArgs>
205  header(
206  verb method,
207  string_view target_,
208  unsigned version_,
209  FieldsArgs&&... fields_args)
210  : Fields(std::forward<FieldsArgs>(fields_args)...)
211  , version(version_)
212  , method_(method)
213  {
214  target(target_);
215  }
216 
217  verb method_ = verb::unknown;
218 };
219 
224 template<class Fields>
225 struct header<false, Fields> : Fields
226 {
227  static_assert(is_fields<Fields>::value,
228  "Fields requirements not met");
229 
231  using is_request = std::false_type;
232 
234  using fields_type = Fields;
235 
248  unsigned version = 11;
249 
251  header() = default;
252 
254  header(header&&) = default;
255 
257  header(header const&) = default;
258 
260  header& operator=(header&&) = default;
261 
263  header& operator=(header const&) = default;
264 
275  template<class Arg1, class... ArgN,
276  class = typename std::enable_if<
277  ! std::is_convertible<typename
278  std::decay<Arg1>::type, status>::value &&
279  ! std::is_convertible<typename
280  std::decay<Arg1>::type, header>::value
281  >::type>
282  explicit
283  header(Arg1&& arg1, ArgN&&... argn);
284 #endif
285 
294  status
295  result() const;
296 
303  void
304  result(status v);
305 
318  void
319  result(unsigned v);
320 
328  unsigned
329  result_int() const;
330 
338  reason() const;
339 
358  void
359  reason(string_view s);
360 
361 private:
362 #if ! BOOST_BEAST_DOXYGEN
363  template<bool, class, class>
364  friend struct message;
365 
366  template<class T>
367  friend
368  void
370 
371  template<class... FieldsArgs>
372  header(
373  status result,
374  unsigned version_,
375  FieldsArgs&&... fields_args)
376  : Fields(std::forward<FieldsArgs>(fields_args)...)
377  , version(version_)
378  , result_(result)
379  {
380  }
381 
382  status result_ = status::ok;
383 #endif
384 };
385 
387 template<class Fields = fields>
389 
391 template<class Fields = fields>
393 
423 template<bool isRequest, class Body, class Fields = fields>
424 struct message : header<isRequest, Fields>
425 {
428 
433  using body_type = Body;
434 
436  typename Body::value_type body;
437 
439  message() = default;
440 
442  message(message&&) = default;
443 
445  message(message const&) = default;
446 
448  message& operator=(message&&) = default;
449 
451  message& operator=(message const&) = default;
452 
460  template<class... BodyArgs>
461  explicit
462  message(header_type&& h, BodyArgs&&... body_args);
463 
471  template<class... BodyArgs>
472  explicit
473  message(header_type const& h, BodyArgs&&... body_args);
474 
485 #if BOOST_BEAST_DOXYGEN
486  message(verb method, string_view target, unsigned version);
487 #else
488  template<class Version,
489  class = typename std::enable_if<isRequest &&
490  std::is_convertible<Version, unsigned>::value>::type>
491  message(verb method, string_view target, Version version);
492 #endif
493 
506 #if BOOST_BEAST_DOXYGEN
507  template<class BodyArg>
508  message(verb method, string_view target,
509  unsigned version, BodyArg&& body_arg);
510 #else
511  template<class Version, class BodyArg,
512  class = typename std::enable_if<isRequest &&
513  std::is_convertible<Version, unsigned>::value>::type>
514  message(verb method, string_view target,
515  Version version, BodyArg&& body_arg);
516 #endif
517 
532 #if BOOST_BEAST_DOXYGEN
533  template<class BodyArg, class FieldsArg>
534  message(verb method, string_view target, unsigned version,
535  BodyArg&& body_arg, FieldsArg&& fields_arg);
536 #else
537  template<class Version, class BodyArg, class FieldsArg,
538  class = typename std::enable_if<isRequest &&
539  std::is_convertible<Version, unsigned>::value>::type>
540  message(verb method, string_view target, Version version,
541  BodyArg&& body_arg, FieldsArg&& fields_arg);
542 #endif
543 
552 #if BOOST_BEAST_DOXYGEN
553  message(status result, unsigned version);
554 #else
555  template<class Version,
556  class = typename std::enable_if<! isRequest &&
557  std::is_convertible<Version, unsigned>::value>::type>
558  message(status result, Version version);
559 #endif
560 
571 #if BOOST_BEAST_DOXYGEN
572  template<class BodyArg>
573  message(status result, unsigned version, BodyArg&& body_arg);
574 #else
575  template<class Version, class BodyArg,
576  class = typename std::enable_if<! isRequest &&
577  std::is_convertible<Version, unsigned>::value>::type>
578  message(status result, Version version, BodyArg&& body_arg);
579 #endif
580 
593 #if BOOST_BEAST_DOXYGEN
594  template<class BodyArg, class FieldsArg>
595  message(status result, unsigned version,
596  BodyArg&& body_arg, FieldsArg&& fields_arg);
597 #else
598  template<class Version, class BodyArg, class FieldsArg,
599  class = typename std::enable_if<! isRequest &&
600  std::is_convertible<Version, unsigned>::value>::type>
601  message(status result, Version version,
602  BodyArg&& body_arg, FieldsArg&& fields_arg);
603 #endif
604 
609  explicit
610  message(std::piecewise_construct_t);
611 
617  template<class... BodyArgs>
618  message(std::piecewise_construct_t,
619  std::tuple<BodyArgs...> body_args);
620 
629  template<class... BodyArgs, class... FieldsArgs>
630  message(std::piecewise_construct_t,
631  std::tuple<BodyArgs...> body_args,
632  std::tuple<FieldsArgs...> fields_args);
633 
635  header_type const&
636  base() const
637  {
638  return *this;
639  }
640 
642  header_type&
644  {
645  return *this;
646  }
647 
649  bool
650  chunked() const
651  {
652  return this->get_chunked_impl();
653  }
654 
666  void
667  chunked(bool value);
668 
683  void
684  content_length(boost::optional<std::uint64_t> const& value);
685 
692  bool
693  keep_alive() const
694  {
695  return this->get_keep_alive_impl(this->version);
696  }
697 
708  void
709  keep_alive(bool value)
710  {
711  this->set_keep_alive_impl(this->version, value);
712  }
713 
726  boost::optional<std::uint64_t>
727  payload_size() const;
728 
743  void
745  {
746  prepare_payload(typename header_type::is_request{});
747  }
748 
749 private:
750  static_assert(is_body<Body>::value,
751  "Body requirements not met");
752 
753  template<
754  class... BodyArgs,
755  std::size_t... IBodyArgs>
756  message(
757  std::piecewise_construct_t,
758  std::tuple<BodyArgs...>& body_args,
760  : body(std::forward<BodyArgs>(
761  std::get<IBodyArgs>(body_args))...)
762  {
763  boost::ignore_unused(body_args);
764  }
765 
766  template<
767  class... BodyArgs,
768  class... FieldsArgs,
769  std::size_t... IBodyArgs,
770  std::size_t... IFieldsArgs>
771  message(
772  std::piecewise_construct_t,
773  std::tuple<BodyArgs...>& body_args,
774  std::tuple<FieldsArgs...>& fields_args,
777  : header_type(std::forward<FieldsArgs>(
778  std::get<IFieldsArgs>(fields_args))...)
779  , body(std::forward<BodyArgs>(
780  std::get<IBodyArgs>(body_args))...)
781  {
782  boost::ignore_unused(body_args);
783  boost::ignore_unused(fields_args);
784  }
785 
786  boost::optional<std::uint64_t>
787  payload_size(std::true_type) const
788  {
789  return Body::size(body);
790  }
791 
792  boost::optional<std::uint64_t>
793  payload_size(std::false_type) const
794  {
795  return boost::none;
796  }
797 
798  void
799  prepare_payload(std::true_type);
800 
801  void
802  prepare_payload(std::false_type);
803 };
804 
806 template<class Body, class Fields = fields>
808 
810 template<class Body, class Fields = fields>
812 
813 //------------------------------------------------------------------------------
814 
815 #if BOOST_BEAST_DOXYGEN
816 
821 template<bool isRequest, class Fields>
822 void
823 swap(
826 #endif
827 
833 template<bool isRequest, class Body, class Fields>
834 void
835 swap(
838 
839 } // http
840 } // beast
841 } // boost
842 
844 
845 #endif
void swap(basic_fields< Allocator > &lhs, basic_fields< Allocator > &rhs)
Definition: fields.ipp:654
Fields fields_type
The type representing the fields.
Definition: message.hpp:234
Definition: async_result.hpp:20
header_type const & base() const
Returns the header portion of the message.
Definition: message.hpp:636
Definition: type_traits.hpp:25
void keep_alive(bool value)
Definition: message.hpp:709
STL namespace.
Body::value_type body
A value representing the body.
Definition: message.hpp:436
verb
Definition: verb.hpp:26
Definition: beast_common.hpp:6
std::false_type is_request
Indicates if the header is a request or response.
Definition: message.hpp:231
Definition: type_traits.hpp:73
Definition: integer_sequence.hpp:23
boost::string_ref string_view
The type of string view used by the library.
Definition: string.hpp:36
Body body_type
Definition: message.hpp:433
typename detail::is_fields_helper< T >::type is_fields
Definition: type_traits.hpp:178
bool chunked() const
Returns true if the chunked Transfer-Encoding is specified.
Definition: message.hpp:650
bool keep_alive() const
Definition: message.hpp:693
void prepare_payload()
Definition: message.hpp:744
Definition: type_traits.hpp:22
header_type & base()
Returns the header portion of the message.
Definition: message.hpp:643
status
Definition: status.hpp:21
Fields fields_type
The type representing the fields.
Definition: message.hpp:69
std::true_type is_request
Indicates if the header is a request or response.
Definition: message.hpp:65