1 /*
2 * Copyright (c) 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 "fetch_response.h"
17
18 #include "constant.h"
19 #include "netstack_common_utils.h"
20 #include "netstack_log.h"
21
22 namespace OHOS::NetStack {
FetchResponse()23 FetchResponse::FetchResponse() : responseCode_(0) {}
24
AppendData(const void * data,size_t length)25 void FetchResponse::AppendData(const void *data, size_t length)
26 {
27 data_.append(static_cast<const char *>(data), length);
28 }
29
AppendRawHeader(const void * data,size_t length)30 void FetchResponse::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 FetchResponse::SetResponseCode(uint32_t responseCode)
36 {
37 responseCode_ = responseCode;
38 }
39
ParseHeaders()40 void FetchResponse::ParseHeaders()
41 {
42 std::vector<std::string> vec = CommonUtils::Split(rawHeader_, FetchConstant::HTTP_LINE_SEPARATOR);
43 for (const auto &header : vec) {
44 if (CommonUtils::Strip(header).empty()) {
45 continue;
46 }
47 size_t index = header.find(FetchConstant::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
GetData() const57 const std::string &FetchResponse::GetData() const
58 {
59 return data_;
60 }
61
GetResponseCode() const62 uint32_t FetchResponse::GetResponseCode() const
63 {
64 return responseCode_;
65 }
66
GetHeader() const67 const std::map<std::string, std::string> &FetchResponse::GetHeader() const
68 {
69 return header_;
70 }
71 } // namespace OHOS::NetStack