58 lines
1.8 KiB
C++
58 lines
1.8 KiB
C++
/* This file is part of libravatarserv.
|
|
* Copyright © 2018 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 <iostream>
|
|
#include <cstdlib>
|
|
#include "libravatarserv.hpp"
|
|
|
|
using std::cout;
|
|
using std::cerr;
|
|
using global::avatar_dir;
|
|
using namespace http;
|
|
|
|
const Request http::parse_request(const string &request)
|
|
{
|
|
if (request.substr(0, 8) != "/avatar/" ||
|
|
request.find('/', 8) != std::string::npos)
|
|
{
|
|
cout << "Status: 404 Not Found\n\n";
|
|
cerr << "Error: Invalid URL.\n";
|
|
std::exit(0);
|
|
}
|
|
|
|
uint16_t size = 80;
|
|
string digest = request.substr(8);
|
|
std::transform(digest.begin(), digest.end(), digest.begin(), ::tolower);
|
|
std::size_t pos_digest = digest.find('?');
|
|
|
|
if (pos_digest != std::string::npos)
|
|
{
|
|
digest = digest.substr(0, pos_digest);
|
|
std::size_t pos_size = request.find("s=", pos_digest);
|
|
if (pos_size != std::string::npos)
|
|
{
|
|
size = static_cast<uint16_t>(std::stoul(request.substr(pos_size + 2)));
|
|
}
|
|
pos_size = request.find("size=", pos_digest);
|
|
if (pos_size != std::string::npos)
|
|
{
|
|
size = static_cast<uint16_t>(std::stoul(request.substr(pos_size + 5)));
|
|
}
|
|
}
|
|
|
|
return { digest, size };
|
|
}
|