Skip to content

Commit

Permalink
Cleanup codes
Browse files Browse the repository at this point in the history
inja: Using a better implementation for stripping blanks for expressions.
  • Loading branch information
tindy2013 committed Aug 11, 2020
1 parent a90ceb4 commit 65ac33d
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 499 deletions.
48 changes: 24 additions & 24 deletions include/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,9 @@ struct LexerConfig {
std::string statement_close_force_rstrip {"-%}"};
std::string line_statement {"##"};
std::string expression_open {"{{"};
std::string expression_open_force_lstrip {"{{-"};
std::string expression_close {"}}"};
std::string expression_close_force_rstrip {"-}}"};
std::string comment_open {"{#"};
std::string comment_close {"#}"};
std::string open_chars {"#{"};
Expand All @@ -1485,6 +1487,9 @@ struct LexerConfig {
if (open_chars.find(expression_open[0]) == std::string::npos) {
open_chars += expression_open[0];
}
if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) {
open_chars += expression_open_force_lstrip[0];
}
if (open_chars.find(comment_open[0]) == std::string::npos) {
open_chars += comment_open[0];
}
Expand Down Expand Up @@ -1786,7 +1791,7 @@ struct Token {
Unknown,
Eof,
};

Kind kind {Kind::Unknown};
nonstd::string_view text;

Expand Down Expand Up @@ -1895,6 +1900,7 @@ class Lexer {
enum class State {
Text,
ExpressionStart,
ExpressionStartForceLstrip,
ExpressionBody,
LineStart,
LineBody,
Expand All @@ -1918,8 +1924,6 @@ class Lexer {
nonstd::string_view m_in;
size_t tok_start;
size_t pos;
bool temp_trim_flag = false;


Token scan_body(nonstd::string_view close, Token::Kind closeKind, nonstd::string_view close_trim = nonstd::string_view(), bool trim = false) {
again:
Expand All @@ -1942,22 +1946,13 @@ class Lexer {
return tok;
}

if (ch == '-') {
if (inja::string_view::starts_with(m_in.substr(tok_start + 1), close)) {
tok_start += 1;
temp_trim_flag = true;
} else
return make_token(Token::Kind::Unknown);
}

if (inja::string_view::starts_with(m_in.substr(tok_start), close)) {
state = State::Text;
pos = tok_start + close.size();
Token tok = make_token(closeKind);
if (trim || temp_trim_flag) {
if (trim) {
skip_whitespaces_and_first_newline();
}
temp_trim_flag = false;
return tok;
}

Expand Down Expand Up @@ -2192,7 +2187,12 @@ class Lexer {
nonstd::string_view open_str = m_in.substr(pos);
bool must_lstrip = false;
if (inja::string_view::starts_with(open_str, config.expression_open)) {
state = State::ExpressionStart;
if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) {
state = State::ExpressionStartForceLstrip;
must_lstrip = true;
} else {
state = State::ExpressionStart;
}
} else if (inja::string_view::starts_with(open_str, config.statement_open)) {
if (inja::string_view::starts_with(open_str, config.statement_open_no_lstrip)) {
state = State::StatementStartNoLstrip;
Expand Down Expand Up @@ -2227,11 +2227,11 @@ class Lexer {
case State::ExpressionStart: {
state = State::ExpressionBody;
pos += config.expression_open.size();
// whitespace control
if (m_in[pos] == '-') {
pos += 1;
temp_trim_flag = true;
}
return make_token(Token::Kind::ExpressionOpen);
}
case State::ExpressionStartForceLstrip: {
state = State::ExpressionBody;
pos += config.expression_open_force_lstrip.size();
return make_token(Token::Kind::ExpressionOpen);
}
case State::LineStart: {
Expand Down Expand Up @@ -2260,7 +2260,7 @@ class Lexer {
return make_token(Token::Kind::CommentOpen);
}
case State::ExpressionBody:
return scan_body(config.expression_close, Token::Kind::ExpressionClose);
return scan_body(config.expression_close, Token::Kind::ExpressionClose, config.expression_close_force_rstrip);
case State::LineBody:
return scan_body("\n", Token::Kind::LineStatementClose);
case State::StatementBody:
Expand Down Expand Up @@ -2869,7 +2869,7 @@ class Parser {
// Functions
} else if (peek_tok.kind == Token::Kind::LeftParen) {
operator_stack.emplace(std::make_shared<FunctionNode>(static_cast<std::string>(tok.text), tok.text.data() - tmpl.content.c_str()));
function_stack.emplace(operator_stack.top().get(), current_paren_level);
function_stack.emplace(operator_stack.top().get(), current_paren_level);

// Variables
} else {
Expand Down Expand Up @@ -3477,10 +3477,10 @@ class Renderer : public NodeVisitor {
void visit(const JsonNode& node) {
if (json_additional_data.contains(node.ptr)) {
json_eval_stack.push(&json_additional_data[node.ptr]);

} else if (json_input->contains(node.ptr)) {
json_eval_stack.push(&(*json_input)[node.ptr]);

} else {
// Try to evaluate as a no-argument callback
auto function_data = function_storage.find_function(node.name, 0);
Expand Down Expand Up @@ -4137,4 +4137,4 @@ inline void render_to(std::ostream &os, nonstd::string_view input, const json &d
// #include "template.hpp"


#endif // INCLUDE_INJA_INJA_HPP_
#endif // INCLUDE_INJA_INJA_HPP_
5 changes: 3 additions & 2 deletions scripts/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM alpine:latest
MAINTAINER Tindy X <tindy.it@gmail.com>
FROM alpine:3.12
LABEL maintainer "tindy.it@gmail.com"

# build minimized
WORKDIR /
RUN apk add --no-cache --virtual .build-tools git g++ build-base linux-headers cmake python2 && \
apk add --no-cache --virtual .build-deps curl-dev rapidjson-dev libevent-dev pcre2-dev yaml-cpp-dev && \
git clone https://github.com/svaarala/duktape --depth=1 && \
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.alpine.release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ g++ -o base/subconverter $(find CMakeFiles/subconverter.dir/src/ -name "*.o") -

cd base
chmod +rx subconverter
chmod +r *
chmod +r ./*
cd ..
mv base subconverter
2 changes: 1 addition & 1 deletion scripts/build.macos.release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ c++ -Xlinker -unexported_symbol -Xlinker "*" -o base/subconverter -framework Cor

cd base
chmod +rx subconverter
chmod +r *
chmod +r ./*
cd ..
mv base subconverter

Expand Down
2 changes: 1 addition & 1 deletion scripts/config.termux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cc -c -O3 -o duktape.o duktape.c
cc -c -O3 -o duk_module_node.o -I. ../extras/module-node/duk_module_node.c
ar cr libduktape.a duktape.o
ar cr libduktape_module.a duk_module_node.o
install -m0644 *.a /data/data/com.termux/files/usr/lib
install -m0644 ./*.a /data/data/com.termux/files/usr/lib
install -m0644 duk*.h /data/data/com.termux/files/usr/include
install -m0644 ../extras/module-node/duk_module_node.h /data/data/com.termux/files/usr/include
cd ../../..
36 changes: 17 additions & 19 deletions src/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1451,10 +1451,10 @@ std::string subconverter(RESPONSE_CALLBACK_ARGS)
expand.define(true);

/// read preference from argument, assign global var if not in argument
ext.tfo.read(tfo).read(tfo_flag);
ext.udp.read(udp).read(udp_flag);
ext.skip_cert_verify.read(scv).read(scv_flag);
ext.tls13.read(tls13).read(tls13_flag);
ext.tfo.parse(tfo).parse(tfo_flag);
ext.udp.parse(udp).parse(udp_flag);
ext.skip_cert_verify.parse(scv).parse(scv_flag);
ext.tls13.parse(tls13).parse(tls13_flag);

ext.sort_flag = sort_flag.get(do_sort);
use_sort_script.define(sort_script.size() != 0);
Expand Down Expand Up @@ -2032,11 +2032,11 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
singlegroup["type"] = type;
for(unsigned int i = 1; i < dummy_str_array.size(); i++)
{
if(dummy_str_array[i].find("url") == 0)
if(startsWith(dummy_str_array[i], "url"))
singlegroup["url"] = trim(dummy_str_array[i].substr(dummy_str_array[i].find("=") + 1));
else if(dummy_str_array[i].find("interval") == 0)
else if(startsWith(dummy_str_array[i], "interval"))
singlegroup["interval"] = trim(dummy_str_array[i].substr(dummy_str_array[i].find("=") + 1));
else if(dummy_str_array[i].find("policy-path") == 0)
else if(startsWith(dummy_str_array[i], "policy-path"))
links.emplace_back(trim(dummy_str_array[i].substr(dummy_str_array[i].find("=") + 1)));
else
singlegroup["proxies"].push_back(trim(dummy_str_array[i]));
Expand Down Expand Up @@ -2114,9 +2114,7 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
std::string::size_type lineSize;
for(std::string &x : dummy_str_array)
{
if(x.find("USER-AGENT") == 0 || x.find("URL-REGEX") == 0 || x.find("PROCESS-NAME") == 0 || x.find("AND") == 0 || x.find("OR") == 0) //remove unsupported types
continue;
else if(x.find("RULE-SET") == 0)
if(startsWith(x, "RULE-SET"))
{
strArray = split(x, ",");
if(strArray.size() != 3)
Expand All @@ -2135,7 +2133,7 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
strLine.erase(--lineSize);
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue;
else if(strLine.find("USER-AGENT") == 0 || strLine.find("URL-REGEX") == 0 || strLine.find("PROCESS-NAME") == 0 || strLine.find("AND") == 0 || strLine.find("OR") == 0) //remove unsupported types
else if(!std::any_of(clash_rule_type.begin(), clash_rule_type.end(), [&strLine](std::string type){return startsWith(strLine, type);})) //remove unsupported types
continue;
strLine += strArray[2];
if(count_least(strLine, ',', 3))
Expand All @@ -2145,6 +2143,8 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
ss.clear();
continue;
}
else if(!std::any_of(clash_rule_type.begin(), clash_rule_type.end(), [&strLine](std::string type){return startsWith(strLine, type);}))
continue;
rule.push_back(x);
}
clash[rule_name] = rule;
Expand Down Expand Up @@ -2456,12 +2456,10 @@ int simpleGenerator()
std::string proxy = parseProxy(proxy_subscription);
Request request;
Response response;
int &ret_code = response.status_code;
string_map &headers = response.headers;
for(std::string &x : sections)
{
arguments.clear();
ret_code = 200;
response.status_code = 200;
//std::cerr<<"Generating artifact '"<<x<<"'...\n";
writeLog(0, "Generating artifact '" + x + "'...", LOG_LEVEL_INFO);
ini.EnterSection(x);
Expand Down Expand Up @@ -2508,7 +2506,7 @@ int simpleGenerator()
request.argument = arguments;
content = subconverter(request, response);
}
if(ret_code != 200)
if(response.status_code != 200)
{
//std::cerr<<"Artifact '"<<x<<"' generate ERROR! Reason: "<<content<<"\n\n";
writeLog(0, "Artifact '" + x + "' generate ERROR! Reason: " + content + "\n", LOG_LEVEL_ERROR);
Expand All @@ -2517,12 +2515,12 @@ int simpleGenerator()
continue;
}
fileWrite(path, content, true);
auto iter = std::find_if(headers.begin(), headers.end(), [](auto y){ return y.first == "Subscription-UserInfo"; });
if(iter != headers.end())
auto iter = std::find_if(response.headers.begin(), response.headers.end(), [](auto y){ return y.first == "Subscription-UserInfo"; });
if(iter != response.headers.end())
writeLog(0, "User Info for artifact '" + x + "': " + subInfoToMessage(iter->second), LOG_LEVEL_INFO);
//std::cerr<<"Artifact '"<<x<<"' generate SUCCESS!\n\n";
writeLog(0, "Artifact '" + x + "' generate SUCCESS!\n", LOG_LEVEL_INFO);
eraseElements(headers);
eraseElements(response.headers);
}
//std::cerr<<"All artifact generated. Exiting...\n";
writeLog(0, "All artifact generated. Exiting...", LOG_LEVEL_INFO);
Expand All @@ -2537,7 +2535,7 @@ std::string renderTemplate(RESPONSE_CALLBACK_ARGS)
std::string path = UrlDecode(getUrlArg(argument, "path"));
writeLog(0, "Trying to render template '" + path + "'...", LOG_LEVEL_INFO);

if(path.find(template_path) != 0 || !fileExist(path))
if(startsWith(path, template_path) || !fileExist(path))
{
*status_code = 404;
return "Not found";
Expand Down
38 changes: 13 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int main(int argc, char *argv[])
generateBase();

std::string env_api_mode = GetEnv("API_MODE"), env_managed_prefix = GetEnv("MANAGED_PREFIX"), env_token = GetEnv("API_TOKEN");
api_mode = tribool().read(toLower(env_api_mode)).get(api_mode);
api_mode = tribool().parse(toLower(env_api_mode)).get(api_mode);
if(env_managed_prefix.size())
managed_config_prefix = env_managed_prefix;
if(env_token.size())
Expand All @@ -156,15 +156,12 @@ int main(int argc, char *argv[])

append_response("GET", "/refreshrules", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
std::string &argument = request.argument;
int *status_code = &response.status_code;

if(access_token.size())
{
std::string token = getUrlArg(argument, "token");
std::string token = getUrlArg(request.argument, "token");
if(token != access_token)
{
*status_code = 403;
response.status_code = 403;
return "Forbidden\n";
}
}
Expand All @@ -175,15 +172,12 @@ int main(int argc, char *argv[])

append_response("GET", "/readconf", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
std::string &argument = request.argument;
int *status_code = &response.status_code;

if(access_token.size())
{
std::string token = getUrlArg(argument, "token");
std::string token = getUrlArg(request.argument, "token");
if(token != access_token)
{
*status_code = 403;
response.status_code = 403;
return "Forbidden\n";
}
}
Expand All @@ -194,27 +188,23 @@ int main(int argc, char *argv[])

append_response("POST", "/updateconf", "text/plain", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
std::string &argument = request.argument;
std::string postdata = request.postdata;
int *status_code = &response.status_code;

if(access_token.size())
{
std::string token = getUrlArg(argument, "token");
std::string token = getUrlArg(request.argument, "token");
if(token != access_token)
{
*status_code = 403;
response.status_code = 403;
return "Forbidden\n";
}
}
std::string type = getUrlArg(argument, "type");
std::string type = getUrlArg(request.argument, "type");
if(type == "form")
fileWrite(pref_path, getFormData(postdata), true);
fileWrite(pref_path, getFormData(request.postdata), true);
else if(type == "direct")
fileWrite(pref_path, postdata, true);
fileWrite(pref_path, request.postdata, true);
else
{
*status_code = 501;
response.status_code = 501;
return "Not Implemented\n";
}

Expand Down Expand Up @@ -247,15 +237,13 @@ int main(int argc, char *argv[])
{
append_response("GET", "/get", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
std::string &argument = request.argument;
std::string url = UrlDecode(getUrlArg(argument, "url"));
std::string url = UrlDecode(getUrlArg(request.argument, "url"));
return webGet(url, "");
});

append_response("GET", "/getlocal", "text/plain;charset=utf-8", [](RESPONSE_CALLBACK_ARGS) -> std::string
{
std::string &argument = request.argument;
return fileGet(UrlDecode(getUrlArg(argument, "path")));
return fileGet(UrlDecode(getUrlArg(request.argument, "path")));
});
}

Expand Down
Loading

0 comments on commit 65ac33d

Please sign in to comment.