44 lines
1.4 KiB
C++
44 lines
1.4 KiB
C++
|
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
|
|
||
|
#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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Handler::Poll::error(Request& request, Result result, Response::Status status) const {
|
||
|
Response& res = request.createResponse(status);
|
||
|
nlohmann::json body = nlohmann::json::object();
|
||
|
body["result"] = result;
|
||
|
|
||
|
res.setBody(body);
|
||
|
res.send();
|
||
|
}
|