HttpMessage.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <alibabacloud/core/HttpMessage.h>
  17. #include <algorithm>
  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[] {
  27. "Accept",
  28. "Accept-Charset",
  29. "Accept-Encoding",
  30. "Accept-Language",
  31. "Authorization",
  32. "Connection",
  33. "Content-Length",
  34. "Content-MD5",
  35. "Content-Type",
  36. "Date",
  37. "Host",
  38. "Server",
  39. "User-Agent"
  40. };
  41. } // namespace
  42. HttpMessage::HttpMessage() :
  43. body_(nullptr),
  44. bodySize_(0),
  45. headers_() {
  46. }
  47. HttpMessage::HttpMessage(const HttpMessage &other) :
  48. body_(nullptr),
  49. bodySize_(other.bodySize_),
  50. headers_(other.headers_) {
  51. setBody(other.body_, other.bodySize_);
  52. }
  53. HttpMessage::HttpMessage(HttpMessage &&other) {
  54. *this = std::move(other);
  55. }
  56. HttpMessage& HttpMessage::operator=(const HttpMessage &other) {
  57. if (this != &other) {
  58. body_ = nullptr;
  59. bodySize_ = 0;
  60. headers_ = other.headers_;
  61. setBody(other.body_, other.bodySize_);
  62. }
  63. return *this;
  64. }
  65. HttpMessage& HttpMessage::operator=(HttpMessage &&other) {
  66. if (this != &other)
  67. *this = std::move(other);
  68. return *this;
  69. }
  70. void HttpMessage::addHeader(const HeaderNameType & name,
  71. const HeaderValueType & value) {
  72. setHeader(name, value);
  73. }
  74. void HttpMessage::addHeader(KnownHeader header, const HeaderValueType & value) {
  75. setHeader(header, value);
  76. }
  77. HttpMessage::HeaderValueType HttpMessage::header(
  78. const HeaderNameType & name) const {
  79. auto it = headers_.find(name);
  80. if (it != headers_.end())
  81. return it->second;
  82. else
  83. return std::string();
  84. }
  85. HttpMessage::HeaderCollection HttpMessage::headers() const {
  86. return headers_;
  87. }
  88. void HttpMessage::removeHeader(const HeaderNameType & name) {
  89. headers_.erase(name);
  90. }
  91. void HttpMessage::removeHeader(KnownHeader header) {
  92. removeHeader(KnownHeaderMapper[header]);
  93. }
  94. void HttpMessage::setHeader(const HeaderNameType & name,
  95. const HeaderValueType & value) {
  96. headers_[name] = value;
  97. }
  98. void HttpMessage::setHeader(KnownHeader header,
  99. const std::string & value) {
  100. setHeader(KnownHeaderMapper[header], value);
  101. }
  102. HttpMessage::~HttpMessage() {
  103. setBody(nullptr, 0);
  104. }
  105. const char* HttpMessage::body()const {
  106. return body_;
  107. }
  108. size_t HttpMessage::bodySize()const {
  109. return bodySize_;
  110. }
  111. bool HttpMessage::hasBody() const {
  112. return (bodySize_ != 0);
  113. }
  114. HttpMessage::HeaderValueType HttpMessage::header(KnownHeader header)const {
  115. return this->header(KnownHeaderMapper[header]);
  116. }
  117. void HttpMessage::setBody(const char *data, size_t size) {
  118. if (body_)
  119. delete[] body_;
  120. body_ = nullptr;
  121. bodySize_ = 0;
  122. if (size) {
  123. bodySize_ = size;
  124. body_ = new char[size + 1];
  125. std::copy(data, data + size, body_);
  126. body_[size] = '\0';
  127. }
  128. }
  129. bool HttpMessage::nocaseLess::operator()(const std::string & s1,
  130. const std::string & s2) const {
  131. return strcasecmp(s1.c_str(), s2.c_str()) < 0;
  132. }
  133. } // namespace AlibabaCloud