File indexing completed on 2024-11-15 09:03:52
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_HTTP_MESSAGE_HPP
0011 #define BOOST_BEAST_HTTP_MESSAGE_HPP
0012
0013 #include <boost/beast/core/detail/config.hpp>
0014 #include <boost/beast/http/fields.hpp>
0015 #include <boost/beast/http/verb.hpp>
0016 #include <boost/beast/http/status.hpp>
0017 #include <boost/beast/http/type_traits.hpp>
0018 #include <boost/beast/core/string.hpp>
0019 #include <boost/core/empty_value.hpp>
0020 #include <boost/mp11/integer_sequence.hpp>
0021 #include <boost/assert.hpp>
0022 #include <boost/optional.hpp>
0023 #include <boost/throw_exception.hpp>
0024 #include <memory>
0025 #include <stdexcept>
0026 #include <string>
0027 #include <tuple>
0028 #include <utility>
0029
0030 namespace boost {
0031 namespace beast {
0032 namespace http {
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #if BOOST_BEAST_DOXYGEN
0049 template<bool isRequest, class Fields = fields>
0050 class header : public Fields
0051
0052 #else
0053 template<bool isRequest, class Fields = fields>
0054 class header;
0055
0056 template<class Fields>
0057 class header<true, Fields> : public Fields
0058 #endif
0059 {
0060 public:
0061 static_assert(is_fields<Fields>::value,
0062 "Fields type requirements not met");
0063
0064
0065 #if BOOST_BEAST_DOXYGEN
0066 using is_request = std::integral_constant<bool, isRequest>;
0067 #else
0068 using is_request = std::true_type;
0069 #endif
0070
0071
0072 using fields_type = Fields;
0073
0074
0075 header() = default;
0076
0077
0078 header(header&&) = default;
0079
0080
0081 header(header const&) = default;
0082
0083
0084 header& operator=(header&&) = default;
0085
0086
0087 header& operator=(header const&) = default;
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 unsigned version() const noexcept
0101 {
0102 return version_;
0103 }
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 void version(unsigned value) noexcept
0119 {
0120 BOOST_ASSERT(value > 0 && value < 100);
0121 version_ = value;
0122 }
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 verb
0135 method() const;
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 void
0149 method(verb v);
0150
0151
0152
0153
0154
0155
0156
0157 string_view
0158 method_string() const;
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 void
0171 method_string(string_view s);
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 string_view
0183 target() const;
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197 void
0198 target(string_view s);
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 #if BOOST_BEAST_DOXYGEN
0214 template<class... Args>
0215 explicit
0216 header(Args&&... args);
0217
0218 #else
0219 template<class Arg1, class... ArgN,
0220 class = typename std::enable_if<
0221 ! std::is_convertible<typename
0222 std::decay<Arg1>::type, header>::value &&
0223 ! std::is_convertible<typename
0224 std::decay<Arg1>::type, verb>::value &&
0225 ! std::is_convertible<typename
0226 std::decay<Arg1>::type, status>::value
0227 >::type>
0228 explicit
0229 header(Arg1&& arg1, ArgN&&... argn);
0230
0231 private:
0232 template<bool, class, class>
0233 friend class message;
0234
0235 template<class T>
0236 friend
0237 void
0238 swap(header<true, T>& m1, header<true, T>& m2);
0239
0240 template<class... FieldsArgs>
0241 header(
0242 verb method,
0243 string_view target_,
0244 unsigned version_value,
0245 FieldsArgs&&... fields_args)
0246 : Fields(std::forward<FieldsArgs>(fields_args)...)
0247 , method_(method)
0248 {
0249 version(version_value);
0250 target(target_);
0251 }
0252
0253 unsigned version_ = 11;
0254 verb method_ = verb::unknown;
0255 };
0256
0257
0258
0259
0260
0261 template<class Fields>
0262 class header<false, Fields> : public Fields
0263 {
0264 public:
0265 static_assert(is_fields<Fields>::value,
0266 "Fields type requirements not met");
0267
0268
0269 using is_request = std::false_type;
0270
0271
0272 using fields_type = Fields;
0273
0274
0275 header() = default;
0276
0277
0278 header(header&&) = default;
0279
0280
0281 header(header const&) = default;
0282
0283
0284 header& operator=(header&&) = default;
0285
0286
0287 header& operator=(header const&) = default;
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299 template<class Arg1, class... ArgN,
0300 class = typename std::enable_if<
0301 ! std::is_convertible<typename
0302 std::decay<Arg1>::type, header>::value &&
0303 ! std::is_convertible<typename
0304 std::decay<Arg1>::type, verb>::value &&
0305 ! std::is_convertible<typename
0306 std::decay<Arg1>::type, status>::value
0307 >::type>
0308 explicit
0309 header(Arg1&& arg1, ArgN&&... argn);
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 unsigned version() const noexcept
0323 {
0324 return version_;
0325 }
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340 void version(unsigned value) noexcept
0341 {
0342 BOOST_ASSERT(value > 0 && value < 100);
0343 version_ = value;
0344 }
0345 #endif
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355 status
0356 result() const;
0357
0358
0359
0360
0361
0362
0363
0364 void
0365 result(status v);
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379 void
0380 result(unsigned v);
0381
0382
0383
0384
0385
0386
0387
0388
0389 unsigned
0390 result_int() const;
0391
0392
0393
0394
0395
0396
0397
0398 string_view
0399 reason() const;
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419 void
0420 reason(string_view s);
0421
0422 private:
0423 #if ! BOOST_BEAST_DOXYGEN
0424 template<bool, class, class>
0425 friend class message;
0426
0427 template<class T>
0428 friend
0429 void
0430 swap(header<false, T>& m1, header<false, T>& m2);
0431
0432 template<class... FieldsArgs>
0433 header(
0434 status result,
0435 unsigned version_value,
0436 FieldsArgs&&... fields_args)
0437 : Fields(std::forward<FieldsArgs>(fields_args)...)
0438 , result_(result)
0439 {
0440 version(version_value);
0441 }
0442
0443 unsigned version_ = 11;
0444 status result_ = status::ok;
0445 #endif
0446 };
0447
0448
0449 template<class Fields = fields>
0450 using request_header = header<true, Fields>;
0451
0452
0453 template<class Fields = fields>
0454 using response_header = header<false, Fields>;
0455
0456 #if defined(BOOST_MSVC)
0457
0458 namespace detail {
0459 template<class T>
0460 using value_type_t = typename T::value_type;
0461 }
0462 #endif
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493 template<bool isRequest, class Body, class Fields = fields>
0494 class message
0495 : public header<isRequest, Fields>
0496 #if ! BOOST_BEAST_DOXYGEN
0497 , boost::empty_value<
0498 typename Body::value_type>
0499 #endif
0500 {
0501 public:
0502
0503 using header_type = header<isRequest, Fields>;
0504
0505
0506
0507
0508
0509 using body_type = Body;
0510
0511
0512 message() = default;
0513
0514
0515 message(message&&) = default;
0516
0517
0518 message(message const&) = default;
0519
0520
0521 message& operator=(message&&) = default;
0522
0523
0524 message& operator=(message const&) = default;
0525
0526
0527
0528
0529
0530
0531
0532
0533 template<class... BodyArgs>
0534 explicit
0535 message(header_type&& h, BodyArgs&&... body_args);
0536
0537
0538
0539
0540
0541
0542
0543
0544 template<class... BodyArgs>
0545 explicit
0546 message(header_type const& h, BodyArgs&&... body_args);
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 #if BOOST_BEAST_DOXYGEN
0559 message(verb method, string_view target, unsigned version);
0560 #else
0561 template<class Version,
0562 class = typename std::enable_if<isRequest &&
0563 std::is_convertible<Version, unsigned>::value>::type>
0564 message(verb method, string_view target, Version version);
0565 #endif
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579 #if BOOST_BEAST_DOXYGEN
0580 template<class BodyArg>
0581 message(verb method, string_view target,
0582 unsigned version, BodyArg&& body_arg);
0583 #else
0584 template<class Version, class BodyArg,
0585 class = typename std::enable_if<isRequest &&
0586 std::is_convertible<Version, unsigned>::value>::type>
0587 message(verb method, string_view target,
0588 Version version, BodyArg&& body_arg);
0589 #endif
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605 #if BOOST_BEAST_DOXYGEN
0606 template<class BodyArg, class FieldsArg>
0607 message(verb method, string_view target, unsigned version,
0608 BodyArg&& body_arg, FieldsArg&& fields_arg);
0609 #else
0610 template<class Version, class BodyArg, class FieldsArg,
0611 class = typename std::enable_if<isRequest &&
0612 std::is_convertible<Version, unsigned>::value>::type>
0613 message(verb method, string_view target, Version version,
0614 BodyArg&& body_arg, FieldsArg&& fields_arg);
0615 #endif
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625 #if BOOST_BEAST_DOXYGEN
0626 message(status result, unsigned version);
0627 #else
0628 template<class Version,
0629 class = typename std::enable_if<! isRequest &&
0630 std::is_convertible<Version, unsigned>::value>::type>
0631 message(status result, Version version);
0632 #endif
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644 #if BOOST_BEAST_DOXYGEN
0645 template<class BodyArg>
0646 message(status result, unsigned version, BodyArg&& body_arg);
0647 #else
0648 template<class Version, class BodyArg,
0649 class = typename std::enable_if<! isRequest &&
0650 std::is_convertible<Version, unsigned>::value>::type>
0651 message(status result, Version version, BodyArg&& body_arg);
0652 #endif
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666 #if BOOST_BEAST_DOXYGEN
0667 template<class BodyArg, class FieldsArg>
0668 message(status result, unsigned version,
0669 BodyArg&& body_arg, FieldsArg&& fields_arg);
0670 #else
0671 template<class Version, class BodyArg, class FieldsArg,
0672 class = typename std::enable_if<! isRequest &&
0673 std::is_convertible<Version, unsigned>::value>::type>
0674 message(status result, Version version,
0675 BodyArg&& body_arg, FieldsArg&& fields_arg);
0676 #endif
0677
0678
0679
0680
0681
0682 explicit
0683 message(std::piecewise_construct_t);
0684
0685
0686
0687
0688
0689
0690 template<class... BodyArgs>
0691 message(std::piecewise_construct_t,
0692 std::tuple<BodyArgs...> body_args);
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702 template<class... BodyArgs, class... FieldsArgs>
0703 message(std::piecewise_construct_t,
0704 std::tuple<BodyArgs...> body_args,
0705 std::tuple<FieldsArgs...> fields_args);
0706
0707
0708 header_type const&
0709 base() const
0710 {
0711 return *this;
0712 }
0713
0714
0715 header_type&
0716 base()
0717 {
0718 return *this;
0719 }
0720
0721
0722 bool
0723 chunked() const
0724 {
0725 return this->get_chunked_impl();
0726 }
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739 void
0740 chunked(bool value);
0741
0742
0743
0744
0745
0746
0747
0748 bool
0749 has_content_length() const
0750 {
0751 return this->has_content_length_impl();
0752 }
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768 void
0769 content_length(boost::optional<std::uint64_t> const& value);
0770
0771
0772
0773
0774
0775
0776
0777 bool
0778 keep_alive() const
0779 {
0780 return this->get_keep_alive_impl(this->version());
0781 }
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793 void
0794 keep_alive(bool value)
0795 {
0796 this->set_keep_alive_impl(this->version(), value);
0797 }
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821 bool
0822 need_eof() const
0823 {
0824 return need_eof(typename header_type::is_request{});
0825 }
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839 boost::optional<std::uint64_t>
0840 payload_size() const;
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856 void
0857 prepare_payload()
0858 {
0859 prepare_payload(typename header_type::is_request{});
0860 }
0861
0862
0863 #if BOOST_BEAST_DOXYGEN || ! defined(BOOST_MSVC)
0864 typename body_type::value_type&
0865 #else
0866 detail::value_type_t<Body>&
0867 #endif
0868 body()& noexcept
0869 {
0870 return this->boost::empty_value<
0871 typename Body::value_type>::get();
0872 }
0873
0874
0875 #if BOOST_BEAST_DOXYGEN || ! defined(BOOST_MSVC)
0876 typename body_type::value_type&&
0877 #else
0878 detail::value_type_t<Body>&&
0879 #endif
0880 body()&& noexcept
0881 {
0882 return std::move(
0883 this->boost::empty_value<
0884 typename Body::value_type>::get());
0885 }
0886
0887
0888 #if BOOST_BEAST_DOXYGEN || ! defined(BOOST_MSVC)
0889 typename body_type::value_type const&
0890 #else
0891 detail::value_type_t<Body> const&
0892 #endif
0893 body() const& noexcept
0894 {
0895 return this->boost::empty_value<
0896 typename Body::value_type>::get();
0897 }
0898
0899 private:
0900 static_assert(is_body<Body>::value,
0901 "Body type requirements not met");
0902
0903 template<
0904 class... BodyArgs,
0905 std::size_t... IBodyArgs>
0906 message(
0907 std::piecewise_construct_t,
0908 std::tuple<BodyArgs...>& body_args,
0909 mp11::index_sequence<IBodyArgs...>)
0910 : boost::empty_value<
0911 typename Body::value_type>(boost::empty_init_t(),
0912 std::forward<BodyArgs>(
0913 std::get<IBodyArgs>(body_args))...)
0914 {
0915 boost::ignore_unused(body_args);
0916 }
0917
0918 template<
0919 class... BodyArgs,
0920 class... FieldsArgs,
0921 std::size_t... IBodyArgs,
0922 std::size_t... IFieldsArgs>
0923 message(
0924 std::piecewise_construct_t,
0925 std::tuple<BodyArgs...>& body_args,
0926 std::tuple<FieldsArgs...>& fields_args,
0927 mp11::index_sequence<IBodyArgs...>,
0928 mp11::index_sequence<IFieldsArgs...>)
0929 : header_type(std::forward<FieldsArgs>(
0930 std::get<IFieldsArgs>(fields_args))...)
0931 , boost::empty_value<
0932 typename Body::value_type>(boost::empty_init_t(),
0933 std::forward<BodyArgs>(
0934 std::get<IBodyArgs>(body_args))...)
0935 {
0936 boost::ignore_unused(body_args);
0937 boost::ignore_unused(fields_args);
0938 }
0939
0940 bool
0941 need_eof(std::true_type) const
0942 {
0943 return ! keep_alive();
0944 }
0945
0946 bool
0947 need_eof(std::false_type) const;
0948
0949 boost::optional<std::uint64_t>
0950 payload_size(std::true_type) const
0951 {
0952 return Body::size(this->body());
0953 }
0954
0955 boost::optional<std::uint64_t>
0956 payload_size(std::false_type) const
0957 {
0958 return boost::none;
0959 }
0960
0961 void
0962 prepare_payload(std::true_type);
0963
0964 void
0965 prepare_payload(std::false_type);
0966 };
0967
0968
0969 template<class Body, class Fields = fields>
0970 using request = message<true, Body, Fields>;
0971
0972
0973 template<class Body, class Fields = fields>
0974 using response = message<false, Body, Fields>;
0975
0976
0977
0978 #if BOOST_BEAST_DOXYGEN
0979
0980
0981
0982
0983
0984 template<bool isRequest, class Fields>
0985 void
0986 swap(
0987 header<isRequest, Fields>& m1,
0988 header<isRequest, Fields>& m2);
0989 #endif
0990
0991
0992
0993
0994
0995
0996 template<bool isRequest, class Body, class Fields>
0997 void
0998 swap(
0999 message<isRequest, Body, Fields>& m1,
1000 message<isRequest, Body, Fields>& m2);
1001
1002 }
1003 }
1004 }
1005
1006 #include <boost/beast/http/impl/message.hpp>
1007
1008 #endif