Skip to content

Commit

Permalink
refactor: refactor POST and PUT logic
Browse files Browse the repository at this point in the history
* fix(POST, PUT): remove '\r'

* refactor(GET): refactor

* refactor(POST): remove unusing data member

* refactor(PUT): remove unusing data member

* refactor: refactor logic

* fix(Response): file fail check
  • Loading branch information
Clearsu authored Aug 4, 2023
1 parent e3afb94 commit c9c4ae1
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 225 deletions.
15 changes: 9 additions & 6 deletions srcs/clients/method/include/GET.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ class GET : public IMethod {
void handlePath(RequestDts& dts, IResponse& response);
void getPublicEndpoint(RequestDts& dts, IResponse& response);
bool getSpecificEndpoint(RequestDts& dts, IResponse& response);
bool checkFile(std::string& path);
bool checkDirectory(std::string& path);
bool checkIndexFile(std::string& pathIndex);
bool checkFile(const std::string& path);
bool checkDirectory(const std::string& path);
bool checkIndexFile(const std::string& pathIndex);
bool checkAutoIndex(std::string& pathIndex, const std::string& autoindex);
void prepareBody(const std::string& pathIndex, IResponse& response);
void prepareBody(RequestDts& dts, const std::string& pathIndex,
IResponse& response);
void prepareFileList(const std::string& path, RequestDts& dts,
IResponse& response);
std::vector<std::string> getFileList(const std::string& path,
RequestDts& dts);
std::string generateHTML(const std::string& path,
const std::vector<std::string>& files);
void getContentType(const std::string& path, IResponse& response);
void prepareTextBody(const std::string& path, IResponse& response);
void prepareBinaryBody(const std::string& path, IResponse& response);
void prepareTextBody(RequestDts& dts, const std::string& path,
IResponse& response);
void prepareBinaryBody(RequestDts& dts, const std::string& path,
IResponse& response);
void bodyCheck(RequestDts& dts, IResponse& response);

private:
Expand Down
9 changes: 2 additions & 7 deletions srcs/clients/method/include/POST.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
class POST : public IMethod {
private:
std::string _location;
std::string _contentType;
std::string _boundary;
std::string _title;
std::string _content;
std::string _randName;

struct stat fileinfo;

void generateResource(RequestDts& dts);

Expand All @@ -26,10 +21,10 @@ class POST : public IMethod {
void createSuccessResponse(IResponse& response);
void generateUrlEncoded(RequestDts& dts);
void generateMultipart(RequestDts& dts);
void writeTextBody(RequestDts& dts, std::string mimeType);
void writeTextBody(RequestDts& dts, const std::string& mimeType);
void writeBinaryBody(RequestDts& dts);

std::string decodeURL(std::string encoded_string);
std::string decodeURL(std::string encodedString);

std::string makeRandomFileName(RequestDts& dts);

Expand Down
6 changes: 1 addition & 5 deletions srcs/clients/method/include/PUT.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ class PUT : public IMethod {
std::string _path;
std::string _uniqueID;

std::string _contentType;
std::string _boundary;
std::string _title;
std::string _content;

std::string _pathFinder;

struct stat fileinfo;

void generateResource(RequestDts& dts);

public:
Expand All @@ -33,7 +29,7 @@ class PUT : public IMethod {
void writeTextBody(RequestDts& dts);
void writeBinaryBody(RequestDts& dts);

std::string decodeURL(std::string encoded_string);
std::string decodeURL(std::string encodedString);

void initUniqueIdandPath(RequestDts& dts);

Expand Down
78 changes: 52 additions & 26 deletions srcs/clients/method/src/GET.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ void GET::doRequest(RequestDts& dts, IResponse& response) {
* @param response
*/
void GET::handlePath(RequestDts& dts, IResponse& response) {
if (getSpecificEndpoint(dts, response))
if (getSpecificEndpoint(dts, response)) {
return;
else
} else {
getPublicEndpoint(dts, response);
}
}

/**
Expand All @@ -71,24 +72,23 @@ void GET::handlePath(RequestDts& dts, IResponse& response) {
* @param response
*/
void GET::getPublicEndpoint(RequestDts& dts, IResponse& response) {
std::string path = *dts.path;
const std::string& path = *dts.path;
std::string pathIndex;
std::string matchedIndex;
std::string autoindex;
const std::string& autoindex = (*dts.matchedLocation)->getAutoindex();

if ((*dts.matchedLocation)->getIndex() != "") {
matchedIndex = (*dts.matchedLocation)->getIndex();
pathIndex = path + (path[path.size() - 1] == '/' ? matchedIndex
: "/" + matchedIndex);
}
autoindex = (*dts.matchedLocation)->getAutoindex();
if (checkFile(path)) {
*dts.statusCode = E_200_OK;
prepareBody(path, response);
prepareBody(dts, path, response);
} else if (checkDirectory(path)) {
if (checkIndexFile(pathIndex)) {
*dts.statusCode = E_200_OK;
prepareBody(pathIndex, response);
prepareBody(dts, pathIndex, response);
} else if (checkAutoIndex(pathIndex, autoindex)) {
*dts.statusCode = E_200_OK;
prepareFileList(path, dts, response);
Expand All @@ -110,11 +110,12 @@ void GET::getPublicEndpoint(RequestDts& dts, IResponse& response) {
* @return true
* @return false
*/
bool GET::checkFile(std::string& path) {
bool GET::checkFile(const std::string& path) {
struct stat fileStat;

if (stat(path.c_str(), &fileStat) == 0 && (S_ISREG(fileStat.st_mode)))
if (stat(path.c_str(), &fileStat) == 0 && (S_ISREG(fileStat.st_mode))) {
return true;
}
return false;
}

Expand All @@ -128,11 +129,12 @@ bool GET::checkFile(std::string& path) {
* @return true
* @return false
*/
bool GET::checkDirectory(std::string& path) {
bool GET::checkDirectory(const std::string& path) {
struct stat fileStat;

if (stat(path.c_str(), &fileStat) == 0 && (S_ISDIR(fileStat.st_mode)))
if (stat(path.c_str(), &fileStat) == 0 && (S_ISDIR(fileStat.st_mode))) {
return true;
}
return false;
}

Expand All @@ -146,10 +148,12 @@ bool GET::checkDirectory(std::string& path) {
* @return true
* @return false
*/
bool GET::checkIndexFile(std::string& pathIndex) {
bool GET::checkIndexFile(const std::string& pathIndex) {
struct stat fileStat;
if (stat(pathIndex.c_str(), &fileStat) == 0 && (S_ISREG(fileStat.st_mode)))

if (stat(pathIndex.c_str(), &fileStat) == 0 && (S_ISREG(fileStat.st_mode))) {
return true;
}
return false;
}

Expand Down Expand Up @@ -187,16 +191,21 @@ std::vector<std::string> GET::getFileList(const std::string& path,
DIR* dir;
struct dirent* ent;
std::vector<std::string> files;
std::string& originalPath = *dts.originalPath;

if ((*dts.originalPath)[(*dts.originalPath).size() - 1] != '/')
*dts.originalPath += '/';
if (originalPath[originalPath.size() - 1] != '/') {
originalPath += '/';
}
if ((dir = opendir(path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.' && ent->d_namlen == 1) continue;
if (ent->d_type == DT_DIR)
files.push_back((*dts.originalPath + ent->d_name + '/'));
else
files.push_back((*dts.originalPath + ent->d_name));
if (ent->d_name[0] == '.' && ent->d_namlen == 1) {
continue;
}
if (ent->d_type == DT_DIR) {
files.push_back(originalPath + ent->d_name + '/');
} else {
files.push_back(originalPath + ent->d_name);
}
}
closedir(dir);
} else {
Expand Down Expand Up @@ -252,14 +261,15 @@ void GET::prepareFileList(const std::string& path, RequestDts& dts,
* @param path
* @param response
*/
void GET::prepareBody(const std::string& path, IResponse& response) {
void GET::prepareBody(RequestDts& dts, const std::string& path,
IResponse& response) {
getContentType(path, response);
const std::string& value = response.getFieldValue("Content-Type");
if (value == "text/html" || value == "text/css" ||
value == "application/json") {
prepareTextBody(path, response);
prepareTextBody(dts, path, response);
} else {
prepareBinaryBody(path, response);
prepareBinaryBody(dts, path, response);
}
}

Expand All @@ -274,10 +284,17 @@ void GET::prepareBody(const std::string& path, IResponse& response) {
* @param path
* @param response
*/
void GET::prepareTextBody(const std::string& path, IResponse& response) {
void GET::prepareTextBody(RequestDts& dts, const std::string& path,
IResponse& response) {
std::ifstream file(path.c_str(), std::ios::in);
if (!file.is_open()) {
throw(*dts.statusCode = E_500_INTERNAL_SERVER_ERROR);
}
std::string buff;
while (std::getline(file, buff)) {
if (file.fail()) {
throw(*dts.statusCode = E_500_INTERNAL_SERVER_ERROR);
}
response.addBody(buff);
response.addBody("\r\n");
}
Expand All @@ -292,10 +309,17 @@ void GET::prepareTextBody(const std::string& path, IResponse& response) {
* @param path
* @param response
*/
void GET::prepareBinaryBody(const std::string& path, IResponse& response) {
void GET::prepareBinaryBody(RequestDts& dts, const std::string& path,
IResponse& response) {
std::ifstream file(path.c_str(), std::ios::binary);
if (!file.is_open()) {
throw(*dts.statusCode = E_500_INTERNAL_SERVER_ERROR);
}
std::stringstream buffer;
buffer << file.rdbuf();
if (file.fail()) {
throw(*dts.statusCode = E_500_INTERNAL_SERVER_ERROR);
}
response.addBody(buffer.str());
file.close();
}
Expand Down Expand Up @@ -394,7 +418,9 @@ void GET::getContentType(const std::string& path, IResponse& response) {
* @return false : false를 리턴하면 해당 트랜잭션을 퍼블릭으로 넘어가서 진행.
*/
bool GET::getSpecificEndpoint(RequestDts& dts, IResponse& response) {
if (*dts.is_session == false) return false;
if (*dts.is_session == false) {
return false;
}
const std::string& originalPath = *dts.originalPath;
try {
SessionData& sessionData =
Expand Down
Loading

0 comments on commit c9c4ae1

Please sign in to comment.