1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "URIUtils"
16
17 #include "datashare_uri_utils.h"
18
19 #include <string>
20
21 #include "log_print.h"
22 #include "string_ex.h"
23 #include "uri.h"
24
25 namespace OHOS::DataShare {
FormatUri(const std::string & uri)26 std::string DataShareURIUtils::FormatUri(const std::string &uri)
27 {
28 auto pos = uri.find_last_of('?');
29 if (pos == std::string::npos) {
30 return uri;
31 }
32
33 return uri.substr(0, pos);
34 }
35
Strtoul(const std::string & str)36 std::pair<bool, uint32_t> DataShareURIUtils::Strtoul(const std::string &str)
37 {
38 unsigned long data = 0;
39 if (str.empty()) {
40 return std::make_pair(false, data);
41 }
42 char* end = nullptr;
43 errno = 0;
44 data = strtoul(str.c_str(), &end, BASE_TEN);
45 if (errno == ERANGE || end == nullptr || end == str || *end != '\0') {
46 return std::make_pair(false, data);
47 }
48 return std::make_pair(true, data);
49 }
50
GetQueryParams(const std::string & uri)51 std::map<std::string, std::string> DataShareURIUtils::GetQueryParams(const std::string& uri)
52 {
53 size_t queryStartPos = uri.find('?');
54 if (queryStartPos == std::string::npos) {
55 return {};
56 }
57 std::map<std::string, std::string> params;
58 std::string queryParams = uri.substr(queryStartPos + 1);
59 size_t startPos = 0;
60 while (startPos < queryParams.size()) {
61 size_t delimiterIndex = queryParams.find('&', startPos);
62 if (delimiterIndex == std::string::npos) {
63 delimiterIndex = queryParams.size();
64 }
65 size_t equalIndex = queryParams.find('=', startPos);
66 if (equalIndex == std::string::npos || equalIndex > delimiterIndex) {
67 startPos = delimiterIndex + 1;
68 continue;
69 }
70 std::string key = queryParams.substr(startPos, equalIndex - startPos);
71 std::string value = queryParams.substr(equalIndex + 1, delimiterIndex - equalIndex - 1);
72 params[key] = value;
73 startPos = delimiterIndex + 1;
74 }
75 return params;
76 }
77
GetUserFromUri(const std::string & uri)78 std::pair<bool, int32_t> DataShareURIUtils::GetUserFromUri(const std::string &uri)
79 {
80 auto queryParams = GetQueryParams(uri);
81 if (queryParams[USER_PARAM].empty()) {
82 // -1 is placeholder for visit provider's user
83 return std::make_pair(true, -1);
84 }
85 auto [success, data] = Strtoul(queryParams[USER_PARAM]);
86 if (!success) {
87 return std::make_pair(false, -1);
88 }
89 if (data < 0 || data > INT32_MAX) {
90 return std::make_pair(false, -1);
91 }
92 return std::make_pair(true, data);
93 }
94 } // namespace OHOS::DataShare