Some thoughts about poll

This commit is contained in:
Blue 2023-12-28 17:26:08 -03:00
parent 4b87b560ac
commit 59c1ffd027
Signed by: blue
GPG key ID: 9B203B252A63EE38
19 changed files with 280 additions and 49 deletions

View file

@ -1,5 +1,6 @@
set(HEADERS
request.h
redirectable.h
)
set(SOURCES

13
request/accepting.h Normal file
View file

@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include "request/request.h"
class Accepting {
public:
virtual void accept(std::unique_ptr<Request> request) = 0;
};

12
request/redirect.cpp Normal file
View file

@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "redirect.h"
Redirect::Redirect(Accepting* destination):
destination(destination)
{}
const char* Redirect::what() const noexcept {
return "This is a redirect, should have beeh handled in router, but if you see it - something went terrebly wrong";
}

16
request/redirect.h Normal file
View file

@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <exception>
#include "accepting.h"
class Redirect : std::exception {
public:
Redirect(Accepting* destination);
Accepting* destination;
const char* what() const noexcept override;
};

View file

@ -10,6 +10,7 @@ constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME");
constexpr static const char* SERVER_NAME("SERVER_NAME");
constexpr static const char* CONTENT_TYPE("CONTENT_TYPE");
constexpr static const char* CONTENT_LENGTH("CONTENT_LENGTH");
constexpr static const char* AUTHORIZATION("HTTP_AUTHORIZATION");
constexpr static const char* urlEncoded("application/x-www-form-urlencoded");
@ -31,7 +32,8 @@ constexpr std::array<
Request::Request ():
state(State::initial),
raw(),
response(nullptr)
response(nullptr),
path()
{}
Request::~Request() {
@ -39,25 +41,34 @@ Request::~Request() {
}
void Request::terminate() {
switch (state) {
switch (state) {
case State::terminated:
case State::initial:
break;
case State::accepted:
std::cout << "A termination of accepted request that was not responded to, it's probably an error" << std::endl;
FCGX_Finish_r(&raw);
break;
case State::responding:
std::cout << "A termination of responding request that was not actually sent, it's probably an error" << std::endl;
FCGX_Finish_r(&raw);
break;
case State::responded:
FCGX_Finish_r(&raw);
break;
default:
break;
}
}
state = State::terminated;
}
std::string_view Request::methodName() const {
if (state == State::initial)
if (state == State::initial || state == State::terminated)
throw std::runtime_error("An attempt to read request method on not accepted request");
return FCGX_GetParam(REQUEST_METHOD, raw.envp);
}
Request::Method Request::method() const {
std::string_view method = methodName();
std::string_view method = methodName();
for (const auto& pair : methods) {
if (pair.first == method)
return pair.second;
@ -89,6 +100,10 @@ OStream Request::getErrorStream() {
return OStream(raw.err);
}
bool Request::active() const {
return state != State::initial && state != State::terminated;
}
Response& Request::createResponse() {
if (state != State::accepted)
throw std::runtime_error("An attempt create response to the request in the wrong state");
@ -125,11 +140,15 @@ void Request::responseIsComplete() {
throw std::runtime_error("An attempt to mark the request as complete, but it wasn't responded");
break;
case State::responding:
std::cout << responseCode() << '\t' << methodName() << '\t' << path << std::endl;
state = State::responded;
break;
case State::responded:
throw std::runtime_error("An attempt to mark the request as a complete for the second time");
break;
case State::terminated:
throw std::runtime_error("An attempt to mark the request as a complete on a terminated request");
break;
}
}
@ -137,25 +156,28 @@ Request::State Request::currentState() const {
return state;
}
std::string Request::getPath(const std::string& serverName) const {
void Request::readPath(const std::string& serverName) {
if (state != State::accepted)
throw std::runtime_error("An attempt to request path on a wrong request state");
std::string path;
if (!path.empty())
std::cout << "Request already has path \"" + path + "\", but it's being read again, probably an error";
std::string_view scriptFileName(FCGX_GetParam(SCRIPT_FILENAME, raw.envp));
std::string::size_type snLocation = scriptFileName.find(serverName);
if (snLocation != std::string::npos) {
if (snLocation + serverName.size() < scriptFileName.size())
path = scriptFileName.substr(snLocation + serverName.size() + 1);
}
if (!path.empty()) {
while (path.back() == '/')
path.erase(path.end() - 1);
}
}
std::string Request::getPath() const {
return path;
}
@ -167,6 +189,9 @@ std::string Request::getServerName() const {
}
void Request::printEnvironment(std::ostream& out) {
if (!active())
throw std::runtime_error("An attempt to print environment of a request in a wrong state");
char **envp = raw.envp;
for (int i = 0; envp[i] != nullptr; ++i) {
out << envp[i] << "\n";
@ -187,8 +212,8 @@ void Request::printEnvironment(nlohmann::json& out) {
}
bool Request::isFormUrlEncoded() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read request content type on not accepted request");
if (!active())
throw std::runtime_error("An attempt to read content type of a request in a wrong state");
std::string_view contentType(FCGX_GetParam(CONTENT_TYPE, raw.envp));
if (!contentType.empty() && contentType.find(urlEncoded) != std::string_view::npos) {
@ -199,15 +224,15 @@ bool Request::isFormUrlEncoded() const {
}
unsigned int Request::contentLength() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read request content length on not accepted request");
if (!active())
throw std::runtime_error("An attempt to read content length of a request in a wrong state");
return atoi(FCGX_GetParam(CONTENT_LENGTH, raw.envp));
}
std::map<std::string, std::string> Request::getForm() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read form on not accepted request");
if (!active())
throw std::runtime_error("An attempt to read form of a request in a wrong state");
std::map<std::string, std::string> result;
std::string_view contentType(FCGX_GetParam(CONTENT_TYPE, raw.envp));
@ -223,3 +248,21 @@ std::map<std::string, std::string> Request::getForm() const {
return result;
}
std::string Request::getAuthorizationToken() const {
if (!active())
throw std::runtime_error("An attempt to read authorization token of a request in a wrong state");
const char* auth = FCGX_GetParam(AUTHORIZATION, raw.envp);
if (auth == nullptr)
return std::string();
std::string result(auth);
if (result.find("Bearer") != 0)
return std::string();
result.erase(0, 6);
trim(result);
return result;
}

View file

@ -16,6 +16,7 @@
#include "stream/ostream.h"
#include "utils/formdecode.h"
#include "utils/helpers.h"
#include "response/response.h"
class Request {
@ -25,7 +26,8 @@ public:
initial,
accepted,
responding,
responded
responded,
terminated
};
enum class Method {
@ -42,7 +44,7 @@ public:
Request& operator = (Request&& other) = delete;
bool wait(int socketDescriptor);
void terminate();
bool active() const;
Response& createResponse();
Response& createResponse(Response::Status status);
@ -55,8 +57,10 @@ public:
unsigned int contentLength() const;
std::map<std::string, std::string> getForm() const;
std::string getPath(const std::string& serverName) const;
void readPath(const std::string& serverName);
std::string getPath() const;
std::string getServerName() const;
std::string getAuthorizationToken() const;
void printEnvironment(std::ostream& out);
void printEnvironment(nlohmann::json& out);
@ -64,9 +68,11 @@ private:
OStream getOutputStream();
OStream getErrorStream();
void responseIsComplete();
void terminate();
private:
State state;
FCGX_Request raw;
std::unique_ptr<Response> response;
std::string path;
};