parser: Loads content straight from buffer.

This commit is contained in:
Felipe Zimmerle 2015-07-23 01:10:19 -03:00
parent d3eb0fd913
commit 9c2158958e
4 changed files with 35 additions and 37 deletions

View File

@ -66,12 +66,10 @@ int Driver::addSecRule(ModSecurity::Rule *rule) {
int Driver::parse(const std::string &f) {
file = f;
buffer = f;
scan_begin();
yy::seclang_parser parser(*this);
parser.set_debug_level(trace_parsing);
// yy_scan_buffer
parser.set_debug_level(0);
int res = parser.parse();
if (audit_log->init() == false) {
@ -79,7 +77,7 @@ int Driver::parse(const std::string &f) {
}
scan_end();
return res;
return res == 0;
}
@ -90,10 +88,12 @@ int Driver::parseFile(const std::string &f) {
parser.set_debug_level(trace_parsing);
int res = parser.parse();
// std::cout << "Leaving the parser: " << res << std::endl;
if (audit_log->init() == false) {
return false;
}
scan_end();
return res;
return res == 0;
}
@ -112,3 +112,4 @@ void Driver::error(const yy::location& l, const std::string& m,
void Driver::parser_error(const yy::location& l, const std::string& m) {
parserError << ". " << m << "." << std::endl;
}

View File

@ -34,6 +34,7 @@
using ModSecurity::Rule;
using ModSecurity::Rules;
# define YY_DECL \
yy::seclang_parser::symbol_type yylex(Driver& driver)
@ -83,6 +84,9 @@ class Driver : public Rules {
void parser_error(const yy::location& l, const std::string& m);
void error(const yy::location& l, const std::string& m,
const std::string& c);
std::string buffer;
};
#endif // SRC_PARSER_DRIVER_H_

View File

@ -185,23 +185,24 @@ FREE_TEXT_NEW_LINE [^\"|\n]+
%%
void
Driver::scan_begin ()
{
yy_flex_debug = trace_scanning;
if (file.empty () || file == "-")
yyin = stdin;
else if (!(yyin = fopen (file.c_str (), "r")))
{
exit (EXIT_FAILURE);
void Driver::scan_begin () {
yy_flex_debug = trace_scanning;
if (buffer.empty() == false) {
yy_scan_string(buffer.c_str());
} else if (file.empty() == false) {
if (!(yyin = fopen (file.c_str (), "r"))) {
// FIXME: we should return a decent error.
exit (EXIT_FAILURE);
}
}
}
void Driver::scan_end () {
if (buffer.empty() == false) {
yy_scan_string(buffer.c_str());
} else if (file.empty() == false) {
fclose(yyin);
}
}
void
Driver::scan_end ()
{
fclose (yyin);
}

View File

@ -111,25 +111,17 @@ int Rules::loadRemote(char *key, char *uri) {
int Rules::load(const char *plain_rules) {
bool ret = true;
/**
* @todo rgg. we should make the parser work out of the buffer.
*
*/
std::ofstream myfile;
myfile.open("/tmp/modsec_ugly_hack.txt");
myfile << plain_rules;
myfile.close();
Driver *driver = new Driver();
if (driver->parse("/tmp/modsec_ugly_hack.txt")) {
ret = false;
if (driver->parse(plain_rules) == false) {
parserError << driver->parserError.rdbuf();
return false;
}
this->merge(driver);
delete driver;
return ret;
return true;
}