mirror of
https://github.com/asdlokj1qpi233/subconverter.git
synced 2026-11-03 02:02:24 +00:00
Enhancements
Change section and key names in configuration files. Add support for using Quantumult X rulesets/Clash rule-providers as rule sources. Optimize codes.
This commit is contained in:
18 files changed
+475
-201
No files matched your search
+32
-24
@@ -40,47 +40,45 @@ static inline void buffer_cleanup(struct evbuffer *eb)
|
||||
#endif // MALLOC_TRIM
|
||||
}
|
||||
|
||||
static inline int process_request(const char *method_str, std::string uri, std::string &postdata, std::string &content_type, std::string &return_data, int *status_code, std::map<std::string, std::string> &extra_headers)
|
||||
//static inline int process_request(const char *method_str, std::string uri, std::string &postdata, std::string &content_type, std::string &return_data, int *status_code, std::map<std::string, std::string> &extra_headers)
|
||||
static inline int process_request(Request &request, Response &response, std::string &return_data)
|
||||
{
|
||||
std::string path, arguments;
|
||||
//std::cerr << "handle_cmd: " << method_str << std::endl << "handle_uri: " << uri << std::endl;
|
||||
writeLog(0, "handle_cmd: " + std::string(method_str) + " handle_uri: " + uri, LOG_LEVEL_VERBOSE);
|
||||
writeLog(0, "handle_cmd: " + request.method + " handle_uri: " + request.url, LOG_LEVEL_VERBOSE);
|
||||
|
||||
if(strFind(uri, "?"))
|
||||
string_size pos = request.url.find("?");
|
||||
if(pos != request.url.npos)
|
||||
{
|
||||
path = uri.substr(0, uri.find("?"));
|
||||
arguments = uri.substr(uri.find("?") + 1);
|
||||
request.argument = request.url.substr(pos + 1);
|
||||
request.url.erase(pos);
|
||||
}
|
||||
else
|
||||
path = uri;
|
||||
|
||||
for(responseRoute &x : responses)
|
||||
{
|
||||
if(strcmp(method_str, "OPTIONS") == 0 && postdata.find(x.method) != 0 && x.path == path)
|
||||
if(request.method == "OPTIONS" && request.postdata.find(x.method) != 0 && x.path == request.url)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if(x.method.compare(method_str) == 0 && x.path == path)
|
||||
else if(x.method == request.method && x.path == request.url)
|
||||
{
|
||||
response_callback &rc = x.rc;
|
||||
return_data = rc(arguments, postdata, status_code, extra_headers);
|
||||
content_type = x.content_type;
|
||||
return_data = rc(request, response);
|
||||
response.content_type = x.content_type;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
auto iter = redirect_map.find(path);
|
||||
auto iter = redirect_map.find(request.url);
|
||||
if(iter != redirect_map.end())
|
||||
{
|
||||
return_data = iter->second;
|
||||
if(arguments.size())
|
||||
if(request.argument.size())
|
||||
{
|
||||
if(return_data.find("?") != return_data.npos)
|
||||
return_data += "&" + arguments;
|
||||
return_data += "&" + request.argument;
|
||||
else
|
||||
return_data += "?" + arguments;
|
||||
return_data += "?" + request.argument;
|
||||
}
|
||||
content_type = "REDIRECT";
|
||||
response.content_type = "REDIRECT";
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -117,16 +115,26 @@ void OnReq(evhttp_request *req, void *args)
|
||||
postdata.assign(req_ac_method);
|
||||
}
|
||||
|
||||
int status_code = 200;
|
||||
std::map<std::string, std::string> extra_headers;
|
||||
retVal = process_request(req_method, uri, postdata, content_type, return_data, &status_code, extra_headers);
|
||||
Request request;
|
||||
Response response;
|
||||
request.method = req_method;
|
||||
request.url = uri;
|
||||
|
||||
struct evkeyval* kv = req->input_headers->tqh_first;
|
||||
while (kv)
|
||||
{
|
||||
request.headers.emplace(kv->key, kv->value);
|
||||
kv = kv->next.tqe_next;
|
||||
}
|
||||
retVal = process_request(request, response, return_data);
|
||||
content_type = response.content_type;
|
||||
|
||||
auto *OutBuf = evhttp_request_get_output_buffer(req);
|
||||
//struct evbuffer *OutBuf = evbuffer_new();
|
||||
if (!OutBuf)
|
||||
return;
|
||||
|
||||
for(auto &x : extra_headers)
|
||||
for(auto &x : response.headers)
|
||||
evhttp_add_header(req->output_headers, x.first.data(), x.second.data());
|
||||
|
||||
switch(retVal)
|
||||
@@ -134,7 +142,7 @@ void OnReq(evhttp_request *req, void *args)
|
||||
case 1: //found OPTIONS
|
||||
evhttp_add_header(req->output_headers, "Access-Control-Allow-Origin", "*");
|
||||
evhttp_add_header(req->output_headers, "Access-Control-Allow-Headers", "*");
|
||||
evhttp_send_reply(req, status_code, "", NULL);
|
||||
evhttp_send_reply(req, response.status_code, "", NULL);
|
||||
break;
|
||||
case 0: //found normal
|
||||
if(content_type.size())
|
||||
@@ -152,7 +160,7 @@ void OnReq(evhttp_request *req, void *args)
|
||||
evhttp_add_header(req->output_headers, "Access-Control-Allow-Origin", "*");
|
||||
evhttp_add_header(req->output_headers, "Connection", "close");
|
||||
evbuffer_add(OutBuf, return_data.data(), return_data.size());
|
||||
evhttp_send_reply(req, status_code, "", OutBuf);
|
||||
evhttp_send_reply(req, response.status_code, "", OutBuf);
|
||||
break;
|
||||
case -1: //not found
|
||||
return_data = "File not found.";
|
||||
|
||||
Reference in new issue
Block a user