nicolas-arnaud
2 years ago
16 changed files with 564 additions and 573 deletions
@ -1,18 +1,17 @@ |
|||||
#pragma once |
#pragma once |
||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
|
|
||||
class JSONParser { |
class JSONParser { |
||||
std::fstream file; |
std::fstream file; |
||||
Tokenizer tokenizer; |
Tokenizer tokenizer; |
||||
|
|
||||
public: |
public: |
||||
JSONParser(const string filename); |
JSONParser(const string filename); |
||||
JSONNode *parse(); |
JSONNode *parse(); |
||||
JSONNode *parseObject(); |
JSONNode *parseObject(); |
||||
JSONNode *parseList(); |
JSONNode *parseList(); |
||||
JSONNode *parseString(); |
JSONNode *parseString(); |
||||
JSONNode *parseNumber(); |
JSONNode *parseNumber(); |
||||
JSONNode *parseBoolean(); |
JSONNode *parseBoolean(); |
||||
JSONNode *parseNull(); |
JSONNode *parseNull(); |
||||
}; |
}; |
||||
|
@ -1,111 +1,110 @@ |
|||||
|
|
||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
#define DEBUG 0 |
#define DEBUG 0 |
||||
|
|
||||
JSONObject JSONNode::obj() { |
JSONObject JSONNode::obj() { |
||||
if (type == OBJECT) |
if (type == OBJECT) |
||||
return *values.object; |
return *values.object; |
||||
throw std::logic_error("Improper return"); |
throw std::logic_error("Improper return"); |
||||
} |
} |
||||
JSONList JSONNode::lst() { |
JSONList JSONNode::lst() { |
||||
if (type == LIST) |
if (type == LIST) |
||||
return *values.list; |
return *values.list; |
||||
throw std::logic_error("Improper return"); |
throw std::logic_error("Improper return"); |
||||
} |
} |
||||
string JSONNode::str() { |
string JSONNode::str() { |
||||
if (type == STRING) |
if (type == STRING) |
||||
return *values.str; |
return *values.str; |
||||
throw std::logic_error("Improper return"); |
throw std::logic_error("Improper return"); |
||||
} |
} |
||||
int JSONNode::nbr() { |
int JSONNode::nbr() { |
||||
if (type == NUMBER) |
if (type == NUMBER) |
||||
return values.nbr; |
return values.nbr; |
||||
throw std::logic_error("Improper return"); |
throw std::logic_error("Improper return"); |
||||
} |
} |
||||
bool JSONNode::boo() { |
bool JSONNode::boo() { |
||||
if (type == BOOLEAN) |
if (type == BOOLEAN) |
||||
return values.bValue; |
return values.bValue; |
||||
throw std::logic_error("Improper return"); |
throw std::logic_error("Improper return"); |
||||
} |
} |
||||
|
|
||||
void JSONNode::setObject(JSONObject *object) { |
void JSONNode::setObject(JSONObject *object) { |
||||
this->values.object = object; |
this->values.object = object; |
||||
type = OBJECT; |
type = OBJECT; |
||||
} |
} |
||||
void JSONNode::setList(JSONList *list) { |
void JSONNode::setList(JSONList *list) { |
||||
this->values.list = list; |
this->values.list = list; |
||||
type = LIST; |
type = LIST; |
||||
} |
} |
||||
void JSONNode::setString(string *str) { |
void JSONNode::setString(string *str) { |
||||
this->values.str = str; |
this->values.str = str; |
||||
type = STRING; |
type = STRING; |
||||
} |
} |
||||
void JSONNode::setNumber(int nbr) { |
void JSONNode::setNumber(int nbr) { |
||||
this->values.nbr = nbr; |
this->values.nbr = nbr; |
||||
type = NUMBER; |
type = NUMBER; |
||||
} |
} |
||||
void JSONNode::setBoolean(bool v) { |
void JSONNode::setBoolean(bool v) { |
||||
this->values.bValue = v; |
this->values.bValue = v; |
||||
type = BOOLEAN; |
type = BOOLEAN; |
||||
} |
} |
||||
void JSONNode::setNull() { type = NULL_TYPE; } |
void JSONNode::setNull() { type = NULL_TYPE; } |
||||
|
|
||||
string JSONNode::stringify(int indentationLevel) { |
string JSONNode::stringify(int indentationLevel) { |
||||
string spaceString = string(indentationLevel, ' '); |
string spaceString = string(indentationLevel, ' '); |
||||
// sstreams
|
// sstreams
|
||||
std::stringstream output; |
std::stringstream output; |
||||
// cout < type << "\n";
|
// cout < type << "\n";
|
||||
switch (type) { |
switch (type) { |
||||
case STRING: { |
case STRING: { |
||||
output << spaceString << *values.str; |
output << spaceString << *values.str; |
||||
break; |
break; |
||||
} |
} |
||||
case NUMBER: { |
case NUMBER: { |
||||
output << spaceString << values.nbr; |
output << spaceString << values.nbr; |
||||
break; |
break; |
||||
} |
} |
||||
case BOOLEAN: { |
case BOOLEAN: { |
||||
output << spaceString << (values.bValue ? "true" : "false"); |
output << spaceString << (values.bValue ? "true" : "false"); |
||||
break; |
break; |
||||
} |
} |
||||
case NULL_TYPE: { |
case NULL_TYPE: { |
||||
output << spaceString << "null"; |
output << spaceString << "null"; |
||||
break; |
break; |
||||
} |
} |
||||
case LIST: { |
case LIST: { |
||||
// cout << "[";
|
// cout << "[";
|
||||
output << spaceString << "[\n"; |
output << spaceString << "[\n"; |
||||
unsigned int index = 0; |
unsigned int index = 0; |
||||
for (JSONList::iterator i = (*values.list).begin(); |
for (JSONList::iterator i = (*values.list).begin(); |
||||
i != (*values.list).end(); i++) { |
i != (*values.list).end(); i++) { |
||||
output << (*i)->stringify(indentationLevel + 1); |
output << (*i)->stringify(indentationLevel + 1); |
||||
if (index < (*values.list).size() - 1) { |
if (index < (*values.list).size() - 1) { |
||||
output << ",\n"; |
output << ",\n"; |
||||
} |
} |
||||
index++; |
index++; |
||||
}; |
}; |
||||
output << "\n" << spaceString << "]\n"; |
output << "\n" << spaceString << "]\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case OBJECT: { |
case OBJECT: { |
||||
output << spaceString << "{\n"; |
output << spaceString << "{\n"; |
||||
for (JSONObject::iterator i = (*values.object).begin(); |
for (JSONObject::iterator i = (*values.object).begin(); |
||||
i != (*values.object).end(); i++) { |
i != (*values.object).end(); i++) { |
||||
output << spaceString << " " |
output << spaceString << " " |
||||
<< "\"" << i->first << "\"" |
<< "\"" << i->first << "\"" |
||||
<< ": "; |
<< ": "; |
||||
output << i->second->stringify(indentationLevel + 1); |
output << i->second->stringify(indentationLevel + 1); |
||||
JSONObject::iterator next = i; |
JSONObject::iterator next = i; |
||||
next++; |
next++; |
||||
if ((next) != (*values.object).end()) { |
if ((next) != (*values.object).end()) { |
||||
output << ",\n"; |
output << ",\n"; |
||||
} |
} |
||||
output << spaceString << "\n"; |
output << spaceString << "\n"; |
||||
} |
} |
||||
output << spaceString << "}"; |
output << spaceString << "}"; |
||||
return output.str(); |
return output.str(); |
||||
} |
} |
||||
} |
} |
||||
return output.str(); |
return output.str(); |
||||
} |
} |
||||
|
@ -1,200 +1,199 @@ |
|||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
|
|
||||
JSONParser::JSONParser(const string filename) : tokenizer(filename) {} |
JSONParser::JSONParser(const string filename) : tokenizer(filename) {} |
||||
|
|
||||
JSONNode *JSONParser::parse() { |
JSONNode *JSONParser::parse() { |
||||
string key = ""; |
string key = ""; |
||||
JSONNode *parsed; |
JSONNode *parsed; |
||||
while (tokenizer.hasMoreTokens()) { |
while (tokenizer.hasMoreTokens()) { |
||||
Token token; |
Token token; |
||||
try { |
try { |
||||
token = tokenizer.getToken(); |
token = tokenizer.getToken(); |
||||
switch (token.type) { |
switch (token.type) { |
||||
case CURLY_OPEN: { |
case CURLY_OPEN: { |
||||
parsed = parseObject(); |
parsed = parseObject(); |
||||
break; |
break; |
||||
} |
} |
||||
case ARRAY_OPEN: { |
case ARRAY_OPEN: { |
||||
parsed = parseList(); |
parsed = parseList(); |
||||
break; |
break; |
||||
} |
} |
||||
case STRING: { |
case STRING: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
parsed = parseString(); |
parsed = parseString(); |
||||
break; |
break; |
||||
} |
} |
||||
case NUMBER: { |
case NUMBER: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
parsed = parseNumber(); |
parsed = parseNumber(); |
||||
break; |
break; |
||||
} |
} |
||||
case BOOLEAN: { |
case BOOLEAN: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
parsed = parseBoolean(); |
parsed = parseBoolean(); |
||||
break; |
break; |
||||
} |
} |
||||
default: |
default: |
||||
break; |
break; |
||||
} |
} |
||||
} catch (std::logic_error &e) { |
} catch (std::logic_error &e) { |
||||
break; |
break; |
||||
} |
} |
||||
} |
} |
||||
return parsed; |
return parsed; |
||||
} |
} |
||||
|
|
||||
JSONNode *JSONParser::parseObject() { |
JSONNode *JSONParser::parseObject() { |
||||
JSONNode *node = new JSONNode; |
JSONNode *node = new JSONNode; |
||||
JSONObject *keyObjectMap = new JSONObject; |
JSONObject *keyObjectMap = new JSONObject; |
||||
while (1) { |
while (1) { |
||||
if (tokenizer.hasMoreTokens()) { |
if (tokenizer.hasMoreTokens()) { |
||||
Token token = tokenizer.getToken(); |
Token token = tokenizer.getToken(); |
||||
string key = token.value; |
string key = token.value; |
||||
tokenizer.getToken(); |
tokenizer.getToken(); |
||||
token = tokenizer.getToken(); |
token = tokenizer.getToken(); |
||||
switch (token.type) { |
switch (token.type) { |
||||
case CURLY_OPEN: { |
case CURLY_OPEN: { |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "=object=|" << key << "|===>>\n"; |
cout << "=object=|" << key << "|===>>\n"; |
||||
(*keyObjectMap)[key] = parseObject(); |
(*keyObjectMap)[key] = parseObject(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "<<===end object\n"; |
cout << "<<===end object\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case ARRAY_OPEN: { |
case ARRAY_OPEN: { |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "-list-|" << key << "|--->>\n"; |
cout << "-list-|" << key << "|--->>\n"; |
||||
(*keyObjectMap)[key] = parseList(); |
(*keyObjectMap)[key] = parseList(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "<<---end list\n"; |
cout << "<<---end list\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case STRING: { |
case STRING: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
(*keyObjectMap)[key] = parseString(); |
(*keyObjectMap)[key] = parseString(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << key << "='" << (*keyObjectMap)[key]->str() << "'\n"; |
cout << key << "='" << (*keyObjectMap)[key]->str() << "'\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case NUMBER: { |
case NUMBER: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
(*keyObjectMap)[key] = parseNumber(); |
(*keyObjectMap)[key] = parseNumber(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << key << "=" << (*keyObjectMap)[key]->nbr() << "\n"; |
cout << key << "=" << (*keyObjectMap)[key]->nbr() << "\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case BOOLEAN: { |
case BOOLEAN: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
(*keyObjectMap)[key] = parseBoolean(); |
(*keyObjectMap)[key] = parseBoolean(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << key << "(BOOL)\n"; |
cout << key << "(BOOL)\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case NULL_TYPE: { |
case NULL_TYPE: { |
||||
(*keyObjectMap)[key] = parseNull(); |
(*keyObjectMap)[key] = parseNull(); |
||||
break; |
break; |
||||
} |
} |
||||
default: |
default: |
||||
break; |
break; |
||||
} |
} |
||||
token = tokenizer.getToken(); |
token = tokenizer.getToken(); |
||||
if (token.type == CURLY_CLOSE) |
if (token.type == CURLY_CLOSE) |
||||
break; |
break; |
||||
|
|
||||
} else { |
} else { |
||||
throw std::logic_error("No more tokens"); |
throw std::logic_error("No more tokens"); |
||||
} |
} |
||||
} |
} |
||||
node->setObject(keyObjectMap); |
node->setObject(keyObjectMap); |
||||
return node; |
return node; |
||||
} |
} |
||||
JSONNode *JSONParser::parseList() { |
JSONNode *JSONParser::parseList() { |
||||
JSONNode *node = new JSONNode(); |
JSONNode *node = new JSONNode(); |
||||
JSONList *list = new JSONList(); |
JSONList *list = new JSONList(); |
||||
bool hasCompleted = false; |
bool hasCompleted = false; |
||||
while (!hasCompleted) { |
while (!hasCompleted) { |
||||
if (!tokenizer.hasMoreTokens()) { |
if (!tokenizer.hasMoreTokens()) { |
||||
throw std::logic_error("No more tokens"); |
throw std::logic_error("No more tokens"); |
||||
} else { |
} else { |
||||
Token token = tokenizer.getToken(); |
Token token = tokenizer.getToken(); |
||||
JSONNode *subNode; |
JSONNode *subNode; |
||||
switch (token.type) { |
switch (token.type) { |
||||
case CURLY_OPEN: { |
case CURLY_OPEN: { |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "=object===>>\n"; |
cout << "=object===>>\n"; |
||||
subNode = parseObject(); |
subNode = parseObject(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "<<===end object\n"; |
cout << "<<===end object\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case ARRAY_OPEN: { |
case ARRAY_OPEN: { |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "-list--->>\n"; |
cout << "-list--->>\n"; |
||||
subNode = parseList(); |
subNode = parseList(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "<<---end list\n"; |
cout << "<<---end list\n"; |
||||
break; |
break; |
||||
} |
} |
||||
case STRING: { |
case STRING: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
subNode = parseString(); |
subNode = parseString(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "|'" << subNode->str() << "'"; |
cout << "|'" << subNode->str() << "'"; |
||||
break; |
break; |
||||
} |
} |
||||
case NUMBER: { |
case NUMBER: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
subNode = parseNumber(); |
subNode = parseNumber(); |
||||
if (DEBUG) |
if (DEBUG) |
||||
cout << "|" << subNode->nbr(); |
cout << "|" << subNode->nbr(); |
||||
break; |
break; |
||||
} |
} |
||||
case BOOLEAN: { |
case BOOLEAN: { |
||||
tokenizer.rollBackToken(); |
tokenizer.rollBackToken(); |
||||
subNode = parseBoolean(); |
subNode = parseBoolean(); |
||||
break; |
break; |
||||
} |
} |
||||
case NULL_TYPE: { |
case NULL_TYPE: { |
||||
subNode = parseNull(); |
subNode = parseNull(); |
||||
break; |
break; |
||||
} |
} |
||||
default: |
default: |
||||
break; |
break; |
||||
} |
} |
||||
list->push_back(subNode); |
list->push_back(subNode); |
||||
token = tokenizer.getToken(); |
token = tokenizer.getToken(); |
||||
if (token.type == ARRAY_CLOSE) { |
if (token.type == ARRAY_CLOSE) { |
||||
hasCompleted = true; |
hasCompleted = true; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
node->setList(list); |
node->setList(list); |
||||
return node; |
return node; |
||||
} |
} |
||||
JSONNode *JSONParser::parseString() { |
JSONNode *JSONParser::parseString() { |
||||
JSONNode *node = new JSONNode(); |
JSONNode *node = new JSONNode(); |
||||
Token token = tokenizer.getToken(); |
Token token = tokenizer.getToken(); |
||||
string *sValue = new string(token.value); |
string *sValue = new string(token.value); |
||||
node->setString(sValue); |
node->setString(sValue); |
||||
return node; |
return node; |
||||
} |
} |
||||
JSONNode *JSONParser::parseNumber() { |
JSONNode *JSONParser::parseNumber() { |
||||
JSONNode *node = new JSONNode(); |
JSONNode *node = new JSONNode(); |
||||
Token token = tokenizer.getToken(); |
Token token = tokenizer.getToken(); |
||||
string value = token.value; |
string value = token.value; |
||||
int nbr = std::atoi(value.c_str()); |
int nbr = std::atoi(value.c_str()); |
||||
node->setNumber(nbr); |
node->setNumber(nbr); |
||||
return node; |
return node; |
||||
} |
} |
||||
JSONNode *JSONParser::parseBoolean() { |
JSONNode *JSONParser::parseBoolean() { |
||||
JSONNode *node = new JSONNode(); |
JSONNode *node = new JSONNode(); |
||||
Token token = tokenizer.getToken(); |
Token token = tokenizer.getToken(); |
||||
node->setBoolean(token.value == "True" ? true : false); |
node->setBoolean(token.value == "True" ? true : false); |
||||
return node; |
return node; |
||||
} |
} |
||||
JSONNode *JSONParser::parseNull() { |
JSONNode *JSONParser::parseNull() { |
||||
JSONNode *node = new JSONNode(); |
JSONNode *node = new JSONNode(); |
||||
node->setNull(); |
node->setNull(); |
||||
return node; |
return node; |
||||
} |
} |
||||
|
@ -1,111 +1,97 @@ |
|||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
/*
|
|
||||
enum TOKEN { |
|
||||
CURLY_OPEN, |
|
||||
CURLY_CLOSE, |
|
||||
COLON, |
|
||||
STRING, |
|
||||
NUMBER, |
|
||||
ARRAY_OPEN, |
|
||||
ARRAY_CLOSE, |
|
||||
COMMA, |
|
||||
BOOLEAN, |
|
||||
NULL_TYPE |
|
||||
}; |
|
||||
*/ |
|
||||
|
|
||||
Tokenizer::Tokenizer(string fileName) { |
Tokenizer::Tokenizer(string fileName) { |
||||
file.open(fileName.c_str(), std::ios::in); |
file.open(fileName.c_str(), std::ios::in); |
||||
if (!file.good()) |
if (!file.good()) |
||||
cout << "File open error" |
cout << "File open error" |
||||
<< "\n"; |
<< "\n"; |
||||
} |
} |
||||
bool Tokenizer::hasMoreTokens() { return !file.eof(); } |
bool Tokenizer::hasMoreTokens() { return !file.eof(); } |
||||
|
|
||||
char Tokenizer::getWithoutWhiteSpace() { |
char Tokenizer::getWithoutWhiteSpace() { |
||||
char c = ' '; |
char c = ' '; |
||||
while ((c == ' ' || c == '\n') || c == '\t') { |
while ((c == ' ' || c == '\n') || c == '\t') { |
||||
file.get(c); // check
|
file.get(c); // check
|
||||
|
|
||||
if ((c == ' ' || c == '\n') && !file.good()) { |
if ((c == ' ' || c == '\n') && !file.good()) { |
||||
// cout << file.eof() << " " << file.fail() << "\n";
|
// cout << file.eof() << " " << file.fail() << "\n";
|
||||
throw std::logic_error("Ran out of tokens"); |
throw std::logic_error("Ran out of tokens"); |
||||
} else if (!file.good()) { |
} else if (!file.good()) { |
||||
return c; |
return c; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
return c; |
return c; |
||||
} |
} |
||||
void Tokenizer::rollBackToken() { |
void Tokenizer::rollBackToken() { |
||||
if (file.eof()) |
if (file.eof()) |
||||
file.clear(); |
file.clear(); |
||||
file.seekg(prevPos); |
file.seekg(prevPos); |
||||
} |
} |
||||
Token Tokenizer::getToken() { |
Token Tokenizer::getToken() { |
||||
char c; |
char c; |
||||
if (file.eof()) { |
if (file.eof()) { |
||||
cout << "Exhaused tokens" |
cout << "Exhaused tokens" |
||||
<< "\n"; |
<< "\n"; |
||||
// throw std::exception("Exhausted tokens");
|
// throw std::exception("Exhausted tokens");
|
||||
} |
} |
||||
prevPos = file.tellg(); |
prevPos = file.tellg(); |
||||
c = getWithoutWhiteSpace(); |
c = getWithoutWhiteSpace(); |
||||
|
|
||||
Token token; |
Token token; |
||||
token.type = NULL_TYPE; |
token.type = NULL_TYPE; |
||||
if (c == '"') { |
if (c == '"') { |
||||
token.type = STRING; |
token.type = STRING; |
||||
token.value = ""; |
token.value = ""; |
||||
file.get(c); |
file.get(c); |
||||
while (c != '"') { |
while (c != '"') { |
||||
token.value += c; |
token.value += c; |
||||
file.get(c); |
file.get(c); |
||||
} |
} |
||||
} else if (c == '{') { |
} else if (c == '{') { |
||||
token.type = CURLY_OPEN; |
token.type = CURLY_OPEN; |
||||
} else if (c == '}') { |
} else if (c == '}') { |
||||
token.type = CURLY_CLOSE; |
token.type = CURLY_CLOSE; |
||||
} else if (c == '-' || (c >= '0' && c <= '9')) { |
} else if (c == '-' || (c >= '0' && c <= '9')) { |
||||
// Check if string is numeric
|
// Check if string is numeric
|
||||
token.type = NUMBER; |
token.type = NUMBER; |
||||
token.value = ""; |
token.value = ""; |
||||
token.value += c; |
token.value += c; |
||||
std::streampos prevCharPos = file.tellg(); |
std::streampos prevCharPos = file.tellg(); |
||||
while ((c == '-') || (c >= '0' && c <= '9') || c == '.') { |
while ((c == '-') || (c >= '0' && c <= '9') || c == '.') { |
||||
prevCharPos = file.tellg(); |
prevCharPos = file.tellg(); |
||||
file.get(c); |
file.get(c); |
||||
|
|
||||
if (file.eof()) { |
if (file.eof()) { |
||||
break; |
break; |
||||
} else { |
} else { |
||||
if ((c == '-') || (c >= '0' && c <= '9') || (c == '.')) { |
if ((c == '-') || (c >= '0' && c <= '9') || (c == '.')) { |
||||
token.value += c; |
token.value += c; |
||||
} else { |
} else { |
||||
file.seekg(prevCharPos); |
file.seekg(prevCharPos); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
} else if (c == 'f') { |
} else if (c == 'f') { |
||||
token.type = BOOLEAN; |
token.type = BOOLEAN; |
||||
token.value = "False"; |
token.value = "False"; |
||||
file.seekg(4, std::ios_base::cur); |
file.seekg(4, std::ios_base::cur); |
||||
} else if (c == 't') { |
} else if (c == 't') { |
||||
token.type = BOOLEAN; |
token.type = BOOLEAN; |
||||
token.value = "True"; |
token.value = "True"; |
||||
file.seekg(3, std::ios_base::cur); |
file.seekg(3, std::ios_base::cur); |
||||
} else if (c == 'n') { |
} else if (c == 'n') { |
||||
token.type = NULL_TYPE; |
token.type = NULL_TYPE; |
||||
file.seekg(3, std::ios_base::cur); |
file.seekg(3, std::ios_base::cur); |
||||
} else if (c == '[') { |
} else if (c == '[') { |
||||
token.type = ARRAY_OPEN; |
token.type = ARRAY_OPEN; |
||||
} else if (c == ']') { |
} else if (c == ']') { |
||||
token.type = ARRAY_CLOSE; |
token.type = ARRAY_CLOSE; |
||||
} else if (c == ':') { |
} else if (c == ':') { |
||||
token.type = COLON; |
token.type = COLON; |
||||
} else if (c == ',') { |
} else if (c == ',') { |
||||
token.type = COMMA; |
token.type = COMMA; |
||||
} |
} |
||||
// cout << token.type << " : " << token.value << "\n";
|
// cout << token.type << " : " << token.value << "\n";
|
||||
return token; |
return token; |
||||
} |
} |
||||
|
@ -1,30 +1,34 @@ |
|||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
Env::Env(JSONNode *conf) { |
Env::Env(JSONNode *conf) { |
||||
JSONList servers = conf->obj()["servers"]->lst(); |
try { |
||||
int i = 0; |
JSONList servers = conf->obj()["servers"]->lst(); |
||||
string th[8] = {"first", "second", "third", "fourth", |
int i = 0; |
||||
"fifth", "sixth", "seventh", "eigth"}; |
string th[8] = {"first", "second", "third", "fourth", |
||||
for (std::vector<JSONNode *>::iterator it = servers.begin(); |
"fifth", "sixth", "seventh", "eigth"}; |
||||
it < servers.end(); it++) { |
for (std::vector<JSONNode *>::iterator it = servers.begin(); |
||||
Server *server = new Server(*it); |
it < servers.end(); it++) { |
||||
_servers.push_back(server); |
Server *server = new Server(*it); |
||||
// delete *it;
|
_servers.push_back(server); |
||||
cout << th[i] << " server launched.\n"; |
// delete *it;
|
||||
i++; |
cout << th[i] << " server launched.\n"; |
||||
} |
i++; |
||||
// delete conf;
|
} |
||||
|
} catch (std::exception &e) { |
||||
|
cout << e.what(); |
||||
|
} |
||||
|
delete conf; |
||||
} |
} |
||||
void Env::set_fds() { |
void Env::set_fds() { |
||||
for (std::vector<Server *>::iterator it = _servers.begin(); |
for (std::vector<Server *>::iterator it = _servers.begin(); |
||||
it < _servers.end(); it++) { |
it < _servers.end(); it++) { |
||||
(*it)->set_fds(); |
(*it)->set_fds(); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
void Env::refresh() { |
void Env::refresh() { |
||||
for (std::vector<Server *>::iterator it = _servers.begin(); |
for (std::vector<Server *>::iterator it = _servers.begin(); |
||||
it < _servers.end(); it++) { |
it < _servers.end(); it++) { |
||||
(*it)->refresh(); |
(*it)->refresh(); |
||||
} |
} |
||||
} |
} |
||||
|
@ -1,50 +1,51 @@ |
|||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
Server::Server(JSONNode *server):Route(server) { |
Server::Server(JSONNode *server) : Route(server) { |
||||
JSONObject datas = server->obj(); |
JSONObject datas = server->obj(); |
||||
if (datas["server_name"]) |
if (datas["server_name"]) |
||||
_name = datas["server_name"]->str(); |
_name = datas["server_name"]->str(); |
||||
if (datas["listens"]) { |
if (datas["listens"]) { |
||||
JSONList listens = datas["listens"]->lst(); |
JSONList listens = datas["listens"]->lst(); |
||||
for (JSONList::iterator it = listens.begin(); it < listens.end(); it++) { |
for (JSONList::iterator it = listens.begin(); it < listens.end(); |
||||
Socket *sock = new Socket(this, (*it)->str()); |
it++) { |
||||
if (sock->launch() == EXIT_SUCCESS) |
Socket *sock = new Socket(this, (*it)->str()); |
||||
_sockets.push_back(sock); |
if (sock->launch() == EXIT_SUCCESS) |
||||
|
_sockets.push_back(sock); |
||||
else |
else |
||||
delete sock; |
delete sock; |
||||
} |
} |
||||
} |
} |
||||
if (datas["locations"]) { |
if (datas["locations"]) { |
||||
JSONObject locations = datas["locations"]->obj(); |
JSONObject locations = datas["locations"]->obj(); |
||||
for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) { |
for (JSONObject::iterator it = locations.begin(); it != locations.end(); |
||||
|
it++) { |
||||
Route *route = new Route((*it).second); |
Route *route = new Route((*it).second); |
||||
_routes[(*it).first] = route; |
_routes[(*it).first] = route; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
Server::~Server(void) { |
Server::~Server(void) { cout << "Server destroyed!\n"; } |
||||
cout << "Server destroyed!\n"; |
|
||||
} |
|
||||
|
|
||||
void Server::set_fds(void) { |
void Server::set_fds(void) { |
||||
for (std::vector<Socket *>::iterator it = _sockets.begin(); |
for (std::vector<Socket *>::iterator it = _sockets.begin(); |
||||
it < _sockets.end(); it++) { |
it < _sockets.end(); it++) { |
||||
(*it)->set_fds(); |
(*it)->set_fds(); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
void Server::refresh(void) { |
void Server::refresh(void) { |
||||
for (std::vector<Socket *>::iterator it = _sockets.begin(); |
for (std::vector<Socket *>::iterator it = _sockets.begin(); |
||||
it < _sockets.end(); it++) { |
it < _sockets.end(); it++) { |
||||
(*it)->refresh(); |
(*it)->refresh(); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
Route *Server::get_route(string uri) { |
Route *Server::get_route(string uri) { |
||||
for (std::map<string, Route *>::iterator it = _routes.begin(); it != _routes.end(); it++) { |
for (std::map<string, Route *>::iterator it = _routes.begin(); |
||||
|
it != _routes.end(); it++) { |
||||
if (uri == (*it).first) |
if (uri == (*it).first) |
||||
return (*it).second; |
return (*it).second; |
||||
} |
} |
||||
return this; |
return this; |
||||
} |
} |
||||
|
@ -1,18 +1,19 @@ |
|||||
#include "webserv.hpp" |
#include "webserv.hpp" |
||||
|
|
||||
void *ft_memset(void *b, int c, size_t len) { |
void *ft_memset(void *b, int c, size_t len) { |
||||
size_t i; |
size_t i; |
||||
unsigned char *b_cpy; |
unsigned char *b_cpy; |
||||
|
|
||||
b_cpy = (unsigned char *)b; |
b_cpy = (unsigned char *)b; |
||||
i = 0; |
i = 0; |
||||
while (i < len) |
while (i < len) |
||||
*(unsigned char *)(b_cpy + i++) = (unsigned char)c; |
*(unsigned char *)(b_cpy + i++) = (unsigned char)c; |
||||
return ((void *)b); |
return ((void *)b); |
||||
} |
} |
||||
|
|
||||
bool isInt(string str) { |
bool isInt(string str) { |
||||
for (string::iterator it = str.begin(); it < str.end(); it++) |
for (string::iterator it = str.begin(); it < str.end(); it++) |
||||
if (*it < '0' || *it > '9') return false; |
if (*it < '0' || *it > '9') |
||||
|
return false; |
||||
return true; |
return true; |
||||
} |
} |
||||
|
Loading…
Reference in new issue