-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
182 lines (161 loc) · 6.44 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
// PREPROCESS:
// rimuovere i commenti
// rimuovere le righe vuote
enum STATES{
WAITING_STATE,
SERVER_DIRECTIVE_STATE,
LOCATION_DIRECTIVE_STATE,
STATE4,
STATE5
};
// Server{
// serverDirectives: [listen:8001][host:120.0.0.1] ...
// locations{
// /tours[root docs/fusion_web][autoindex on] ...
// /cgi-bin[][][]
// /images[][][]
// }
// }
// Server{
// serverDirectives: [listen:8001][host:120.0.0.1] ...
// locations{
// /tours[root docs/fusion_web][autoindex on] ...
// /cgi-bin[][][]
// /images[][][]
// }
// }
class Server{
public:
std::map<std::string, std::vector<std::string> > directives;
std::map<std::string, std::map<std::string, std::vector<std::string> > > locations;
};
std::string trim(const std::string& str) {
bool isOnlySpaces = true;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
if (*it != ' ') {
isOnlySpaces = false;
break;
}
}
if (isOnlySpaces)
return "";
size_t first = str.find_first_not_of(' ');
if (first == std::string::npos)
return str;
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}
int main(){
std::ifstream file("./nginx.conf");
if (!file) {
std::cerr << "Unable to open file nginx.conf" << std::endl;
return 1;
}
std::vector<Server*> serverVector;
bool serverOpen = false;
bool locationOpen = false;
std::string locationPath;
int serverCounter = 0;
std::string line;
int state = WAITING_STATE;
while (std::getline(file, line)) {
//rimuovere i commenti
line = line.substr(0, line.find("#"));
//rimuovere le righe vuote
line = trim(line);
if(line.empty()){continue;}
switch (state){
case WAITING_STATE: // waiting state
if(line.find("}") != std::string::npos){
break;
}
if(line == "server {"){
//create new server
serverOpen = true;
Server *server = new Server();
serverVector.push_back(server);
serverCounter++;
state = SERVER_DIRECTIVE_STATE;
continue;
}
throw std::runtime_error("Errore di parsing: server { non trovato");
case SERVER_DIRECTIVE_STATE: // server directives
if(line.find("server {") != std::string::npos && serverOpen){
throw std::runtime_error("Errore di parsing: server { non chiuso");
}
if(line.find(";") != std::string::npos){
std::string key = trim(line.substr(0, line.find(" ")));
//TODO handle multiple values
//Issue URL: https://github.com/PapaLeoneIV/42WebServer/issues/4
std::string value = trim(line.substr(line.find(" ") + 1, line.find(";") - line.find(" ") - 1));
serverVector[serverCounter - 1]->directives[key].push_back(value);
continue;
}
if(line.back() == '{' && line.find("location") != std::string::npos){
locationOpen = true;
locationPath = trim(line.substr(line.find("location") + 8,
line.find("{") - line.find("location") - 8));
//create entry into map of location using locationPath as key
serverVector[serverCounter - 1]->locations[locationPath] = std::map<std::string, std::vector<std::string> >();
state = LOCATION_DIRECTIVE_STATE;
continue;
}
if(line.find("}") != std::string::npos){
serverOpen = false;
state = WAITING_STATE;
continue;
}
case LOCATION_DIRECTIVE_STATE: // location directives
if(line.back() == '{' && line.find("location") != std::string::npos && locationOpen){
throw std::runtime_error("Errore di parsing: location { non chiuso");
}
if(line.find(";") != std::string::npos){
std::string key = trim(line.substr(0, line.find(" ")));
//TODO handle multiple values
//Issue URL: https://github.com/PapaLeoneIV/42WebServer/issues/3
std::string value = trim(line.substr(line.find(" ") + 1, line.find(";") - line.find(" ") - 1));
//add entry into map of location
serverVector[serverCounter - 1]->locations[locationPath][key].push_back(value);
continue;
}
if(line.back() == '}'){
locationOpen = false;
state = SERVER_DIRECTIVE_STATE;
continue;
}
default:
continue;
}
}
for(std::vector<Server*>::iterator it = serverVector.begin(); it != serverVector.end(); ++it){
Server* server = *it;
std::cout << "Server: " << std::endl;
for(std::map<std::string, std::vector<std::string> >::iterator dirIt = server->directives.begin(); dirIt != server->directives.end(); ++dirIt){
std::cout << " " <<dirIt->first << " : ";
for(std::vector<std::string>::iterator valIt = dirIt->second.begin(); valIt != dirIt->second.end(); ++valIt){
std::cout << *valIt << " ";
}
std::cout << std::endl;
}
std::map<std::string, std::map<std::string, std::vector<std::string> > >::iterator locationMapIterator = server->locations.begin();
for(; locationMapIterator != server->locations.end(); ++locationMapIterator){
std::string path = locationMapIterator->first;
std::cout << " Location: " << path << std::endl;
std::map<std::string, std::vector<std::string> >::iterator ValueVector = locationMapIterator->second.begin();
for(; ValueVector != locationMapIterator->second.end(); ++ValueVector){
std::cout << " " << ValueVector->first << " : ";
std::vector<std::string>::iterator Value = ValueVector->second.begin();
for(; Value != ValueVector->second.end(); ++Value){
std::cout << *Value;
}
std::cout << std::endl;
}
}
}
file.close();
}