http_define.h 9.5 KB

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