xref: /aosp_15_r20/external/sandboxed-api/contrib/uriparser/example/main.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 #include <fstream>
16 #include <iostream>
17 #include <string>
18 #include <vector>
19 
20 #include "absl/flags/parse.h"
21 #include "absl/log/globals.h"
22 #include "absl/log/initialize.h"
23 #include "contrib/uriparser/sandboxed.h"
24 #include "contrib/uriparser/utils/utils_uriparser.h"
25 
Print(const char * name,const absl::StatusOr<std::string> & r)26 void Print(const char* name, const absl::StatusOr<std::string>& r) {
27   if (!r.ok()) {
28     std::cerr << "Unable to fetch " << name << "\n";
29     std::cerr << r.status() << "\n";
30     return;
31   }
32 
33   if (r.value().empty()) {
34     return;
35   }
36 
37   std::cout << name << ": " << *r << "\n";
38 }
39 
main(int argc,char * argv[])40 int main(int argc, char* argv[]) {
41   std::string prog_name(argv[0]);
42   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
43   std::vector<char*> args = absl::ParseCommandLine(argc, argv);
44   absl::InitializeLog();
45 
46   if (args.size() < 2) {
47     std::cerr << "Usage:\n  " << prog_name << " URI ...\n";
48     return EXIT_FAILURE;
49   }
50 
51   UriparserSapiSandbox sandbox;
52   if (!sandbox.Init().ok()) {
53     std::cerr << "Unable to start sandbox\n";
54     return EXIT_FAILURE;
55   }
56 
57   int retval = EXIT_SUCCESS;
58   for (int i = 1; i < args.size(); ++i) {
59     UriParser uri(&sandbox, args[i]);
60     if (!uri.GetStatus().ok()) {
61       std::cerr << "Unable to parse: " << args[i] << "\n";
62       std::cerr << uri.GetStatus() << "\n";
63       retval = EXIT_FAILURE;
64       continue;
65     }
66 
67     Print("scheme", uri.GetScheme());
68     Print("user info", uri.GetUserInfo());
69     Print("host", uri.GetHostText());
70     Print("host IP", uri.GetHostIP());
71     Print("port", uri.GetPortText());
72     Print("query", uri.GetQuery());
73     Print("fragment", uri.GetFragment());
74 
75     absl::StatusOr<std::vector<std::string>> path = uri.GetPath();
76     if (!path.ok()) {
77       std::cerr << "Unable to get path.\n";
78       std::cerr << path.status() << "\n";
79       retval = EXIT_FAILURE;
80       continue;
81     }
82     if (!path->empty()) {
83       std::cout << "pathSeq: \n";
84       for (const auto& s : path.value()) {
85         std::cout << " - " << s << "\n";
86       }
87     }
88 
89     absl::StatusOr<absl::btree_map<std::string, std::string>> query_map;
90     query_map = uri.GetQueryElements();
91     if (!query_map.ok()) {
92       std::cerr << "Unable to get query.\n";
93       std::cerr << query_map.status() << "\n";
94       retval = EXIT_FAILURE;
95       continue;
96     }
97     if (!query_map->empty()) {
98       std::cout << "Query elements: \n";
99       for (const auto& mp : query_map.value()) {
100         std::cout << " - " << mp.first << ": " << mp.second << "\n";
101       }
102     }
103 
104     if (!uri.NormalizeSyntax().ok()) {
105       std::cerr << "Unable to normalize: " << args[i] << "\n";
106       continue;
107     }
108     absl::StatusOr<std::string> newuris = uri.GetUri();
109     if (!newuris.ok()) {
110       std::cerr << "Unable to reconstruct path.\n";
111       std::cerr << newuris.status() << "\n";
112       retval = EXIT_FAILURE;
113       continue;
114     }
115     std::cout << "Normalized path: " << newuris.value() << "\n";
116   }
117 
118   return retval;
119 }
120