refactor(ADirective, HttpHeaders, HttpRequest, Server, Socket): improve code structure and readability
This commit is contained in:
parent
9bacc4f8a4
commit
35432729dc
@ -1,6 +1,11 @@
|
|||||||
#include <webserv/config/directive/ADirective.hpp>
|
#include <webserv/config/directive/ADirective.hpp>
|
||||||
|
|
||||||
#include <webserv/config/directive/DirectiveValue.hpp> // for DirectiveValue, operator<<
|
#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
|
DirectiveValue ADirective::getValue() const
|
||||||
{
|
{
|
||||||
|
|||||||
@ -4,14 +4,13 @@
|
|||||||
|
|
||||||
#include <iostream> // for ostream
|
#include <iostream> // for ostream
|
||||||
#include <string> // for string, basic_string
|
#include <string> // for string, basic_string
|
||||||
#include <utility> // for move
|
|
||||||
|
|
||||||
class ADirective
|
class ADirective
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ADirective() = delete;
|
ADirective() = delete;
|
||||||
|
|
||||||
ADirective(std::string name) : name_(std::move(name)) {}
|
ADirective(std::string name);
|
||||||
|
|
||||||
ADirective(const ADirective &other) = delete;
|
ADirective(const ADirective &other) = delete;
|
||||||
ADirective &operator=(const ADirective &other) = delete;
|
ADirective &operator=(const ADirective &other) = delete;
|
||||||
|
|||||||
@ -17,6 +17,13 @@ class FileHandler
|
|||||||
public:
|
public:
|
||||||
FileHandler(const AConfig *config, const URI &uri);
|
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;
|
[[nodiscard]] std::unique_ptr<HttpResponse> getResponse() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -18,18 +18,17 @@
|
|||||||
class HttpHeaders
|
class HttpHeaders
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
const std::string &get(const std::string &name) const;
|
[[nodiscard]] const std::string &get(const std::string &name) const;
|
||||||
bool has(const std::string &name) const;
|
[[nodiscard]] bool has(const std::string &name) const;
|
||||||
|
|
||||||
void parse(const std::string &rawHeaders);
|
void parse(const std::string &rawHeaders);
|
||||||
void add(const std::string &name, const std::string &value);
|
void add(const std::string &name, const std::string &value);
|
||||||
void remove(const std::string &name);
|
void remove(const std::string &name);
|
||||||
|
|
||||||
std::string toString() const;
|
[[nodiscard]] std::string toString() const;
|
||||||
|
[[nodiscard]] std::optional<size_t> getContentLength() const;
|
||||||
std::optional<size_t> getContentLength() const;
|
[[nodiscard]] std::optional<std::string> getContentType() const;
|
||||||
std::optional<std::string> getContentType() const;
|
[[nodiscard]] std::optional<std::string> getHost() const;
|
||||||
std::optional<std::string> getHost() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<std::string, std::string> headers_;
|
std::unordered_map<std::string, std::string> headers_;
|
||||||
|
|||||||
@ -223,3 +223,19 @@ void HttpRequest::reset()
|
|||||||
body_.clear();
|
body_.clear();
|
||||||
// contentLength_ = 0;
|
// 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_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -32,27 +32,24 @@ class HttpRequest
|
|||||||
~HttpRequest();
|
~HttpRequest();
|
||||||
|
|
||||||
[[nodiscard]] State getState() const;
|
[[nodiscard]] State getState() const;
|
||||||
void setState(State state);
|
|
||||||
[[nodiscard]] const HttpHeaders &getHeaders() const;
|
[[nodiscard]] const HttpHeaders &getHeaders() const;
|
||||||
[[nodiscard]] const std::string &getBody() 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_; }
|
void setState(State state);
|
||||||
|
|
||||||
[[nodiscard]] const std::string &getTarget() const { return target_; }
|
|
||||||
|
|
||||||
[[nodiscard]] const std::string &getHttpVersion() const { return httpVersion_; }
|
|
||||||
|
|
||||||
void receiveData(const char *data, size_t length);
|
void receiveData(const char *data, size_t length);
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void parseBuffer();
|
|
||||||
[[nodiscard]] bool parseBufferforRequestLine();
|
[[nodiscard]] bool parseBufferforRequestLine();
|
||||||
[[nodiscard]] bool parseBufferforHeaders();
|
[[nodiscard]] bool parseBufferforHeaders();
|
||||||
[[nodiscard]] bool parseHeaderLine();
|
[[nodiscard]] bool parseHeaderLine();
|
||||||
[[nodiscard]] bool parseBufferforBody();
|
[[nodiscard]] bool parseBufferforBody();
|
||||||
[[nodiscard]] bool parseBufferforChunkedBody();
|
[[nodiscard]] bool parseBufferforChunkedBody();
|
||||||
|
|
||||||
|
void parseBuffer();
|
||||||
void parseContentLength();
|
void parseContentLength();
|
||||||
|
|
||||||
Client *client_;
|
Client *client_;
|
||||||
@ -66,7 +63,4 @@ class HttpRequest
|
|||||||
std::string method_;
|
std::string method_;
|
||||||
std::string target_;
|
std::string target_;
|
||||||
std::string httpVersion_;
|
std::string httpVersion_;
|
||||||
// std::string requestLine_;
|
|
||||||
// std::string headers_;
|
|
||||||
// size_t contentLength_ = 0;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -25,13 +25,10 @@ class Server
|
|||||||
Server() = delete;
|
Server() = delete;
|
||||||
Server(const ConfigManager &configManager);
|
Server(const ConfigManager &configManager);
|
||||||
|
|
||||||
Server(const Server &other) = delete; // Disable copy constructor
|
Server(const Server &other) = delete;
|
||||||
Server &operator=(const Server &other) = delete; // Disable copy assignment
|
Server &operator=(const Server &other) = delete;
|
||||||
Server(Server &&other) noexcept = delete; // Move constructor
|
Server(Server &&other) noexcept = delete;
|
||||||
Server &operator=(Server &&other) noexcept = delete; // Move assignment
|
Server &operator=(Server &&other) noexcept = delete;
|
||||||
|
|
||||||
// The constructor must initialize the reference member 'configManager'
|
|
||||||
// Implementation should be in the .cpp file using an initializer list
|
|
||||||
|
|
||||||
~Server();
|
~Server();
|
||||||
|
|
||||||
@ -49,7 +46,6 @@ class Server
|
|||||||
const ConfigManager &configManager_;
|
const ConfigManager &configManager_;
|
||||||
std::vector<std::unique_ptr<ServerSocket>> listeners_;
|
std::vector<std::unique_ptr<ServerSocket>> listeners_;
|
||||||
std::set<int> listener_fds_;
|
std::set<int> listener_fds_;
|
||||||
// std::unordered_map<int, std::unique_ptr<Client>> clients_;
|
|
||||||
std::vector<std::unique_ptr<Client>> clients_;
|
std::vector<std::unique_ptr<Client>> clients_;
|
||||||
std::unordered_map<int, Client *> socketToClient_;
|
std::unordered_map<int, Client *> socketToClient_;
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
class CGISocket : public ASocket
|
class CGISocket : public ASocket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] ASocket::Type getType() const override;
|
|
||||||
|
|
||||||
explicit CGISocket(int fd);
|
explicit CGISocket(int fd);
|
||||||
|
|
||||||
|
[[nodiscard]] ASocket::Type getType() const override;
|
||||||
};
|
};
|
||||||
@ -9,7 +9,7 @@
|
|||||||
class ClientSocket : public ASocket
|
class ClientSocket : public ASocket
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] ASocket::Type getType() const override;
|
|
||||||
|
|
||||||
explicit ClientSocket(int fd);
|
explicit ClientSocket(int fd);
|
||||||
|
|
||||||
|
[[nodiscard]] ASocket::Type getType() const override;
|
||||||
};
|
};
|
||||||
@ -10,13 +10,11 @@ class ServerSocket : public ASocket
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ServerSocket();
|
ServerSocket();
|
||||||
|
|
||||||
ServerSocket(int fd);
|
ServerSocket(int fd);
|
||||||
|
|
||||||
void listen(int backlog) const;
|
void listen(int backlog) const;
|
||||||
void bind(const std::string &host, int port) const;
|
void bind(const std::string &host, int port) const;
|
||||||
|
|
||||||
[[nodiscard]] ASocket::Type getType() const override;
|
[[nodiscard]] ASocket::Type getType() const override;
|
||||||
|
|
||||||
[[nodiscard]] std::unique_ptr<ClientSocket> accept() const;
|
[[nodiscard]] std::unique_ptr<ClientSocket> accept() const;
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user