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