Some thoughts about poll
This commit is contained in:
parent
4b87b560ac
commit
59c1ffd027
19 changed files with 280 additions and 49 deletions
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "router.h"
|
||||
|
||||
#include "request/redirect.h"
|
||||
|
||||
Router::Router():
|
||||
get(),
|
||||
post()
|
||||
|
@ -25,54 +27,54 @@ void Router::addRoute(Handler handler) {
|
|||
throw std::runtime_error("could'not add route " + handler->path + " to the routing table");
|
||||
}
|
||||
|
||||
void Router::route(const std::string& path, std::unique_ptr<Request> request) {
|
||||
void Router::route(std::unique_ptr<Request> request) {
|
||||
std::map<std::string, Handler>::const_iterator itr, end;
|
||||
switch (request->method()) {
|
||||
case Request::Method::get:
|
||||
itr = get.find(path);
|
||||
itr = get.find(request->getPath());
|
||||
end = get.end();
|
||||
break;
|
||||
case Request::Method::post:
|
||||
itr = post.find(path);
|
||||
itr = post.find(request->getPath());
|
||||
end = post.end();
|
||||
break;
|
||||
default:
|
||||
return handleMethodNotAllowed(path, std::move(request));
|
||||
return handleMethodNotAllowed(std::move(request));
|
||||
}
|
||||
|
||||
if (itr == end)
|
||||
return handleNotFound(path, std::move(request));
|
||||
return handleNotFound(std::move(request));
|
||||
|
||||
try {
|
||||
std::cout << "Handling " << path << "..." << std::endl;
|
||||
itr->second->handle(*request.get());
|
||||
|
||||
if (request->currentState() != Request::State::responded)
|
||||
handleInternalError(path, std::runtime_error("handler failed to handle the request"), std::move(request));
|
||||
else
|
||||
std::cout << request->responseCode() << '\t' << request->methodName() << '\t' << path << std::endl;
|
||||
handleInternalError(std::runtime_error("handler failed to handle the request"), std::move(request));
|
||||
} catch (const Redirect& redirect) {
|
||||
redirect.destination->accept(std::move(request));
|
||||
} catch (const std::exception& e) {
|
||||
handleInternalError(path, e, std::move(request));
|
||||
handleInternalError(e, std::move(request));
|
||||
}
|
||||
}
|
||||
|
||||
void Router::handleNotFound(const std::string& path, std::unique_ptr<Request> request) {
|
||||
void Router::handleNotFound(std::unique_ptr<Request> request) {
|
||||
Response& notFound = request->createResponse(Response::Status::notFound);
|
||||
std::string path = request->getPath();
|
||||
notFound.setBody(std::string("Path \"") + path + "\" was not found");
|
||||
notFound.send();
|
||||
std::cerr << notFound.statusCode() << '\t' << request->methodName() << '\t' << path << std::endl;
|
||||
}
|
||||
|
||||
void Router::handleInternalError(const std::string& path, const std::exception& exception, std::unique_ptr<Request> request) {
|
||||
void Router::handleInternalError(const std::exception& exception, std::unique_ptr<Request> request) {
|
||||
Response& error = request->createResponse(Response::Status::internalError);
|
||||
error.setBody(std::string(exception.what()));
|
||||
error.send();
|
||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << path << std::endl;
|
||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << request->getPath() << std::endl;
|
||||
}
|
||||
|
||||
void Router::handleMethodNotAllowed(const std::string& path, std::unique_ptr<Request> request) {
|
||||
void Router::handleMethodNotAllowed(std::unique_ptr<Request> request) {
|
||||
Response& error = request->createResponse(Response::Status::methodNotAllowed);
|
||||
error.setBody(std::string("Method not allowed"));
|
||||
error.send();
|
||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << path << std::endl;
|
||||
std::cerr << error.statusCode() << '\t' << request->methodName() << '\t' << request->getPath() << std::endl;
|
||||
}
|
||||
|
|
|
@ -20,12 +20,12 @@ public:
|
|||
Router();
|
||||
|
||||
void addRoute(Handler handler);
|
||||
void route(const std::string& path, std::unique_ptr<Request> request);
|
||||
void route(std::unique_ptr<Request> request);
|
||||
|
||||
private:
|
||||
void handleNotFound(const std::string& path, std::unique_ptr<Request> request);
|
||||
void handleInternalError(const std::string& path, const std::exception& exception, std::unique_ptr<Request> request);
|
||||
void handleMethodNotAllowed(const std::string& path, std::unique_ptr<Request> request);
|
||||
void handleNotFound(std::unique_ptr<Request> request);
|
||||
void handleInternalError(const std::exception& exception, std::unique_ptr<Request> request);
|
||||
void handleMethodNotAllowed(std::unique_ptr<Request> request);
|
||||
|
||||
private:
|
||||
std::map<std::string, Handler> get;
|
||||
|
|
|
@ -5,13 +5,15 @@
|
|||
|
||||
#include <random>
|
||||
|
||||
#include "database/exceptions.h"
|
||||
|
||||
#include "handler/info.h"
|
||||
#include "handler/env.h"
|
||||
#include "handler/register.h"
|
||||
#include "handler/login.h"
|
||||
|
||||
constexpr const char* pepper = "well, not much of a secret, huh?";
|
||||
constexpr uint8_t currentDbVesion = 1;
|
||||
|
||||
constexpr const char* randomChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
constexpr uint8_t saltSize = 16;
|
||||
constexpr uint8_t hashSize = 32;
|
||||
|
@ -19,6 +21,8 @@ constexpr uint8_t hashParallel = 1;
|
|||
constexpr uint8_t hashIterations = 2;
|
||||
constexpr uint32_t hashMemoryCost = 65536;
|
||||
|
||||
constexpr uint8_t currentDbVesion = 1;
|
||||
|
||||
Server::Server():
|
||||
terminating(false),
|
||||
requestCount(0),
|
||||
|
@ -72,8 +76,8 @@ void Server::handleRequest(std::unique_ptr<Request> request) {
|
|||
}
|
||||
}
|
||||
|
||||
std::string path = request->getPath(serverName.value());
|
||||
router.route(path.data(), std::move(request));
|
||||
request->readPath(serverName.value());
|
||||
router.route(std::move(request));
|
||||
}
|
||||
|
||||
std::string Server::generateRandomString(std::size_t length) {
|
||||
|
@ -128,9 +132,22 @@ bool Server::validatePassword(const std::string& login, const std::string& passw
|
|||
}
|
||||
|
||||
Session& Server::openSession(const std::string& login) {
|
||||
std::string accessToken = generateRandomString(32);
|
||||
std::string renewToken = generateRandomString(32);
|
||||
unsigned int sessionId = db->createSession(login, accessToken, renewToken);
|
||||
std::string accessToken, renewToken;
|
||||
unsigned int sessionId = 0;
|
||||
int counter = 10;
|
||||
do {
|
||||
try {
|
||||
accessToken = generateRandomString(32);
|
||||
renewToken = generateRandomString(32);
|
||||
sessionId = db->createSession(login, accessToken, renewToken);
|
||||
break;
|
||||
} catch (const DBInterface::Duplicate& e) {
|
||||
std::cout << "Duplicate on creating session, trying again with different tokens";
|
||||
}
|
||||
} while (--counter != 0);
|
||||
|
||||
if (sessionId == 0)
|
||||
throw std::runtime_error("Couldn't create session, ran out of attempts");
|
||||
|
||||
std::unique_ptr<Session>& session = sessions[accessToken] = std::make_unique<Session>(sessionId, accessToken, renewToken);
|
||||
return *session.get();
|
||||
|
|
|
@ -36,6 +36,7 @@ public:
|
|||
unsigned int registerAccount(const std::string& login, const std::string& password);
|
||||
bool validatePassword(const std::string& login, const std::string& password);
|
||||
Session& openSession(const std::string& login);
|
||||
Session& getSession(const std::string& accessToken);
|
||||
|
||||
private:
|
||||
void handleRequest(std::unique_ptr<Request> request);
|
||||
|
|
|
@ -3,10 +3,13 @@
|
|||
|
||||
#include "session.h"
|
||||
|
||||
#include "handler/poll.h"
|
||||
|
||||
Session::Session(unsigned int id, const std::string& access, const std::string& renew):
|
||||
id(id),
|
||||
access(access),
|
||||
renew(renew)
|
||||
renew(renew),
|
||||
polling(nullptr)
|
||||
{}
|
||||
|
||||
std::string Session::getAccessToken() const {
|
||||
|
@ -16,3 +19,16 @@ std::string Session::getAccessToken() const {
|
|||
std::string Session::getRenewToken() const {
|
||||
return renew;
|
||||
}
|
||||
|
||||
void Session::accept(std::unique_ptr<Request> request) {
|
||||
if (polling) {
|
||||
Response& res = request->createResponse(Response::Status::ok);
|
||||
nlohmann::json body = nlohmann::json::object();
|
||||
body["result"] = Handler::Poll::Result::replace;
|
||||
|
||||
res.setBody(body);
|
||||
res.send();
|
||||
}
|
||||
|
||||
polling = std::move(request);
|
||||
}
|
|
@ -5,15 +5,19 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
class Session {
|
||||
#include "request/accepting.h"
|
||||
|
||||
class Session : public Accepting {
|
||||
public:
|
||||
Session(unsigned int id, const std::string& access, const std::string& renew);
|
||||
|
||||
std::string getAccessToken() const;
|
||||
std::string getRenewToken() const;
|
||||
void accept(std::unique_ptr<Request> request) override;
|
||||
|
||||
private:
|
||||
unsigned int id;
|
||||
std::string access;
|
||||
std::string renew;
|
||||
std::unique_ptr<Request> polling;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue