Distribution_Service/CC_SDK/Include/CCServlet/CCHttpResolver.h

147 lines
3.5 KiB
C
Raw Permalink Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CCHTTPRESOLVER_H
#define CCHTTPRESOLVER_H
#include "CC.h"
#include "CCString.h"
#define CR '\r'
#define LF '\n'
/**
*
*/
struct StringBuffer {
char *begin = nullptr;//字符串开始位置
char *end = nullptr;//字符串结束位置
/**
* StringBuffer转换为CTL::String
* @return CTL::String对象
*/
operator CTL::String() const {
return CTL::String(begin, end);
}
};
/**
* Http请求行的状态
*/
enum class HttpRequestDecodeState {
INVALID,//无效
INVALID_METHOD,//无效请求方法
INVALID_URI,//无效的请求路径
INVALID_VERSION,//无效的协议版本号
INVALID_HEADER,//无效请求头
START,//请求行开始
METHOD,//请求方法
BEFORE_URI,//请求连接前的状态,需要'/'开头
IN_URI,//url处理
BEFORE_URI_PARAM_KEY,//URL请求参数键之前
URI_PARAM_KEY,//URL请求参数键
BEFORE_URI_PARAM_VALUE,//URL的参数值之前
URI_PARAM_VALUE,//URL请求参数值
BEFORE_PROTOCOL,//协议解析之前
PROTOCOL,//协议
BEFORE_VERSION,//版本开始前
VERSION_SPLIT,//版本分隔符 '.'
VERSION,//版本
HEADER_KEY,
HEADER_BEFORE_COLON,//冒号之前
HEADER_AFTER_COLON,//冒号
HEADER_VALUE,//值
WHEN_CR,//遇到一个回车之后
CR_LF,//回车换行
CR_LF_CR,//回车换行之后的状态
BODY,//请求体
COMPLETE,//完成
};
2025-12-03 18:08:23 +08:00
namespace CTL {
/**
2025-11-11 17:46:19 +08:00
* Http协议解析类
*/
2025-12-03 18:08:23 +08:00
class HttpResolver {
public:
/**
* http协议
* @param buf
*/
void tryDecode(const std::string &buf);
/**
*
* @return
*/
const std::string &getMethod() const;
/**
*
* @return
*/
const std::string &getUrl() const;
/**
*
* @return
*/
const std::map<std::string, std::string> &getRequestParams() const;
/**
*
* @return
*/
const std::string &getProtocol() const;
/**
*
* @return
*/
const std::string &getVersion() const;
/**
*
* @return
*/
const std::map<CTL::String, CTL::String> &getHeaders() const;
/**
*
* @return
*/
const std::string &getBody() const;
private:
/**
*
* @param buf
* @param size
*/
void parseInternal(const char *buf, int size);
private:
std::string _method;//请求方法
std::string _url;//请求路径[不包含请求参数]
std::map<std::string, std::string> _requestParams;//请求参数
std::string _protocol;//协议
std::string _version;//版本
std::map<CTL::String, CTL::String> _headers;//所有的请求头
std::string _body;//请求体
int _nextPos = 0;//下一个位置的
HttpRequestDecodeState _decodeState = HttpRequestDecodeState::START;//解析状态
};
}
typedef CTL::HttpResolver CCHttpResolver;
2025-11-11 17:46:19 +08:00
#endif