1
0
forked from blue/pica
pica/handler/poll.cpp

52 lines
1.7 KiB
C++
Raw Normal View History

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"
#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;
} 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) {
std::cerr << "Exception on poll:\n\t" << e.what() << std::endl;
return error(request, Result::unknownError, Response::Status::internalError);
} catch (...) {
std::cerr << "Unknown exception on poll" << std::endl;
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
}