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