http_define.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. struct server_cache_config {
  41. // 缓存保存目录
  42. std::string dirpath;
  43. // 开启
  44. bool enable = false;
  45. // 最小过期时间
  46. uint32 timeout_min;
  47. // 支持扩展名
  48. std::vector<std::string> exts;
  49. };
  50. /// <summary>
  51. /// SSL验证类型
  52. /// </summary>
  53. enum ssl_verify_type
  54. {
  55. // 完全忽略验证证书的结果。当握手必须完成的话,就选中这个选项。其实真正有证书的人很少,尤其是在中国,那么如果 SSL 运用于一些免费的服务,比如 EMAIL 的时候,SERVER 端最好采用这个模式。
  56. SVT_VERIFY_NONE = 0x00,
  57. // 希望验证对方的证书。这个是最一般的模式。对 CLIENT 来说,如果设置了这样的模式,验证SERVER的证书出了任何错误,SSL 握手都告吹。对 SERVER 来说,如果设置了这样的模式,CLIENT 倒不一定要把自己的证书交出去。如果 CLIENT 没有交出证书,SERVER 自己决定下一步怎么做。
  58. SVT_VERIFY_PEER = 0x01,
  59. // 这是 SERVER 使用的一种模式,在这种模式下, SERVER 会向 CLIENT 要证书。如果 CLIENT 不给,SSL 握手告吹
  60. SVT_VERIFY_FAIL_IF_NO_PEER_CERT = 0x02,
  61. // 这是仅能使用在 SSL SESSION RENEGOTIATION 阶段的一种方式。如果不是用这个模式的话,那么在 RENEGOTIATION 的时候,CLIENT 都要把自己的证书送给 SERVER,然后做一番分析。这个过程很消耗 CPU 时间的,而这个模式则不需要 CLIENT 在 RENEGOTIATION 的时候重复送自己的证书了。
  62. SVT_VERIFY_CLIENT_ONCE = 0x03
  63. };
  64. /// <summary>
  65. /// 主机域名配置
  66. /// </summary>
  67. struct host_config {
  68. // 域名
  69. std::string domain;
  70. // 端口
  71. ushort port = 0;
  72. // 开启SSL
  73. bool ssl = false;
  74. };
  75. /// <summary>
  76. /// CDN配置
  77. /// </summary>
  78. struct cdn_config {
  79. struct node_config {
  80. std::string host;
  81. std::string key;
  82. std::string mang_domain;
  83. };
  84. bool manager = false;
  85. bool enable = false;
  86. std::vector<node_config> node;
  87. uint64 max_band = 0;
  88. std::string key;
  89. };
  90. /// <summary>
  91. /// SSL配置
  92. /// </summary>
  93. struct ssl_config {
  94. bool enable = false;
  95. // 验证类型
  96. ssl_verify_type type = SVT_VERIFY_PEER;
  97. std::string pem_cert;
  98. std::string pem_key;
  99. std::string pem_ca;
  100. std::string pem_password;
  101. };
  102. /// <summary>
  103. /// 线程池配置
  104. /// </summary>
  105. struct threadpool_config {
  106. // 线程数量
  107. uint32 size = 0;
  108. // 最大队列数量
  109. uint32 queuemax = 0;
  110. };
  111. /// <summary>
  112. /// 路由配置
  113. /// </summary>
  114. struct router_config {
  115. // 线程池
  116. threadpool_config threadpool;
  117. };
  118. /// <summary>
  119. /// 会话配置
  120. /// </summary>
  121. struct session_config {
  122. // 超时时间
  123. uint32 timeout_sec = 0;
  124. // 保存路径
  125. std::string dirpath;
  126. };
  127. /// <summary>
  128. /// 代理配置
  129. /// </summary>
  130. struct proxy_config {
  131. std::string src;
  132. std::string dst;
  133. std::string remote;
  134. std::string host;
  135. std::map<std::string, std::string> header_request;
  136. std::map<std::string, std::string> header_response;
  137. };
  138. /// <summary>
  139. /// 网站配置
  140. /// </summary>
  141. struct website_config {
  142. // 名称
  143. std::string name;
  144. // 域名列表
  145. std::vector<host_config> host;
  146. // 路由
  147. router_config router;
  148. // 会话
  149. session_config session;
  150. // 本地缓存
  151. server_cache_config cache;
  152. // CDN
  153. cdn_config cdn;
  154. // 代理
  155. std::vector<proxy_config> proxy;
  156. // GZIP
  157. bool gzip = false;
  158. };
  159. /// <summary>
  160. /// HTTP服务配置
  161. /// </summary>
  162. struct server_config {
  163. // 最大上传大小限制
  164. uint64 max_upload_size = 0;
  165. // Https
  166. bool https = false;
  167. // 端口
  168. ushort port = 0;
  169. };
  170. /// <summary>
  171. /// 启动信息
  172. /// </summary>
  173. struct start_config {
  174. // 网站
  175. std::vector<website_config> website;
  176. // 证书
  177. std::map<std::string, ssl_config> cert;
  178. // 最大上传大小限制
  179. uint64 max_upload_size = 0;
  180. };
  181. class agent;
  182. class website;
  183. // 临时接收
  184. struct temp_recv
  185. {
  186. temp_recv()
  187. {
  188. agent_connid = 0;
  189. agent_ssl = false;
  190. }
  191. void clear()
  192. {
  193. agent_connid = 0;
  194. agent_ssl = false;
  195. url.clear();
  196. ipaddress_port.clear();
  197. host.clear();
  198. }
  199. // 接收数据
  200. ylib::buffer data;
  201. // 代理ID
  202. uint64 agent_connid;
  203. // 代理SSL
  204. bool agent_ssl;
  205. // URL
  206. std::string url;
  207. // 代理连接IP及端口
  208. std::string ipaddress_port;
  209. // Host
  210. std::string host;
  211. // 缓存路径
  212. ylib::file_io cache_file;
  213. };
  214. // 代理
  215. struct proxy
  216. {
  217. proxy()
  218. {
  219. remote_port = 0;
  220. ssl = false;
  221. }
  222. // 拦截地址正则
  223. std::regex src_express;
  224. // 拦截地址字符串
  225. std::string src_str;
  226. // 目标地址
  227. std::string dst;
  228. // 请求URL
  229. std::string remote_url;
  230. // 请求地址
  231. std::string remote_ipaddress;
  232. // 请求端口
  233. ushort remote_port;
  234. // 主机
  235. std::string host;
  236. // 附加协议头
  237. std::map<std::string, std::string> request_headers;
  238. std::map<std::string, std::string> response_headers;
  239. // SSL
  240. bool ssl;
  241. };
  242. // httpserver代理后连接附加数据
  243. struct httpserver_proxy_extra
  244. {
  245. httpserver_proxy_extra()
  246. {
  247. agent = nullptr;
  248. connid = 0;
  249. index = 45854;
  250. }
  251. void* agent;
  252. uint64 connid;
  253. uint64 index;
  254. };
  255. struct multipart {
  256. std::map<std::string,std::string> param;
  257. size_t offset = 0;
  258. size_t length = 0;
  259. };
  260. }
  261. }
  262. }