http_parser.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include <map>
  5. namespace ylib
  6. {
  7. namespace network
  8. {
  9. namespace http
  10. {
  11. class parser;
  12. /*************************************************************************
  13. * class:FORM表单解析器
  14. *************************************************************************/
  15. class form_parser {
  16. public:
  17. form_parser();
  18. ~form_parser();
  19. std::vector<std::string> names();
  20. bool get(const std::string& name, form_info& info);
  21. bool write_file(const std::string& name, const std::string& filepath);
  22. friend class parser;
  23. private:
  24. bool parse(ylib::buffer* data);
  25. void parse_count(std::vector<uint32>& starts, std::vector<uint32>& lengths);
  26. void parse_form(uint32 start, uint32 length);
  27. char getchar(size_t index);
  28. private:
  29. ylib::buffer* m_data;
  30. std::map<std::string, form_info> m_infos;
  31. };
  32. /*************************************************************************
  33. * class:解析器
  34. *************************************************************************/
  35. class parser
  36. {
  37. public:
  38. /******************************************************************
  39. * function:初始化
  40. * param
  41. *
  42. * url : 请求地址
  43. * method : 请求类型
  44. * data : 请求数据
  45. * content_type : 类型协议头
  46. ******************************************************************/
  47. bool init(const std::string& url, const network::http::method& method, const ylib::buffer& data, const std::string& content_type);
  48. public:
  49. parser();
  50. ~parser();
  51. const ylib::json& json();
  52. std::string text();
  53. /******************************************************************
  54. * function:取URL参数
  55. * param
  56. * name : 名称
  57. * value : 数据
  58. ******************************************************************/
  59. bool url_param(const std::string& name, std::string& value);
  60. bool url_param_exist(const std::string& name);
  61. /******************************************************************
  62. * function:取URL请求参数名列表
  63. ******************************************************************/
  64. std::vector<std::string> url_param_names();
  65. /******************************************************************
  66. * function:取BODY参数
  67. * param
  68. * name : 名称
  69. * value : 数据
  70. ******************************************************************/
  71. bool body_param(const std::string& name, std::string& value);
  72. bool body_param_exist(const std::string& name);
  73. /******************************************************************
  74. * function:取BODY请求参数名列表
  75. ******************************************************************/
  76. std::vector<std::string> body_param_names();
  77. /******************************************************************
  78. * function:取BODY请求参数名列表
  79. ******************************************************************/
  80. form_parser* form();
  81. const std::map<std::string, std::string>& url_param() { return m_url_param; }
  82. const std::map<std::string, std::string>& body_param() { return m_body_param; }
  83. private:
  84. /******************************************************************
  85. * function:解析URL
  86. ******************************************************************/
  87. void parse_url(const std::string& url, std::map<std::string, std::string>& map);
  88. /******************************************************************
  89. * function:解析JSON
  90. ******************************************************************/
  91. void parse_json();
  92. /******************************************************************
  93. * function:解析FORM表单
  94. ******************************************************************/
  95. void parse_form();
  96. /******************************************************************
  97. * function:解析URL格式数据
  98. ******************************************************************/
  99. bool read_url_param(const std::map<std::string, std::string>& map, const std::string& name, std::string& value);
  100. private:
  101. // URL参数
  102. std::map<std::string, std::string> m_url_param;
  103. // BODY参数
  104. std::map<std::string, std::string> m_body_param;
  105. // JSON参数
  106. ylib::json m_json_param;
  107. // FORM解析器
  108. network::http::form_parser m_form;
  109. // 请求地址
  110. std::string m_url;
  111. // 请求数据
  112. ylib::buffer m_data;
  113. // 请求内容类型
  114. std::string m_content_type;
  115. // 请求类型
  116. network::http::method m_method;
  117. };
  118. }
  119. }
  120. }
  121. #endif