feat: add remote addr to cgi env
This commit is contained in:
parent
a68e7fd33c
commit
2d9bf7a37f
@ -20,6 +20,8 @@
|
||||
#include <utility> // for move, pair
|
||||
#include <vector> // for vector
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h> // for send
|
||||
#include <sys/types.h> // for ssize_t
|
||||
|
||||
@ -183,4 +185,19 @@ HttpRequest &Client::getHttpRequest() const noexcept
|
||||
HttpResponse &Client::getHttpResponse() const noexcept
|
||||
{
|
||||
return *httpResponse_;
|
||||
}
|
||||
}
|
||||
|
||||
// NOLINTBEGIN
|
||||
std::string Client::getClientAddress() const noexcept
|
||||
{
|
||||
const struct sockaddr *addr = clientSocket_->getAddress();
|
||||
if (addr->sa_family == AF_INET)
|
||||
{
|
||||
const struct sockaddr_in *ipv4 = reinterpret_cast<const struct sockaddr_in *>(addr);
|
||||
const char *addr = inet_ntoa(ipv4->sin_addr);
|
||||
return std::string(addr);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
// NOLINTEND
|
||||
@ -15,6 +15,7 @@
|
||||
|
||||
#include <cstddef> // for size_t
|
||||
#include <memory> // for unique_ptr
|
||||
#include <string>
|
||||
#include <unordered_map> // for unordered_map
|
||||
|
||||
class Server;
|
||||
@ -41,20 +42,16 @@ class Client
|
||||
void respond() const;
|
||||
void poll() const;
|
||||
|
||||
// [[nodiscard]] int getStatusCode() const noexcept;
|
||||
|
||||
[[nodiscard]] ASocket &getSocket(int fd = -1) const;
|
||||
|
||||
// void setStatusCode(int code);
|
||||
// void setCgiSockets(CgiSocket *cgiStdIn, CgiSocket *cgiStdOut);
|
||||
// void removeCgiSocket(CgiSocket *cgiSocket);
|
||||
|
||||
void addSocket(ASocket *socket);
|
||||
void removeSocket(ASocket *socket);
|
||||
|
||||
[[nodiscard]] HttpRequest &getHttpRequest() const noexcept;
|
||||
[[nodiscard]] HttpResponse &getHttpResponse() const noexcept;
|
||||
|
||||
[[nodiscard]] std::string getClientAddress() const noexcept;
|
||||
|
||||
private:
|
||||
// int statusCode_ = Http::StatusCode::OK;
|
||||
constexpr static size_t bufferSize_ = 8192; // 8KB buffer for reading CGI output
|
||||
|
||||
@ -42,6 +42,8 @@ ConfigValidator::ConfigValidator(const GlobalConfig *config) : engine_(std::make
|
||||
engine_->addLocationRule("root", std::make_unique<FolderExistsRule>(true));
|
||||
engine_->addLocationRule("cgi_ext", std::make_unique<CgiExtValidationRule>(false));
|
||||
|
||||
// TODO: Add a validation rule for redirect
|
||||
|
||||
engine_->validate();
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +37,7 @@ CgiEnvironment::CgiEnvironment(const URI &uri, const HttpRequest &request)
|
||||
|
||||
env_["SERVER_NAME"] = host;
|
||||
env_["SERVER_PORT"] = std::to_string(port);
|
||||
env_["REMOTE_ADDR"] = "<REMOTE_ADDR>"; // Placeholder, should be set to actual remote address
|
||||
env_["REMOTE_ADDR"] = request.getClient().getClientAddress(); // Placeholder, should be set to actual remote address
|
||||
env_["REDIRECT_STATUS"] = "200"; // Required by PHP with force-cgi-redirect enabled
|
||||
env_["SERVER_SOFTWARE"] = "Webserv/1.0";
|
||||
env_["REQUEST_SCHEME"] = "HTTP";
|
||||
|
||||
@ -72,8 +72,6 @@ void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type)
|
||||
if (type == DIRECTORY_AUTOINDEX)
|
||||
{
|
||||
Log::debug("Requested path is a directory: " + dirpath);
|
||||
// ErrorHandler::createErrorResponse(Http::StatusCode::FORBIDDEN, response_, config_);
|
||||
// TODO: This doesn't trigger for some reason :p (I Know why, i hacked the index in uri.cpp)
|
||||
response_.setBody(AutoIndex::generate(dirpath, uri_));
|
||||
response_.setStatus(Http::StatusCode::OK);
|
||||
return;
|
||||
|
||||
@ -27,7 +27,6 @@ void RedirectHandler::handle()
|
||||
"<body><a href=\"" +
|
||||
redirect.second + "\">Found</a></body></html>";
|
||||
response_.setBody(body);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <webserv/log/Log.hpp> // for LOCATION, Log
|
||||
#include <webserv/socket/ASocket.hpp> // for ASocket
|
||||
|
||||
ClientSocket::ClientSocket(int fd) : ASocket(fd)
|
||||
ClientSocket::ClientSocket(int fd, struct sockaddr address) : ASocket(fd), address_(address)
|
||||
{
|
||||
Log::trace(LOCATION);
|
||||
}
|
||||
@ -12,3 +12,8 @@ ASocket::Type ClientSocket::getType() const noexcept
|
||||
{
|
||||
return ASocket::Type::CLIENT_SOCKET;
|
||||
}
|
||||
|
||||
const struct sockaddr *ClientSocket::getAddress() const noexcept
|
||||
{
|
||||
return &address_;
|
||||
}
|
||||
@ -9,7 +9,10 @@
|
||||
class ClientSocket : public ASocket
|
||||
{
|
||||
public:
|
||||
explicit ClientSocket(int fd);
|
||||
explicit ClientSocket(int fd, struct sockaddr address);
|
||||
|
||||
[[nodiscard]] ASocket::Type getType() const noexcept override;
|
||||
[[nodiscard]] const struct sockaddr *getAddress() const noexcept;
|
||||
private:
|
||||
struct sockaddr address_;
|
||||
};
|
||||
@ -1,8 +1,7 @@
|
||||
#include <webserv/socket/ServerSocket.hpp>
|
||||
|
||||
#include <webserv/log/Log.hpp> // for Log, LOCATION
|
||||
#include <webserv/socket/ASocket.hpp> // for ASocket
|
||||
#include <webserv/socket/ClientSocket.hpp> // for ClientSocket
|
||||
#include <webserv/socket/ServerSocket.hpp>
|
||||
|
||||
#include <memory> // for allocator, make_unique, unique_ptr
|
||||
#include <stdexcept> // for runtime_error
|
||||
@ -72,11 +71,13 @@ ASocket::Type ServerSocket::getType() const noexcept
|
||||
std::unique_ptr<ClientSocket> ServerSocket::accept() const
|
||||
{
|
||||
Log::trace(LOCATION);
|
||||
int client_fd = ::accept(getFd(), nullptr, nullptr);
|
||||
struct sockaddr client_address{};
|
||||
socklen_t address_len = sizeof(client_address);
|
||||
int client_fd = ::accept(getFd(), &client_address, &address_len);
|
||||
if (client_fd < 0)
|
||||
{
|
||||
Log::error("Accept failed");
|
||||
throw std::runtime_error("Accept failed");
|
||||
}
|
||||
return std::make_unique<ClientSocket>(client_fd);
|
||||
return std::make_unique<ClientSocket>(client_fd, client_address);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user