HttpMessage.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 1999-2019 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include <alibabacloud/core/HttpMessage.h>
  18. namespace AlibabaCloud {
  19. namespace {
  20. #if defined(WIN32) && defined(_MSC_VER)
  21. #define strcasecmp _stricmp
  22. #define strncasecmp _strnicmp
  23. #else
  24. #include <strings.h>
  25. #endif
  26. std::string KnownHeaderMapper[]{"Accept",
  27. "Accept-Charset",
  28. "Accept-Encoding",
  29. "Accept-Language",
  30. "Authorization",
  31. "Connection",
  32. "Content-Length",
  33. "Content-MD5",
  34. "Content-Type",
  35. "Date",
  36. "Host",
  37. "Server",
  38. "User-Agent"};
  39. } // namespace
  40. HttpMessage::HttpMessage() : body_(nullptr), bodySize_(0), headers_() {}
  41. HttpMessage::HttpMessage(const HttpMessage &other)
  42. : body_(nullptr), bodySize_(other.bodySize_), headers_(other.headers_) {
  43. setBody(other.body_, other.bodySize_);
  44. }
  45. HttpMessage::HttpMessage(HttpMessage &&other) { *this = std::move(other); }
  46. HttpMessage &HttpMessage::operator=(const HttpMessage &other) {
  47. if (this != &other) {
  48. body_ = nullptr;
  49. bodySize_ = 0;
  50. headers_ = other.headers_;
  51. setBody(other.body_, other.bodySize_);
  52. }
  53. return *this;
  54. }
  55. HttpMessage &HttpMessage::operator=(HttpMessage &&other) {
  56. if (this != &other)
  57. *this = std::move(other);
  58. return *this;
  59. }
  60. void HttpMessage::addHeader(const HeaderNameType &name,
  61. const HeaderValueType &value) {
  62. setHeader(name, value);
  63. }
  64. void HttpMessage::addHeader(KnownHeader header, const HeaderValueType &value) {
  65. setHeader(header, value);
  66. }
  67. HttpMessage::HeaderValueType
  68. HttpMessage::header(const HeaderNameType &name) const {
  69. auto it = headers_.find(name);
  70. if (it != headers_.end())
  71. return it->second;
  72. else
  73. return std::string();
  74. }
  75. HttpMessage::HeaderCollection HttpMessage::headers() const { return headers_; }
  76. void HttpMessage::removeHeader(const HeaderNameType &name) {
  77. headers_.erase(name);
  78. }
  79. void HttpMessage::removeHeader(KnownHeader header) {
  80. removeHeader(KnownHeaderMapper[header]);
  81. }
  82. void HttpMessage::setHeader(const HeaderNameType &name,
  83. const HeaderValueType &value) {
  84. headers_[name] = value;
  85. }
  86. void HttpMessage::setHeader(KnownHeader header, const std::string &value) {
  87. setHeader(KnownHeaderMapper[header], value);
  88. }
  89. HttpMessage::~HttpMessage() { setBody(nullptr, 0); }
  90. const char *HttpMessage::body() const { return body_; }
  91. size_t HttpMessage::bodySize() const { return bodySize_; }
  92. bool HttpMessage::hasBody() const { return (bodySize_ != 0); }
  93. HttpMessage::HeaderValueType HttpMessage::header(KnownHeader header) const {
  94. return this->header(KnownHeaderMapper[header]);
  95. }
  96. void HttpMessage::setBody(const char *data, size_t size) {
  97. if (body_)
  98. delete[] body_;
  99. body_ = nullptr;
  100. bodySize_ = 0;
  101. if (size) {
  102. bodySize_ = size;
  103. body_ = new char[size + 1];
  104. std::copy(data, data + size, body_);
  105. body_[size] = '\0';
  106. }
  107. }
  108. bool HttpMessage::nocaseLess::operator()(const std::string &s1,
  109. const std::string &s2) const {
  110. return strcasecmp(s1.c_str(), s2.c_str()) < 0;
  111. }
  112. } // namespace AlibabaCloud