http_reqpack.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include "http_server.h"
  5. #include "util/strutils.h"
  6. #include "util/time.h"
  7. namespace ylib
  8. {
  9. namespace network
  10. {
  11. namespace http
  12. {
  13. class server;
  14. class response;
  15. class request;
  16. class website;
  17. /*************************************************************************
  18. * class:请求包
  19. *************************************************************************/
  20. class reqpack
  21. {
  22. public:
  23. reqpack();
  24. ~reqpack();
  25. void init(const std::string& url, const std::string& host, const ylib::buffer& data, uint64 connid, network::http::server* server);
  26. void clear();
  27. network::http::request* request();
  28. network::http::response* response();
  29. const std::string& host()
  30. {
  31. /*获取基本请求信息*/
  32. return m_host;
  33. }
  34. network::http::method method();
  35. const std::string& filepath();
  36. void filepath(const std::string& path);
  37. network::http::server* server()
  38. {
  39. return m_server;
  40. }
  41. ylib::buffer& data()
  42. {
  43. return m_data;
  44. }
  45. const std::string& url()
  46. {
  47. return m_url;
  48. }
  49. const std::string& referrer()
  50. {
  51. return m_referrer;
  52. }
  53. const uint64& connid()
  54. {
  55. return m_connid;
  56. }
  57. network::http::website* website() {
  58. return m_website;
  59. }
  60. void website(network::http::website* website) {
  61. m_website = website;
  62. }
  63. timestamp begin_msec() {
  64. return m_begin_msec;
  65. }
  66. std::string exec_msec() {
  67. return std::to_string(time::now_msec() - m_begin_msec);
  68. }
  69. const std::string& remote() {
  70. if (m_remote_ipaddress.empty()) {
  71. ushort port;
  72. m_server->remote(connid(), m_remote_ipaddress, port);
  73. }
  74. return m_remote_ipaddress;
  75. }
  76. ylib::json& extra() { return m_extra; }
  77. void extra(ylib::json& e) { m_extra = e; }
  78. private:
  79. // 请求主机
  80. std::string m_host;
  81. // 请求方式
  82. network::http::method m_method;
  83. // 请求路径
  84. std::string m_filepath;
  85. // HPSERVER
  86. network::http::server* m_server;
  87. // 转发来路
  88. std::string m_referrer;
  89. // 请求ID
  90. uint64 m_connid;
  91. // 站点
  92. network::http::website* m_website;
  93. // 接收数据
  94. ylib::buffer m_data;
  95. // URL
  96. std::string m_url;
  97. // 请求发起时间
  98. timestamp m_begin_msec;
  99. // 远程IP
  100. std::string m_remote_ipaddress;
  101. // 附加数据
  102. ylib::json m_extra;
  103. private:
  104. network::http::request* m_request;
  105. network::http::response* m_response;
  106. };
  107. }
  108. }
  109. }
  110. #endif