pica/handler/poll.cpp

45 lines
1.4 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"
Handler::Poll::Poll (Server* server):
Handler("login", Request::Method::get),
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);
try {
Session& session = server->getSession(access);
throw Redirect(&session);
} catch (const Redirect& r) {
throw r;
} 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
}