1 /* 2 * Copyright (c) 2021-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 #ifndef OHOS_ABILITY_BASE_URI_H 17 #define OHOS_ABILITY_BASE_URI_H 18 19 #include <string> 20 #include <vector> 21 #include "parcel.h" 22 23 namespace OHOS { 24 class Uri : public Parcelable { 25 public: 26 explicit Uri(const std::string& uriString); 27 ~Uri(); 28 29 /** 30 * Get the Scheme part. 31 * 32 * @return the scheme string. 33 */ 34 std::string GetScheme(); 35 36 /** 37 * Get the Ssp part. 38 * 39 * @return the SchemeSpecificPart string. 40 */ 41 std::string GetSchemeSpecificPart(); 42 43 /** 44 * Get the GetAuthority part. 45 * 46 * @return the authority string. 47 */ 48 std::string GetAuthority(); 49 50 /** 51 * Get the Host part. 52 * 53 * @return the host string. 54 */ 55 std::string GetHost(); 56 57 /** 58 * Get the Port part. 59 * 60 * @return the port number. 61 */ 62 int GetPort(); 63 64 /** 65 * Get the User part. 66 * 67 * @return the user string. 68 */ 69 std::string GetUserInfo(); 70 71 /** 72 * Get the Query part. 73 * 74 * @return the query string. 75 */ 76 std::string GetQuery(); 77 78 /** 79 * Get the Path part. 80 * 81 * @return the path string. 82 */ 83 std::string GetPath(); 84 85 /** 86 * Get the path segments. 87 * 88 * @param the path segments of Uri. 89 */ 90 void GetPathSegments(std::vector<std::string>& segments); 91 92 /** 93 * Get the Fragment part. 94 * 95 * @return the fragment string. 96 */ 97 std::string GetFragment(); 98 99 /** 100 * Returns true if this URI is hierarchical like "http://www.example.com". 101 * Absolute URIs are hierarchical if the scheme-specific part starts with a '/'. 102 * Relative URIs are always hierarchical. 103 * 104 * @return true if this URI is hierarchical, false if it's opaque. 105 */ 106 bool IsHierarchical(); 107 108 /** 109 * Returns true if this URI is absolute, i.e. if it contains an explicit scheme. 110 * 111 * @return true if this URI is absolute, false if it's relative. 112 */ 113 bool IsAbsolute(); 114 115 /** 116 * Returns true if this URI is relative, i.e. if it doesn't contain an explicit scheme. 117 * 118 * @return true if this URI is relative, false if it's absolute. 119 */ 120 bool IsRelative(); 121 122 /** 123 * Check whether the other is the same as this. 124 * 125 * @return true if the same string. 126 */ 127 bool Equals(const Uri& other) const; 128 129 /** 130 * Compare to other uri. 131 * 132 * @return the string compare result. 133 */ 134 int CompareTo(const Uri& other) const; 135 136 /** 137 * Convert to a string object. 138 * 139 * @return a string object. 140 */ 141 std::string ToString() const; 142 143 /** 144 * override the == method. 145 * 146 * @return true if the same content, false not the same content. 147 */ 148 bool operator==(const Uri& other) const; 149 150 /** 151 * Override Parcelable' interface. 152 * 153 * @return true if parcel write success, false write fail. 154 */ 155 virtual bool Marshalling(Parcel& parcel) const override; 156 157 /** 158 * Support the Ummarshlling method for construct object by Parcel read. 159 * 160 * @return the uri object address. 161 */ 162 static Uri* Unmarshalling(Parcel& parcel); 163 164 private: 165 bool CheckScheme(); 166 std::string ParseScheme(); 167 std::string ParseSsp(); 168 std::string ParseAuthority(); 169 std::string ParseUserInfo(); 170 std::string ParseHost(); 171 int ParsePort(); 172 std::string ParsePath(size_t ssi); 173 std::string ParsePath(); 174 std::string ParseQuery(); 175 std::string ParseFragment(); 176 177 /** 178 * Finds the first ':'. 179 * 180 * @return the pos of the ':', string::npos if none found. 181 */ 182 size_t FindSchemeSeparator(); 183 184 /** 185 * Finds the first '#'. 186 * 187 * @return the pos of the '#', string::npos if none found. 188 */ 189 size_t FindFragmentSeparator(); 190 191 std::string uriString_; 192 std::string scheme_; 193 std::string ssp_; 194 std::string authority_; 195 std::string host_; 196 int port_; 197 std::string userInfo_; 198 std::string query_; 199 std::string path_; 200 std::string fragment_; 201 size_t cachedSsi_; 202 size_t cachedFsi_; 203 }; 204 } // namespace OHOS 205 #endif // OHOS_ABILITY_BASE_URI_H_ 206