-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigParser.cpp
More file actions
236 lines (215 loc) · 7.68 KB
/
ConfigParser.cpp
File metadata and controls
236 lines (215 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright [2022] <Two-Jay>
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include "../includes/ConfigParser.hpp"
#include "../includes/testFunctions.hpp"
#include "../includes/webserv.hpp"
#define space "\n\t "
ConfigParser::ConfigParser() {}
ConfigParser::ConfigParser(const char *file_path) {
if (!*file_path) {
file_path_ = DEFAULT_CONFIGFILE_PATH;
} else {
file_path_ = file_path;
}
}
ConfigParser::ConfigParser(const ConfigParser& other) { (void)other; }
ConfigParser::~ConfigParser() {}
ConfigParser& ConfigParser::operator= (const ConfigParser& other) {
(void)other;
return *this;
}
static void deleteComments(std::string& line) {
size_t idx = line.find("#");
if (idx != STRING_NPOS) {
line.erase(idx, line.length() - idx);
}
}
static bool isSpecialCharactersBlockKeyword(const char c) {
return (c == '~' || c == '^' || c == '/'
|| c == '(' || c == ')' || c == '_');
}
static std::string getConfigFileLines(std::ifstream& fs) {
std::string buf;
std::string ret;
while (std::getline(fs, buf)) {
deleteComments(buf);
ret += buf;
}
return ret;
}
static bool isAllofBracketClosed(std::string& line) {
std::stack<bool> bracket;
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 == '{') {
bracket.push(true);
} 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) {
size_t idx = 0;
std::stack<bool> bracket;
while (idx < line.length()) {
if (line[idx] == '{') {
bracket.push(true);
} else if (line[idx] == '}') {
bracket.pop();
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 ; }
if (it == line.end())
return false;
return true;
}
static std::string getkeywordBlock(std::string& line) {
size_t start = 0;
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; // 키워드블록 파싱 에러
line.erase(0, ret.length());
return ret;
}
static std::string getvaluesBlock(std::string& lines) {
size_t bracket_end_idx = getLastBracketIndex(lines);
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<std::string, std::vector<std::string> >& map) {
std::size_t idx_first_block = lines.find("{", (lines[0] == '{'));
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++;
}
if (map.find(key) == map.end()) {
std::vector<std::string> 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;
}
}
void ConfigParser::parseMainContext(std::string& lines) {
parseDirectives(lines, this->mainDirectives);
}
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) {
parseBlock(lines);
}
}
void ConfigParser::parseServerBlock(std::string& lines,
std::vector<std::map<std::string, std::vector<std::string> > >& a) {
// 두번째 인자는 로직 입증을 위해 임시로 넣어둠
// 향후 구현 방향에 따라 서버 블록 내의 Directives를 저장할 수 있도록 알맞게 수정해야함.
std::map<std::string, std::vector<std::string> > t;
parseDirectives(lines, t);
while (lines.find('{', 0) != STRING_NPOS) {
parseBlock(lines);
}
if (t.size() != 0) a.push_back(t);
}
void ConfigParser::parseBlock(std::string& lines) {
std::pair<std::string, std::string> blockContents = make_pair(getkeywordBlock(lines), getvaluesBlock(lines));
if (blockContents.first.length() == 0) {
std::cout << "ak!" << std::endl;
return;
}
std::cout << blockContents.first << std::endl;
if (!blockContents.first.compare("events")) {
parseEventsBlock(blockContents.second);
}
if (!blockContents.first.compare("http")) {
parseHTTPBlock(blockContents.second);
}
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()) {
std::cerr << ERR_MSG_HAED << "file open err" << std::endl;
return;
}
lines = getConfigFileLines(fs);
fs.close();
if (isAllofBracketClosed(lines) == false) {
std::cerr << ERR_MSG_HAED << "unclosed bracket" << std::endl;
return;
}
parseMainContext(lines);
while (lines.find('{', 0) != STRING_NPOS) {
parseBlock(lines);
}
test_map_data_print();
}
void test_serverBlock_print(std::vector<std::map<std::string, std::vector<std::string> > >& a) {
std::vector<std::map<std::string, std::vector<std::string> > >::iterator it = a.begin();
for (; 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 << "upstream -------------" << std::endl;
test::showParsedDirectives(this->upstreamDirectives);
std::cout << "server -------------" << std::endl;
test_serverBlock_print(this->serverBlocks);
}