1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/http
7  
// Official repository: https://github.com/cppalliance/http
8  
//
8  
//
9  

9  

10  
#include <boost/http/json/json_body.hpp>
10  
#include <boost/http/json/json_body.hpp>
11  
#include <boost/http/json/json_sink.hpp>
11  
#include <boost/http/json/json_sink.hpp>
12  
#include <boost/http/field.hpp>
12  
#include <boost/http/field.hpp>
13  
#include <boost/http/method.hpp>
13  
#include <boost/http/method.hpp>
14  
#include <boost/capy/io/push_to.hpp>
14  
#include <boost/capy/io/push_to.hpp>
15  
#include <boost/json/value.hpp>
15  
#include <boost/json/value.hpp>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace http {
18  
namespace http {
19  

19  

20  
json_body::
20  
json_body::
21  
json_body(json_body_options options) noexcept
21  
json_body(json_body_options options) noexcept
22  
    : options_(std::move(options))
22  
    : options_(std::move(options))
23  
{
23  
{
24  
}
24  
}
25  

25  

26  
route_task
26  
route_task
27  
json_body::
27  
json_body::
28  
operator()(route_params& p) const
28  
operator()(route_params& p) const
29  
{
29  
{
30  
    if( ! p.is_method(method::post) ||
30  
    if( ! p.is_method(method::post) ||
31  
        ! p.req.value_or(
31  
        ! p.req.value_or(
32  
            field::content_type, "")
32  
            field::content_type, "")
33  
                .starts_with("application/json"))
33  
                .starts_with("application/json"))
34  
        co_return route_next;
34  
        co_return route_next;
35  

35  

36  
    json_sink sink(
36  
    json_sink sink(
37  
        options_.storage, options_.parse_opts);
37  
        options_.storage, options_.parse_opts);
38  
    auto [ec, n] = co_await capy::push_to(
38  
    auto [ec, n] = co_await capy::push_to(
39  
        p.req_body, sink);
39  
        p.req_body, sink);
40  
    if(ec)
40  
    if(ec)
41  
        co_return route_error(ec);
41  
        co_return route_error(ec);
42  

42  

43  
    p.route_data.emplace<json::value>(
43  
    p.route_data.emplace<json::value>(
44  
        sink.release());
44  
        sink.release());
45  

45  

46  
    co_return route_next;
46  
    co_return route_next;
47  
}
47  
}
48  

48  

49  
} // namespace http
49  
} // namespace http
50  
} // namespace boost
50  
} // namespace boost