http_center.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include <functional>
  5. #include <vector>
  6. #include <unordered_map>
  7. #include "util/map.hpp"
  8. #include "util/json.h"
  9. namespace ylib
  10. {
  11. namespace network
  12. {
  13. namespace http
  14. {
  15. /*绑定域名*/
  16. struct domain_info
  17. {
  18. domain_info() {
  19. https = false;
  20. }
  21. // 是否HTTPS
  22. bool https;
  23. // 域名
  24. std::string domain;
  25. };
  26. class server;
  27. class router;
  28. class website;
  29. /***************************************************************
  30. * Class:控制中心
  31. ***************************************************************/
  32. class center :public ylib::error_base
  33. {
  34. public:
  35. center();
  36. ~center();
  37. /******************************************************************
  38. * function:创建
  39. * param
  40. * config : 配置项
  41. * return:
  42. * 失败可通过 last_error() 返回错误信息。
  43. ******************************************************************/
  44. bool create(const start_config& config);
  45. bool start();
  46. /******************************************************************
  47. * function:关闭
  48. ******************************************************************/
  49. void close();
  50. network::http::server* server(ushort port);
  51. network::http::website* website(const std::string& host);
  52. network::http::website* website_byname(const std::string& name);
  53. const start_config& config() { return m_config; }
  54. private:
  55. //所有监听端口
  56. std::vector<ushort> listen_ports();
  57. //端口是否需要SSL
  58. bool port_have_ssl(ushort port);
  59. void rebuild_host_index();
  60. private:
  61. // HTTP 服务
  62. std::vector<network::http::server*> m_server;
  63. // 站点
  64. std::vector<network::http::website*> m_website;
  65. // Host -> 站点 精确匹配
  66. std::unordered_map<std::string, network::http::website*> m_host_index;
  67. // 通配站点(0.0.0.0 / *)
  68. std::vector<network::http::website*> m_catchall_websites;
  69. // 启动信息
  70. start_config m_config;
  71. public:
  72. uint64 m_temp[10];
  73. };
  74. }
  75. }
  76. }
  77. #endif