base_resp.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (c) 2017, Tencent Inc.
  2. // All rights reserved.
  3. //
  4. // Author: sevenyou <sevenyou@tencent.com>
  5. // Created: 07/18/17
  6. // Description:
  7. #ifndef BASERESP_H
  8. #define BASERESP_H
  9. #pragma once
  10. #include <stdint.h>
  11. #include <map>
  12. #include <string>
  13. #include <vector>
  14. #include "cos_defines.h"
  15. #include "cos_params.h"
  16. #include "util/string_util.h"
  17. namespace qcloud_cos {
  18. typedef std::map<std::string, std::string> StringMap;
  19. typedef StringMap::const_iterator citerator;
  20. // 封装HTTP调用成功时的返回信息, 包括header和body
  21. // CosResult::IsSucc返回true时, 调用其子类的Get方法获取具体字段
  22. class BaseResp {
  23. public:
  24. BaseResp()
  25. : m_content_length(0), m_x_cos_storage_class(kStorageClassStandard) {}
  26. BaseResp(const BaseResp& rhs) { InternalCopyFrom(rhs); }
  27. BaseResp& operator=(const BaseResp& rhs) {
  28. if (&rhs != this) {
  29. InternalCopyFrom(rhs);
  30. }
  31. return *this;
  32. }
  33. virtual ~BaseResp() {}
  34. // debug使用
  35. virtual std::string DebugString() const {
  36. std::string ret;
  37. for (std::map<std::string, std::string>::const_iterator c_itr =
  38. m_headers.begin();
  39. c_itr != m_headers.end(); ++c_itr) {
  40. ret += c_itr->first + " = " + c_itr->second + "\n";
  41. }
  42. ret += m_body_str;
  43. return ret;
  44. }
  45. std::string GetHeader(const std::string& key) const;
  46. std::map<std::string, std::string> GetHeaders() const { return m_headers; }
  47. std::map<std::string, std::string>* GetHeadersPtr() { return &m_headers; }
  48. void SetHeaders(const std::map<std::string, std::string>& headers) {
  49. m_headers = headers;
  50. }
  51. // ==========================头部相关==============================
  52. // TODO 头部可以不需要解析
  53. virtual void ParseFromHeaders(
  54. const std::map<std::string, std::string>& headers);
  55. uint64_t GetContentLength() const {
  56. citerator cit = m_headers.find(kHttpHeaderContentLength);
  57. if (m_headers.end() != cit) {
  58. return StringUtil::StringToUint64(cit->second);
  59. } else {
  60. return 0;
  61. }
  62. }
  63. std::string GetContentRange() const {
  64. return GetHeader(kHttpHeaderContentRange);
  65. }
  66. std::string GetContentType() const {
  67. return GetHeader(kHttpHeaderContentType);
  68. }
  69. std::string GetEtag() const {
  70. std::string etag;
  71. citerator cit = m_headers.find(kHttpHeaderEtag);
  72. if (m_headers.end() != cit) {
  73. etag = StringUtil::Trim(cit->second, "\"");
  74. } else {
  75. // 某些代理软件可能会修改HTTP Header,比如把ETag改成Etag
  76. // 此处找不到ETag,再尝试查找Etag
  77. cit = m_headers.find(kHttpHeaderLowerCaseEtag);
  78. if (m_headers.end() != cit) {
  79. etag = StringUtil::Trim(cit->second, "\"");
  80. }
  81. }
  82. return etag;
  83. }
  84. std::string GetConnection() const { return GetHeader(kHttpHeaderConnection); }
  85. std::string GetDate() const { return GetHeader(kHttpHeaderDate); }
  86. std::string GetServer() const { return GetHeader(kHttpHeaderServer); }
  87. std::string GetContentDisposition() const {
  88. return GetHeader(kHttpHeaderContentDisposition);
  89. }
  90. std::string GetContentEncoding() const {
  91. return GetHeader(kHttpHeaderContentEncoding);
  92. }
  93. std::string GetCacheControl() const {
  94. return GetHeader(kHttpHeaderCacheControl);
  95. }
  96. std::string GetExpires() const { return GetHeader(kHttpHeaderExpires); }
  97. std::string GetXCosRequestId() const {
  98. return GetHeader(kRespHeaderXCosReqId);
  99. }
  100. std::string GetXCosTraceId() const {
  101. return GetHeader(kRespHeaderXCosTraceId);
  102. }
  103. std::string GetXCosStorageTier() const {
  104. return GetHeader(kRespHeaderXCosStorageTier);
  105. }
  106. /// \brief 获取Object 的存储类型
  107. /// 枚举值:MAZ_STANDARD,MAZ_STANDARD_IA,INTELLIGENT_TIERING,MAZ_INTELLIGENT_TIERING,STANDARD_IA,ARCHIVE,DEEP_ARCHIVE
  108. std::string GetXCosStorageClass() const {
  109. return GetHeader(kRespHeaderXCosStorageClass);
  110. }
  111. std::string GetXCosHashCrc64Ecma() const {
  112. return GetHeader(kRespHeaderXCosHashCrc64Ecma);
  113. }
  114. /// \brief 获取object最后被修改的时间, 字符串格式Date, 类似"Wed, 28 Oct 2014
  115. /// 20:30:00 GMT"
  116. std::string GetLastModified() const {
  117. return GetHeader(kRespHeaderLastModified);
  118. }
  119. void SetContentLength(uint64_t content_length) {
  120. m_headers[kHttpHeaderContentLength] =
  121. StringUtil::Uint64ToString(content_length);
  122. }
  123. void SetContentType(const std::string& content_type) {
  124. m_headers[kHttpHeaderContentType] = content_type;
  125. }
  126. void SetEtag(const std::string& etag) { m_headers[kHttpHeaderEtag] = etag; }
  127. void SetConnection(const std::string& conn) {
  128. m_headers[kHttpHeaderConnection] = conn;
  129. }
  130. void SetDate(const std::string& date) { m_headers[kHttpHeaderDate] = date; }
  131. void SetServer(const std::string& server) {
  132. m_headers[kHttpHeaderServer] = server;
  133. }
  134. void SetContentDisposition(const std::string& content_disposition) {
  135. m_headers[kHttpHeaderContentDisposition] = content_disposition;
  136. }
  137. void SetContentEncoding(const std::string& content_encoding) {
  138. m_headers[kHttpHeaderContentEncoding] = content_encoding;
  139. }
  140. void SetCacheControl(const std::string& cache_control) {
  141. m_headers[kHttpHeaderCacheControl] = cache_control;
  142. }
  143. void SetExpires(const std::string& expires) {
  144. m_headers[kHttpHeaderExpires] = expires;
  145. }
  146. void SetXCosTraceId(const std::string& trace_id) {
  147. m_headers[kRespHeaderXCosTraceId] = trace_id;
  148. }
  149. void SetXCosRequestId(const std::string& request_id) {
  150. m_headers[kRespHeaderXCosReqId] = request_id;
  151. }
  152. void SetXCosStorageTier(const std::string& storage_tier) {
  153. m_headers[kRespHeaderXCosStorageTier] = storage_tier;
  154. }
  155. void SetXCosStorageClass(const std::string& storage_class) {
  156. m_headers[kRespHeaderXCosStorageClass] = storage_class;
  157. }
  158. void SetXCosHashCrc64Ecma(const std::string& crc64) {
  159. m_headers[kRespHeaderXCosHashCrc64Ecma] = crc64;
  160. }
  161. void SetLastModified(const std::string& last_modified) {
  162. m_headers[kRespHeaderLastModified] = last_modified;
  163. }
  164. // ==========================响应体================================
  165. // 解析响应体的XML字符串
  166. virtual bool ParseFromXmlString(const std::string& body) {
  167. m_body_str = body;
  168. return true;
  169. }
  170. void SetBody(const std::string& body) { m_body_str = body; }
  171. const std::string& GetBody() const { return m_body_str; }
  172. std::string* GetBodyPtr() { return &m_body_str; }
  173. void CopyFrom(const BaseResp& resp) { InternalCopyFrom(resp); }
  174. protected:
  175. std::string GetHeader(const std::string& key) {
  176. citerator cit = m_headers.find(key);
  177. if (m_headers.end() != cit) {
  178. return cit->second;
  179. }
  180. return "";
  181. }
  182. bool ParseFromACLXMLString(const std::string& body, std::string* owner_id,
  183. std::string* owner_display_name,
  184. std::vector<Grant>* acl);
  185. void InternalCopyFrom(const BaseResp& resp);
  186. private:
  187. std::map<std::string, std::string> m_headers;
  188. std::string m_body_str;
  189. // 公共头部字段
  190. uint64_t m_content_length;
  191. std::string m_content_range;
  192. std::string m_content_type;
  193. std::string m_etag;
  194. std::string m_connection;
  195. std::string m_date;
  196. std::string m_server;
  197. std::string m_content_disposition;
  198. std::string m_content_encoding;
  199. std::string m_cache_control;
  200. std::string m_expires;
  201. std::string m_last_modified;
  202. // cos响应头部
  203. std::string m_x_cos_request_id;
  204. std::string m_x_cos_trace_id;
  205. std::string m_x_cos_storage_tier;
  206. std::string m_x_cos_storage_class;
  207. std::string m_x_cos_hash_crc64ecma;
  208. };
  209. } // namespace qcloud_cos
  210. #endif // BASERESP_H