File indexing completed on 2025-09-16 08:29:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_HTTP_SERIALIZER_HPP
0011 #define BOOST_BEAST_HTTP_SERIALIZER_HPP
0012
0013 #include <boost/beast/http/serializer_fwd.hpp>
0014
0015 #include <boost/beast/core/buffers_cat.hpp>
0016 #include <boost/beast/core/buffers_prefix.hpp>
0017 #include <boost/beast/core/buffers_suffix.hpp>
0018 #include <boost/beast/core/detail/config.hpp>
0019 #include <boost/beast/core/detail/variant.hpp>
0020 #include <boost/beast/core/string.hpp>
0021 #include <boost/beast/http/chunk_encode.hpp>
0022 #include <boost/beast/http/message.hpp>
0023 #include <boost/asio/buffer.hpp>
0024 #include <boost/optional.hpp>
0025
0026 namespace boost {
0027 namespace beast {
0028 namespace http {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 #if BOOST_BEAST_DOXYGEN
0063 template<bool isRequest, class Body, class Fields = fields>
0064 #else
0065 template<bool isRequest, class Body, class Fields>
0066 #endif
0067 class serializer
0068 {
0069 public:
0070 static_assert(is_body<Body>::value,
0071 "Body type requirements not met");
0072
0073 static_assert(is_body_writer<Body>::value,
0074 "BodyWriter type requirements not met");
0075
0076
0077
0078
0079
0080
0081 #if BOOST_BEAST_DOXYGEN
0082 using value_type = __implementation_defined__;
0083 #else
0084 using value_type = typename std::conditional<
0085 std::is_constructible<typename Body::writer,
0086 header<isRequest, Fields>&,
0087 typename Body::value_type&>::value &&
0088 ! std::is_constructible<typename Body::writer,
0089 header<isRequest, Fields> const&,
0090 typename Body::value_type const&>::value,
0091 message<isRequest, Body, Fields>,
0092 message<isRequest, Body, Fields> const>::type;
0093 #endif
0094
0095 private:
0096 enum
0097 {
0098 do_construct = 0,
0099
0100 do_init = 10,
0101 do_header_only = 20,
0102 do_header = 30,
0103 do_body = 40,
0104
0105 do_init_c = 50,
0106 do_header_only_c = 60,
0107 do_header_c = 70,
0108 do_body_c = 80,
0109 do_final_c = 90,
0110 #ifndef BOOST_BEAST_NO_BIG_VARIANTS
0111 do_body_final_c = 100,
0112 do_all_c = 110,
0113 #endif
0114
0115 do_complete = 120
0116 };
0117
0118 void fwrinit(std::true_type);
0119 void fwrinit(std::false_type);
0120
0121 template<std::size_t, class Visit>
0122 void
0123 do_visit(error_code& ec, Visit& visit);
0124
0125 using writer = typename Body::writer;
0126
0127 using cb1_t = buffers_suffix<typename
0128 Fields::writer::const_buffers_type>;
0129 using pcb1_t = buffers_prefix_view<cb1_t const&>;
0130
0131 using cb2_t = buffers_suffix<buffers_cat_view<
0132 typename Fields::writer::const_buffers_type,
0133 typename writer::const_buffers_type>>;
0134 using pcb2_t = buffers_prefix_view<cb2_t const&>;
0135
0136 using cb3_t = buffers_suffix<
0137 typename writer::const_buffers_type>;
0138 using pcb3_t = buffers_prefix_view<cb3_t const&>;
0139
0140 using cb4_t = buffers_suffix<buffers_cat_view<
0141 typename Fields::writer::const_buffers_type,
0142 detail::chunk_size,
0143 net::const_buffer,
0144 chunk_crlf,
0145 typename writer::const_buffers_type,
0146 chunk_crlf>>;
0147 using pcb4_t = buffers_prefix_view<cb4_t const&>;
0148
0149 using cb5_t = buffers_suffix<buffers_cat_view<
0150 detail::chunk_size,
0151 net::const_buffer,
0152 chunk_crlf,
0153 typename writer::const_buffers_type,
0154 chunk_crlf>>;
0155 using pcb5_t = buffers_prefix_view<cb5_t const&>;
0156
0157 using cb6_t = buffers_suffix<buffers_cat_view<
0158 detail::chunk_size,
0159 net::const_buffer,
0160 chunk_crlf,
0161 typename writer::const_buffers_type,
0162 chunk_crlf,
0163 net::const_buffer,
0164 net::const_buffer,
0165 chunk_crlf>>;
0166 using pcb6_t = buffers_prefix_view<cb6_t const&>;
0167
0168 using cb7_t = buffers_suffix<buffers_cat_view<
0169 typename Fields::writer::const_buffers_type,
0170 detail::chunk_size,
0171 net::const_buffer,
0172 chunk_crlf,
0173 typename writer::const_buffers_type,
0174 chunk_crlf,
0175 net::const_buffer,
0176 net::const_buffer,
0177 chunk_crlf>>;
0178 using pcb7_t = buffers_prefix_view<cb7_t const&>;
0179
0180 using cb8_t = buffers_suffix<buffers_cat_view<
0181 net::const_buffer,
0182 net::const_buffer,
0183 chunk_crlf>>;
0184 using pcb8_t = buffers_prefix_view<cb8_t const&>;
0185
0186 value_type& m_;
0187 writer wr_;
0188 boost::optional<typename Fields::writer> fwr_;
0189 beast::detail::variant<
0190 cb1_t, cb2_t, cb3_t, cb4_t,
0191 cb5_t ,cb6_t, cb7_t, cb8_t> v_;
0192 beast::detail::variant<
0193 pcb1_t, pcb2_t, pcb3_t, pcb4_t,
0194 pcb5_t ,pcb6_t, pcb7_t, pcb8_t> pv_;
0195 std::size_t limit_ =
0196 (std::numeric_limits<std::size_t>::max)();
0197 int s_ = do_construct;
0198 bool split_ = false;
0199 bool header_done_ = false;
0200 bool more_ = false;
0201
0202 public:
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 serializer(serializer&&) = default;
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223 serializer(serializer const&) = default;
0224
0225
0226 serializer& operator=(serializer const&) = delete;
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 explicit
0243 serializer(value_type& msg);
0244
0245
0246 value_type&
0247 get()
0248 {
0249 return m_;
0250 }
0251
0252
0253 std::size_t
0254 limit()
0255 {
0256 return limit_;
0257 }
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270 void
0271 limit(std::size_t limit)
0272 {
0273 limit_ = limit > 0 ? limit :
0274 (std::numeric_limits<std::size_t>::max)();
0275 }
0276
0277
0278
0279 bool
0280 split()
0281 {
0282 return split_;
0283 }
0284
0285
0286
0287
0288
0289
0290
0291
0292 void
0293 split(bool v)
0294 {
0295 split_ = v;
0296 }
0297
0298
0299
0300
0301
0302
0303 bool
0304 is_header_done()
0305 {
0306 return header_done_;
0307 }
0308
0309
0310
0311
0312
0313
0314
0315 bool
0316 is_done() const
0317 {
0318 return s_ == do_complete;
0319 }
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345 template<class Visit>
0346 void
0347 next(error_code& ec, Visit&& visit);
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 void
0364 consume(std::size_t n);
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376 writer&
0377 writer_impl()
0378 {
0379 return wr_;
0380 }
0381 };
0382
0383 #if BOOST_BEAST_DOXYGEN
0384
0385 template<class Body, class Fields = fields>
0386 using request_serializer = serializer<true, Body, Fields>;
0387
0388
0389 template<class Body, class Fields = fields>
0390 using response_serializer = serializer<false, Body, Fields>;
0391 #endif
0392
0393 }
0394 }
0395 }
0396
0397 #include <boost/beast/http/impl/serializer.hpp>
0398
0399 #endif