• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "uri_utils.h"
18 
19 #include "log_print.h"
20 #include "string_ex.h"
21 #include "uri.h"
22 #include "utils/anonymous.h"
23 
24 namespace OHOS::DataShare {
25 constexpr const char USER_PARAM[] = "user";
26 constexpr const char TOKEN_ID_PARAM[] = "srcToken";
27 constexpr const char DST_BUNDLE_NAME_PARAM[] = "dstBundleName";
GetInfoFromURI(const std::string & uri,UriInfo & uriInfo)28 bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo)
29 {
30     Uri uriTemp(uri);
31     std::vector<std::string> splitUri;
32     SplitStr(uriTemp.GetPath(), "/", splitUri);
33     if (splitUri.size() < PARAM_SIZE) {
34         ZLOGE("Invalid uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
35         return false;
36     }
37 
38     if (splitUri[BUNDLE_NAME].empty() || splitUri[MODULE_NAME].empty() ||
39         splitUri[STORE_NAME].empty() || splitUri[TABLE_NAME].empty()) {
40         ZLOGE("Uri has empty field! bundleName: %{public}s  uri: %{public}s", splitUri[BUNDLE_NAME].c_str(),
41             DistributedData::Anonymous::Change(uri).c_str());
42         return false;
43     }
44 
45     uriInfo.bundleName = splitUri[BUNDLE_NAME];
46     uriInfo.moduleName = splitUri[MODULE_NAME];
47     uriInfo.storeName = splitUri[STORE_NAME];
48     uriInfo.tableName = splitUri[TABLE_NAME];
49     return true;
50 }
51 
IsDataProxyURI(const std::string & uri)52 bool URIUtils::IsDataProxyURI(const std::string &uri)
53 {
54     return uri.compare(0, DATA_PROXY_SCHEMA_LEN, URIUtils::DATA_PROXY_SCHEMA) == 0;
55 }
56 
GetBundleNameFromProxyURI(const std::string & uri,std::string & bundleName)57 bool URIUtils::GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName)
58 {
59     Uri uriTemp(uri);
60     if (!uriTemp.GetAuthority().empty()) {
61         bundleName = uriTemp.GetAuthority();
62     }
63     return true;
64 }
65 
GetInfoFromProxyURI(const std::string & uri,int32_t & user,uint32_t & callerTokenId,std::string & calledBundleName)66 bool URIUtils::GetInfoFromProxyURI(
67     const std::string &uri, int32_t &user, uint32_t &callerTokenId, std::string &calledBundleName)
68 {
69     auto queryPos = uri.find_last_of('?');
70     if (queryPos == std::string::npos) {
71         return true;
72     }
73     std::string query = uri.substr(queryPos + 1);
74     std::string::size_type pos = 0;
75     std::string::size_type nextPos;
76     std::string::size_type valueStartPos;
77     while (pos != std::string::npos) {
78         valueStartPos = query.find_first_of('=', pos);
79         if (valueStartPos == std::string::npos) {
80             ZLOGE("parse failed %{public}s", query.c_str());
81             return false;
82         }
83         valueStartPos += 1;
84         nextPos = query.find_first_of('&', pos);
85         std::string value = (nextPos == std::string::npos ? query.substr(valueStartPos)
86                                                         : query.substr(valueStartPos, nextPos - valueStartPos));
87         if (!value.empty()) {
88             if (query.compare(pos, sizeof(USER_PARAM) - 1, USER_PARAM) == 0) {
89                 user = std::stoi(value);
90             } else if (query.compare(pos, sizeof(TOKEN_ID_PARAM) - 1, TOKEN_ID_PARAM) == 0) {
91                 callerTokenId = std::stoul(value);
92             } else if (query.compare(pos, sizeof(DST_BUNDLE_NAME_PARAM) - 1, DST_BUNDLE_NAME_PARAM) == 0) {
93                 calledBundleName = value;
94             }
95         }
96         if (nextPos == std::string::npos) {
97             break;
98         }
99         pos = nextPos + 1;
100     }
101     return true;
102 }
103 
FormatUri(std::string & uri)104 void URIUtils::FormatUri(std::string &uri)
105 {
106     auto pos = uri.find_last_of('?');
107     if (pos == std::string::npos) {
108         return;
109     }
110 
111     uri.resize(pos);
112 }
113 } // namespace OHOS::DataShare