http_define.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /// 启动信息
  161. /// </summary>
  162. struct start_config {
  163. // 网站
  164. std::vector<website_config> website;
  165. // 证书
  166. std::map<std::string, ssl_config> cert;
  167. };
  168. class agent;
  169. class website;
  170. // 临时接收
  171. struct temp_recv
  172. {
  173. temp_recv()
  174. {
  175. agent_connid = 0;
  176. agent_ssl = false;
  177. }
  178. void clear()
  179. {
  180. agent_connid = 0;
  181. agent_ssl = false;
  182. url.clear();
  183. ipaddress_port.clear();
  184. host.clear();
  185. }
  186. // 接收数据
  187. ylib::buffer data;
  188. // 代理ID
  189. uint64 agent_connid;
  190. // 代理SSL
  191. bool agent_ssl;
  192. // URL
  193. std::string url;
  194. // 代理连接IP及端口
  195. std::string ipaddress_port;
  196. // Host
  197. std::string host;
  198. // 缓存路径
  199. ylib::file_io cache_file;
  200. };
  201. // 代理
  202. struct proxy
  203. {
  204. proxy()
  205. {
  206. remote_port = 0;
  207. ssl = false;
  208. }
  209. // 拦截地址正则
  210. std::regex src_express;
  211. // 拦截地址字符串
  212. std::string src_str;
  213. // 目标地址
  214. std::string dst;
  215. // 请求URL
  216. std::string remote_url;
  217. // 请求地址
  218. std::string remote_ipaddress;
  219. // 请求端口
  220. ushort remote_port;
  221. // 主机
  222. std::string host;
  223. // 附加协议头
  224. std::map<std::string, std::string> request_headers;
  225. std::map<std::string, std::string> response_headers;
  226. // SSL
  227. bool ssl;
  228. };
  229. // httpserver代理后连接附加数据
  230. struct httpserver_proxy_extra
  231. {
  232. httpserver_proxy_extra()
  233. {
  234. agent = nullptr;
  235. connid = 0;
  236. index = 45854;
  237. }
  238. void* agent;
  239. uint64 connid;
  240. uint64 index;
  241. };
  242. //struct website_info
  243. //{
  244. // website_info()
  245. // {
  246. // gzip = false;
  247. // download_maxbaud = 0;
  248. // }
  249. // // 根目录
  250. // std::string rootdir;
  251. // // GZIP
  252. // bool gzip;
  253. // // 默认编码方式
  254. // std::string default_codec;
  255. // // 带宽限制
  256. // uint32 download_maxbaud;
  257. //};
  258. /*表单信息*/
  259. struct form_info
  260. {
  261. form_info()
  262. {
  263. start = -1;
  264. length = -1;
  265. }
  266. std::string disposition;
  267. std::string content_type;
  268. std::string name;
  269. std::string filename;
  270. int64 start;
  271. int64 length;
  272. ylib::buffer data;
  273. };
  274. class controller;
  275. //请求类型
  276. enum content_type
  277. {
  278. CT_FORM = 0, //表单: application/x-www-CT_FORM-urlencoded
  279. CT_JSON = 1, //CT_JSON: application/CT_JSON
  280. CT_TEXT = 2, //文本: text/plain
  281. CT_NIL = 3, //无
  282. CT_ALL = 4 //所有
  283. };
  284. /*HTTP请求类型*/
  285. enum method
  286. {
  287. GET = 0,
  288. POST = 1,
  289. PUT = 2,
  290. DEL = 3,
  291. OTHER = 4,
  292. HEAD = 5,
  293. ALL = 100
  294. };
  295. inline std::string method_to_string(enum method m) {
  296. switch (m) {
  297. case GET:
  298. return "GET";
  299. case POST:
  300. return "POST";
  301. case PUT:
  302. return "PUT";
  303. case DEL:
  304. return "DEL";
  305. case OTHER:
  306. return "OTHER";
  307. case ALL:
  308. return "ALL";
  309. default:
  310. return "unknown";
  311. }
  312. return "";
  313. };
  314. /*控制器内返回结果*/
  315. enum response_type
  316. {
  317. RT_OK, //成功或已处理
  318. RT_500, //服务器内部错误
  319. RT_406, //服务器解析客户端数据失败或格式不正确拒绝解析
  320. RT_401 //需要鉴权,无权限
  321. };
  322. //控制器指针
  323. typedef response_type(network::http::controller::* HTTP_CTR_FUNCTION)();
  324. }
  325. }
  326. }