2#include <slick/logger.hpp>
12using json = nlohmann::json;
16constexpr size_t MAX_NAME = 64;
17constexpr size_t MAX_TOOL = 32;
18constexpr size_t MAX_PARAM = 128;
19constexpr size_t MAX_WINDOWS = 128;
22constexpr std::string_view BLANKS[] = {
" ",
"\t",
"\r",
"\n",
"\f",
"\v",
"\xC2\xA0",
"\xE3\x80\x80"};
25std::string_view trim(std::string_view name) {
26 for (
bool cut =
true; cut;) {
28 for (
const auto blank : BLANKS) {
29 if (name.starts_with(blank)) {
30 name.remove_prefix(blank.size());
33 if (name.ends_with(blank)) {
34 name.remove_suffix(blank.size());
42bool valid_name(std::string_view name) {
43 if (name.empty() || name.size() > MAX_NAME) {
46 for (
const unsigned char c : name) {
47 if (c < 0x20 || c == 0x7f) {
55json clean_windows(
const json& windows, std::string& error) {
56 if (!windows.is_array() || windows.size() > MAX_WINDOWS) {
57 error =
"windows must be an array of at most " + std::to_string(MAX_WINDOWS);
60 json out = json::array();
61 for (
const auto& w : windows) {
62 const auto tool = w.is_object() ? w.find(
"tool") : w.end();
63 if (!w.is_object() || tool == w.end() || !tool->is_string() || tool->get_ref<
const std::string&>().empty() ||
64 tool->get_ref<
const std::string&>().size() > MAX_TOOL) {
65 error =
"each window needs a tool name";
68 json kept = {{
"tool", *tool}};
69 for (
const char* key : {
"left",
"top",
"width",
"height"}) {
70 const auto value = w.find(key);
71 if (value == w.end() || !value->is_number_integer()) {
72 error = std::string(
"each window needs an integer ") + key;
77 if (
const auto params = w.find(
"params"); params != w.end()) {
78 if (!params->is_object()) {
79 error =
"params must be an object";
82 for (
const auto& [key, value] : params->items()) {
83 if (!value.is_string() || key.size() > MAX_PARAM || value.get_ref<
const std::string&>().size() > MAX_PARAM) {
84 error =
"params must be short strings";
88 kept[
"params"] = *params;
90 out.push_back(std::move(kept));
97 return {{
"last", name}, {
"workspaces", {{name, {{
"windows", json::array()}}}}}};
100bool is_last(
const json& doc,
const std::string& name) {
101 return doc[
"last"].get_ref<
const std::string&>() == name;
107 path_ = std::move(path);
110 if (path_.empty() || !std::filesystem::exists(path_, ec)) {
113 std::ifstream in(path_);
114 const json file = json::parse(in,
nullptr,
false);
115 if (!file.is_object() || !file.contains(
"workspaces") || !file[
"workspaces"].is_object()) {
116 LOG_WARN(
"WebServer: {} is not a workspaces file: starting with Default alone", path_);
121 auto& kept = doc_[
"workspaces"];
122 std::set<std::string> loaded;
123 for (
const auto& [raw, workspace] : file[
"workspaces"].items()) {
124 const std::string name(trim(raw));
125 std::string error = loaded.contains(name) ?
"another workspace has that name" :
"";
126 json windows = error.empty() && valid_name(name) && workspace.is_object() && workspace.contains(
"windows")
127 ? clean_windows(workspace[
"windows"], error) :
json();
128 if (windows.is_null()) {
129 LOG_WARN(
"WebServer: dropping workspace '{}' from {}: {}", raw, path_,
130 error.empty() ?
"not a workspace" : error);
134 kept[name] = {{
"windows", std::move(windows)}};
136 if (
const auto last = file.find(
"last"); last != file.end() && last->is_string()) {
137 const std::string name(trim(last->get_ref<
const std::string&>()));
138 if (kept.contains(name)) {
146 if (!valid_name(name)) {
147 return {400,
"a workspace name is 1 to 64 characters, none of them control characters"};
150 json kept = clean_windows(windows, error);
151 if (!error.empty()) {
152 return {400, std::move(error)};
155 const std::string key(name);
156 doc_[
"workspaces"][key] = {{
"windows", std::move(kept)}};
158 return commit(std::move(before));
164 return {400,
"Default can't be deleted"};
166 const std::string key(name);
167 if (!doc_[
"workspaces"].contains(key)) {
168 return {404,
"no workspace " + key};
171 doc_[
"workspaces"].erase(key);
172 if (is_last(doc_, key)) {
173 doc_[
"last"] = std::string(
DEFAULT);
175 return commit(std::move(before));
179 const std::string key(trim(name));
180 if (!doc_[
"workspaces"].contains(key)) {
181 return {404,
"no workspace " + key};
183 if (is_last(doc_, key)) {
188 return commit(std::move(before));
195 namespace fs = std::filesystem;
196 const fs::path file(path_);
197 fs::path temp = file;
200 if (file.has_parent_path()) {
201 fs::create_directories(file.parent_path(), ec);
203 bool written =
false;
205 std::ofstream out(temp, std::ios::trunc);
206 out << doc_.dump(2) <<
'\n';
208 written =
static_cast<bool>(out);
211 fs::rename(temp, file, ec);
213 if (!written || ec) {
214 fs::remove(temp, ec);
215 doc_ = std::move(before);
216 LOG_ERROR(
"WebServer: cannot write workspaces to {}", path_);
217 return {500,
"cannot write " + path_};
static constexpr std::string_view DEFAULT
Result save(std::string_view name, const nlohmann::json &windows)
Result remove(std::string_view name)
Result use(std::string_view name)
void load(std::string path)