diff --git a/default.json b/default.json index b23d2c4..cf702e3 100644 --- a/default.json +++ b/default.json @@ -21,7 +21,7 @@ "indexs": ["basique.html"], "cgi": { ".php": "/usr/bin/php", - ".py": "/usr/bin/python + ".py": "/usr/bin/python" }, "client_max_body_size": 10000, "locations": { diff --git a/srcs/json/Parser.cpp b/srcs/json/Parser.cpp index 2f28703..d06cffb 100644 --- a/srcs/json/Parser.cpp +++ b/srcs/json/Parser.cpp @@ -15,15 +15,18 @@ JSONNode *JSONParser::parse() { JSONNode *JSONParser::parseObject() { JSONNode *node = new JSONNode; JSONObject *keyObjectMap = new JSONObject; + node->setObj(keyObjectMap); try { while (1) { if (!tokenizer.hasMoreTokens()) { delete node; throw std::logic_error("No more tokens"); } - Token token = tokenizer.getToken(); + Token token = tokenizer.getToken(); + if (token.type != STRING) throw std::logic_error("Invalid json syntax: object key isn't a string"); string key = token.value; - tokenizer.getToken(); + token = tokenizer.getToken(); + if (token.type != COLON) throw std::logic_error("Invalid json syntax: missing colon"); token = tokenizer.getToken(); switch (token.type) { case CURLY_OPEN: { @@ -61,22 +64,22 @@ JSONNode *JSONParser::parseObject() { break; } default: + throw std::logic_error("Invalid json syntax: Invalid object member type"); break; } token = tokenizer.getToken(); if (token.type == CURLY_CLOSE) break; } - node->setObj(keyObjectMap); return node; } catch (std::exception &e) { delete node; - std::cout << e.what(); - throw std::runtime_error("."); + throw std::logic_error(e.what()); } } JSONNode *JSONParser::parseList() { JSONNode *node = new JSONNode(); JSONList *list = new JSONList(); + node->setLst(list); try { bool hasCompleted = false; @@ -122,18 +125,17 @@ JSONNode *JSONParser::parseList() { break; } default: + throw std::logic_error("Invalid json syntax: Invalid list member type"); break; } list->push_back(subNode); token = tokenizer.getToken(); if (token.type == ARRAY_CLOSE) { hasCompleted = true; } } - node->setLst(list); return node; } catch (std::exception &e) { delete node; - std::cout << e.what(); - throw std::runtime_error("."); + throw std::logic_error(e.what()); } } JSONNode *JSONParser::parseString() { @@ -143,10 +145,9 @@ JSONNode *JSONParser::parseString() { string *sValue = new string(token.value); node->setStr(sValue); return node; - } catch (std::exception &e) { delete node; - throw std::runtime_error("."); + throw std::logic_error(e.what()); } } JSONNode *JSONParser::parseNumber() { @@ -159,7 +160,7 @@ JSONNode *JSONParser::parseNumber() { return node; } catch (std::exception &e) { delete node; - throw std::runtime_error("."); + throw std::logic_error(e.what()); } } JSONNode *JSONParser::parseBoolean() { @@ -171,7 +172,7 @@ JSONNode *JSONParser::parseBoolean() { return node; } catch (std::exception &e) { delete node; - throw std::runtime_error("."); + throw std::logic_error(e.what()); } } JSONNode *JSONParser::parseNull() { @@ -181,6 +182,6 @@ JSONNode *JSONParser::parseNull() { return node; } catch (std::exception &e) { delete node; - throw std::runtime_error("."); + throw std::logic_error(e.what()); } } diff --git a/srcs/json/Token.cpp b/srcs/json/Token.cpp index c5d9152..e1d8c3f 100644 --- a/srcs/json/Token.cpp +++ b/srcs/json/Token.cpp @@ -43,6 +43,7 @@ Token Tokenizer::getToken() { token.value = ""; file.get(c); while (c != '"') { + if (c == '}' || c == ']' || c == ',') throw std::logic_error("Invalid json syntax: a string is not close"); token.value += c; file.get(c); } diff --git a/srcs/webserv.cpp b/srcs/webserv.cpp index 373b967..112ef00 100644 --- a/srcs/webserv.cpp +++ b/srcs/webserv.cpp @@ -40,7 +40,7 @@ int main(int ac, char **av) { Env env(conf); while (1) env.cycle(); } catch (const std::exception &e) { - std::cerr << e.what() << '\n'; + std::cerr << e.what(); return EXIT_FAILURE; } return EXIT_SUCCESS;