1 /*
2 * Copyright (c) 2021-2022 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 #include "http_response.h"
20
21 namespace OHOS::NetStack {
HttpResponse()22 HttpResponse::HttpResponse() : responseCode_(0) {}
23
AppendResult(const void * data,size_t length)24 void HttpResponse::AppendResult(const void *data, size_t length)
25 {
26 result_.append(static_cast<const char *>(data), length);
27 }
28
AppendRawHeader(const void * data,size_t length)29 void HttpResponse::AppendRawHeader(const void *data, size_t length)
30 {
31 rawHeader_.append(static_cast<const char *>(data), length);
32 }
33
SetResponseCode(uint32_t responseCode)34 void HttpResponse::SetResponseCode(uint32_t responseCode)
35 {
36 responseCode_ = responseCode;
37 }
38
ParseHeaders()39 void HttpResponse::ParseHeaders()
40 {
41 std::vector<std::string> vec = CommonUtils::Split(rawHeader_, HttpConstant::HTTP_LINE_SEPARATOR);
42 for (const auto &header : vec) {
43 if (CommonUtils::Strip(header).empty()) {
44 continue;
45 }
46 auto index = header.find(HttpConstant::HTTP_HEADER_SEPARATOR);
47 if (index == std::string::npos) {
48 header_[CommonUtils::Strip(header)] = "";
49 NETSTACK_LOGI("HEAD: %{public}s", CommonUtils::Strip(header).c_str());
50 continue;
51 }
52 header_[CommonUtils::ToLower(CommonUtils::Strip(header.substr(0, index)))] =
53 CommonUtils::Strip(header.substr(index + 1));
54 }
55 }
56
AppendCookies(const void * data,size_t length)57 void HttpResponse::AppendCookies(const void *data, size_t length)
58 {
59 cookies_.append(static_cast<const char *>(data), length);
60 }
61
GetResult() const62 const std::string &HttpResponse::GetResult() const
63 {
64 return result_;
65 }
66
GetResponseCode() const67 uint32_t HttpResponse::GetResponseCode() const
68 {
69 return responseCode_;
70 }
71
GetHeader() const72 const std::map<std::string, std::string> &HttpResponse::GetHeader() const
73 {
74 return header_;
75 }
76
GetCookies() const77 const std::string &HttpResponse::GetCookies() const
78 {
79 return cookies_;
80 }
81
SetRawHeader(const std::string & header)82 void HttpResponse::SetRawHeader(const std::string &header)
83 {
84 rawHeader_ = header;
85 }
86
GetRawHeader() const87 const std::string &HttpResponse::GetRawHeader() const
88 {
89 return rawHeader_;
90 }
91
SetResult(const std::string & res)92 void HttpResponse::SetResult(const std::string &res)
93 {
94 result_ = res;
95 }
96
SetCookies(const std::string & cookies)97 void HttpResponse::SetCookies(const std::string &cookies)
98 {
99 cookies_ = cookies;
100 }
101
SetResponseTime(const std::string & time)102 void HttpResponse::SetResponseTime(const std::string &time)
103 {
104 responseTime_ = time;
105 }
106
GetResponseTime() const107 const std::string &HttpResponse::GetResponseTime() const
108 {
109 return responseTime_;
110 }
111
SetRequestTime(const std::string & time)112 void HttpResponse::SetRequestTime(const std::string &time)
113 {
114 requestTime_ = time;
115 }
116
GetRequestTime() const117 const std::string &HttpResponse::GetRequestTime() const
118 {
119 return requestTime_;
120 }
121
SetWarning(const std::string & val)122 void HttpResponse::SetWarning(const std::string &val)
123 {
124 header_[WARNING] = val;
125 }
126 } // namespace OHOS::NetStack