base_req.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2017, Tencent Inc.
  2. // All rights reserved.
  3. //
  4. // Author: sevenyou <sevenyou@tencent.com>
  5. // Created: 07/15/17
  6. // Description:
  7. #pragma once
  8. #include <stdint.h>
  9. #include <map>
  10. #include <string>
  11. #include "util/string_util.h"
  12. namespace qcloud_cos {
  13. /// \brief 封装Http请求
  14. class BaseReq {
  15. public:
  16. typedef std::map<std::string, std::string> Str2StrMap;
  17. public:
  18. BaseReq();
  19. virtual ~BaseReq() {}
  20. /// \brief 设置请求头部
  21. void AddHeader(const std::string& key, const std::string& value);
  22. void AddHeaders(const Str2StrMap& user_headers);
  23. /// \brief 获取请求头部
  24. std::string GetHeader(const std::string& key) const;
  25. const Str2StrMap& GetHeaders() const { return m_headers_map; }
  26. /// \brief 清空请求头部
  27. void ClearHeaders() { m_headers_map.clear(); }
  28. /// \brief 设置请求参数
  29. void AddParam(const std::string& key, const std::string& value);
  30. void AddParams(const Str2StrMap& user_params);
  31. /// \brief 获取请求参数
  32. std::string GetParam(const std::string& key) const;
  33. const Str2StrMap& GetParams() const { return m_params_map; };
  34. /// \brief 清空请求参数
  35. void ClearParams() { m_params_map.clear(); }
  36. /// \brief 获取请求的http方法
  37. std::string GetMethod() const { return m_method; }
  38. /// \brief 设置请求的http方法
  39. void SetMethod(const std::string& method) {
  40. m_method = StringUtil::StringToUpper(method);
  41. }
  42. /// \brief 获取请求的body
  43. std::string GetBody() const { return m_body; }
  44. /// \brief 设置请求的body
  45. void SetBody(const std::string& body) { m_body = body; }
  46. /// \brief 获取Path
  47. std::string GetPath() const { return m_path; }
  48. /// \brief 设置Path
  49. void SetPath(const std::string& path) { m_path = path; }
  50. void SetConnTimeoutInms(uint64_t conn_timeout_in_ms) {
  51. m_conn_timeout_in_ms = conn_timeout_in_ms;
  52. }
  53. uint64_t GetConnTimeoutInms() const { return m_conn_timeout_in_ms; }
  54. void SetRecvTimeoutInms(uint64_t recv_timeout_in_ms) {
  55. m_recv_timeout_in_ms = recv_timeout_in_ms;
  56. }
  57. uint64_t GetRecvTimeoutInms() const { return m_recv_timeout_in_ms; }
  58. /// \brief 设置当前请求是否使用https
  59. void SetHttps() { m_is_https = true; }
  60. bool IsHttps() const { return m_is_https; }
  61. void SetSSLCtxCallback(const SSLCtxCallback& ssl_ctx_cb, void *user_data) {
  62. m_ssl_ctx_cb = ssl_ctx_cb;
  63. m_user_data = user_data;
  64. }
  65. const SSLCtxCallback& GetSSLCtxCallback() const { return m_ssl_ctx_cb; }
  66. void *GetSSLCtxCbData() const { return m_user_data; }
  67. /// \brief set whether check content md5 each request, used for close range
  68. /// download check
  69. void SetCheckMD5(bool check_md5) { mb_check_md5 = check_md5; }
  70. bool CheckMD5() const { return mb_check_md5; }
  71. void SetCheckCRC64(bool check_crc64) { mb_check_crc64 = check_crc64; }
  72. bool CheckCRC64() const { return mb_check_crc64; }
  73. void SetCaLocation(const std::string& ca_location) { m_ca_location = ca_location; }
  74. const std::string& GetCaLocation() const { return m_ca_location; }
  75. void SetVerifyCert(bool verify_cert) { mb_verify_cert = verify_cert; }
  76. bool GetVerifyCert() const { return mb_verify_cert; }
  77. /// \brief 输出请求的header和param信息
  78. std::string DebugString() const;
  79. /// \brief 是否生成对header host
  80. /// 是否对host进行签名,默认为true,您也可以选择不签入host,
  81. /// 但可能导致请求失败或安全漏洞
  82. void SetSignHeaderHost(bool sign_header_host) {
  83. m_sign_header_host = sign_header_host;
  84. }
  85. bool SignHeaderHost() const { return m_sign_header_host; }
  86. protected:
  87. // acl相关的请求,供PutObjectACL和PutBucketACL使用
  88. bool GenerateAclRequestBody(const Owner& owner, const std::vector<Grant>& acl,
  89. std::string* body) const;
  90. protected:
  91. Str2StrMap m_headers_map;
  92. Str2StrMap m_params_map;
  93. std::string m_path;
  94. std::string m_method;
  95. std::string m_body;
  96. uint64_t m_conn_timeout_in_ms;
  97. uint64_t m_recv_timeout_in_ms;
  98. bool m_is_https;
  99. bool mb_check_md5; // default is true
  100. bool mb_check_crc64; // default is false
  101. bool mb_verify_cert; // default is true
  102. bool m_sign_header_host; // 是否对header host进行签名
  103. std::string m_ca_location;
  104. SSLCtxCallback m_ssl_ctx_cb;
  105. void* m_user_data;
  106. };
  107. } // namespace qcloud_cos