refactor(ADirective, HttpHeaders, HttpRequest, Server, Socket): improve code structure and readability

This commit is contained in:
whaffman 2025-10-14 22:43:02 +02:00
parent 9bacc4f8a4
commit 35432729dc
10 changed files with 48 additions and 34 deletions

View File

@ -1,6 +1,11 @@
#include <webserv/config/directive/ADirective.hpp>
#include <webserv/config/directive/DirectiveValue.hpp> // for DirectiveValue, operator<<
#include <utility> // for move
ADirective::ADirective(std::string name) : name_(std::move(name)) //TODO Move here but derived classes take const ref
{
}
DirectiveValue ADirective::getValue() const
{

View File

@ -4,14 +4,13 @@
#include <iostream> // for ostream
#include <string> // for string, basic_string
#include <utility> // for move
class ADirective
{
public:
ADirective() = delete;
ADirective(std::string name) : name_(std::move(name)) {}
ADirective(std::string name);
ADirective(const ADirective &other) = delete;
ADirective &operator=(const ADirective &other) = delete;

View File

@ -17,6 +17,13 @@ class FileHandler
public:
FileHandler(const AConfig *config, const URI &uri);
FileHandler(const FileHandler &other) = delete;
FileHandler(FileHandler &&other) noexcept = delete;
FileHandler &operator=(const FileHandler &other) = delete;
FileHandler &operator=(FileHandler &&other) noexcept = delete;
~FileHandler() = default;
[[nodiscard]] std::unique_ptr<HttpResponse> getResponse() const;
private:

View File

@ -18,18 +18,17 @@
class HttpHeaders
{
public:
const std::string &get(const std::string &name) const;
bool has(const std::string &name) const;
[[nodiscard]] const std::string &get(const std::string &name) const;
[[nodiscard]] bool has(const std::string &name) const;
void parse(const std::string &rawHeaders);
void add(const std::string &name, const std::string &value);
void remove(const std::string &name);
std::string toString() const;
std::optional<size_t> getContentLength() const;
std::optional<std::string> getContentType() const;
std::optional<std::string> getHost() const;
[[nodiscard]] std::string toString() const;
[[nodiscard]] std::optional<size_t> getContentLength() const;
[[nodiscard]] std::optional<std::string> getContentType() const;
[[nodiscard]] std::optional<std::string> getHost() const;
private:
std::unordered_map<std::string, std::string> headers_;

View File

@ -223,3 +223,19 @@ void HttpRequest::reset()
body_.clear();
// contentLength_ = 0;
}
const std::string &HttpRequest::getMethod() const
{
return method_;
}
const std::string &HttpRequest::getTarget() const
{
return target_;
}
const std::string &HttpRequest::getHttpVersion() const
{
return httpVersion_;
}

View File

@ -32,27 +32,24 @@ class HttpRequest
~HttpRequest();
[[nodiscard]] State getState() const;
void setState(State state);
[[nodiscard]] const HttpHeaders &getHeaders() const;
[[nodiscard]] const std::string &getBody() const;
[[nodiscard]] const std::string &getMethod() const;
[[nodiscard]] const std::string &getTarget() const;
[[nodiscard]] const std::string &getHttpVersion() const;
[[nodiscard]] const std::string &getMethod() const { return method_; }
[[nodiscard]] const std::string &getTarget() const { return target_; }
[[nodiscard]] const std::string &getHttpVersion() const { return httpVersion_; }
void setState(State state);
void receiveData(const char *data, size_t length);
void reset();
private:
void parseBuffer();
[[nodiscard]] bool parseBufferforRequestLine();
[[nodiscard]] bool parseBufferforHeaders();
[[nodiscard]] bool parseHeaderLine();
[[nodiscard]] bool parseBufferforBody();
[[nodiscard]] bool parseBufferforChunkedBody();
void parseBuffer();
void parseContentLength();
Client *client_;
@ -66,7 +63,4 @@ class HttpRequest
std::string method_;
std::string target_;
std::string httpVersion_;
// std::string requestLine_;
// std::string headers_;
// size_t contentLength_ = 0;
};

View File

@ -25,13 +25,10 @@ class Server
Server() = delete;
Server(const ConfigManager &configManager);
Server(const Server &other) = delete; // Disable copy constructor
Server &operator=(const Server &other) = delete; // Disable copy assignment
Server(Server &&other) noexcept = delete; // Move constructor
Server &operator=(Server &&other) noexcept = delete; // Move assignment
// The constructor must initialize the reference member 'configManager'
// Implementation should be in the .cpp file using an initializer list
Server(const Server &other) = delete;
Server &operator=(const Server &other) = delete;
Server(Server &&other) noexcept = delete;
Server &operator=(Server &&other) noexcept = delete;
~Server();
@ -49,7 +46,6 @@ class Server
const ConfigManager &configManager_;
std::vector<std::unique_ptr<ServerSocket>> listeners_;
std::set<int> listener_fds_;
// std::unordered_map<int, std::unique_ptr<Client>> clients_;
std::vector<std::unique_ptr<Client>> clients_;
std::unordered_map<int, Client *> socketToClient_;

View File

@ -9,7 +9,7 @@
class CGISocket : public ASocket
{
public:
[[nodiscard]] ASocket::Type getType() const override;
explicit CGISocket(int fd);
[[nodiscard]] ASocket::Type getType() const override;
};

View File

@ -9,7 +9,7 @@
class ClientSocket : public ASocket
{
public:
[[nodiscard]] ASocket::Type getType() const override;
explicit ClientSocket(int fd);
[[nodiscard]] ASocket::Type getType() const override;
};

View File

@ -10,13 +10,11 @@ class ServerSocket : public ASocket
{
public:
ServerSocket();
ServerSocket(int fd);
void listen(int backlog) const;
void bind(const std::string &host, int port) const;
[[nodiscard]] ASocket::Type getType() const override;
[[nodiscard]] std::unique_ptr<ClientSocket> accept() const;
};