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::Fetch {
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 auto 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::ToLower(CommonUtils::Strip(header.substr(0, index)))] =
54 CommonUtils::Strip(header.substr(index + 1));
55 }
56 }
57
GetData() const58 const std::string &FetchResponse::GetData() const
59 {
60 return data_;
61 }
62
GetResponseCode() const63 uint32_t FetchResponse::GetResponseCode() const
64 {
65 return responseCode_;
66 }
67
GetHeader() const68 const std::map<std::string, std::string> &FetchResponse::GetHeader() const
69 {
70 return header_;
71 }
72 } // namespace OHOS::NetStack::Fetch