Kylin/HttpProxy/NetworkUtility.h

68 lines
2.3 KiB
C
Raw Normal View History

2023-07-21 11:53:19 +08:00
#ifndef __NETWORKUTILITY_H__
#define __NETWORKUTILITY_H__
#include "Singleton.h"
#include <boost/asio/io_context.hpp>
#include <boost/system/error_code.hpp>
#include <string>
#include <string_view>
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();
};
class Http {
public:
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 Https {
public:
static std::string get(boost::asio::io_context &ioContext, const std::string_view &host,
const std::string_view &port, const std::string_view &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_view &host,
const std::string_view &port, const std::string_view &url, const std::string_view &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__