From 843d86c606bae0adba06ce52881fb880b8bff7a9 Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Tue, 19 Apr 2022 17:36:39 +0900 Subject: [PATCH 1/7] =?UTF-8?q?:sparkles:=20testFunctions.hpp=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 테스트함수 전용 네임스페이스 추가 --- includes/testFunctions.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 includes/testFunctions.hpp diff --git a/includes/testFunctions.hpp b/includes/testFunctions.hpp new file mode 100644 index 0000000..3a6e9a6 --- /dev/null +++ b/includes/testFunctions.hpp @@ -0,0 +1,18 @@ +#ifndef TESTFUNCTIONS_HPP +#define TESTFUNCTIONS_HPP + +#define TEST_MODE 0 +#ifndef TEST_MODE +#define TEST_MODE + +namespace test { + void showSizeDirectives(void) { + std::cout << "main :" << this->mainDirectives.size() << std::endl; + std::cout << "event :" << this->eventDirectives.size() << std::endl; + std::cout << "http :" << this->httpDirectives.size() << std::endl; + }; +} + +#endif // TEST_MODE + +#endif // TESTFUNCTIONS_HPP \ No newline at end of file From a0434271f925826b4d107d6755827b96b0355bee Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Tue, 19 Apr 2022 18:21:15 +0900 Subject: [PATCH 2/7] =?UTF-8?q?:fire:=20parseDirectives=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 필요성이 없어진 이유로 함수의 특정 시그니쳐를 삭제합니다. --- src/ConfigParser.cpp | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index afbacde..89a4d77 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -146,38 +146,6 @@ static void parseDirectives(std::string& lines, std::map& map) -{ - std::size_t idx_first_block = lines.find("{", 1); - std::string tmp, key, value; - for (std::size_t base = 0, found = 0;;) - { - while (isspace(lines[base])) base++; - found = lines.find(";", base); - if (found > idx_first_block || found == STRING_NPOS) { - lines.erase(0, base); - break; - } - tmp = lines.substr(base, found - base); - std::string::iterator it = tmp.begin(); - while (isalpha(*it) || isSpecialCharactersBlockKeyword(*it)) { - key += *it; - it++; - } - while (isspace(*it)) - it++; - while (it != tmp.end()) { - value += *it; - it++; - } - map.insert(make_pair(key, value)); - key.clear(); - value.clear(); - base = found + 1; - } -} - - void ConfigParser::parseMainContext(std::string& lines) { parseDirectives(lines, this->mainDirectives); } From 8ddc3fb0596055cc1eb2c88130d7b22fc7bb559b Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Tue, 19 Apr 2022 19:35:58 +0900 Subject: [PATCH 3/7] =?UTF-8?q?:white=5Fcheck=5Fmark:=20Configuration=20fi?= =?UTF-8?q?le=20parsing=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=20=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 테스트로그 함수 업로드, server block도 볼 수 있도록 임시 지역변수 생성 --- includes/ConfigParser.hpp | 12 ++++----- src/ConfigParser.cpp | 52 +++++++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/includes/ConfigParser.hpp b/includes/ConfigParser.hpp index 8ecac85..d4aba98 100644 --- a/includes/ConfigParser.hpp +++ b/includes/ConfigParser.hpp @@ -4,7 +4,6 @@ #include #include #include -#include "./HTTPmoduler.hpp" class ConfigParser { public : @@ -22,13 +21,14 @@ class ConfigParser { void parseMainContext(std::string& lines); void parseEventsBlock(std::string& lines); void parseHTTPBlock(std::string& lines); - void parseServerBlock(std::string& lines); - void showSizeDirectives(void); + void parseServerBlock(std::string& lines, std::vector > >& a); + void test_map_data_print(void); const char *file_path_; - std::map mainDirectives; - std::map eventDirectives; - std::multimap httpDirectives; + std::map > mainDirectives; + std::map > eventDirectives; + std::map > httpDirectives; + std::vector > > serverBlocks; }; #endif // INCLUDES_CONFIGPARSER_HPP_ diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 89a4d77..da662fb 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -1,6 +1,7 @@ // Copyright [2022] #include "../includes/ConfigParser.hpp" +#include "../includes/testFunctions.hpp" #include "../includes/webserv.hpp" #include #include @@ -110,12 +111,13 @@ static std::string getkeywordBlock(std::string& line) static std::string getvaluesBlock(std::string& lines) { size_t bracket_end_idx = getLastBracketIndex(lines); - std::string ret = lines.substr(lines.find('{', 0), bracket_end_idx); + size_t bracket_start_idx = lines.find('{', 0); + std::string ret = lines.substr(++bracket_start_idx, bracket_end_idx); lines.erase(0, ret.length()); return ret; } -static void parseDirectives(std::string& lines, std::map& map) +static void parseDirectives(std::string& lines, std::map >& map) { std::size_t idx_first_block = lines.find("{", (lines[0] == '{')); std::string tmp, key, value; @@ -139,7 +141,14 @@ static void parseDirectives(std::string& lines, std::map bucket; + bucket.push_back(value); + map.insert(make_pair(key, bucket)); + } + else { + map.find(key)->second.push_back(value); + } key.clear(); value.clear(); base = found + 1; @@ -161,14 +170,15 @@ void ConfigParser::parseHTTPBlock(std::string& lines) { } } - -void ConfigParser::parseServerBlock(std::string& lines) { +void ConfigParser::parseServerBlock(std::string& lines, std::vector > >& a) { // 두번째 인자는 로직 입증을 위해 임시로 넣어둠 // 향후 구현 방향에 따라 서버 블록 내의 Directives를 저장할 수 있도록 알맞게 수정해야함. - parseDirectives(lines, this->eventDirectives); + std::map > t; + parseDirectives(lines, t); while (lines.find('{', 0) != STRING_NPOS) { parseBlock(lines); } + a.push_back(t); return ; } @@ -187,18 +197,11 @@ void ConfigParser::parseBlock (std::string& lines) { parseHTTPBlock(blockContents.second); } if (!blockContents.first.compare("server")) { - parseServerBlock(blockContents.second); - } - if (blockContents.first.length() > 8 && !blockContents.first.find("location", 0, 8)) { - parseServerBlock(blockContents.second); + parseServerBlock(blockContents.second, this->serverBlocks); } } -void ConfigParser::showSizeDirectives(void) { - std::cout << "main :" << this->mainDirectives.size() << std::endl; - std::cout << "event :" << this->eventDirectives.size() << std::endl; - std::cout << "http :" << this->httpDirectives.size() << std::endl; -}; + void ConfigParser::parseConfigFile(void) { std::ifstream fs; @@ -221,5 +224,22 @@ void ConfigParser::parseConfigFile(void) { while (lines.find('{', 0) != STRING_NPOS) { parseBlock(lines); } - showSizeDirectives(); + test_map_data_print(); +} + +void test_serverBlock_print(std::vector > >& a) { + for (std::vector > >::iterator it = a.begin(); it != a.end(); it++) { + test::showParsedDirectives(*it); + } +} + +void ConfigParser::test_map_data_print(void) { + std::cout << "main -------------" << std::endl; + test::showParsedDirectives(this->mainDirectives); + std::cout << "events -------------" << std::endl; + test::showParsedDirectives(this->eventDirectives); + std::cout << "http -------------" << std::endl; + test::showParsedDirectives(this->httpDirectives); + std::cout << "server -------------" << std::endl; + test_serverBlock_print(this->serverBlocks); } From 277ea9434d777682aa4e4d79d325ae5ef40c7f26 Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Tue, 19 Apr 2022 19:54:11 +0900 Subject: [PATCH 4/7] =?UTF-8?q?:recycle:=20server=20=EB=B8=94=EB=A1=9D=20?= =?UTF-8?q?=EB=94=94=EB=A0=89=ED=8B=B0=EB=B8=8C=20=ED=8C=8C=EC=8B=B1=20?= =?UTF-8?q?=EC=9E=84=EC=8B=9C=20=EC=A0=80=EC=9E=A5=20=EB=B0=8F=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=B6=9C=EB=A0=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 제곧내입니다... --- src/ConfigParser.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index da662fb..7cbbfba 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -199,6 +199,9 @@ void ConfigParser::parseBlock (std::string& lines) { if (!blockContents.first.compare("server")) { parseServerBlock(blockContents.second, this->serverBlocks); } + if (blockContents.first.length() >= 8 && blockContents.first.find("location", 0) != STRING_NPOS) { + parseServerBlock(blockContents.second, this->serverBlocks); + } } From 01ee8b6b669951e29518b42ad13656e25ca59d82 Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Wed, 20 Apr 2022 10:36:42 +0900 Subject: [PATCH 5/7] =?UTF-8?q?:art:=20ConfigParser=20cpplint=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20&=20=EC=A7=9C=EC=9E=98=ED=95=9C=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 짜잘한 버그를 수정하고 린트룰에 맞추고 아침늦잠을 잤습니다.... --- includes/ConfigParser.hpp | 7 ++- src/ConfigParser.cpp | 118 +++++++++++++++++--------------------- 2 files changed, 59 insertions(+), 66 deletions(-) diff --git a/includes/ConfigParser.hpp b/includes/ConfigParser.hpp index d4aba98..121115f 100644 --- a/includes/ConfigParser.hpp +++ b/includes/ConfigParser.hpp @@ -1,3 +1,5 @@ + // Copyright [2022] + #ifndef INCLUDES_CONFIGPARSER_HPP_ #define INCLUDES_CONFIGPARSER_HPP_ @@ -21,13 +23,16 @@ class ConfigParser { void parseMainContext(std::string& lines); void parseEventsBlock(std::string& lines); void parseHTTPBlock(std::string& lines); - void parseServerBlock(std::string& lines, std::vector > >& a); + void parseUpstreamBlock(std::string& lines); + void parseServerBlock(std::string& lines, + std::vector > >& a); void test_map_data_print(void); const char *file_path_; std::map > mainDirectives; std::map > eventDirectives; std::map > httpDirectives; + std::map > upstreamDirectives; std::vector > > serverBlocks; }; diff --git a/src/ConfigParser.cpp b/src/ConfigParser.cpp index 7cbbfba..bc4cfe7 100644 --- a/src/ConfigParser.cpp +++ b/src/ConfigParser.cpp @@ -1,12 +1,12 @@ // Copyright [2022] -#include "../includes/ConfigParser.hpp" -#include "../includes/testFunctions.hpp" -#include "../includes/webserv.hpp" #include #include #include #include +#include "../includes/ConfigParser.hpp" +#include "../includes/testFunctions.hpp" +#include "../includes/webserv.hpp" #define space "\n\t " @@ -14,8 +14,7 @@ ConfigParser::ConfigParser() {} ConfigParser::ConfigParser(const char *file_path) { if (!*file_path) { file_path_ = DEFAULT_CONFIGFILE_PATH; - } - else { + } else { file_path_ = file_path; } } @@ -33,78 +32,67 @@ static void deleteComments(std::string& line) { } } -static bool isSpecialCharactersBlockKeyword(const char c) -{ - return (c == '~' || c == '^' || c == '/' || c == '(' || c == ')' || c == '_'); +static bool isSpecialCharactersBlockKeyword(const char c) { + return (c == '~' || c == '^' || c == '/' + || c == '(' || c == ')' || c == '_'); } -static std::string getConfigFileLines(std::ifstream& fs) -{ +static std::string getConfigFileLines(std::ifstream& fs) { std::string buf; std::string ret; - while (std::getline(fs, buf)) - { + while (std::getline(fs, buf)) { deleteComments(buf); ret += buf; } return ret; } -static bool isAllofBracketClosed(std::string& line) -{ +static bool isAllofBracketClosed(std::string& line) { std::stack bracket; - - if (line.find("{", 0) == STRING_NPOS) - return false; // needed throw exception needed http server config + if (line.find("{", 0) == STRING_NPOS) return false; // needed throw exception needed http server config for (std::string::iterator it = line.begin(); it != line.end(); it++) { - if (*it == '{') + if (*it == '{') { bracket.push(true); - else if (*it == '}') { - if (bracket.empty()) - return false; // throw exception unclosed bracket; + } else if (*it == '}') { + if (bracket.empty()) return false; // throw exception unclosed bracket; bracket.pop(); } } return bracket.empty() ? true : false; } -static size_t getLastBracketIndex(std::string& line) -{ +static size_t getLastBracketIndex(std::string& line) { size_t idx = 0; std::stack bracket; - while (idx < line.length()) { - if (line[idx] == '{') + if (line[idx] == '{') { bracket.push(true); - else if (line[idx] == '}') { + } else if (line[idx] == '}') { bracket.pop(); - if (bracket.empty()) { - break; - } + if (bracket.empty()) break; } idx++; } return idx; } +// 블록 파싱할 때 키워드 체크함수 +// 이곳에서 블록 키워드가 제대로 파싱이 안되어 있는 걸 체크합니다. +// 향후 허용되지 않는 키가 들어오는 경우는 여기에서 체크하도록 합시다. static bool isKeywordBlock(std::string& line) { std::string::iterator it = line.begin(); - for (; it != line.end(); it++) { - if (isalpha(*it)) - break ; - } - return it == line.end() ? false : true; + for (; it != line.end(); it++) if (isalpha(*it)) { break ; } + if (it == line.end()) + return false; + return true; } -static std::string getkeywordBlock(std::string& line) -{ +static std::string getkeywordBlock(std::string& line) { size_t start = 0; - while (!isalpha(line[start]) || line[start] == '}') - start++; + while (!isalpha(line[start]) || line[start] == '}') start++; size_t idx = line.find_first_of(" \t\r\n{", start); std::string ret = line.substr(start, idx - start); - if (!isKeywordBlock(ret)) - std::cout << "Error !!!!!!!!" << ret << "!!!!!!!!!!!" << std::endl; + if (!isKeywordBlock(ret)) std::cout << "Error !!!!!!!!" << ret << "!!!!!!!!!!!" << std::endl; // 키워드블록 파싱 에러 line.erase(0, ret.length()); return ret; } @@ -117,12 +105,10 @@ static std::string getvaluesBlock(std::string& lines) { return ret; } -static void parseDirectives(std::string& lines, std::map >& map) -{ +static void parseDirectives(std::string& lines, std::map >& map) { std::size_t idx_first_block = lines.find("{", (lines[0] == '{')); std::string tmp, key, value; - for (std::size_t base = 0, found = 0;;) - { + for (std::size_t base = 0, found = 0;;) { while (isspace(lines[base])) base++; found = lines.find(";", base); if (found > idx_first_block || found == STRING_NPOS) { @@ -135,8 +121,7 @@ static void parseDirectives(std::string& lines, std::map bucket; bucket.push_back(value); map.insert(make_pair(key, bucket)); - } - else { + } else { map.find(key)->second.push_back(value); } key.clear(); @@ -163,6 +147,10 @@ void ConfigParser::parseEventsBlock(std::string& lines) { parseDirectives(lines, this->eventDirectives); } +void ConfigParser::parseUpstreamBlock(std::string& lines) { + parseDirectives(lines, this->upstreamDirectives); +} + void ConfigParser::parseHTTPBlock(std::string& lines) { parseDirectives(lines, this->httpDirectives); while (lines.find('{', 0) != STRING_NPOS) { @@ -170,7 +158,8 @@ void ConfigParser::parseHTTPBlock(std::string& lines) { } } -void ConfigParser::parseServerBlock(std::string& lines, std::vector > >& a) { +void ConfigParser::parseServerBlock(std::string& lines, + std::vector > >& a) { // 두번째 인자는 로직 입증을 위해 임시로 넣어둠 // 향후 구현 방향에 따라 서버 블록 내의 Directives를 저장할 수 있도록 알맞게 수정해야함. std::map > t; @@ -178,16 +167,14 @@ void ConfigParser::parseServerBlock(std::string& lines, std::vector blockContents = make_pair(getkeywordBlock(lines), getvaluesBlock(lines)); - if (blockContents.first.length() == 0) - { + if (blockContents.first.length() == 0) { std::cout << "ak!" << std::endl; - return ; + return; } std::cout << blockContents.first << std::endl; if (!blockContents.first.compare("events")) { @@ -199,29 +186,27 @@ void ConfigParser::parseBlock (std::string& lines) { if (!blockContents.first.compare("server")) { parseServerBlock(blockContents.second, this->serverBlocks); } + if (!blockContents.first.compare("upstream")) { + parseUpstreamBlock(blockContents.second); + } if (blockContents.first.length() >= 8 && blockContents.first.find("location", 0) != STRING_NPOS) { parseServerBlock(blockContents.second, this->serverBlocks); } } - - void ConfigParser::parseConfigFile(void) { std::ifstream fs; std::string lines; - fs.open(file_path_, std::ios::in); - if (!fs.is_open()) - { + if (!fs.is_open()) { std::cerr << ERR_MSG_HAED << "file open err" << std::endl; - return ; + return; } lines = getConfigFileLines(fs); fs.close(); - if (isAllofBracketClosed(lines) == false) - { + if (isAllofBracketClosed(lines) == false) { std::cerr << ERR_MSG_HAED << "unclosed bracket" << std::endl; - return ; + return; } parseMainContext(lines); while (lines.find('{', 0) != STRING_NPOS) { @@ -231,7 +216,8 @@ void ConfigParser::parseConfigFile(void) { } void test_serverBlock_print(std::vector > >& a) { - for (std::vector > >::iterator it = a.begin(); it != a.end(); it++) { + std::vector > >::iterator it = a.begin(); + for (; it != a.end(); it++) { test::showParsedDirectives(*it); } } @@ -243,6 +229,8 @@ void ConfigParser::test_map_data_print(void) { test::showParsedDirectives(this->eventDirectives); std::cout << "http -------------" << std::endl; test::showParsedDirectives(this->httpDirectives); + std::cout << "upstream -------------" << std::endl; + test::showParsedDirectives(this->upstreamDirectives); std::cout << "server -------------" << std::endl; test_serverBlock_print(this->serverBlocks); } From b13a23545f2ce4c2b12d06d3b0008bbc5ef9515d Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Wed, 20 Apr 2022 22:02:28 +0900 Subject: [PATCH 6/7] =?UTF-8?q?:fire:=20testFunctions.hpp=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 불필요해진 파일 삭제 --- includes/testFunctions.hpp | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 includes/testFunctions.hpp diff --git a/includes/testFunctions.hpp b/includes/testFunctions.hpp deleted file mode 100644 index 3a6e9a6..0000000 --- a/includes/testFunctions.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef TESTFUNCTIONS_HPP -#define TESTFUNCTIONS_HPP - -#define TEST_MODE 0 -#ifndef TEST_MODE -#define TEST_MODE - -namespace test { - void showSizeDirectives(void) { - std::cout << "main :" << this->mainDirectives.size() << std::endl; - std::cout << "event :" << this->eventDirectives.size() << std::endl; - std::cout << "http :" << this->httpDirectives.size() << std::endl; - }; -} - -#endif // TEST_MODE - -#endif // TESTFUNCTIONS_HPP \ No newline at end of file From a9d6d76df7c0e4bd628afa1c209f6053210d4f2a Mon Sep 17 00:00:00 2001 From: KIM JEONGJUN Date: Wed, 20 Apr 2022 22:07:44 +0900 Subject: [PATCH 7/7] =?UTF-8?q?:white=5Fcheck=5Fmark:=20testFunctions.hpp?= =?UTF-8?q?=20=EC=9E=AC=EC=97=85=EB=A1=9C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 에러 없는 버전으로 다시 올라갑니다. --- includes/testFunctions.hpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 includes/testFunctions.hpp diff --git a/includes/testFunctions.hpp b/includes/testFunctions.hpp new file mode 100644 index 0000000..fb06d39 --- /dev/null +++ b/includes/testFunctions.hpp @@ -0,0 +1,22 @@ +#ifndef TESTFUNCTIONS_HPP +#define TESTFUNCTIONS_HPP + +#include +namespace test { + void showSizeDirectives(const std::map& m) { + std::cout << "size : " << m.size() << std::endl; + }; + void showParsedDirectives(std::map >& a) { + std::cout << "size : " << a.size() << std::endl; + for (std::map >::iterator it = a.begin(); it != a.end(); it++) { + std:: cout << it->first << " : [ "; + for (std::vector::iterator itv = it->second.begin(); itv != it->second.end(); itv++) { + std::cout << *itv << ", "; + } + std::cout << "]"<< std::endl; + } +}; + +} + +#endif // TESTFUNCTIONS_HPP \ No newline at end of file