base_req.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// \brief set whether check content md5 each request, used for close range
  62. /// download check
  63. void SetCheckMD5(bool check_md5) { mb_check_md5 = check_md5; }
  64. bool CheckMD5() const { return mb_check_md5; }
  65. void SetCheckCRC64(bool check_crc64) { mb_check_crc64 = check_crc64; }
  66. bool CheckCRC64() const { return mb_check_crc64; }
  67. void SetCaLocation(const std::string& ca_location) { m_ca_location = ca_location; }
  68. const std::string& GetCaLocation() const { return m_ca_location; }
  69. void SetVerifyCert(bool verify_cert) { mb_verify_cert = verify_cert; }
  70. bool GetVerifyCert() const { return mb_verify_cert; }
  71. /// \brief 输出请求的header和param信息
  72. std::string DebugString() const;
  73. /// \brief 是否生成对header host
  74. /// 是否对host进行签名,默认为true,您也可以选择不签入host,
  75. /// 但可能导致请求失败或安全漏洞
  76. void SetSignHeaderHost(bool sign_header_host) {
  77. m_sign_header_host = sign_header_host;
  78. }
  79. bool SignHeaderHost() const { return m_sign_header_host; }
  80. protected:
  81. // acl相关的请求,供PutObjectACL和PutBucketACL使用
  82. bool GenerateAclRequestBody(const Owner& owner, const std::vector<Grant>& acl,
  83. std::string* body) const;
  84. protected:
  85. Str2StrMap m_headers_map;
  86. Str2StrMap m_params_map;
  87. std::string m_path;
  88. std::string m_method;
  89. std::string m_body;
  90. uint64_t m_conn_timeout_in_ms;
  91. uint64_t m_recv_timeout_in_ms;
  92. bool m_is_https;
  93. bool mb_check_md5; // default is true
  94. bool mb_check_crc64; // default is false
  95. bool mb_verify_cert; // default is true
  96. bool m_sign_header_host; // 是否对header host进行签名
  97. std::string m_ca_location;
  98. };
  99. } // namespace qcloud_cos