120 lines
3.9 KiB
C++
120 lines
3.9 KiB
C++
#ifndef __NETWORKUTILITY_H__
|
|
#define __NETWORKUTILITY_H__
|
|
|
|
#include "Singleton.h"
|
|
#include <boost/asio/io_context.hpp>
|
|
#include <boost/beast/http/string_body.hpp>
|
|
#include <boost/system/error_code.hpp>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace boost {
|
|
namespace asio {
|
|
namespace ssl {
|
|
class context;
|
|
}
|
|
} // namespace asio
|
|
} // namespace boost
|
|
|
|
class Network {
|
|
friend class Amass::Singleton<Network, Amass::GlobalInstance>;
|
|
|
|
public:
|
|
static bool isConnected(const std::string_view &host, size_t size);
|
|
static uint64_t htonll(uint64_t val);
|
|
static uint64_t ntohll(uint64_t val);
|
|
|
|
protected:
|
|
Network(int argc, char *argv[]);
|
|
~Network();
|
|
};
|
|
|
|
namespace Http {
|
|
using Request = boost::beast::http::request<boost::beast::http::string_body>;
|
|
enum Type {
|
|
Transparent,
|
|
SSL,
|
|
};
|
|
enum CertificateLocation {
|
|
None,
|
|
Default,
|
|
SystemDependent,
|
|
};
|
|
|
|
enum Version : unsigned {
|
|
Version_1_1 = 11,
|
|
};
|
|
struct ParseItem {
|
|
std::string filename;
|
|
// std::string::const_iterator begin;
|
|
// std::string::const_iterator end;
|
|
size_t begin;
|
|
size_t end;
|
|
};
|
|
|
|
/**
|
|
* @brief
|
|
*
|
|
* @param buffer
|
|
* @return std::vector<ParseItem>
|
|
* @example auto items = parseFormData(request.body());
|
|
* for (auto &item : items) {
|
|
* std::string filename("E:/");
|
|
* filename += item.filename;
|
|
* std::ofstream ofs(filename.c_str(), std::ios::binary);
|
|
* ofs.write(buffer.data() + item.begin, item.end - item.begin);
|
|
* ofs.close();
|
|
* }
|
|
*/
|
|
static std::vector<ParseItem> parseFormData(const std::string_view &buffer);
|
|
|
|
class ClientPrivate;
|
|
class Client {
|
|
public:
|
|
Client(boost::asio::io_context &ioContext, Type type, Version version = Version_1_1);
|
|
~Client();
|
|
void loadRootCertificates(boost::system::error_code &error);
|
|
std::string get(const std::string &host, const std::string &port, const std::string &url,
|
|
boost::system::error_code &error);
|
|
std::string post(const std::string &host, const std::string &port, const std::string &url, const std::string &body,
|
|
boost::system::error_code &error);
|
|
std::string put(const std::string &host, const std::string &port, const std::string &url, const std::string &body,
|
|
boost::system::error_code &error);
|
|
|
|
void addRequestField(boost::beast::http::field name, const std::string &value);
|
|
|
|
protected:
|
|
std::string execute(const std::string &host, const std::string &port, const Request &request,
|
|
boost::system::error_code &error);
|
|
void connect(const std::string &host, const std::string &port, boost::system::error_code &error);
|
|
void shutdown(boost::system::error_code &error);
|
|
|
|
private:
|
|
ClientPrivate *m_d = nullptr;
|
|
boost::asio::io_context &m_ioContext;
|
|
std::shared_ptr<boost::asio::ssl::context> m_sslContext;
|
|
CertificateLocation m_certificateLocation = CertificateLocation::None;
|
|
std::string m_host;
|
|
std::string m_port;
|
|
Request m_request;
|
|
};
|
|
|
|
} // namespace Http
|
|
|
|
class Https {
|
|
public:
|
|
static std::string get(boost::asio::io_context &ioContext, const std::string &host, const std::string &port,
|
|
const std::string &url, boost::system::error_code &error,
|
|
Http::Version version = Http::Version_1_1);
|
|
|
|
static std::string post(boost::asio::io_context &ioContext, const std::string &host, const std::string &port,
|
|
const std::string &url, const std::string &body, boost::system::error_code &error,
|
|
Http::Version version = Http::Version_1_1);
|
|
|
|
static std::string put(boost::asio::io_context &ioContext, const std::string &host, const std::string &port,
|
|
const std::string &url, const std::string &body, boost::system::error_code &error,
|
|
Http::Version version = Http::Version_1_1);
|
|
};
|
|
|
|
#endif // __NETWORKUTILITY_H__
|