Kylin/HttpProxy/NetworkUtility.h

113 lines
3.8 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 Client {
public:
Client(boost::asio::io_context &ioContext, Type type, Version version = Version_1_1);
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);
private:
boost::asio::io_context &m_ioContext;
std::shared_ptr<boost::asio::ssl::context> m_sslContext;
CertificateLocation m_certificateLocation = CertificateLocation::None;
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__