srctree

Robin Linden parent 6588a2c5 726720cd
uri: Create a new library for the Uri class

It's important enough that I don't think it's fair that it lives withthe parser util, and when we add a spec-compliant parser for it it willget significantly bigger.

inlinesplit
browser/BUILD added: 48, removed: 44, total 4
@@ -18,7 +18,7 @@ cc_binary(
"//layout",
"//style",
"//tui",
"//util",
"//uri",
"@spdlog",
],
)
@@ -33,7 +33,7 @@ cc_binary(
"//http",
"//layout",
"//style",
"//util",
"//uri",
"@imgui",
"@imgui-sfml",
"@sfml//:graphics",
 
browser/gui.cpp added: 48, removed: 44, total 4
@@ -94,7 +94,7 @@ int main() {
ImGui::Begin("Navigation");
if (ImGui::InputText(
"Url", url_buf, sizeof(url_buf), ImGuiInputTextFlags_EnterReturnsTrue)) {
auto uri = util::Uri::parse(url_buf);
auto uri = uri::Uri::parse(url_buf);
if (!uri) {
continue;
}
@@ -132,7 +132,7 @@ int main() {
auto const &elem = std::get<dom::Element>(link->data);
auto stylesheet_url = fmt::format("{}{}", url_buf, elem.attributes.at("href"));
spdlog::info("Downloading stylesheet from {}", stylesheet_url);
auto style_data = http::get(*util::Uri::parse(stylesheet_url));
auto style_data = http::get(*uri::Uri::parse(stylesheet_url));
 
auto new_rules = css::parse(style_data.body);
stylesheet.reserve(stylesheet.size() + new_rules.size());
 
browser/tui.cpp added: 48, removed: 44, total 4
@@ -17,7 +17,7 @@ char const *const kDefaultUri = "http://www.example.com";
int main(int argc, char **argv) {
spdlog::cfg::load_env_levels();
 
auto uri = argc > 1 ? util::Uri::parse(argv[1]) : util::Uri::parse(kDefaultUri);
auto uri = argc > 1 ? uri::Uri::parse(argv[1]) : uri::Uri::parse(kDefaultUri);
if (!uri) {
spdlog::error("Unable to parse uri from {}", argc > 1 ? argv[1] : kDefaultUri);
return 1;
 
http/BUILD added: 48, removed: 44, total 4
@@ -6,7 +6,7 @@ cc_library(
hdrs = ["get.h"],
visibility = ["//visibility:public"],
deps = [
"//util",
"//uri",
"@asio",
"@fmt",
],
 
http/get.cpp added: 48, removed: 44, total 4
@@ -23,7 +23,7 @@ std::pair<std::string_view, std::string_view> split(std::string_view str, std::s
 
} // namespace
 
Response get(util::Uri const &uri) {
Response get(uri::Uri const &uri) {
if (uri.scheme == "http"sv) {
asio::ip::tcp::iostream stream(uri.authority.host, "http"sv);
stream << fmt::format("GET {} HTTP/1.1\r\n", uri.path);
 
http/get.h added: 48, removed: 44, total 4
@@ -1,7 +1,7 @@
#ifndef HTTP_GET_H_
#define HTTP_GET_H_
 
#include "util/uri.h"
#include "uri/uri.h"
 
#include <string>
#include <string_view>
@@ -20,7 +20,7 @@ struct Response {
std::string body;
};
 
Response get(util::Uri const &uri);
Response get(uri::Uri const &uri);
 
} // namespace http
 
 
filename was Deleted added: 48, removed: 44, total 4
@@ -0,0 +1,18 @@
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
 
cc_library(
name = "uri",
srcs = ["uri.cpp"],
hdrs = ["uri.h"],
visibility = ["//visibility:public"],
)
 
cc_test(
name = "uri_test",
size = "small",
srcs = ["uri_test.cpp"],
deps = [
":uri",
"//etest",
],
)
 
util/uri.cpp added: 48, removed: 44, total 4
@@ -1,9 +1,9 @@
#include "util/uri.h"
#include "uri/uri.h"
 
#include <regex>
#include <utility>
 
namespace util {
namespace uri {
 
std::optional<Uri> Uri::parse(std::string uristr){
std::smatch match;
@@ -55,4 +55,4 @@ std::optional<Uri> Uri::parse(std::string uristr){
return uri;
}
 
} // namespace util
} // namespace uri
 
util/uri.h added: 48, removed: 44, total 4
@@ -1,10 +1,10 @@
#ifndef UTIL_URI_H_
#define UTIL_URI_H_
#ifndef URI_URI_H_
#define URI_URI_H_
 
#include <optional>
#include <string>
 
namespace util {
namespace uri {
 
struct Authority {
std::string user;
@@ -28,6 +28,6 @@ struct Uri {
bool operator==(Uri const &) const = default;
};
 
} //namespace util
} //namespace uri
 
#endif
 
util/uri_test.cpp added: 48, removed: 44, total 4
@@ -1,9 +1,9 @@
#include "util/uri.h"
#include "uri/uri.h"
 
#include "etest/etest.h"
 
using etest::expect;
using util::Uri;
using uri::Uri;
 
int main() {
etest::test("https: simple uri", [] {
 
util/BUILD added: 48, removed: 44, total 4
@@ -2,11 +2,7 @@ load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
 
cc_library(
name = "util",
srcs = ["uri.cpp"],
hdrs = [
"base_parser.h",
"uri.h",
],
hdrs = ["base_parser.h"],
visibility = ["//visibility:public"],
)
 
@@ -19,13 +15,3 @@ cc_test(
"//etest",
],
)
 
cc_test(
name = "uri_test",
size = "small",
srcs = ["uri_test.cpp"],
deps = [
":util",
"//etest",
],
)