refactor: move locking responsibility to user
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
2a373f3841
commit
088322c18f
6 changed files with 100 additions and 88 deletions
18
src/main.cpp
18
src/main.cpp
|
|
@ -34,7 +34,12 @@ static const std::string index_page = R"(
|
|||
crow::response add_redirect(std::string base_url, Trie *trie, const char *url,
|
||||
bool secure) {
|
||||
Entry *new_entry = entry_new(Redirect, url);
|
||||
|
||||
// The key already gets copied into the trie, so this pointer is safe to use
|
||||
// ever after unlocking the trie
|
||||
trie_wlock(trie);
|
||||
char *key = trie_add_random(trie, new_entry, secure);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
|
|
@ -64,7 +69,10 @@ bool store_paste(const char *key, const char *body) {
|
|||
crow::response add_paste(std::string base_url, Trie *trie, const char *body,
|
||||
bool secure) {
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
|
||||
trie_wlock(trie);
|
||||
char *key = trie_add_random(trie, new_entry, secure);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
|
|
@ -95,6 +103,7 @@ int main() {
|
|||
std::cout << "Populating trie from file '" << file_path << "'..."
|
||||
<< std::endl;
|
||||
|
||||
// Web server hasn't started yet, so there's no point in locking the trie
|
||||
int count = trie_populate(trie, file_path.c_str());
|
||||
|
||||
if (count == -1) {
|
||||
|
|
@ -102,7 +111,8 @@ int main() {
|
|||
|
||||
exit(1);
|
||||
} else {
|
||||
std::cout << "Added " << count << " (" << trie_size(trie) << ") entries to trie." << std::endl;
|
||||
std::cout << "Added " << count << " (" << trie_size(trie)
|
||||
<< ") entries to trie." << std::endl;
|
||||
}
|
||||
|
||||
// Create pastes directory if not present
|
||||
|
|
@ -119,6 +129,7 @@ int main() {
|
|||
CROW_ROUTE(app, "/<string>")
|
||||
.methods(crow::HTTPMethod::Get)(
|
||||
[trie](crow::response &res, std::string key) {
|
||||
trie_rlock(trie);
|
||||
Entry *entry = trie_search(trie, key.c_str());
|
||||
|
||||
if (entry != NULL) {
|
||||
|
|
@ -132,6 +143,7 @@ int main() {
|
|||
}
|
||||
|
||||
res.end();
|
||||
trie_unlock(trie);
|
||||
});
|
||||
|
||||
// Add a new Redirect with a short randomly generated key
|
||||
|
|
@ -160,7 +172,9 @@ int main() {
|
|||
|
||||
Entry *new_entry = entry_new(Redirect, req.body.c_str());
|
||||
|
||||
trie_wlock(trie);
|
||||
bool added = trie_add(trie, key.c_str(), new_entry);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (!added) {
|
||||
return crow::response(crow::status::CONFLICT);
|
||||
|
|
@ -194,7 +208,9 @@ int main() {
|
|||
AUTH();
|
||||
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
trie_wlock(trie);
|
||||
bool added = trie_add(trie, key.c_str(), new_entry);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (!added) {
|
||||
return crow::response(crow::status::CONFLICT);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue