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 "http_response.h"
17
18 #include "constant.h"
19 #include "netstack_common_utils.h"
20 #include "netstack_log.h"
21
22 namespace OHOS::NetStack {
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_, HttpConstant::HTTP_LINE_SEPARATOR);
43 for (const auto &header : vec) {
44 if (CommonUtils::Strip(header).empty()) {
45 continue;
46 }
47 size_t index = header.find(HttpConstant::HTTP_HEADER_SEPARATOR);
48 if (index == std::string::npos) {
49 header_[CommonUtils::Strip(header)] = "";
50 NETSTACK_LOGI("HEAD: %{public}s", CommonUtils::Strip(header).c_str());
51 continue;
52 }
53 header_[CommonUtils::Strip(header.substr(0, index))] = 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 } // namespace OHOS::NetStack