1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "constant.h"
17 #include "netstack_common_utils.h"
18 #include "netstack_log.h"
19 
20 #include "net_http_response.h"
21 
22 namespace OHOS::NetStack::Http {
HttpResponse()23 HttpResponse::HttpResponse() : responseCode_(0) {}
24 
AppendResult(const void * data,size_t length)25 void HttpResponse::AppendResult(const void *data, size_t length)
26 {
27     result_.append(static_cast<const char *>(data), length);
28 }
29 
AppendRawHeader(const void * data,size_t length)30 void HttpResponse::AppendRawHeader(const void *data, size_t length)
31 {
32     rawHeader_.append(static_cast<const char *>(data), length);
33 }
34 
SetResponseCode(uint32_t responseCode)35 void HttpResponse::SetResponseCode(uint32_t responseCode)
36 {
37     responseCode_ = responseCode;
38 }
39 
ParseHeaders()40 void HttpResponse::ParseHeaders()
41 {
42     std::vector<std::string> vec = CommonUtils::Split(rawHeader_, HTTP_LINE_SEPARATOR);
43     for (const auto &header : vec) {
44         if (CommonUtils::Strip(header).empty()) {
45             continue;
46         }
47         auto index = header.find(HTTP_HEADER_SEPARATOR);
48         if (index == std::string::npos) {
49             NETSTACK_LOGI("HEAD: %{public}s", CommonUtils::Strip(header).c_str());
50             continue;
51         }
52         if (CommonUtils::ToLower(CommonUtils::Strip(header.substr(0, index))) ==
53             RESPONSE_KEY_SET_COOKIE) {
54             setCookie_.push_back(CommonUtils::Strip(header.substr(index + 1)));
55             continue;
56         }
57         header_[CommonUtils::ToLower(CommonUtils::Strip(header.substr(0, index)))] =
58             CommonUtils::Strip(header.substr(index + 1));
59     }
60 }
61 
AppendCookies(const void * data,size_t length)62 void HttpResponse::AppendCookies(const void *data, size_t length)
63 {
64     cookies_.append(static_cast<const char *>(data), length);
65 }
66 
GetResult() const67 const std::string &HttpResponse::GetResult() const
68 {
69     return result_;
70 }
71 
GetResponseCode() const72 uint32_t HttpResponse::GetResponseCode() const
73 {
74     return responseCode_;
75 }
76 
GetHeader() const77 const std::map<std::string, std::string> &HttpResponse::GetHeader() const
78 {
79     return header_;
80 }
81 
GetsetCookie() const82 const std::vector<std::string> &HttpResponse::GetsetCookie() const
83 {
84     return setCookie_;
85 }
86 
GetCookies() const87 const std::string &HttpResponse::GetCookies() const
88 {
89     return cookies_;
90 }
91 
SetRawHeader(const std::string & header)92 void HttpResponse::SetRawHeader(const std::string &header)
93 {
94     rawHeader_ = header;
95 }
96 
GetRawHeader() const97 const std::string &HttpResponse::GetRawHeader() const
98 {
99     return rawHeader_;
100 }
101 
SetResult(const std::string & res)102 void HttpResponse::SetResult(const std::string &res)
103 {
104     result_ = res;
105 }
106 
SetCookies(const std::string & cookies)107 void HttpResponse::SetCookies(const std::string &cookies)
108 {
109     cookies_ = cookies;
110 }
111 
SetResponseTime(const std::string & time)112 void HttpResponse::SetResponseTime(const std::string &time)
113 {
114     responseTime_ = time;
115 }
116 
GetResponseTime() const117 const std::string &HttpResponse::GetResponseTime() const
118 {
119     return responseTime_;
120 }
121 
SetRequestTime(const std::string & time)122 void HttpResponse::SetRequestTime(const std::string &time)
123 {
124     requestTime_ = time;
125 }
126 
GetRequestTime() const127 const std::string &HttpResponse::GetRequestTime() const
128 {
129     return requestTime_;
130 }
131 
SetWarning(const std::string & val)132 void HttpResponse::SetWarning(const std::string &val)
133 {
134     header_[WARNING] = val;
135 }
136 } // namespace OHOS::NetStack::Http