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 headers_[CommonUtils::ToLower(CommonUtils::Strip(header.substr(0, index)))] =
63 CommonUtils::Strip(header.substr(index + 1));
64 }
65 }
66
GetHeaders() const67 const std::map<std::string, std::string> &HttpClientResponse::GetHeaders() const
68 {
69 return headers_;
70 }
71
AppendCookies(const char * data,size_t length)72 void HttpClientResponse::AppendCookies(const char *data, size_t length)
73 {
74 cookies_.append(static_cast<const char *>(data), length);
75 }
76
GetCookies() const77 const std::string &HttpClientResponse::GetCookies() const
78 {
79 return cookies_;
80 }
81
SetRequestTime(const std::string & time)82 void HttpClientResponse::SetRequestTime(const std::string &time)
83 {
84 requestTime_ = time;
85 }
86
GetRequestTime() const87 const std::string &HttpClientResponse::GetRequestTime() const
88 {
89 return requestTime_;
90 }
91
SetResponseTime(const std::string & time)92 void HttpClientResponse::SetResponseTime(const std::string &time)
93 {
94 responseTime_ = time;
95 }
96
GetResponseTime() const97 const std::string &HttpClientResponse::GetResponseTime() const
98 {
99 return responseTime_;
100 }
101
SetWarning(const std::string & val)102 void HttpClientResponse::SetWarning(const std::string &val)
103 {
104 headers_[WARNING] = val;
105 }
106
SetRawHeader(const std::string & header)107 void HttpClientResponse::SetRawHeader(const std::string &header)
108 {
109 rawHeader_ = header;
110 }
111
SetCookies(const std::string & cookies)112 void HttpClientResponse::SetCookies(const std::string &cookies)
113 {
114 cookies_ = cookies;
115 }
116
AppendResult(const void * data,size_t length)117 void HttpClientResponse::AppendResult(const void *data, size_t length)
118 {
119 result_.append(static_cast<const char *>(data), length);
120 }
121
SetResult(const std::string & res)122 void HttpClientResponse::SetResult(const std::string &res)
123 {
124 result_ = res;
125 }
126
GetResult() const127 const std::string &HttpClientResponse::GetResult() const
128 {
129 return result_;
130 }
131 } // namespace HttpClient
132 } // namespace NetStack
133 } // namespace OHOS