Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement GET method for static files(#9) #12

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions error_pages/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
<hr />
ngin-xs
</body>
</html>
8 changes: 8 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<head>
<title>Ngin-xs</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions html/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<head>
<title>test</title>
</head>
<body>
<h1>/test.html</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions html/test/a.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<head>
<title>a</title>
</head>
<body>
<h1>a</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions html/test/b.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<head>
<title>b</title>
</head>
<body>
<h1>b</h1>
</body>
</html>
8 changes: 8 additions & 0 deletions html/test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<head>
<title>test index</title>
</head>
<body>
<h1>test/index.html</h1>
</body>
</html>
31 changes: 27 additions & 4 deletions srcs/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ const char *Worker::InvalidHostHeaderException::what() const throw() {
return message_.c_str();
}

Worker::FileNotFoundException::FileNotFoundException(const std::string str) {
message_ = str;
}

Worker::FileNotFoundException::~FileNotFoundException() throw() {}

const char *Worker::FileNotFoundException::what() const throw() {
return message_.c_str();
}

Worker::Worker(int listenSocket) {
struct sockaddr_in clientAddress;
socklen_t addressLen;
Expand All @@ -55,6 +65,7 @@ Worker::Worker(int listenSocket) {

Worker::~Worker() {
resetPollfd();
delete request_;
}

void Worker::setPollfd(struct pollfd *pollfd) {
Expand All @@ -78,11 +89,23 @@ ft_bool Worker::work() {
ret = recv();
if (ret == FT_FALSE)
return ret;
//
send(buf_);
if (request_->getMethod() == "GET") {
std::string filePath = std::string(WEB_ROOT) + request_->getUri();
if (isDirectory(filePath))
filePath += "/index.html"; // TODO: read default file from config
if (isFileExist(filePath) == FT_FALSE)
throw FileNotFoundException("File not found");
Response response(HTTP_OK, fileToString(filePath));
send(response.createMessage().c_str());
}
return ret;
}
return ret;
} catch(FileNotFoundException &e) {
std::cerr << e.what() << std::endl;
Response response(HTTP_NOT_FOUND, fileToString(std::string(ERROR_PAGES_PATH) + "404.html"));
send(response.createMessage().c_str());
return FT_TRUE;
} catch(InvalidMethodException &e) {
std::cerr << e.what() << std::endl;
Response response(HTTP_METHOD_NOT_ALLOWED, fileToString(std::string(ERROR_PAGES_PATH) + "405.html"));
Expand Down Expand Up @@ -114,8 +137,8 @@ ft_bool Worker::recv() {
throw WorkerException("fail: recv()\n");
buf_[ret] = '\0';
std::cout << "server received(" << ret << "): " << buf_ << std::endl;
Request request(buf_);
validate(request);
request_ = new Request(buf_);
validate(*request_);
return FT_TRUE;
}

Expand Down
11 changes: 10 additions & 1 deletion srcs/Worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Worker {
int connectSocket_;
char buf_[BUFFER_LENGTH];
struct pollfd *pollfd_;
std::string statusCode_;
Request *request_;

ft_bool recv();
// void unchunk();
Expand Down Expand Up @@ -73,6 +73,15 @@ class Worker {
~InvalidHostHeaderException() throw();
virtual const char *what() const throw();
};

class FileNotFoundException : public std::exception {
private:
std::string message_;
public:
FileNotFoundException(const std::string str);
~FileNotFoundException() throw();
virtual const char *what() const throw();
};
};

#endif
14 changes: 13 additions & 1 deletion srcs/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ std::vector<std::string> split(const std::string &str, std::string delim) {
return splitedMessage;
}

std::string fileToString(const std::string filePath) {
ft_bool isFileExist(const std::string &filePath)
{
std::ifstream file(filePath);
return file.good();
}

std::string fileToString(const std::string &filePath) {
std::string str = "";
std::ifstream openFile(filePath);

Expand All @@ -37,3 +43,9 @@ ft_bool isIncluded(const std::string &value, const std::vector<std::string> &arr
{
return (std::find(array.begin(), array.end(), value) != array.end());
}

ft_bool isDirectory(const std::string &filePath)
{
struct stat buffer;
return (stat(filePath.c_str(), &buffer) == 0);
}
5 changes: 4 additions & 1 deletion srcs/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include "macro.hpp"

template <typename T>
Expand All @@ -17,7 +18,9 @@ std::string ntos (T n)
}

std::vector<std::string> split(const std::string &str, std::string delim);
std::string fileToString(const std::string filePath);
ft_bool isFileExist(const std::string &filePath);
std::string fileToString(const std::string &filePath);
ft_bool isIncluded(const std::string &value, const std::vector<std::string> &array);
ft_bool isDirectory(const std::string &filePath);

#endif