1 /*
2 * Copyright (c) 2023 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 <iostream>
17 #include <vector>
18 #include <algorithm>
19
20 #include "http_client_response.h"
21 #include "http_client_constant.h"
22 #include "netstack_common_utils.h"
23 #include "netstack_log.h"
24
25 namespace OHOS {
26 namespace NetStack {
27 namespace HttpClient {
28
SetResponseCode(ResponseCode code)29 void HttpClientResponse::SetResponseCode(ResponseCode code)
30 {
31 responseCode_ = code;
32 }
33
GetResponseCode() const34 ResponseCode HttpClientResponse::GetResponseCode() const
35 {
36 return responseCode_;
37 }
38
AppendHeader(const char * data,size_t length)39 void HttpClientResponse::AppendHeader(const char *data, size_t length)
40 {
41 rawHeader_.append(static_cast<const char *>(data), length);
42 }
43
GetHeader() const44 const std::string &HttpClientResponse::GetHeader() const
45 {
46 return rawHeader_;
47 }
48
ParseHeaders()49 void HttpClientResponse::ParseHeaders()
50 {
51 std::vector<std::string> vec = CommonUtils::Split(rawHeader_, HttpConstant::HTTP_LINE_SEPARATOR);
52 for (const auto &header : vec) {
53 if (CommonUtils::Strip(header).empty()) {
54 continue;
55 }
56 auto index = header.find(HttpConstant::HTTP_HEADER_SEPARATOR);
57 if (index == std::string::npos) {
58 headers_[CommonUtils::Strip(header)] = "";
59 NETSTACK_LOGD("HEAD: %{public}s", CommonUtils::Strip(header).c_str());
60 continue;
61 }
62 if (CommonUtils::ToLower(CommonUtils::Strip(header.substr(0, index))) ==
63 HttpConstant::RESPONSE_KEY_SET_COOKIE) {
64 setCookie_.push_back(CommonUtils::Strip(header.substr(index + 1)));
65 continue;
66 }
67 headers_[CommonUtils::ToLower(CommonUtils::Strip(header.substr(0, index)))] =
68 CommonUtils::Strip(header.substr(index + 1));
69 }
70 }
71
GetHeaders() const72 const std::map<std::string, std::string> &HttpClientResponse::GetHeaders() const
73 {
74 return headers_;
75 }
76
AppendCookies(const char * data,size_t length)77 void HttpClientResponse::AppendCookies(const char *data, size_t length)
78 {
79 cookies_.append(static_cast<const char *>(data), length);
80 }
81
GetsetCookie() const82 const std::vector<std::string> &HttpClientResponse::GetsetCookie() const
83 {
84 return setCookie_;
85 }
86
GetCookies() const87 const std::string &HttpClientResponse::GetCookies() const
88 {
89 return cookies_;
90 }
91
SetRequestTime(const std::string & time)92 void HttpClientResponse::SetRequestTime(const std::string &time)
93 {
94 requestTime_ = time;
95 }
96
GetRequestTime() const97 const std::string &HttpClientResponse::GetRequestTime() const
98 {
99 return requestTime_;
100 }
101
SetResponseTime(const std::string & time)102 void HttpClientResponse::SetResponseTime(const std::string &time)
103 {
104 responseTime_ = time;
105 }
106
GetResponseTime() const107 const std::string &HttpClientResponse::GetResponseTime() const
108 {
109 return responseTime_;
110 }
111
SetWarning(const std::string & val)112 void HttpClientResponse::SetWarning(const std::string &val)
113 {
114 headers_[WARNING] = val;
115 }
116
SetRawHeader(const std::string & header)117 void HttpClientResponse::SetRawHeader(const std::string &header)
118 {
119 rawHeader_ = header;
120 }
121
SetCookies(const std::string & cookies)122 void HttpClientResponse::SetCookies(const std::string &cookies)
123 {
124 cookies_ = cookies;
125 }
126
AppendResult(const void * data,size_t length)127 void HttpClientResponse::AppendResult(const void *data, size_t length)
128 {
129 result_.append(static_cast<const char *>(data), length);
130 }
131
SetResult(const std::string & res)132 void HttpClientResponse::SetResult(const std::string &res)
133 {
134 result_ = res;
135 }
136
GetResult() const137 const std::string &HttpClientResponse::GetResult() const
138 {
139 return result_;
140 }
141
GetPerformanceTiming() const142 PerformanceInfo HttpClientResponse::GetPerformanceTiming() const
143 {
144 return performanceInfo_;
145 }
146 } // namespace HttpClient
147 } // namespace NetStack
148 } // namespace OHOS