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 #ifndef URI_JS_URI_H 17 #define URI_JS_URI_H 18 19 #include <bitset> 20 #include <cstdlib> 21 #include <regex> 22 #include <string> 23 #include <string_view> 24 #include <vector> 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 28 namespace OHOS::Uri { 29 constexpr int MAX_BIT_SIZE = 128; 30 struct UriData { 31 int port = -1; 32 std::string scheme = ""; 33 std::string userInfo = ""; 34 std::string host = ""; 35 std::string query = ""; 36 std::string fragment = ""; 37 std::string path = ""; 38 std::string authority = ""; 39 std::string SchemeSpecificPart = ""; 40 }; 41 42 class Uri { 43 public: 44 /** 45 * URI constructor, which is used to instantiate a URI object. 46 * 47 * @param input Constructs a URI by parsing a given string. 48 */ 49 explicit Uri(const std::string input); 50 51 /** 52 * The destructor of the Uri. 53 */ ~Uri()54 ~Uri() {} 55 56 /** 57 * Tests whether this URI is equivalent to other URI objects. 58 * 59 * @param other URI object to be compared 60 */ 61 bool Equals(const Uri other) const; 62 63 /** 64 * Indicates whether this URI is an absolute URI. 65 */ 66 bool IsAbsolute() const; 67 68 /** 69 * Determine whether parsing failed. 70 */ 71 std::string IsFailed() const; 72 73 /** 74 * Returns the serialized URI as a string. 75 */ 76 std::string ToString() const; 77 78 /** 79 * Indicates whether this URI is an relative URI. 80 */ 81 bool IsRelative() const; 82 83 /** 84 * Indicates whether this URI is an opaque URI. 85 */ 86 bool IsOpaque() const; 87 88 /** 89 * Indicates whether this URI is an hierarchical URI. 90 */ 91 bool IsHierarchical() const; 92 93 /** 94 * Add key and value to Uri's query 95 */ 96 std::string AddQueryValue(const std::string key, const std::string value) const; 97 98 /** 99 * Add pathSegment to Uri's segment 100 */ 101 std::string AddSegment(const std::string pathSegment) const; 102 103 /** 104 * Gets the LastSegment of the URI. 105 */ 106 std::string GetLastSegment() const; 107 108 /** 109 * Normalize the path of this URI. 110 */ 111 std::string Normalize() const; 112 113 /** 114 * Gets the protocol part of the URI. 115 */ 116 std::string GetScheme() const; 117 118 /** 119 * Gets the decoding permission component part of this URI. 120 */ 121 std::string GetAuthority() const; 122 123 /** 124 * Gets the decoding scheme-specific part of the URI. 125 */ 126 std::string GetSsp() const; 127 128 /** 129 * Obtains the user information part of the URI. 130 */ 131 std::string GetUserinfo() const; 132 133 /** 134 * Gets the hostname portion of the URI without a port. 135 */ 136 std::string GetHost() const; 137 138 /** 139 * Gets the hostname portion of the URI without a port. 140 */ 141 std::string GetPort() const; 142 143 /** 144 * Gets the path portion of the URI. 145 */ 146 std::string GetPath() const; 147 148 /** 149 * Gets the query portion of the URI. 150 */ 151 std::string GetQuery() const; 152 153 /** 154 * Gets the fragment part of the URI. 155 */ 156 std::string GetFragment() const; 157 158 /** 159 * Clear Uri's query 160 */ 161 std::string ClearQuery() const; 162 163 /** 164 * Sets the protocol part of the URI. 165 */ 166 void SetScheme(const std::string_view Scheme); 167 168 /** 169 * Sets the user information part of the URI. 170 */ 171 void SetUserInfo(const std::string userInfo); 172 173 /** 174 * Sets the path portion of the URI. 175 */ 176 void SetPath(const std::string pathStr); 177 178 /** 179 * Sets the query portion of the URI. 180 */ 181 void SetQuery(const std::string queryStr); 182 183 /** 184 * Sets the fragment part of the URI. 185 */ 186 void SetFragment(const std::string fragmentStr); 187 188 /** 189 * Sets the permission component part of this URI. 190 */ 191 void SetAuthority(const std::string authorityStr); 192 193 /** 194 * Sets the decoding scheme-specific part of the URI. 195 */ 196 void SetSsp(const std::string sspStr); 197 private: 198 void PreliminaryWork() const; 199 void AnalysisUri(); 200 void SpecialPath(); 201 void AnalysisFragment(size_t pos); 202 void AnalysisQuery(size_t pos); 203 void AnalysisScheme(size_t pos); 204 void AnalysisHostAndPath(); 205 void AnalysisHost(bool isLawfulProt); 206 void AnalysisPath(size_t pos); 207 void AnalysisUserInfo(size_t pos); 208 void AnalysisIPV6(); 209 void AssignSchemeSpecificPart(); 210 211 bool CheckCharacter(std::string data, std::bitset<MAX_BIT_SIZE> rule, bool flag) const; 212 bool AnalysisPort(size_t pos); 213 bool AnalysisIPV4(); 214 215 std::string UpdateToString() const; 216 void UpdateAuthority(); 217 void UpdateSsp(); 218 std::string Split(const std::string &path) const; 219 std::string BuildUriString(const std::string str, const std::string param) const; 220 221 private: 222 UriData uriData_; 223 std::string data_ {}; 224 std::string inputUri_ {}; 225 std::string errStr_ {}; 226 }; 227 } // namespace OHOS::Uri 228 #endif // URI_JS_URI_H 229