update:http&&md5

This commit is contained in:
2025-09-08 15:12:25 +08:00
parent d5250fff74
commit d32f0e9f90
6 changed files with 354 additions and 26 deletions

View File

@@ -20,10 +20,96 @@
#include "httplib.h"
#include <string>
#include <functional>
#include <optional>
#include <future>
#include <chrono>
// C++17/14 可选类型回退适配:统一使用 ntq::optional / ntq::nullopt
#if defined(__has_include)
#if __has_include(<optional>)
#include <optional>
// 仅当启用了 C++17 或库声明了 optional 功能时,才使用 std::optional
#if defined(__cpp_lib_optional) || (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
namespace ntq { template <typename T> using optional = std::optional<T>; }
namespace ntq { constexpr auto nullopt = std::nullopt; using nullopt_t = decltype(std::nullopt); }
#elif __has_include(<experimental/optional>)
#include <experimental/optional>
namespace ntq { template <typename T> using optional = std::experimental::optional<T>; }
namespace ntq { constexpr auto nullopt = std::experimental::nullopt; using nullopt_t = decltype(std::experimental::nullopt); }
#else
#include <utility>
namespace ntq {
struct nullopt_t { explicit constexpr nullopt_t(int) {} };
static constexpr nullopt_t nullopt{0};
template <typename T>
class optional {
public:
optional() : has_(false) {}
optional(nullopt_t) : has_(false) {}
optional(const T &v) : has_(true), value_(v) {}
optional(T &&v) : has_(true), value_(std::move(v)) {}
optional(const optional &o) : has_(o.has_), value_(o.has_ ? o.value_ : T{}) {}
optional(optional &&o) noexcept : has_(o.has_), value_(std::move(o.value_)) {}
optional &operator=(nullopt_t) { has_ = false; return *this; }
optional &operator=(const T &v) { value_ = v; has_ = true; return *this; }
optional &operator=(T &&v) { value_ = std::move(v); has_ = true; return *this; }
explicit operator bool() const { return has_; }
bool has_value() const { return has_; }
T &value() { return value_; }
const T &value() const { return value_; }
T &operator*() { return value_; }
const T &operator*() const { return value_; }
private:
bool has_ = false;
T value_{};
};
}
#endif
#elif __has_include(<experimental/optional>)
#include <experimental/optional>
namespace ntq { template <typename T> using optional = std::experimental::optional<T>; }
namespace ntq { constexpr auto nullopt = std::experimental::nullopt; using nullopt_t = decltype(std::experimental::nullopt); }
#else
#include <utility>
namespace ntq {
struct nullopt_t { explicit constexpr nullopt_t(int) {} };
static constexpr nullopt_t nullopt{0};
template <typename T>
class optional {
public:
optional() : has_(false) {}
optional(nullopt_t) : has_(false) {}
optional(const T &v) : has_(true), value_(v) {}
optional(T &&v) : has_(true), value_(std::move(v)) {}
optional(const optional &o) : has_(o.has_), value_(o.has_ ? o.value_ : T{}) {}
optional(optional &&o) noexcept : has_(o.has_), value_(std::move(o.value_)) {}
optional &operator=(nullopt_t) { has_ = false; return *this; }
optional &operator=(const T &v) { value_ = v; has_ = true; return *this; }
optional &operator=(T &&v) { value_ = std::move(v); has_ = true; return *this; }
explicit operator bool() const { return has_; }
bool has_value() const { return has_; }
T &value() { return value_; }
const T &value() const { return value_; }
T &operator*() { return value_; }
const T &operator*() const { return value_; }
private:
bool has_ = false;
T value_{};
};
}
#endif
#else
// 无 __has_include按语言级别判断
#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
#include <optional>
namespace ntq { template <typename T> using optional = std::optional<T>; }
namespace ntq { constexpr auto nullopt = std::nullopt; using nullopt_t = decltype(std::nullopt); }
#else
#include <experimental/optional>
namespace ntq { template <typename T> using optional = std::experimental::optional<T>; }
namespace ntq { constexpr auto nullopt = std::experimental::nullopt; using nullopt_t = decltype(std::experimental::nullopt); }
#endif
#endif
namespace ntq
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -137,7 +223,7 @@ namespace ntq
* @param err 可选错误码输出
* @return 成功返回响应对象,失败返回 std::nullopt
*/
std::optional<HttpResponse> Get(const std::string &path,
ntq::optional<HttpResponse> Get(const std::string &path,
const httplib::Params &query = {},
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
@@ -150,7 +236,7 @@ namespace ntq
* @param err 可选错误码输出
* @return 成功返回响应对象,失败返回 std::nullopt
*/
std::optional<HttpResponse> PostJson(const std::string &path,
ntq::optional<HttpResponse> PostJson(const std::string &path,
const std::string &json,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
@@ -163,7 +249,7 @@ namespace ntq
* @param err 可选错误码输出
* @return 成功返回响应对象,失败返回 std::nullopt
*/
std::optional<HttpResponse> PostForm(const std::string &path,
ntq::optional<HttpResponse> PostForm(const std::string &path,
const httplib::Params &form,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
@@ -172,7 +258,7 @@ namespace ntq
* @brief 异步 GET 请求
* @return std::future用于获取响应结果
*/
std::future<std::optional<HttpResponse>> GetAsync(const std::string &path,
std::future<ntq::optional<HttpResponse>> GetAsync(const std::string &path,
const httplib::Params &query = {},
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
@@ -181,7 +267,7 @@ namespace ntq
* @brief 异步 POST JSON 请求
* @return std::future用于获取响应结果
*/
std::future<std::optional<HttpResponse>> PostJsonAsync(const std::string &path,
std::future<ntq::optional<HttpResponse>> PostJsonAsync(const std::string &path,
const std::string &json,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
@@ -190,7 +276,7 @@ namespace ntq
* @brief 异步 POST 表单请求
* @return std::future用于获取响应结果
*/
std::future<std::optional<HttpResponse>> PostFormAsync(const std::string &path,
std::future<ntq::optional<HttpResponse>> PostFormAsync(const std::string &path,
const httplib::Params &form,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
@@ -217,6 +303,40 @@ namespace ntq
*/
Stats getStats() const;
/**
* @brief 便捷:直接用完整 URL 发起 GET无需显式实例化
* @param url 形如 http://host:port/path?x=1 或 https://host/path
* @param headers 额外头部
* @param err 可选错误码输出
*/
static ntq::optional<HttpResponse> QuickGet(const std::string &url,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief 便捷:直接用完整 URL 发起 POST JSON无需显式实例化
* @param url 形如 http://host:port/path?x=1 或 https://host/path
* @param json JSON 字符串Content-Type: application/json
* @param headers 额外头部
* @param err 可选错误码输出
*/
static ntq::optional<HttpResponse> QuickPostJson(const std::string &url,
const std::string &json,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
/**
* @brief 便捷:直接用完整 URL 发起 POST 表单(无需显式实例化)
* @param url 形如 http://host:port/path?x=1 或 https://host/path
* @param form 表单参数
* @param headers 额外头部
* @param err 可选错误码输出
*/
static ntq::optional<HttpResponse> QuickPostForm(const std::string &url,
const httplib::Params &form,
const httplib::Headers &headers = {},
ErrorCode *err = nullptr);
private:
struct Impl;
Impl *impl_;

14
include/encrypt.hpp Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
/*
主要是用于各种加密
*/
#include "QCL_Include.hpp"
using namespace std;
namespace encrypt
{
string MD5(const string &info);
}