feat: added secure random URL option
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
b66c0f0e00
commit
22a7b5b3fc
4 changed files with 101 additions and 52 deletions
109
src/main.cpp
109
src/main.cpp
|
|
@ -21,6 +21,55 @@ extern "C" {
|
|||
return crow::response(crow::status::UNAUTHORIZED); \
|
||||
}
|
||||
|
||||
crow::response add_redirect(std::string base_url, TernaryTrie *trie,
|
||||
const char *url, bool secure) {
|
||||
Entry *new_entry = entry_new(Redirect, url);
|
||||
char *key = ternarytrie_add_random(trie, new_entry, secure);
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
std::string res = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
}
|
||||
|
||||
bool store_paste(const char *key, const char *body) {
|
||||
// Write paste contents to file
|
||||
std::fstream file;
|
||||
file.open(std::string("pastes/") + key, std::ios_base::out);
|
||||
|
||||
if (!file.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
file << body;
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
crow::response add_paste(std::string base_url, TernaryTrie *trie,
|
||||
const char *body, bool secure) {
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
char *key = ternarytrie_add_random(trie, new_entry, secure);
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (!store_paste(key, body)) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
std::string res = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Initialize random seed for generating URLs
|
||||
srand(time(NULL));
|
||||
|
|
@ -72,23 +121,22 @@ int main() {
|
|||
res.end();
|
||||
});
|
||||
|
||||
// Add a new Redirect with a randomly generated key
|
||||
// Add a new Redirect with a short randomly generated key
|
||||
CROW_ROUTE(app, "/s/")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request req) {
|
||||
AUTH();
|
||||
|
||||
Entry *new_entry = entry_new(Redirect, req.body.c_str());
|
||||
char *key = ternarytrie_add_random(trie, new_entry);
|
||||
return add_redirect(base_url, trie, req.body.c_str(), false);
|
||||
});
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
// Add a new Redirect with a long randomly generated key
|
||||
CROW_ROUTE(app, "/sl/")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request req) {
|
||||
AUTH();
|
||||
|
||||
std::string res = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
return add_redirect(base_url, trie, req.body.c_str(), true);
|
||||
});
|
||||
|
||||
// Add a new Redirect with a given key
|
||||
|
|
@ -108,35 +156,22 @@ int main() {
|
|||
return crow::response(base_url + key);
|
||||
});
|
||||
|
||||
// Add a new Paste with a randomly generated key
|
||||
// Add a new Paste with a short randomly generated key
|
||||
CROW_ROUTE(app, "/p/")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request &req) {
|
||||
AUTH();
|
||||
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
char *key = ternarytrie_add_random(trie, new_entry);
|
||||
return add_paste(base_url, trie, req.body.c_str(), false);
|
||||
});
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
// Add a new Paste with a long randomly generated key
|
||||
CROW_ROUTE(app, "/pl/")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request &req) {
|
||||
AUTH();
|
||||
|
||||
// Write paste contents to file
|
||||
std::fstream file;
|
||||
file.open(std::string("pastes/") + key, std::ios_base::out);
|
||||
|
||||
if (!file.is_open()) {
|
||||
free(key);
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
file << req.body;
|
||||
file.close();
|
||||
|
||||
std::string res = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
return add_paste(base_url, trie, req.body.c_str(), true);
|
||||
});
|
||||
|
||||
// Add a paste with a given key
|
||||
|
|
@ -146,24 +181,16 @@ int main() {
|
|||
AUTH();
|
||||
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
|
||||
bool added = ternarytrie_add(trie, key.c_str(), new_entry);
|
||||
|
||||
if (!added) {
|
||||
return crow::response(crow::status::CONFLICT);
|
||||
}
|
||||
|
||||
// Write paste contents to file
|
||||
std::fstream file;
|
||||
file.open(std::string("pastes/") + key, std::ios_base::out);
|
||||
|
||||
if (!file.is_open()) {
|
||||
if (!store_paste(key.c_str(), req.body.c_str())) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
file << req.body;
|
||||
file.close();
|
||||
|
||||
return crow::response(base_url + key);
|
||||
});
|
||||
app.port(18080).multithreaded().run();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue