1 // Copyright 2022 Google LLC 2 // 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 // https://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 #ifndef CONTRIB_URIPARSER_UTILS_UTILS_ZIP_H_ 16 #define CONTRIB_URIPARSER_UTILS_UTILS_ZIP_H_ 17 18 #include <vector> 19 20 #include "absl/container/btree_map.h" 21 #include "absl/log/die_if_null.h" 22 #include "contrib/uriparser/sandboxed.h" 23 #include "sandboxed_api/util/status_macros.h" 24 25 class UriParser { 26 public: UriParser(UriparserSandbox * sandbox,const std::string & uri)27 UriParser(UriparserSandbox* sandbox, const std::string& uri) 28 : sandbox_(ABSL_DIE_IF_NULL(sandbox)), 29 api_(sandbox_), 30 c_uri_(uri.c_str()) { 31 status_ = ParseUri(); 32 } 33 34 ~UriParser(); 35 36 absl::Status GetStatus(); 37 absl::Status NormalizeSyntax(); 38 absl::Status NormalizeSyntax(int norm_mask); 39 40 absl::StatusOr<std::string> GetUri(); 41 absl::StatusOr<std::string> GetUriWithBase(const std::string& base); 42 absl::StatusOr<std::string> GetUriWithoutBase(const std::string& base, 43 bool domain_root_mode); 44 absl::StatusOr<std::string> GetScheme(); 45 absl::StatusOr<std::string> GetUserInfo(); 46 absl::StatusOr<std::string> GetHostText(); 47 absl::StatusOr<std::string> GetHostIP(); 48 absl::StatusOr<std::string> GetPortText(); 49 absl::StatusOr<std::string> GetQuery(); 50 absl::StatusOr<std::string> GetFragment(); 51 absl::StatusOr<std::string> GetUriEscaped(bool space_to_plus, 52 bool normalize_breaks); 53 absl::StatusOr<std::vector<std::string>> GetPath(); 54 absl::StatusOr<absl::btree_map<std::string, std::string>> GetQueryElements(); 55 56 protected: 57 absl::StatusOr<std::string> FetchUriText(UriTextRangeA* ptr); 58 absl::StatusOr<std::string> GetUri(sapi::v::Struct<UriUriA>* uri); 59 absl::Status ParseUri(); 60 sapi::v::Struct<UriUriA> uri_; 61 62 private: 63 UriparserSandbox* sandbox_; 64 UriparserApi api_; 65 sapi::v::ConstCStr c_uri_; // We have to keep a orginal string in sandbox 66 absl::Status status_; 67 }; 68 69 #endif // CONTRIB_URIARSER_UTILS_UTILS_ZIP_H_ 70