This repository has been archived on 2021-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
backend/src/cgi.cpp

137 lines
3.5 KiB
C++
Raw Normal View History

2020-06-29 06:10:40 +02:00
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-06-29 05:21:23 +02:00
#include "cgi.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
2020-07-05 08:01:30 +02:00
#include "time.hpp"
2020-06-29 05:21:23 +02:00
#include <cgicc/Cgicc.h>
#include <unicode/unistr.h>
2020-06-29 05:21:23 +02:00
#include <algorithm>
2020-07-05 08:01:30 +02:00
#include <chrono>
2020-06-29 22:37:09 +02:00
#include <fstream>
#include <ios>
#include <iostream>
#include <iterator>
2020-06-29 05:21:23 +02:00
#include <sstream>
2020-06-29 22:37:09 +02:00
#include <stdexcept>
2020-06-29 05:21:23 +02:00
#include <string>
#include <string_view>
2020-06-29 05:21:23 +02:00
#include <vector>
2020-07-01 20:51:35 +02:00
namespace FediBlock::cgi
2020-06-29 05:21:23 +02:00
{
using std::back_inserter;
2020-06-29 05:21:23 +02:00
using std::getline;
2020-06-29 22:37:09 +02:00
using std::ios;
using std::ofstream;
using std::runtime_error;
2020-06-29 05:21:23 +02:00
using std::string;
using std::string_view;
using std::stringstream;
using std::transform;
2020-06-29 05:21:23 +02:00
using std::vector;
2020-07-05 08:01:30 +02:00
using std::chrono::system_clock;
2020-06-29 05:21:23 +02:00
entry_type parse_formdata()
{
2020-06-29 07:05:04 +02:00
entry_type entry;
2020-06-29 22:37:09 +02:00
cgicc::Cgicc cgi;
entry.instance = cgi("instance");
entry.tags = string_to_vector(cgi("tags"));
transform(entry.tags.begin(), entry.tags.end(), entry.tags.begin(),
[](const auto &tag) { return tolower(tag); });
entry.receipts = string_to_vector(cgi("receipts"));
entry.description = cgi("description");
2020-07-05 08:01:30 +02:00
entry.report_time = time::to_string(system_clock::now());
const auto screenshot = cgi.getFile("screenshot");
if (screenshot != cgi.getFiles().end())
{
constexpr size_t size_limit{1024 * 1024 * 2}; // 2 MiB.
if (screenshot->getDataLength() > size_limit)
2020-06-29 22:37:09 +02:00
{
throw runtime_error{"Filesize too big"};
}
2020-06-29 22:37:09 +02:00
const string filepath{files::get_tmpdir() / screenshot->getFilename()};
ofstream file{filepath, ios::binary};
if (!file.good())
{
throw runtime_error{"Could not open temporary file: " + filepath};
2020-06-29 22:37:09 +02:00
}
screenshot->writeToStream(file);
entry.screenshot_filepath = filepath;
2020-06-29 05:21:23 +02:00
}
2020-06-29 07:05:04 +02:00
return entry;
2020-06-29 05:21:23 +02:00
}
2020-06-29 07:05:04 +02:00
vector<string> string_to_vector(const string_view str)
2020-06-29 05:21:23 +02:00
{
2020-06-29 07:05:04 +02:00
vector<string> vec;
2020-06-29 05:21:23 +02:00
stringstream input{str.data()};
string element;
while (getline(input, element, ','))
2020-06-29 05:21:23 +02:00
{
2020-06-30 07:33:15 +02:00
if (!element.empty())
{
const size_t startpos{element.find_first_not_of(' ')};
const size_t length{element.find_last_not_of(' ') - startpos + 1};
vec.push_back(element.substr(startpos, length));
2020-06-30 07:33:15 +02:00
}
2020-06-29 05:21:23 +02:00
}
2020-06-29 07:05:04 +02:00
return vec;
2020-06-29 05:21:23 +02:00
}
vector<string> get_tags()
{
cgicc::Cgicc cgi;
vector<cgicc::FormEntry> tags_form;
vector<string> tags_string;
cgi.getElement("tags[]", tags_form);
2020-10-16 23:52:08 +02:00
for (const auto &element : tags_form)
{
const string tag{element.getValue()};
2020-10-16 23:52:08 +02:00
if (!tag.empty())
{
tags_string.push_back(tolower(tag));
}
}
return tags_string;
}
string tolower(const string_view str)
{
string result;
const auto unistr{icu::UnicodeString(str.data(), "UTF-8").toLower()};
unistr.toUTF8String(result);
return result;
}
2020-07-01 20:51:35 +02:00
} // namespace FediBlock::cgi