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