std::shared_ptr<HTTP::Writer> OverlayImpl::makeRedirectResponse (PeerFinder::Slot::ptr const& slot, beast::http::message const& request, address_type remote_address) { Json::Value json(Json::objectValue); { auto const result = m_peerFinder->redirect(slot); Json::Value& ips = (json["peer-ips"] = Json::arrayValue); for (auto const& _ : m_peerFinder->redirect(slot)) ips.append(_.address.to_string()); } beast::http::message m; m.request(false); m.status(503); m.reason("Service Unavailable"); m.headers.append("Remote-Address", remote_address.to_string()); m.version(request.version()); if (request.version() == std::make_pair(1, 0)) { //? } auto const response = HTTP::make_JsonWriter (m, json); return response; }
bool OverlayImpl::isPeerUpgrade(beast::http::message const& request) { if (! request.upgrade()) return false; auto const versions = parse_ProtocolVersions( request.headers["Upgrade"]); if (versions.size() == 0) return false; if (! request.request() && request.status() != 101) return false; return true; }
bool OverlayImpl::processRequest (beast::http::message const& req, Handoff& handoff) { if (req.url() != "/crawl") return false; beast::http::message resp; resp.request(false); resp.status(200); resp.reason("OK"); Json::Value v; v["overlay"] = crawl(); handoff.response = HTTP::make_JsonWriter(resp, v); return true; }
std::pair<protocol::TMHello, bool> parseHello (beast::http::message const& m, beast::Journal journal) { auto const& h = m.headers; std::pair<protocol::TMHello, bool> result = { {}, false }; protocol::TMHello& hello = result.first; // protocol version in TMHello is obsolete, // it is supplanted by the values in the headers. { // Required auto const iter = h.find ("Upgrade"); if (iter == h.end()) return result; auto const versions = parse_ProtocolVersions(iter->second); if (versions.empty()) return result; hello.set_protoversion( (static_cast<std::uint32_t>(versions.back().first) << 16) | (static_cast<std::uint32_t>(versions.back().second))); hello.set_protoversionmin( (static_cast<std::uint32_t>(versions.front().first) << 16) | (static_cast<std::uint32_t>(versions.front().second))); } { // Required auto const iter = h.find ("Public-Key"); if (iter == h.end()) return result; RippleAddress addr; addr.setNodePublic (iter->second); if (! addr.isValid()) return result; hello.set_nodepublic (iter->second); } { // Required auto const iter = h.find ("Session-Signature"); if (iter == h.end()) return result; // TODO Security Review hello.set_nodeproof (beast::base64_decode (iter->second)); } { auto const iter = h.find (m.request() ? "User-Agent" : "Server"); if (iter != h.end()) hello.set_fullversion (iter->second); } { auto const iter = h.find ("Network-Time"); if (iter != h.end()) { std::uint64_t nettime; if (! beast::lexicalCastChecked(nettime, iter->second)) return result; hello.set_nettime (nettime); } } { auto const iter = h.find ("Ledger"); if (iter != h.end()) { LedgerIndex ledgerIndex; if (! beast::lexicalCastChecked(ledgerIndex, iter->second)) return result; hello.set_ledgerindex (ledgerIndex); } } { auto const iter = h.find ("Closed-Ledger"); if (iter != h.end()) hello.set_ledgerclosed (beast::base64_decode (iter->second)); } { auto const iter = h.find ("Previous-Ledger"); if (iter != h.end()) hello.set_ledgerprevious (beast::base64_decode (iter->second)); } result.second = true; return result; }