• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "uri_utils.h"
17 #include "constant.h"
18 #include "log.h"
19 
20 namespace OHOS::Request::Download {
UriUtils(void)21 UriUtils::UriUtils(void)
22 {
23 }
24 
~UriUtils()25 UriUtils::~UriUtils()
26 {
27 }
28 
getQueryParameter(OHOS::Uri & uri)29 std::map<std::string, std::string> UriUtils::getQueryParameter(OHOS::Uri &uri)
30 {
31     std::map<std::string, std::string> mapQuery;
32     std::string query = uri.GetQuery();
33     if (query.empty()) {
34         return mapQuery;
35     }
36     std::vector<std::string> tempVector = split(query, "&");
37     int size = tempVector.size();
38     for (int i = 0; i < size; i++) {
39         std::vector<std::string> childTempVector = split(tempVector[i], "=");
40         size_t size = childTempVector.size();
41         if (size != REQUEST_PARAMS_NUM) {
42             DOWNLOAD_HILOGE("UriUtils uriParse getQueryParameter query parameter error");
43             break;
44         }
45         std::string key = childTempVector[0];
46         std::string value = childTempVector[1];
47         mapQuery.insert(std::make_pair(key, value));
48     }
49     return mapQuery;
50 }
51 
split(const std::string & str,const std::string & split)52 std::vector<std::string> UriUtils::split(const std::string &str, const std::string &split)
53 {
54     char *saveChar = nullptr;
55     char *token = strtok_r(const_cast<char *>(str.c_str()), split.c_str(), &saveChar);
56     std::vector<std::string> result;
57     while (token != nullptr) {
58         result.emplace_back(token);
59         token = strtok_r(nullptr, split.c_str(), &saveChar);
60     }
61     return result;
62 }
63 
UriParse(OHOS::Uri & uri,std::map<std::string,int> & keyMap)64 int UriUtils::UriParse(OHOS::Uri &uri, std::map<std::string, int> &keyMap)
65 {
66     std::string path = uri.GetPath();
67     if (path.empty()) {
68         return OPERATION_ERROR;
69     }
70     std::map<std::string, int>::iterator iterator = keyMap.find(path);
71     if (iterator != keyMap.end()) {
72         return iterator->second;
73     }
74     return OPERATION_ERROR;
75 }
76 } // namespace OHOS::Request::Download