Skip to content

Commit

Permalink
♻️ fix: refactor : file read & write event loop #23
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshong committed May 24, 2022
2 parents 0093938 + 38f6567 commit 8148f12
Show file tree
Hide file tree
Showing 10 changed files with 657 additions and 516 deletions.
2 changes: 1 addition & 1 deletion request/Request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Request
std::string getProtocol() const;
std::string getRequestBody() const;
std::map<std::string, std::string> getRequestHeader() const;
RequestStage getStage() const;
RequestStage getRequestStage() const;
Resource* getResource() const;
ConfigLocation *getRoute() const;
std::string getFile() const;
Expand Down
79 changes: 43 additions & 36 deletions response/Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,48 @@ Resource::~Resource()

}

int Resource::getWriteFd()
{
return _write_fd;
}

int Resource::getReadFd()
{
return _read_fd;
}

pid_t Resource::getPid()
{
return _pid;
}

std::string &Resource::getContent()
{
return (content);
int Resource::getWriteFd() { return _write_fd; }
int Resource::getReadFd() { return _read_fd; }
pid_t Resource::getPid() { return _pid; }
std::string &Resource::getContent() { return _content; }
std::string const &Resource::getContent() const { return _content; }
std::string Resource::getSrcExtension() const { return _extension; }
std::string Resource::getSrcContentType() const { return _content_type; }

void Resource::setWriteFd(int fd) { _write_fd = fd; }
void Resource::setReadFd(int fd) { _read_fd = fd; }
void Resource::setPid(pid_t pid) { _pid = pid; }
void Resource::setExtension(std::string const &extension) { _extension = extension; }
void Resource::setContentType(std::string const &content_type) { _content_type = content_type; }

void Resource::makeAutoIndex(std::string root, std::string directory)
{
std::string host = "http://" + getSocketHost() + ":" + std::to_string(getSocketPort());
std::string pos = (directory == "" || directory[directory.size() - 1] != '/') ? directory + "/" : directory;
DIR *dir = NULL;

_content += "<!DOCTYPE html>\n";
_content += "<html>\n";
_content += "<head>\n</head>\n";
_content += "<body>\n";
_content += "<h1> Index of "+ pos + "</h1>\n";

dir = opendir((root + directory).c_str());
if (dir == NULL)
return ; // error

struct dirent *file = NULL;
while ((file = readdir(dir)) != NULL) {
std::string d_name = file->d_type == DT_DIR ? std::string(file->d_name) + "/" : std::string(file->d_name);
_content += "<a href=\"" + host + pos + file->d_name + "\">";
_content += file->d_name;
if (file->d_type == DT_DIR)
_content += + "/";
_content += "</a><br>\n";
}
closedir(dir);

_content += "</body>\n";
_content += "</html>\n";
}

std::string const &Resource::getContent() const
{
return (content);
}

void Resource::setWriteFd(int fd)
{
_write_fd = fd;
}

void Resource::setReadFd(int fd)
{
_read_fd = fd;
}
void Resource::getPid(pid_t pid)
{
_pid = pid;
}
12 changes: 10 additions & 2 deletions response/Resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class Resource : public Socket // cgi가 실행이 됐는지를 waitpid로 확
int _write_fd;
int _read_fd;
pid_t _pid;
std::string content;
std::string _content;
std::string _extension;
std::string _content_type;

public:
Resource();
Expand All @@ -21,11 +23,17 @@ class Resource : public Socket // cgi가 실행이 됐는지를 waitpid로 확
pid_t getPid();
std::string &getContent();
std::string const &getContent() const;
std::string getSrcExtension() const;
std::string getSrcContentType() const;
void setContent(std::string const &content);
void setRequest(Request *req);
void setWriteFd(int fd);
void setReadFd(int fd);
void getPid(pid_t pid);
void setPid(pid_t pid);
void setExtension(std::string const &extension);
void setContentType(std::string const &content_type);
void makeAutoIndex(std::string root, std::string directory);

};

#endif
Loading

0 comments on commit 8148f12

Please sign in to comment.