2023-12-30 22:42:11 +00:00
|
|
|
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
2023-12-28 20:26:08 +00:00
|
|
|
|
|
|
|
#include "handler/poll.h"
|
|
|
|
|
|
|
|
#include "response/response.h"
|
|
|
|
#include "server/server.h"
|
|
|
|
#include "request/redirect.h"
|
2024-01-03 22:20:01 +00:00
|
|
|
#include "database/exceptions.h"
|
2023-12-28 20:26:08 +00:00
|
|
|
|
2024-01-22 18:21:55 +00:00
|
|
|
Handler::Poll::Poll (const std::shared_ptr<Server>& server):
|
2024-01-09 17:02:56 +00:00
|
|
|
Handler("poll", Request::Method::get),
|
2023-12-28 20:26:08 +00:00
|
|
|
server(server)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Handler::Poll::handle (Request& request) {
|
|
|
|
std::string access = request.getAuthorizationToken();
|
|
|
|
if (access.empty())
|
|
|
|
return error(request, Result::tokenProblem, Response::Status::unauthorized);
|
|
|
|
|
|
|
|
if (access.size() != 32)
|
|
|
|
return error(request, Result::tokenProblem, Response::Status::badRequest);
|
2024-01-09 17:02:56 +00:00
|
|
|
|
|
|
|
std::shared_ptr<Server> srv = server.lock();
|
|
|
|
if (!srv)
|
|
|
|
return error(request, Result::unknownError, Response::Status::internalError);
|
|
|
|
|
2023-12-28 20:26:08 +00:00
|
|
|
try {
|
2024-01-09 17:02:56 +00:00
|
|
|
Session& session = srv->getSession(access);
|
2023-12-28 20:26:08 +00:00
|
|
|
throw Redirect(&session);
|
|
|
|
} catch (const Redirect& r) {
|
|
|
|
throw r;
|
2024-01-03 22:20:01 +00:00
|
|
|
} catch (const DB::NoSession& e) {
|
|
|
|
return error(request, Result::tokenProblem, Response::Status::unauthorized);
|
2023-12-28 20:26:08 +00:00
|
|
|
} catch (const std::exception& e) {
|
2024-03-28 23:20:21 +00:00
|
|
|
std::cerr << "Exception on " << path << ":\n\t" << e.what() << std::endl;
|
2023-12-28 20:26:08 +00:00
|
|
|
return error(request, Result::unknownError, Response::Status::internalError);
|
|
|
|
} catch (...) {
|
2024-03-28 23:20:21 +00:00
|
|
|
std::cerr << "Unknown exception on " << path << std::endl;
|
2023-12-28 20:26:08 +00:00
|
|
|
return error(request, Result::unknownError, Response::Status::internalError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 01:11:56 +00:00
|
|
|
void Handler::Poll::error(Request& request, Result result, Response::Status status) {
|
2023-12-28 20:26:08 +00:00
|
|
|
Response& res = request.createResponse(status);
|
|
|
|
nlohmann::json body = nlohmann::json::object();
|
|
|
|
body["result"] = result;
|
|
|
|
|
|
|
|
res.setBody(body);
|
|
|
|
res.send();
|
2023-12-30 22:42:11 +00:00
|
|
|
}
|