password hash cheching

This commit is contained in:
Blue 2023-12-22 20:25:20 -03:00
parent 99a9fd507e
commit 534c282226
Signed by: blue
GPG key ID: 9B203B252A63EE38
25 changed files with 390 additions and 84 deletions

View file

@ -5,8 +5,6 @@
#include "response/response.h"
constexpr static const char* GET("GET");
constexpr static const char* REQUEST_METHOD("REQUEST_METHOD");
constexpr static const char* SCRIPT_FILENAME("SCRIPT_FILENAME");
constexpr static const char* SERVER_NAME("SERVER_NAME");
@ -51,11 +49,15 @@ void Request::terminate() {
}
}
Request::Method Request::method() const {
std::string_view Request::methodName() const {
if (state == State::initial)
throw std::runtime_error("An attempt to read request method on not accepted request");
std::string_view method(FCGX_GetParam(REQUEST_METHOD, raw.envp));
return FCGX_GetParam(REQUEST_METHOD, raw.envp);
}
Request::Method Request::method() const {
std::string_view method = methodName();
for (const auto& pair : methods) {
if (pair.first == method)
return pair.second;
@ -79,17 +81,42 @@ bool Request::wait(int socketDescriptor) {
return result;
}
OStream Request::getOutputStream(const Response* response) {
validateResponse(response);
OStream Request::getOutputStream() {
return OStream(raw.out);
}
OStream Request::getErrorStream(const Response* response) {
validateResponse(response);
OStream Request::getErrorStream() {
return OStream(raw.err);
}
void Request::responseIsComplete(const Response* response) {
Response& Request::createResponse() {
if (state != State::accepted)
throw std::runtime_error("An attempt create response to the request in the wrong state");
response = std::unique_ptr<Response>(new Response(*this));
state = State::responding;
return *response.get();
}
Response& Request::createResponse(Response::Status status) {
if (state != State::accepted)
throw std::runtime_error("An attempt create response to the request in the wrong state");
response = std::unique_ptr<Response>(new Response(*this, status));
state = State::responding;
return *response.get();
}
uint16_t Request::responseCode() const {
if (state != State::responded)
throw std::runtime_error("An attempt create read response code on the wrong state");
return response->statusCode();
}
void Request::responseIsComplete() {
switch (state) {
case State::initial:
throw std::runtime_error("An attempt to mark the request as complete, but it wasn't even accepted yet");
@ -98,10 +125,6 @@ void Request::responseIsComplete(const Response* response) {
throw std::runtime_error("An attempt to mark the request as complete, but it wasn't responded");
break;
case State::responding:
if (Request::response != response)
throw std::runtime_error("An attempt to mark the request as complete by the different response who actually started responding");
Request::response = nullptr;
state = State::responded;
break;
case State::responded:
@ -110,26 +133,6 @@ void Request::responseIsComplete(const Response* response) {
}
}
void Request::validateResponse(const Response* response) {
switch (state) {
case State::initial:
throw std::runtime_error("An attempt to request stream while the request wasn't even accepted yet");
break;
case State::accepted:
Request::response = response;
state = State::responding;
break;
case State::responding:
if (Request::response != response)
throw std::runtime_error("Error handling a request: first time one response started replying, then another continued");
break;
case State::responded:
throw std::runtime_error("An attempt to request stream on a request that was already done responding");
break;
}
}
Request::State Request::currentState() const {
return state;
}

View file

@ -16,9 +16,10 @@
#include "stream/ostream.h"
#include "utils/formdecode.h"
#include "response/response.h"
class Response;
class Request {
friend class Response;
public:
enum class State {
initial,
@ -43,26 +44,29 @@ public:
bool wait(int socketDescriptor);
void terminate();
Response& createResponse();
Response& createResponse(Response::Status status);
uint16_t responseCode() const;
Method method() const;
std::string_view methodName() const;
State currentState() const;
bool isFormUrlEncoded() const;
unsigned int contentLength() const;
std::map<std::string, std::string> getForm() const;
OStream getOutputStream(const Response* response);
OStream getErrorStream(const Response* response);
void responseIsComplete(const Response* response);
std::string getPath(const std::string& serverName) const;
std::string getServerName() const;
void printEnvironment(std::ostream& out);
void printEnvironment(nlohmann::json& out);
private:
void validateResponse(const Response* response);
OStream getOutputStream();
OStream getErrorStream();
void responseIsComplete();
private:
State state;
FCGX_Request raw;
const Response* response;
std::unique_ptr<Response> response;
};