rippled
WSInfoSub.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_WSINFOSUB_H
21 #define RIPPLE_RPC_WSINFOSUB_H
22 
23 #include <ripple/beast/net/IPAddressConversion.h>
24 #include <ripple/json/json_writer.h>
25 #include <ripple/net/InfoSub.h>
26 #include <ripple/rpc/Role.h>
27 #include <ripple/server/WSSession.h>
28 #include <boost/utility/string_view.hpp>
29 #include <memory>
30 #include <string>
31 
32 namespace ripple {
33 
34 class WSInfoSub : public InfoSub
35 {
39 
40 public:
42  : InfoSub(source), ws_(ws)
43  {
44  auto const& h = ws->request();
45  if (ipAllowed(
46  beast::IPAddressConversion::from_asio(ws->remote_endpoint())
47  .address(),
48  ws->port().secure_gateway_nets_v4,
49  ws->port().secure_gateway_nets_v6))
50  {
51  auto it = h.find("X-User");
52  if (it != h.end())
53  user_ = it->value().to_string();
55  }
56  }
57 
58  boost::string_view
59  user() const
60  {
61  return user_;
62  }
63 
64  boost::string_view
65  forwarded_for() const
66  {
67  return fwdfor_;
68  }
69 
70  void
71  send(Json::Value const& jv, bool) override
72  {
73  auto sp = ws_.lock();
74  if (!sp)
75  return;
76  boost::beast::multi_buffer sb;
77  Json::stream(jv, [&](void const* data, std::size_t n) {
78  sb.commit(boost::asio::buffer_copy(
79  sb.prepare(n), boost::asio::buffer(data, n)));
80  });
81  auto m = std::make_shared<StreambufWSMsg<decltype(sb)>>(std::move(sb));
82  sp->send(m);
83  }
84 };
85 
86 } // namespace ripple
87 
88 #endif
std::string
STL class.
std::shared_ptr
STL class.
ripple::WSInfoSub
Definition: WSInfoSub.h:34
ripple::WSInfoSub::fwdfor_
std::string fwdfor_
Definition: WSInfoSub.h:38
Json::stream
void stream(Json::Value const &jv, Write const &write)
Stream compact JSON to the specified function.
Definition: json_writer.h:300
ripple::WSInfoSub::ws_
std::weak_ptr< WSSession > ws_
Definition: WSInfoSub.h:36
std::make_shared
T make_shared(T... args)
ripple::InfoSub
Manages a client's subscription to data feeds.
Definition: InfoSub.h:51
beast::IP::Endpoint::address
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:76
ripple::WSInfoSub::WSInfoSub
WSInfoSub(Source &source, std::shared_ptr< WSSession > const &ws)
Definition: WSInfoSub.h:41
ripple::WSInfoSub::send
void send(Json::Value const &jv, bool) override
Definition: WSInfoSub.h:71
beast::IPAddressConversion::from_asio
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
Definition: IPAddressConversion.h:63
ripple::WSInfoSub::user_
std::string user_
Definition: WSInfoSub.h:37
ripple::InfoSub::Source
Abstracts the source of subscription data.
Definition: InfoSub.h:67
ripple::forwardedFor
boost::string_view forwardedFor(http_request_type const &request)
Definition: Role.cpp:261
memory
ripple::StreambufWSMsg
Definition: WSSession.h:72
std::weak_ptr
STL class.
ripple::WSInfoSub::user
boost::string_view user() const
Definition: WSInfoSub.h:59
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::size_t
ripple::WSInfoSub::forwarded_for
boost::string_view forwarded_for() const
Definition: WSInfoSub.h:65
ripple::ipAllowed
bool ipAllowed(beast::IP::Address const &remoteIp, std::vector< boost::asio::ip::network_v4 > const &nets4, std::vector< boost::asio::ip::network_v6 > const &nets6)
True if remoteIp is in any of adminIp.
Definition: Role.cpp:45
Json::Value
Represents a JSON value.
Definition: json_value.h:145
string