2022-06-09 04:28:50 +02:00
|
|
|
/* This file is part of libravatarserv.
|
|
|
|
* Copyright © 2022 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "request.hpp"
|
|
|
|
|
2022-06-09 20:53:26 +02:00
|
|
|
#include "hash.hpp"
|
2022-06-11 03:37:09 +02:00
|
|
|
#include "types.hpp"
|
2022-06-09 20:53:26 +02:00
|
|
|
|
2022-06-09 04:28:50 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
namespace libravatarserv::request {
|
|
|
|
|
2022-06-11 03:37:09 +02:00
|
|
|
request_t parse(const std::string_view uri) {
|
2022-06-09 04:28:50 +02:00
|
|
|
constexpr std::uint8_t offset{8};
|
|
|
|
if (uri.substr(0, offset) != "/avatar/"
|
|
|
|
|| uri.find("..", offset) != std::string_view::npos) {
|
|
|
|
std::cout << "Status: 400 Bad Request\n\n";
|
|
|
|
throw std::runtime_error{"Invalid URL"};
|
|
|
|
}
|
|
|
|
|
2022-06-11 03:37:09 +02:00
|
|
|
request_t req;
|
2022-06-09 04:28:50 +02:00
|
|
|
req.size = 80;
|
|
|
|
|
|
|
|
size_t pos{uri.find('?')};
|
|
|
|
if (pos == std::string_view::npos) {
|
|
|
|
pos = uri.size();
|
|
|
|
}
|
|
|
|
req.hash.resize(pos - offset);
|
|
|
|
std::transform(uri.begin() + offset, uri.begin() + pos, req.hash.begin(),
|
|
|
|
::tolower);
|
|
|
|
|
2022-06-09 20:53:26 +02:00
|
|
|
if (req.hash.size() == 64 && hash::is_valid(req.hash)) {
|
2022-06-11 03:37:09 +02:00
|
|
|
req.algo = algorithm_t::SHA256;
|
2022-06-09 20:53:26 +02:00
|
|
|
} else if (req.hash.size() == 32 && hash::is_valid(req.hash)) {
|
2022-06-11 03:37:09 +02:00
|
|
|
req.algo = algorithm_t::MD5;
|
2022-06-09 20:53:26 +02:00
|
|
|
} else {
|
|
|
|
std::cout << "Status: 400 Bad Request\n\n";
|
|
|
|
throw std::runtime_error{"Invalid hash"};
|
|
|
|
}
|
|
|
|
|
2022-06-09 04:28:50 +02:00
|
|
|
if (pos != uri.size()) {
|
|
|
|
std::string uri_rest;
|
|
|
|
uri_rest.resize(uri.size() - pos);
|
|
|
|
std::transform(uri.begin() + pos, uri.end(), uri_rest.begin(),
|
|
|
|
::tolower);
|
|
|
|
|
|
|
|
auto strsize{get_parameter(uri_rest, "s")};
|
|
|
|
if (strsize.empty()) {
|
|
|
|
strsize = get_parameter(uri_rest, "size");
|
|
|
|
}
|
|
|
|
if (!strsize.empty()) {
|
|
|
|
try {
|
|
|
|
req.size = static_cast<int16_t>(std::stoul(strsize.data()));
|
|
|
|
if (req.size > 512) {
|
|
|
|
req.size = 512;
|
|
|
|
} else if (req.size < 1) {
|
|
|
|
req.size = 80;
|
|
|
|
}
|
|
|
|
} catch (...) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.fallback = get_parameter(uri_rest, "d");
|
|
|
|
if (req.fallback.empty()) {
|
|
|
|
req.fallback = get_parameter(uri_rest, "default");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view get_parameter(const std::string_view uri,
|
|
|
|
const std::string ¶meter) {
|
|
|
|
size_t pos{uri.find("?" + parameter + "=")};
|
|
|
|
if (pos == std::string_view::npos) {
|
|
|
|
pos = uri.find("&" + parameter + "=");
|
|
|
|
}
|
|
|
|
if (pos != std::string_view::npos) {
|
|
|
|
pos += (2 + parameter.length());
|
|
|
|
return uri.substr(pos, uri.find('&', pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace libravatarserv::request
|