2016-01-18 15:33:29 +00:00
|
|
|
// Copyright (c) 2011-2016 The Cryptonote developers
|
2015-09-18 11:55:31 +00:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <HTTP/HttpRequest.h>
|
|
|
|
#include <HTTP/HttpResponse.h>
|
|
|
|
#include <System/TcpConnection.h>
|
|
|
|
#include <System/TcpStream.h>
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "Serialization/SerializationTools.h"
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
namespace CryptoNote {
|
|
|
|
|
2015-08-19 17:06:24 +00:00
|
|
|
class ConnectException : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
ConnectException(const std::string& whatArg);
|
|
|
|
};
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
class HttpClient {
|
|
|
|
public:
|
|
|
|
|
|
|
|
HttpClient(System::Dispatcher& dispatcher, const std::string& address, uint16_t port);
|
2015-07-30 15:22:07 +00:00
|
|
|
~HttpClient();
|
2015-05-27 12:08:46 +00:00
|
|
|
void request(const HttpRequest& req, HttpResponse& res);
|
2015-08-19 17:06:24 +00:00
|
|
|
|
|
|
|
bool isConnected() const;
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void connect();
|
|
|
|
void disconnect();
|
|
|
|
|
|
|
|
const std::string m_address;
|
|
|
|
const uint16_t m_port;
|
|
|
|
|
|
|
|
bool m_connected = false;
|
|
|
|
System::Dispatcher& m_dispatcher;
|
|
|
|
System::TcpConnection m_connection;
|
|
|
|
std::unique_ptr<System::TcpStreambuf> m_streamBuf;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Request, typename Response>
|
|
|
|
void invokeJsonCommand(HttpClient& client, const std::string& url, const Request& req, Response& res) {
|
|
|
|
HttpRequest hreq;
|
|
|
|
HttpResponse hres;
|
|
|
|
|
|
|
|
hreq.setUrl(url);
|
2015-07-15 12:23:00 +00:00
|
|
|
hreq.setBody(storeToJson(req));
|
2015-05-27 12:08:46 +00:00
|
|
|
client.request(hreq, hres);
|
|
|
|
|
|
|
|
if (hres.getStatus() != HttpResponse::STATUS_200) {
|
|
|
|
throw std::runtime_error("HTTP status: " + std::to_string(hres.getStatus()));
|
|
|
|
}
|
|
|
|
|
2015-07-15 12:23:00 +00:00
|
|
|
if (!loadFromJson(res, hres.getBody())) {
|
2015-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("Failed to parse JSON response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Request, typename Response>
|
|
|
|
void invokeBinaryCommand(HttpClient& client, const std::string& url, const Request& req, Response& res) {
|
|
|
|
HttpRequest hreq;
|
|
|
|
HttpResponse hres;
|
|
|
|
|
|
|
|
hreq.setUrl(url);
|
2015-07-15 12:23:00 +00:00
|
|
|
hreq.setBody(storeToBinaryKeyValue(req));
|
2015-05-27 12:08:46 +00:00
|
|
|
client.request(hreq, hres);
|
|
|
|
|
2015-07-15 12:23:00 +00:00
|
|
|
if (!loadFromBinaryKeyValue(res, hres.getBody())) {
|
2015-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("Failed to parse binary response");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|