HttpMessage.cc 3.4 KB

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