initial commit

This commit is contained in:
2025-09-01 20:47:58 +08:00
commit 12c51b5ddb
843 changed files with 475321 additions and 0 deletions
+87
View File
@@ -0,0 +1,87 @@
#ifndef UTIL_AUTHTOOl_H
#define UTIL_AUTHTOOl_H
#include <stdint.h>
#include <map>
#include <string>
#include <vector>
#include "request/base_req.h"
#include "util/noncopyable.h"
namespace qcloud_cos {
class AuthTool : private NonCopyable {
public:
/// \brief 返回签名,可以在指定的有效期内(通过CosSysConfig设置, 默认60s)使用
///
/// \param secret_id 开发者拥有的项目身份识别 ID,用以身份认证
/// \param secret_key 开发者拥有的项目身份密钥
/// \param http_method http方法,如POST/GET/HEAD/PUT等, 传入大小写不敏感
/// \param in_uri http uri
/// \param headers http header的键值对
/// \param params http params的键值对
///
/// \return 字符串形式的签名,返回空串代表失败
static std::string Sign(const std::string& secret_id,
const std::string& secret_key,
const std::string& http_method,
const std::string& in_uri,
const std::map<std::string, std::string>& headers,
const std::map<std::string, std::string>& params);
/// \brief 返回签名,可以在指定的有效期内使用
///
/// \param secret_id 开发者拥有的项目身份识别 ID,用以身份认证
/// \param secret_key 开发者拥有的项目身份密钥
/// \param http_method http方法,如POST/GET/HEAD/PUT等, 传入大小写不敏感
/// \param in_uri http uri
/// \param headers http header的键值对
/// \param params http params的键值对
///
/// \return 字符串形式的签名,返回空串代表失败
static std::string Sign(const std::string& secret_id,
const std::string& secret_key,
const std::string& http_method,
const std::string& in_uri,
const std::map<std::string, std::string>& headers,
const std::map<std::string, std::string>& params,
uint64_t start_time_in_s,
uint64_t end_time_in_s);
/// \brief get rtmp signature
static std::string RtmpSign(const std::string& secret_id,
const std::string& secret_key,
const std::string& token,
const std::string& bucket,
const std::string& channel,
const std::map<std::string, std::string>& params,
uint64_t expire);
private:
/// \brief 把params中的数据,转小写,正排,key放在param_list key=value放param_value_list
/// \param params 参数
/// \param key_encode key是否进行uri编码
/// \param value_encode value是否进行uri编码
/// \param value_lower value是否小写
/// \param param_list 参数名列表,以;分隔
/// \param param_value_list 参数键值对列表,以&分隔
/// \retval 无
static void FillMap(const std::map<std::string,std::string> &params,
bool key_encode,
bool value_encode,
bool value_lower,
std::string* param_list,
std::string* param_value_list);
/// \brief 找出需要鉴权的头部,并设置,目前host conent-type 还有x开头的都要鉴权
/// \param hedaers 头部的kv对
/// \param filted_req_headers 需要鉴权的头部
/// \retval 无
static void FilterAndSetSignHeader(const std::map<std::string,std::string>& headers,
std::map<std::string, std::string>* filted_req_headers);
};
} // namespace qcloud_cos
#endif // AUTHTOOL_H
+88
View File
@@ -0,0 +1,88 @@
#ifndef CODEC_UTIL_H
#define CODEC_UTIL_H
#include <stdint.h>
#include <string>
namespace qcloud_cos {
class CodecUtil {
public:
/**
* @brief 将字符x转成十六进制 (x的值[0, 15])
*
* @param x
*
* @return 十六进制字符
*/
static unsigned char ToHex(const unsigned char& x);
/**
* @brief 将二进制数据转成十六进制 (x的值[0, 15]),上层调用保证hex的大小足够
*
* @param bin
*
* @param binLen
*
* @param hex 存放结果的数据块
*
* @return 无
*/
static void BinToHex(const unsigned char *bin,unsigned int binLen, char *hex);
static std::string EncodeKey(const std::string& key);
/**
* @brief 对字符串进行URL编码
*
* @param str 带编码的字符串
*
* @return 经过URL编码的字符串
*/
static std::string UrlEncode(const std::string& str);
/**
* @brief 对字符串进行base64编码
*
* @param plainText 待编码的字符串
*
* @return 编码后的字符串
*/
static std::string Base64Encode(const std::string& plainText);
/**
* @brief 获取hmacSha1值
*
* @param plainText 明文
* @param key 秘钥
*
* @return 获取的hmacsha1值
*/
static std::string HmacSha1(const std::string& plainText,
const std::string& key);
/**
* @brief 获取hmacSha1的16进制值
*
* @param plainText 明文
* @param key 秘钥
*
* @return 获取的hmacsha1的16进制值
*/
static std::string HmacSha1Hex(const std::string& plainText,
const std::string& key);
static std::string RawMd5(const std::string& plainText);
static std::string RawMd51(const std::string& plainText);
static std::string HexToBin(const std::string &strHex);
};
} // namespace qcloud_cos
#endif
+14
View File
@@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
#include <cstddef>
namespace qcloud_cos {
class CRC64
{
public:
static uint64_t CalcCRC(uint64_t crc, void *buf, size_t len);
static uint64_t CombineCRC(uint64_t crc1, uint64_t crc2, uintmax_t len2);
static uint64_t CalcCRC(uint64_t crc, void *buf, size_t len, bool little);
};
} // namespace qcloud_cos
+29
View File
@@ -0,0 +1,29 @@
#ifndef FILE_UTIL_H
#define FILE_UTIL_H
#include <stdint.h>
#include <fstream>
#include <iostream>
#include <string>
namespace qcloud_cos {
class FileUtil {
public:
//获取文件内容
static std::string GetFileContent(const std::string& path);
//返回文件大小
static uint64_t GetFileLen(const std::string& path);
static bool IsDirectoryExists(const std::string& path);
static bool IsDirectory(const std::string& path);
static std::string GetDirectory(const std::string& path);
static uint64_t GetFileCrc64(const std::string& file);
};
} // namespace qcloud_cos
#endif
+100
View File
@@ -0,0 +1,100 @@
// Copyright (c) 2017, Tencent Inc.
// All rights reserved.
//
// Author: sevenyou <sevenyou@tencent.com>
// Created: 07/18/17
// Description:
#ifndef HTTP_SENDER_H
#define HTTP_SENDER_H
#pragma once
#include <stdint.h>
#include <map>
#include <string>
#include "trsf/transfer_handler.h"
namespace qcloud_cos {
class HttpSender {
public:
static int SendRequest(const std::string& http_method,
const std::string& url_str,
const std::map<std::string, std::string>& req_params,
const std::map<std::string, std::string>& req_headers,
const std::string& req_body,
uint64_t conn_timeout_in_ms,
uint64_t recv_timeout_in_ms,
std::map<std::string, std::string>* resp_headers,
std::string* resp_body,
std::string* err_msg,
bool is_check_md5 = false);
static int SendRequest(const std::string& http_method,
const std::string& url_str,
const std::map<std::string, std::string>& req_params,
const std::map<std::string, std::string>& req_headers,
const std::string& req_body,
uint64_t conn_timeout_in_ms,
uint64_t recv_timeout_in_ms,
std::map<std::string, std::string>* resp_headers,
std::ostream& resp_stream,
std::string* err_msg,
bool is_check_md5 = false);
static int SendRequest(const std::string& http_method,
const std::string& url_str,
const std::map<std::string, std::string>& req_params,
const std::map<std::string, std::string>& req_headers,
std::istream& is,
uint64_t conn_timeout_in_ms,
uint64_t recv_timeout_in_ms,
std::map<std::string, std::string>* resp_headers,
std::string* resp_body,
std::string* err_msg,
bool is_check_md5 = false);
static int SendRequest(const std::string& http_method,
const std::string& url_str,
const std::map<std::string, std::string>& req_params,
const std::map<std::string, std::string>& req_headers,
std::istream& is,
uint64_t conn_timeout_in_ms,
uint64_t recv_timeout_in_ms,
std::map<std::string, std::string>* resp_headers,
std::ostream& resp_stream,
std::string* err_msg,
bool is_check_md5 = false);
static int SendRequest(const std::string& http_method,
const std::string& url_str,
const std::map<std::string, std::string>& req_params,
const std::map<std::string, std::string>& req_headers,
const std::string& req_body,
uint64_t conn_timeout_in_ms,
uint64_t recv_timeout_in_ms,
std::map<std::string, std::string>* resp_headers,
std::string* xml_err_str,
std::ostream& resp_stream,
std::string* err_msg,
uint64_t* real_byte,
bool is_check_md5 = false);
/// \brief 支持更新进度
static int TransferSendRequest(const SharedTransferHandler& handler,
const std::string& http_method,
const std::string& url_str,
const std::map<std::string, std::string>& req_params,
const std::map<std::string, std::string>& req_headers,
std::istream& is,
uint64_t conn_timeout_in_ms,
uint64_t recv_timeout_in_ms,
std::map<std::string, std::string>* resp_headers,
std::ostream& resp_stream,
std::string* err_msg,
bool is_check_md5 = false);
};
} // namespace qcloud_cos
#endif // HTTP_SENDER_H
+21
View File
@@ -0,0 +1,21 @@
#ifndef LOG_UTIL_H
#define LOG_UTIL_H
#include <stdint.h>
#include <string>
#include "cos_defines.h"
namespace qcloud_cos {
typedef void (*LogCallback)(const std::string& logstr);
class LogUtil {
public:
static std::string GetLogPrefix(int level);
static std::string FormatLog(int level, const char* fmt, ...);
static void Syslog(int level, const char* fmt, ...);
};
}
#endif
+24
View File
@@ -0,0 +1,24 @@
// Copyright (c) 2017, Tencent Inc.
// All rights reserved.
//
// Author: sevenyou <sevenyou@tencent.com>
// Created: 08/03/17
// Description:
#ifndef NONCOPYABLE_H
#define NONCOPYABLE_H
#pragma once
namespace qcloud_cos {
class NonCopyable {
protected:
NonCopyable() {}
~NonCopyable() {}
private: // emphasize the following members are private
NonCopyable(const NonCopyable&);
const NonCopyable& operator=(const NonCopyable&);
};
}
#endif // NONCOPYABLE_H
+49
View File
@@ -0,0 +1,49 @@
#ifndef SHA_H
#define SHA_H
#include <stdlib.h>
#include <stdio.h>
#include <string>
namespace qcloud_cos {
/* Useful defines & typedefs */
typedef unsigned char SHA_BYTE; /* 8-bit quantity */
// SHA_LONG already defined in openssl/sha.h
typedef unsigned int SHA_LONG; /* 32-or-more-bit quantity */
#define SHA_BYTE_ORDER 1234
#define SHA_VERSION 1
#define SHA_BLOCKSIZE 64
#define SHA_DIGESTSIZE 20
typedef struct {
SHA_LONG digest[5]; /* message digest */
SHA_LONG count_lo, count_hi; /* 64-bit bit count */
SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
int local; /* unprocessed amount in data */
} SHA_INFO;
void ShaInit(SHA_INFO *);
void ShaUpdate(SHA_INFO *, SHA_BYTE *, int);
void ShaFinal(unsigned char [20], SHA_INFO *);
void ShaOutput(unsigned char [20],unsigned char [40]);
const char* ShaVersion(void);
class Sha1 {
public:
Sha1();
~Sha1();
void Append(const char* data, unsigned int size);
std::string Final();
private:
SHA_INFO m_sha;
};
}
#endif /* SHA_H */
+246
View File
@@ -0,0 +1,246 @@
#ifndef STRING_UTIL_H
#define STRING_UTIL_H
#include <stdint.h>
#include <string>
#include "rapidxml/1.13/rapidxml.hpp"
#include "rapidxml/1.13/rapidxml_utils.hpp"
#include "rapidxml/1.13/rapidxml_print.hpp"
#include "cos_defines.h"
namespace qcloud_cos{
class StringUtil {
public:
/**
* @brief 去除string两端的空格
*
* @param s: 待去除空格的字符串,是入参也是出参
*
* @return 返回去除空格后的字符串,即s
*/
static std::string& Trim(std::string& s);
/**
* @brief 去除string两端指定的字符串
*
* @param s: 待去除的字符串,入参
* @param trim_value: 删除的字符串
*
* @return 返回Trim后的字符串
*/
static std::string Trim(const std::string& s, const std::string& trim_value);
/**
* @brief 将xml转为string
*
* @param xmlObject 需要转换xml对象
*
* @return 返回转换后的string
*/
static std::string XmlToString(const rapidxml::xml_document<>& xml_doc);
/**
* @brief 将string转换为xml
*
* @param xmlStr 待转换的string对象
* @param doc 待返回的xml对象[out]
*
* @return 转换成功返回true,否则返回false
*/
static bool StringToXml(char* xml_str, rapidxml::xml_document<>* doc);
/**
* @brief 把uint64_t类型的num转换成std::string,长度为8个字节
*
* @param num uint64_t类型
*
* @return 转换后的string
*/
static std::string Uint64ToString(uint64_t num);
/**
* @brief 将int转为string
*
* @param num int类型
*
* @return 转换后的string
*/
static std::string IntToString(int num);
/**
* @brief 字符串就地转成全大写
*
* @param s 指向string类型的指针
*
* @return void
*/
static void StringToUpper(std::string* s);
/**
* @brief 字符串转成全大写
*
* @param s string类型的常引用
*
* @return 全大写字符串
*/
static std::string StringToUpper(const std::string& s);
/**
* @brief 字符串就地转成全小写
*
* @param s 指向string类型的指针
*
* @return void
*/
static void StringToLower(std::string* s);
/**
* @brief 字符串转成全小写
*
* @param s string类型的常引用
*
* @return 全大写字符串
*/
static std::string StringToLower(const std::string& s);
/**
* @brief 字符串连接
*
* @param str_vec 字符串vector
* @param delimiter 分隔符
*
* @return 字符串
*/
static std::string JoinStrings(const std::vector<std::string>& str_vec,
const std::string& delimiter);
/**
* @brief 将string转为uint64_t
*
* @param str string类型
*
* @return 转换后的uint64_t
*/
static uint64_t StringToUint64(const std::string& str);
/**
* @brief 将string转为int
*
* @param str string类型
*
* @return 转换后的int
*/
static int StringToInt(const std::string& str);
/**
* @brief 判断字符串是否以指定前缀开头
*
* @param str string类型
* @param prefix string类型
*
* @return str的前缀为prefix,则返回true;反之,返回false
*/
static bool StringStartsWith(const std::string& str, const std::string& prefix);
/**
* @brief 判断字符串是否以指定前缀开头(忽略大小写)
*
* @param str string类型
* @param prefix string类型
*
* @return str的前缀为prefix,则返回true;反之,返回false
*/
static bool StringStartsWithIgnoreCase(const std::string& str, const std::string& prefix);
/**
* @brief 移除前缀
*
* @param str string类型
* @param prefix string类型
*
* @return 返回移除了prefix前缀的字符串
*/
static std::string StringRemovePrefix(const std::string& str, const std::string& prefix);
/**
* @brief 判断字符串是否以指定后缀结尾
*
* @param str string类型
* @param suffix string类型
*
* @return str的后缀为suffix,则返回true;反之,返回false
*/
static bool StringEndsWith(const std::string& str, const std::string& suffix);
/**
* @brief 判断字符串是否以指定后缀结尾(忽略大小写)
*
* @param str string类型
* @param suffix string类型
*
* @return str的后缀为suffix,则返回true;反之,返回false
*/
static bool StringEndsWithIgnoreCase(const std::string& str, const std::string& suffix);
/**
* @brief 移除后缀
*
* @param str string类型
* @param suffix string类型
*
* @return 返回移除了suffix前缀的字符串
*/
static std::string StringRemoveSuffix(const std::string& str, const std::string& suffix);
/**
* @brief 分割字符串
*
* @param str string类型,待分割字符串
* @param delim char类型,分割符
* @param vec std::vector类型,分割结果
*
* @return void
*/
static void SplitString(const std::string& str, char delim, std::vector<std::string>* vec);
/**
* @brief 分割字符串
*
* @param str string类型,待分割字符串
* @param sep string类型,分割字符串
* @param vec std::vector类型,分割结果
*
* @return void
*/
static void SplitString(const std::string& str, const std::string& sep, std::vector<std::string>* vec);
/**
* @brief 将HTTP_METHOD转成对应的字符串格式
*
* @param method HTTP_METHOD
*
* @return string
*/
static std::string HttpMethodToString(HTTP_METHOD method);
static bool IsV4ETag(const std::string& etag);
static bool IsMultipartUploadETag(const std::string& etag);
/**
* @brief 从字符串中获取uint32整型
*/
static uint32_t GetUint32FromStrWithBigEndian(const char *str);
/**
* @brief 从字符串中获取uint16整型
*/
static uint16_t GetUint16FromStrWithBigEndian(const char * str);
};
}
#endif