From f5c0f6dffbd099ea83f81eea391ed5eb70be385c Mon Sep 17 00:00:00 2001 From: clemahieu Date: Fri, 13 Jan 2017 17:31:27 -0600 Subject: [PATCH] Adding Access-Control-Allow-Origin header to allow browsers to hit RPCs. --- rai/core_test/rpc.cpp | 4 ++++ rai/node/rpc.cpp | 1 + 2 files changed, 5 insertions(+) diff --git a/rai/core_test/rpc.cpp b/rai/core_test/rpc.cpp index daad93e6..2a53c77b 100644 --- a/rai/core_test/rpc.cpp +++ b/rai/core_test/rpc.cpp @@ -1226,6 +1226,10 @@ TEST (rpc, version) ASSERT_EQ (boost::network::http::server ::response::ok, static_cast (boost::network::http::status (response1.second))); ASSERT_EQ ("2", response1.first.get ("store_version")); ASSERT_EQ (boost::str (boost::format ("RaiBlocks %1%.%2%.%3%") % RAIBLOCKS_VERSION_MAJOR % RAIBLOCKS_VERSION_MINOR % RAIBLOCKS_VERSION_PATCH), response1.first.get ("node_vendor")); + auto headers (response1.second.headers ()); + auto access_control (std::find_if (headers.begin (), headers.end (), [] (decltype (*headers.begin ()) & header_a) { return header_a.first == "Access-Control-Allow-Origin"; })); + ASSERT_NE (headers.end (), access_control); + ASSERT_EQ ("*", access_control->second); rpc.stop(); thread1.join (); } diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 4ff164d1..f2ae169c 100644 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -105,6 +105,7 @@ void rai::rpc::send_response (boost::network::http::async_server ::con response->headers.push_back (boost::network::http::response_header_narrow {"content-type", "application/json"}); response->content = ostream.str (); response->headers.push_back (boost::network::http::response_header_narrow {"content-length", std::to_string (response->content.size ())}); + response->headers.push_back (boost::network::http::response_header_narrow {"Access-Control-Allow-Origin", "*"}); connection->write (response->to_buffers (), [response, connection] (boost::system::error_code const & ec) { });