http_define.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #pragma once
  2. #include <vector>
  3. #include <map>
  4. #include <regex>
  5. #include "define.h"
  6. #include "util/file.h"
  7. #include "base/buffer.h"
  8. #include "base/environment.h"
  9. #define POINT_QUEUE_REQUEST_CLEAR_MAX 1000
  10. #define POINT_QUEUE_REQUEST_CLEAR_SEC 60
  11. #define POINT_QUEUE_RESPONSE_CLEAR_MAX 1000
  12. #define POINT_QUEUE_RESPONSE_CLEAR_SEC 60
  13. #define POINT_QUEUE_ROUTER_CLEAR_MAX 1000
  14. #define POINT_QUEUE_ROUTER_CLEAR_SEC 60
  15. #define POINT_QUEUE_REQPACK_CLEAR_MAX 1000
  16. #define POINT_QUEUE_REQPACK_CLEAR_SEC 60
  17. // http_agent 调试日志打印
  18. #define HTTP_AGENT_DEBUG_PRINT 0
  19. // http_agent 生产日志打印
  20. #define HTTP_AGENT_PRINT 0
  21. // http_server 调试日志打印
  22. #define HTTP_SERVER_DEBUG_PRINT 0
  23. // http_server 生产日志打印
  24. #define HTTP_SERVER_PRINT 0
  25. // http_router 生产日志打印
  26. #define HTTP_ROUTER_PRINT 0
  27. // http_interceptor 生产日志打印
  28. #define HTTP_INTERCEPTOR_PRINT 0
  29. // http lua 引擎
  30. //#define HTTP_LUA_ENGINE 0
  31. namespace ylib
  32. {
  33. namespace network
  34. {
  35. namespace http
  36. {
  37. /// <summary>
  38. /// 消息类型
  39. /// </summary>
  40. /// <returns></returns>
  41. enum message_type {
  42. // 普通
  43. HTTP_SERVER_MESSAGE_TYPE_NORMAL = 0,
  44. // WebSocket
  45. HTTP_SERVER_MESSAGE_TYPE_WEBSOCKET = 1,
  46. };
  47. enum ws_type {
  48. // 升级
  49. HTTP_SERVER_WEBSOCKET_TYPE_UPGRADE = 0,
  50. // 消息头
  51. HTTP_SERVER_WEBSOCKET_TYPE_MESSAGE_HEADER = 1,
  52. // 消息包
  53. HTTP_SERVER_WEBSOCKET_TYPE_MESSAGE_BODY = 2,
  54. // 关闭
  55. HTTP_SERVER_WEBSOCKET_TYPE_CLOSE = 3,
  56. };
  57. struct websocket_message {
  58. struct ws_header {
  59. // 是否结束帧
  60. bool _final = false;
  61. // 帧操作码
  62. // 0x0 连续帧
  63. // 0x1 文本帧
  64. // 0x2 二进制帧
  65. // 0x3-7 为非控制帧保留
  66. // 0x8 连接关闭
  67. // 0x9 ping
  68. // 0xA pong
  69. // 0xB-F 为控制帧保留
  70. int opcode = 0x00;
  71. // 是否使用掩码
  72. uchar mask[4] = {0};
  73. // 长度
  74. uint64 length = 0;
  75. };
  76. ws_type type;
  77. ws_header header;
  78. std::string sec_websocket_key;
  79. std::string filepath;
  80. uint64 connid = 0;
  81. };
  82. /// <summary>
  83. /// 服务端缓存
  84. /// </summary>
  85. struct server_cache_config {
  86. // 缓存保存目录
  87. std::string dirpath;
  88. // 开启
  89. bool enable = false;
  90. // 最小过期时间
  91. uint32 timeout_min;
  92. // 支持扩展名
  93. std::vector<std::string> exts;
  94. };
  95. /// <summary>
  96. /// SSL验证类型
  97. /// </summary>
  98. enum ssl_verify_type
  99. {
  100. // 完全忽略验证证书的结果。当握手必须完成的话,就选中这个选项。其实真正有证书的人很少,尤其是在中国,那么如果 SSL 运用于一些免费的服务,比如 EMAIL 的时候,SERVER 端最好采用这个模式。
  101. SVT_VERIFY_NONE = 0x00,
  102. // 希望验证对方的证书。这个是最一般的模式。对 CLIENT 来说,如果设置了这样的模式,验证SERVER的证书出了任何错误,SSL 握手都告吹。对 SERVER 来说,如果设置了这样的模式,CLIENT 倒不一定要把自己的证书交出去。如果 CLIENT 没有交出证书,SERVER 自己决定下一步怎么做。
  103. SVT_VERIFY_PEER = 0x01,
  104. // 这是 SERVER 使用的一种模式,在这种模式下, SERVER 会向 CLIENT 要证书。如果 CLIENT 不给,SSL 握手告吹
  105. SVT_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02,
  106. // 这是仅能使用在 SSL SESSION RENEGOTIATION 阶段的一种方式。如果不是用这个模式的话,那么在 RENEGOTIATION 的时候,CLIENT 都要把自己的证书送给 SERVER,然后做一番分析。这个过程很消耗 CPU 时间的,而这个模式则不需要 CLIENT 在 RENEGOTIATION 的时候重复送自己的证书了。
  107. SVT_VERIFY_CLIENT_ONCE = 0x03
  108. };
  109. /// <summary>
  110. /// 主机域名配置
  111. /// </summary>
  112. struct host_config {
  113. // 域名
  114. std::string domain;
  115. // 端口
  116. ushort port = 0;
  117. ushort port_ssl = 0;
  118. // 开启SSL
  119. bool ssl = false;
  120. };
  121. /// <summary>
  122. /// CDN配置
  123. /// </summary>
  124. struct cdn_config {
  125. struct node_config {
  126. std::string host;
  127. std::string key;
  128. std::string mang_domain;
  129. };
  130. bool manager = false;
  131. bool enable = false;
  132. std::vector<node_config> node;
  133. uint64 max_band = 0;
  134. std::string key;
  135. };
  136. /// <summary>
  137. /// SSL配置
  138. /// </summary>
  139. struct ssl_config {
  140. bool enable = false;
  141. // 验证类型
  142. ssl_verify_type type = SVT_VERIFY_PEER;
  143. std::string pem_cert;
  144. std::string pem_key;
  145. std::string pem_ca;
  146. std::string pem_password;
  147. };
  148. /// <summary>
  149. /// 线程池配置
  150. /// </summary>
  151. struct threadpool_config {
  152. // 线程数量
  153. uint32 size = 0;
  154. // 最大队列数量
  155. uint32 queuemax = 0;
  156. };
  157. /// <summary>
  158. /// 路由配置
  159. /// </summary>
  160. struct router_config {
  161. // 线程池
  162. threadpool_config threadpool;
  163. };
  164. /// <summary>
  165. /// 会话配置
  166. /// </summary>
  167. struct session_config {
  168. // 超时时间
  169. uint32 timeout_sec = 0;
  170. // 保存路径
  171. std::string dirpath;
  172. };
  173. /// <summary>
  174. /// 代理配置
  175. /// </summary>
  176. struct proxy_config {
  177. std::string src;
  178. std::string dst;
  179. std::string remote;
  180. std::string host;
  181. std::map<std::string, std::string> header_request;
  182. std::map<std::string, std::string> header_response;
  183. };
  184. /// <summary>
  185. /// 网站配置
  186. /// </summary>
  187. struct website_config {
  188. // 名称
  189. std::string name;
  190. // 域名列表
  191. std::vector<host_config> host;
  192. // 路由
  193. router_config router;
  194. // 会话
  195. session_config session;
  196. // 本地缓存
  197. server_cache_config cache;
  198. // CDN
  199. cdn_config cdn;
  200. // 代理
  201. std::vector<proxy_config> proxy;
  202. // GZIP
  203. bool gzip = false;
  204. };
  205. /// <summary>
  206. /// HTTP服务配置
  207. /// </summary>
  208. struct server_config {
  209. // 最大上传大小限制
  210. uint64 max_upload_size = 0;
  211. // Https
  212. bool https = false;
  213. // 端口
  214. ushort port = 0;
  215. };
  216. /// <summary>
  217. /// 启动信息
  218. /// </summary>
  219. struct start_config {
  220. // 网站
  221. std::vector<website_config> website;
  222. // 证书
  223. std::map<std::string, ssl_config> cert;
  224. // 最大上传大小限制
  225. uint64 max_upload_size = 0;
  226. // 支持WebSocket
  227. bool websocket_enable = false;
  228. };
  229. class agent;
  230. class website;
  231. // 临时接收
  232. struct temp_recv
  233. {
  234. temp_recv()
  235. {
  236. agent_connid = 0;
  237. agent_ssl = false;
  238. }
  239. void clear()
  240. {
  241. agent_connid = 0;
  242. agent_ssl = false;
  243. url.clear();
  244. ipaddress_port.clear();
  245. host.clear();
  246. }
  247. // 接收数据
  248. ylib::buffer data;
  249. // 代理ID
  250. uint64 agent_connid;
  251. // 代理SSL
  252. bool agent_ssl;
  253. // URL
  254. std::string url;
  255. // 代理连接IP及端口
  256. std::string ipaddress_port;
  257. // Host
  258. std::string host;
  259. // 缓存路径
  260. ylib::file_io cache_file;
  261. // WS自定义数据
  262. std::string ws_filepath;
  263. // 是WS
  264. bool is_ws = false;
  265. // WS消息
  266. websocket_message ws_msg;
  267. };
  268. // 代理
  269. struct proxy
  270. {
  271. proxy()
  272. {
  273. remote_port = 0;
  274. ssl = false;
  275. }
  276. // 拦截地址正则
  277. std::regex src_express;
  278. // 拦截地址字符串
  279. std::string src_str;
  280. // 目标地址
  281. std::string dst;
  282. // 请求URL
  283. std::string remote_url;
  284. // 请求地址
  285. std::string remote_ipaddress;
  286. // 请求端口
  287. ushort remote_port;
  288. // 主机
  289. std::string host;
  290. // 附加协议头
  291. std::map<std::string, std::string> request_headers;
  292. std::map<std::string, std::string> response_headers;
  293. // SSL
  294. bool ssl;
  295. };
  296. // httpserver代理后连接附加数据
  297. struct httpserver_proxy_extra
  298. {
  299. httpserver_proxy_extra()
  300. {
  301. agent = nullptr;
  302. connid = 0;
  303. index = 45854;
  304. }
  305. void* agent;
  306. uint64 connid;
  307. uint64 index;
  308. };
  309. struct multipart {
  310. std::map<std::string,std::string> param;
  311. size_t offset = 0;
  312. size_t length = 0;
  313. };
  314. }
  315. }
  316. }