mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
51799f99ae
Instead of calling `google::InitGoogleLogging()` directly, introduce an indirection via a new utility library. After this change, Sandboxed API should consistently use `sapi::InitLogging()` everywhere. For now, `sapi::InitLogging()` simply calls its glog equivalent. However, this enables us to migrate away from the gflags dependency and use Abseil flags. Once a follow-up change lands, `sapi::InitLogging()` will instead initialize the google logging library with flags defined from Aseil. Later still, once Abseil releases logging, we can then drop the glog dependency entirely. PiperOrigin-RevId: 445363592 Change-Id: Ia23a7dc88b8ffe65a422ea4d5233bba7bdd1303a
118 lines
3.3 KiB
C++
118 lines
3.3 KiB
C++
// Copyright 2022 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "absl/flags/parse.h"
|
|
#include "contrib/uriparser/sandboxed.h"
|
|
#include "contrib/uriparser/utils/utils_uriparser.h"
|
|
#include "sandboxed_api/util/logging.h"
|
|
|
|
void Print(const char* name, const absl::StatusOr<std::string>& r) {
|
|
if (!r.ok()) {
|
|
std::cerr << "Unable to fetch " << name << "\n";
|
|
std::cerr << r.status() << "\n";
|
|
return;
|
|
}
|
|
|
|
if (r.value().empty()) {
|
|
return;
|
|
}
|
|
|
|
std::cout << name << ": " << *r << "\n";
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
std::string prog_name(argv[0]);
|
|
std::vector<char*> args = absl::ParseCommandLine(argc, argv);
|
|
sapi::InitLogging(argv[0]);
|
|
|
|
if (args.size() < 2) {
|
|
std::cerr << "Usage:\n " << prog_name << " URI ...\n";
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
UriparserSapiSandbox sandbox;
|
|
if (!sandbox.Init().ok()) {
|
|
std::cerr << "Unable to start sandbox\n";
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
int retval = EXIT_SUCCESS;
|
|
for (int i = 1; i < args.size(); ++i) {
|
|
UriParser uri(&sandbox, args[i]);
|
|
if (!uri.GetStatus().ok()) {
|
|
std::cerr << "Unable to parse: " << args[i] << "\n";
|
|
std::cerr << uri.GetStatus() << "\n";
|
|
retval = EXIT_FAILURE;
|
|
continue;
|
|
}
|
|
|
|
Print("scheme", uri.GetScheme());
|
|
Print("user info", uri.GetUserInfo());
|
|
Print("host", uri.GetHostText());
|
|
Print("host IP", uri.GetHostIP());
|
|
Print("port", uri.GetPortText());
|
|
Print("query", uri.GetQuery());
|
|
Print("fragment", uri.GetFragment());
|
|
|
|
absl::StatusOr<std::vector<std::string>> path = uri.GetPath();
|
|
if (!path.ok()) {
|
|
std::cerr << "Unable to get path.\n";
|
|
std::cerr << path.status() << "\n";
|
|
retval = EXIT_FAILURE;
|
|
continue;
|
|
}
|
|
if (!path->empty()) {
|
|
std::cout << "pathSeq: \n";
|
|
for (const auto& s : path.value()) {
|
|
std::cout << " - " << s << "\n";
|
|
}
|
|
}
|
|
|
|
absl::StatusOr<absl::btree_map<std::string, std::string>> query_map;
|
|
query_map = uri.GetQueryElements();
|
|
if (!query_map.ok()) {
|
|
std::cerr << "Unable to get query.\n";
|
|
std::cerr << query_map.status() << "\n";
|
|
retval = EXIT_FAILURE;
|
|
continue;
|
|
}
|
|
if (!query_map->empty()) {
|
|
std::cout << "Query elements: \n";
|
|
for (const auto& mp : query_map.value()) {
|
|
std::cout << " - " << mp.first << ": " << mp.second << "\n";
|
|
}
|
|
}
|
|
|
|
if (!uri.NormalizeSyntax().ok()) {
|
|
std::cerr << "Unable to normalize: " << args[i] << "\n";
|
|
continue;
|
|
}
|
|
absl::StatusOr<std::string> newuris = uri.GetUri();
|
|
if (!newuris.ok()) {
|
|
std::cerr << "Unable to reconstruct path.\n";
|
|
std::cerr << newuris.status() << "\n";
|
|
retval = EXIT_FAILURE;
|
|
continue;
|
|
}
|
|
std::cout << "Normalized path: " << newuris.value() << "\n";
|
|
}
|
|
|
|
return retval;
|
|
}
|