rippled
json_body.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_RPC_JSON_BODY_H
21 #define RIPPLE_RPC_JSON_BODY_H
22 
23 #include <ripple/json/json_value.h>
24 #include <ripple/json/to_string.h>
25 
26 #include <boost/beast/core/multi_buffer.hpp>
27 #include <boost/beast/http/message.hpp>
28 
29 namespace ripple {
30 
32 struct json_body
33 {
34  explicit json_body() = default;
35 
37 
38  class reader
39  {
40  using dynamic_buffer_type = boost::beast::multi_buffer;
41 
43 
44  public:
45  using const_buffers_type =
46  typename dynamic_buffer_type::const_buffers_type;
47 
49 
50  template <bool isRequest, class Fields>
51  explicit reader(
52  boost::beast::http::message<isRequest, json_body, Fields> const& m)
53  {
54  stream(m.body, [&](void const* data, std::size_t n) {
55  buffer_.commit(boost::asio::buffer_copy(
56  buffer_.prepare(n), boost::asio::buffer(data, n)));
57  });
58  }
59 
60  void
61  init(boost::beast::error_code&) noexcept
62  {
63  }
64 
65  // get() must return a boost::optional (not a std::optional) to meet
66  // requirements of a boost::beast::BodyReader.
67  boost::optional<std::pair<const_buffers_type, bool>>
68  get(boost::beast::error_code& ec)
69  {
70  return {{buffer_.data(), false}};
71  }
72 
73  void
74  finish(boost::beast::error_code&)
75  {
76  }
77  };
78 
79  class writer
80  {
82 
83  public:
84  using const_buffers_type = boost::asio::const_buffer;
85 
86  template <bool isRequest, class Fields>
87  explicit writer(
88  boost::beast::http::header<isRequest, Fields> const& fields,
89  value_type const& value)
90  : body_string_(to_string(value))
91  {
92  }
93 
94  void
95  init(boost::beast::error_code& ec)
96  {
97  ec.assign(0, ec.category());
98  }
99 
100  // get() must return a boost::optional (not a std::optional) to meet
101  // requirements of a boost::beast::BodyWriter.
102  boost::optional<std::pair<const_buffers_type, bool>>
103  get(boost::beast::error_code& ec)
104  {
105  ec.assign(0, ec.category());
106  return {
108  false}};
109  }
110  };
111 };
112 
113 } // namespace ripple
114 
115 #endif
ripple::json_body::writer
Definition: json_body.h:79
ripple::json_body::reader::reader
reader(boost::beast::http::message< isRequest, json_body, Fields > const &m)
Definition: json_body.h:51
ripple::json_body::json_body
json_body()=default
std::false_type
ripple::json_body::reader::const_buffers_type
typename dynamic_buffer_type::const_buffers_type const_buffers_type
Definition: json_body.h:46
std::string
STL class.
ripple::json_body::writer::writer
writer(boost::beast::http::header< isRequest, Fields > const &fields, value_type const &value)
Definition: json_body.h:87
ripple::json_body::reader
Definition: json_body.h:38
ripple::json_body
Body that holds JSON.
Definition: json_body.h:32
std::string::size
T size(T... args)
ripple::json_body::writer::get
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition: json_body.h:103
ripple::json_body::reader::finish
void finish(boost::beast::error_code &)
Definition: json_body.h:74
ripple::json_body::writer::const_buffers_type
boost::asio::const_buffer const_buffers_type
Definition: json_body.h:84
ripple::json_body::writer::init
void init(boost::beast::error_code &ec)
Definition: json_body.h:95
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::json_body::reader::get
boost::optional< std::pair< const_buffers_type, bool > > get(boost::beast::error_code &ec)
Definition: json_body.h:68
ripple::json_body::reader::dynamic_buffer_type
boost::beast::multi_buffer dynamic_buffer_type
Definition: json_body.h:40
ripple::json_body::reader::buffer_
dynamic_buffer_type buffer_
Definition: json_body.h:42
std::size_t
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::json_body::reader::init
void init(boost::beast::error_code &) noexcept
Definition: json_body.h:61
ripple::json_body::writer::body_string_
std::string body_string_
Definition: json_body.h:81
std::string::data
T data(T... args)
Json::Value
Represents a JSON value.
Definition: json_value.h:145