diff --git a/config/default.conf b/config/default.conf
index 1b247f3..0d9ff60 100644
--- a/config/default.conf
+++ b/config/default.conf
@@ -12,13 +12,13 @@ server {
error_page 400 ./htdocs/error_pages/400.html;
error_page 404 ./htdocs/error_pages/404.html;
- error_page 403 /403.html;
- error_page 404 /404.html;
- error_page 405 /405.html;
- error_page 413 /413.html;
- error_page 500 /500.html;
- error_page 502 /502.html;
- error_page 504 /504.html;
+ error_page 404 ./htdocs/error_pages/404.html;
+ error_page 403 ./htdocs/error_pages/403.html;
+ error_page 405 ./htdocs/error_pages/405.html;
+ error_page 413 ./htdocs/error_pages/413.html;
+ error_page 500 ./htdocs/error_pages/500.html;
+ error_page 502 ./htdocs/error_pages/502.html;
+ error_page 504 ./htdocs/error_pages/504.html;
client_max_body_size 1M;
@@ -42,6 +42,13 @@ server {
allowed_methods GET;
}
+ location /test {
+ root ./htdocs/site-1/tester;
+ autoindex off;
+ index index.html;
+ allowed_methods GET;
+ }
+
# cgi_enabled yes;
# cgi_ext .php /usr/bin/php-cgi;
}
diff --git a/htdocs/error_pages/401.html b/htdocs/error_pages/401.html
new file mode 100644
index 0000000..c05c829
--- /dev/null
+++ b/htdocs/error_pages/401.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 401 Unauthorized
+
+
+
+
+
+
401
+
Access Denied!
Authentication is required to access this resource.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/403.html b/htdocs/error_pages/403.html
new file mode 100644
index 0000000..c301197
--- /dev/null
+++ b/htdocs/error_pages/403.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 403 Forbidden
+
+
+
+
+
+
403
+
Forbidden!
You don't have permission to access this resource.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/405.html b/htdocs/error_pages/405.html
new file mode 100644
index 0000000..fcbe9ef
--- /dev/null
+++ b/htdocs/error_pages/405.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 405 Method Not Allowed
+
+
+
+
+
+
405
+
Method Not Allowed!
The request method is not supported for this resource.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/408.html b/htdocs/error_pages/408.html
new file mode 100644
index 0000000..7a0abf7
--- /dev/null
+++ b/htdocs/error_pages/408.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 408 Request Timeout
+
+
+
+
+
+
408
+
Request Timeout!
The server timed out waiting for the request.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/411.html b/htdocs/error_pages/411.html
new file mode 100644
index 0000000..2427e25
--- /dev/null
+++ b/htdocs/error_pages/411.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 411 Length Required
+
+
+
+
+
+
411
+
Length Required!
The request must specify the content length.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/413.html b/htdocs/error_pages/413.html
new file mode 100644
index 0000000..2643331
--- /dev/null
+++ b/htdocs/error_pages/413.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 413 Payload Too Large
+
+
+
+
+
+
413
+
Payload Too Large!
The request entity is larger than the server is able to process.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/414.html b/htdocs/error_pages/414.html
new file mode 100644
index 0000000..3e4fa78
--- /dev/null
+++ b/htdocs/error_pages/414.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 414 URI Too Long
+
+
+
+
+
+
414
+
URI Too Long!
The request URI is longer than the server is willing to interpret.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/415.html b/htdocs/error_pages/415.html
new file mode 100644
index 0000000..87bb504
--- /dev/null
+++ b/htdocs/error_pages/415.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 415 Unsupported Media Type
+
+
+
+
+
+
415
+
Unsupported Media Type!
The media format of the requested data is not supported.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/500.html b/htdocs/error_pages/500.html
new file mode 100644
index 0000000..22db79d
--- /dev/null
+++ b/htdocs/error_pages/500.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 500 Internal Server Error
+
+
+
+
+
+
500
+
Internal Server Error!
Something went wrong on our end. Please try again later.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/501.html b/htdocs/error_pages/501.html
new file mode 100644
index 0000000..1f31d39
--- /dev/null
+++ b/htdocs/error_pages/501.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 501 Not Implemented
+
+
+
+
+
+
501
+
Not Implemented!
The server does not support the functionality required to fulfill the request.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/502.html b/htdocs/error_pages/502.html
new file mode 100644
index 0000000..34a4cbc
--- /dev/null
+++ b/htdocs/error_pages/502.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 502 Bad Gateway
+
+
+
+
+
+
502
+
Bad Gateway!
The server received an invalid response from an upstream server.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/503.html b/htdocs/error_pages/503.html
new file mode 100644
index 0000000..9738812
--- /dev/null
+++ b/htdocs/error_pages/503.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 503 Service Unavailable
+
+
+
+
+
+
503
+
Service Unavailable!
The server is temporarily overloaded or under maintenance.
+
Go Home
+
+
+
diff --git a/htdocs/error_pages/504.html b/htdocs/error_pages/504.html
new file mode 100644
index 0000000..da085f7
--- /dev/null
+++ b/htdocs/error_pages/504.html
@@ -0,0 +1,73 @@
+
+
+
+
+ 504 Gateway Timeout
+
+
+
+
+
+
504
+
Gateway Timeout!
The server did not receive a timely response from an upstream server.
+
Go Home
+
+
+
diff --git a/htdocs/site-1/tester/test.html b/htdocs/site-1/tester/test.html
new file mode 100644
index 0000000..7fdadf8
--- /dev/null
+++ b/htdocs/site-1/tester/test.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+ Test Page
+
+
+ This is a test HTML page
+
+
\ No newline at end of file
diff --git a/webserv/client/Client.cpp b/webserv/client/Client.cpp
index 9395062..e3f877c 100644
--- a/webserv/client/Client.cpp
+++ b/webserv/client/Client.cpp
@@ -35,15 +35,6 @@ Client::~Client()
server_.remove(*clientSocket_);
};
-int Client::getStatusCode() const noexcept
-{
- return statusCode_;
-}
-
-void Client::setStatusCode(int code)
-{
- statusCode_ = code;
-}
ASocket &Client::getSocket(int fd) const
{
@@ -191,7 +182,7 @@ void Client::poll() const
{
Log::info("Response is ready to be sent to client, fd: " + std::to_string(clientSocket_->getFd()));
clientSocket_->setCallback([this]() { respond(); });
- server_.responseReady(clientSocket_->getFd());
+ server_.writable(clientSocket_->getFd());
}
}
diff --git a/webserv/client/Client.hpp b/webserv/client/Client.hpp
index 294b6dc..6c35df0 100644
--- a/webserv/client/Client.hpp
+++ b/webserv/client/Client.hpp
@@ -34,24 +34,24 @@ class Client
void request();
void respond() const;
+ void poll() const;
void writeToCgi();
void readFromCgi();
- void poll() const;
- [[nodiscard]] int getStatusCode() const noexcept;
+ // [[nodiscard]] int getStatusCode() const noexcept;
[[nodiscard]] ASocket &getSocket(int fd = -1) const;
- void setStatusCode(int code);
+ // void setStatusCode(int code);
void setCgiSockets(std::unique_ptr cgiStdIn, std::unique_ptr cgiStdOut);
[[nodiscard]] HttpRequest &getHttpRequest() const noexcept;
[[nodiscard]] HttpResponse &getHttpResponse() const noexcept;
private:
- int statusCode_ = Http::StatusCode::OK;
+ // int statusCode_ = Http::StatusCode::OK;
constexpr static size_t bufferSize_ = 8192; // 8KB buffer for reading CGI output
std::unique_ptr httpRequest_;
std::unique_ptr httpResponse_;
diff --git a/webserv/handler/ErrorHandler.cpp b/webserv/handler/ErrorHandler.cpp
index 1ccfb98..c3a9c8c 100644
--- a/webserv/handler/ErrorHandler.cpp
+++ b/webserv/handler/ErrorHandler.cpp
@@ -20,6 +20,7 @@ void ErrorHandler::createErrorResponse(uint16_t statusCode, HttpResponse &respon
response.addHeader("Content-Type", "text/html");
response.addHeader("Connection", "close");
response.appendBody(generateErrorPage(statusCode, config));
+ response.setComplete();
}
std::string ErrorHandler::generateErrorPage(uint16_t statusCode, const AConfig *config)
diff --git a/webserv/handler/FileHandler.cpp b/webserv/handler/FileHandler.cpp
index 496e09d..db4c44c 100644
--- a/webserv/handler/FileHandler.cpp
+++ b/webserv/handler/FileHandler.cpp
@@ -23,6 +23,7 @@ FileHandler::FileHandler(const HttpRequest &request, HttpResponse &response)
void FileHandler::handleFile(const std::string &filepath) const
{
Log::trace(LOCATION);
+ Log::debug("Requested path is a file: " + filepath);
// auto response = std::make_unique();
std::string extension = FileUtils::getExtension(filepath);
@@ -45,6 +46,7 @@ void FileHandler::handleFile(const std::string &filepath) const
void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type) const
{
Log::trace(LOCATION);
+ Log::debug("Requested path is a directory: " + dirpath);
if (type == DIRECTORY_INDEX)
{
@@ -72,7 +74,13 @@ void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type)
void FileHandler::handle() const
{
Log::trace(LOCATION);
+ if (!uri_.isValid())
+ {
+ ErrorHandler::createErrorResponse(Http::StatusCode::NOT_FOUND, response_, config_);
+ return;
+ }
std::string filepath = uri_.getFullPath();
+ Log::debug("Handling file request for path: " + filepath);
ResourceType resourceType = getResourceType(filepath);
switch (resourceType)
diff --git a/webserv/handler/URI.cpp b/webserv/handler/URI.cpp
index 9ecf87b..ced19a0 100644
--- a/webserv/handler/URI.cpp
+++ b/webserv/handler/URI.cpp
@@ -17,6 +17,7 @@ URI::URI(const HttpRequest &request, const ServerConfig &serverConfig)
{
Log::trace(LOCATION);
parseUri(request.getTarget());
+ Log::debug("Parsed URI: " + uriTrimmed_, {{"ConfigType", config_->getType()}});
parseFullpath();
authority_ = request.getHeaders().getHost().value();
@@ -84,17 +85,17 @@ void URI::parseFullpath()
for (const auto &segment : uriSegments)
{
- std::string curDir = FileUtils::joinPath(dir_, segment);
+ std::string currentPath = FileUtils::joinPath(dir_, segment);
if (segment.empty())
{
continue;
}
- if (FileUtils::isFile(curDir) && baseName_.empty())
+ if (FileUtils::isFile(currentPath) && baseName_.empty())
{
baseName_ = segment;
}
- else if (FileUtils::isDirectory(curDir))
+ else if (FileUtils::isDirectory(currentPath))
{
dir_ = FileUtils::joinPath(dir_, segment);
}
@@ -102,6 +103,12 @@ void URI::parseFullpath()
{
pathInfo_ = FileUtils::joinPath(pathInfo_, baseName_);
}
+ else // not file or dir, and no baseName yet
+ {
+ valid_ = false;
+ Log::warning("Invalid path segment encountered: " + currentPath);
+ return;
+ }
}
fullPath_ = FileUtils::joinPath(dir_, baseName_);
}
@@ -146,7 +153,7 @@ bool URI::isDirectory() const noexcept
bool URI::isValid() const noexcept
{
- return FileUtils::isValidPath(fullPath_);
+ return valid_ && FileUtils::isValidPath(fullPath_);
}
bool URI::isCgi() const noexcept
diff --git a/webserv/handler/URI.hpp b/webserv/handler/URI.hpp
index ed7fc3d..b9ce7b5 100644
--- a/webserv/handler/URI.hpp
+++ b/webserv/handler/URI.hpp
@@ -49,6 +49,7 @@ class URI
std::string query_;
std::string fragment_;
std::string authority_;
+ bool valid_ = true;
static const AConfig *matchConfig(const std::string &uri, const ServerConfig &serverConfig);
};
\ No newline at end of file
diff --git a/webserv/http/HttpRequest.cpp b/webserv/http/HttpRequest.cpp
index 7cec63d..cbacd67 100644
--- a/webserv/http/HttpRequest.cpp
+++ b/webserv/http/HttpRequest.cpp
@@ -94,7 +94,6 @@ void HttpRequest::parseBuffer()
return; // Request is complete
case State::ParseError:
Log::warning("Parse error occurred, stopping further processing");
- client_->setStatusCode(Http::StatusCode::BAD_REQUEST);
return;
}
}
diff --git a/webserv/server/Server.cpp b/webserv/server/Server.cpp
index 0588d96..817056a 100644
--- a/webserv/server/Server.cpp
+++ b/webserv/server/Server.cpp
@@ -168,7 +168,7 @@ void Server::handleRequest(struct epoll_event *event) const
client.getSocket(client_fd).callback();
}
-void Server::responseReady(int client_fd) const
+void Server::writable(int client_fd) const
{
Log::trace(LOCATION);
Log::debug("Response ready for client fd: " + std::to_string(client_fd));
diff --git a/webserv/server/Server.hpp b/webserv/server/Server.hpp
index 85f42ae..3b45635 100644
--- a/webserv/server/Server.hpp
+++ b/webserv/server/Server.hpp
@@ -36,7 +36,7 @@ class Server
void add(const ASocket &socket, uint32_t events, Client *client = nullptr);
void remove(const ASocket &socket);
void disconnect(const Client &client);
- void responseReady(int client_fd) const;
+ void writable(int client_fd) const;
ServerSocket &getListener(int fd) const;
Client &getClient(int fd) const;