52 lines
1.7 KiB
C++
52 lines
1.7 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"
|
|
#include "database/exceptions.h"
|
|
|
|
Handler::Poll::Poll (std::weak_ptr<Server> server):
|
|
Handler("poll", 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);
|
|
|
|
std::shared_ptr<Server> srv = server.lock();
|
|
if (!srv)
|
|
return error(request, Result::unknownError, Response::Status::internalError);
|
|
|
|
try {
|
|
Session& session = srv->getSession(access);
|
|
throw Redirect(&session);
|
|
} catch (const Redirect& r) {
|
|
throw r;
|
|
} catch (const DB::NoSession& e) {
|
|
return error(request, Result::tokenProblem, Response::Status::unauthorized);
|
|
} 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) {
|
|
Response& res = request.createResponse(status);
|
|
nlohmann::json body = nlohmann::json::object();
|
|
body["result"] = result;
|
|
|
|
res.setBody(body);
|
|
res.send();
|
|
}
|