命中替换条件,但未发生实际替换时,不发日志。
This commit is contained in:
@@ -6,3 +6,6 @@ target_link_libraries(pangu-http maatframe)
|
||||
add_executable(test_pattern_replace test_pattern_replace.cpp pattern_replace.cpp)
|
||||
target_link_libraries(test_pattern_replace common gtest pcre2-static)
|
||||
file(COPY test_data DESTINATION ./)
|
||||
|
||||
add_executable(replace_tool replace_tool.cpp pattern_replace.cpp)
|
||||
target_link_libraries(replace_tool common pcre2-static)
|
||||
@@ -228,6 +228,7 @@ struct replace_ctx
|
||||
size_t n_rule;
|
||||
struct tfe_http_half * replacing;
|
||||
struct evbuffer * http_body;
|
||||
int actually_replaced;
|
||||
};
|
||||
|
||||
struct pangu_http_ctx
|
||||
@@ -440,7 +441,7 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
||||
{
|
||||
rewrite_uri_sz = execute_replace_rule(in_req_spec->uri, strlen(in_req_spec->uri),
|
||||
kZoneRequestUri, rep_ctx->rule, rep_ctx->n_rule, &rewrite_uri);
|
||||
|
||||
if(rewrite_uri_sz>0) rep_ctx->actually_replaced=1;
|
||||
rep_ctx->replacing = tfe_http_session_request_create(to_write_sess, in_req_spec->method,
|
||||
rewrite_uri_sz >0 ? rewrite_uri : in_req_spec->uri);
|
||||
|
||||
@@ -475,6 +476,8 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
||||
rewrite_sz = 0;
|
||||
rewrite_sz=execute_replace_rule(in_header_value,
|
||||
strlen(in_header_value), zone, rep_ctx->rule, rep_ctx->n_rule, &rewrite_buff);
|
||||
|
||||
if(rewrite_sz>0) rep_ctx->actually_replaced=1;
|
||||
tfe_http_field_write(rep_ctx->replacing, &in_header_field, rewrite_sz>0? rewrite_buff : in_header_value);
|
||||
if(rewrite_buff != NULL)
|
||||
{
|
||||
@@ -511,6 +514,7 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
||||
if (rewrite_sz >0 )
|
||||
{
|
||||
tfe_http_half_append_body(rep_ctx->replacing, rewrite_buff, rewrite_sz, 0);
|
||||
rep_ctx->actually_replaced=1;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -746,7 +750,7 @@ void pangu_on_http_end(const struct tfe_stream * stream,
|
||||
{
|
||||
struct pangu_http_ctx * ctx = *(struct pangu_http_ctx **) pme;
|
||||
struct pangu_log log_msg = {.stream=stream, .http=session, .result=ctx->enforce_rules, .result_num=ctx->n_enforce};
|
||||
if (ctx->action != PG_ACTION_NONE)
|
||||
if (ctx->action != PG_ACTION_NONE&& !(ctx->action == PG_ACTION_REPLACE && ctx->rep_ctx->actually_replaced==0))
|
||||
{
|
||||
pangu_send_log(g_pangu_rt->send_logger, &log_msg);
|
||||
}
|
||||
|
||||
@@ -227,5 +227,16 @@ size_t execute_replace_rule(const char * in, size_t in_sz,
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz)
|
||||
{
|
||||
char* exec_para=NULL;
|
||||
asprintf(&exec_para,"zone=http_resp_body;substitute=/%s/%s", find, replacement);
|
||||
size_t n_got_rule=0;
|
||||
struct replace_rule rules[16];
|
||||
n_got_rule=format_replace_rule(exec_para, rules, sizeof(rules)/sizeof(rules[0]));
|
||||
*output_sz=execute_replace_rule(input, strlen(input), kZoneResponseBody, rules, n_got_rule, output);
|
||||
free(exec_para);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -22,4 +22,5 @@ struct replace_rule
|
||||
//@return formated rule number.
|
||||
size_t format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace);
|
||||
size_t execute_replace_rule(const char * in, size_t in_sz, enum replace_zone zone, const struct replace_rule * rules, size_t n_rule, char** out);
|
||||
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz);
|
||||
|
||||
|
||||
55
plugin/business/pangu-http/replace_tool.cpp
Normal file
55
plugin/business/pangu-http/replace_tool.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "pattern_replace.h"
|
||||
|
||||
#include <sys/types.h>//fstat
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
void print_help(void)
|
||||
{
|
||||
printf("Escape $ with \\$\n");
|
||||
printf("-i Input Text");
|
||||
printf("-f Find What\n");
|
||||
printf("-r Replace With\n");
|
||||
}
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
const char* find=NULL, *replacement=NULL, *input=NULL;
|
||||
|
||||
int oc=0;
|
||||
if(argc<6)
|
||||
{
|
||||
print_help();
|
||||
return -1;
|
||||
}
|
||||
while((oc=getopt(argc,argv,"f:r:i:"))!=-1)
|
||||
{
|
||||
switch(oc)
|
||||
{
|
||||
case 'f':
|
||||
find=strdup(optarg);
|
||||
break;
|
||||
case 'r':
|
||||
replacement=strdup(optarg);
|
||||
break;
|
||||
case 'i':
|
||||
input=strdup(optarg);
|
||||
break;
|
||||
default:
|
||||
printf("Invalid parameter.\n");
|
||||
print_help();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
printf("Subject: %s\n", input);
|
||||
printf("Subsitute: %s\n", output);
|
||||
free(output);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,23 +6,13 @@
|
||||
#include <stdio.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
static void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz)
|
||||
{
|
||||
char* exec_para=NULL;
|
||||
asprintf(&exec_para,"zone=http_resp_body;substitute=/%s/%s", find, replacement);
|
||||
size_t n_got_rule=0;
|
||||
struct replace_rule rules[16];
|
||||
n_got_rule=format_replace_rule(exec_para, rules, sizeof(rules)/sizeof(rules[0]));
|
||||
*output_sz=execute_replace_rule(input, strlen(input), kZoneResponseBody, rules, n_got_rule, output);
|
||||
free(exec_para);
|
||||
return;
|
||||
}
|
||||
TEST(PatternReplace, Grouping)
|
||||
|
||||
TEST(PatternReplace, Grouping1)
|
||||
{
|
||||
const char* find="(?<name1>John)|(?<name2>李梅梅)|(?<name3>Jake)";
|
||||
const char* replacement="${name1:+Joseph:${name2:+王桃花:Keith}}";
|
||||
const char* input="John loves 李梅梅, 李梅梅 loves Jake and Jake doesn't care about John and 李梅梅.";
|
||||
const char* expect="Joseph loves Jessica, Jessica loves Keith and Keith doesn't care about Joseph and Jessica.";
|
||||
const char* expect="Joseph loves 王桃花, 王桃花 loves Keith and Keith doesn't care about Joseph and 王桃花.";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
@@ -33,6 +23,22 @@ TEST(PatternReplace, Grouping)
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
TEST(PatternReplace, Grouping2)
|
||||
{
|
||||
const char* find="(?<=中央)(?<ac1>空调)|(?<=家用)(?<ac2>空调)";
|
||||
const char* replacement="${ac1:+Air conditioner:${ac2:+立式空调}}";
|
||||
const char* input="中央空调,家用空调,只有第一个空调会被替换。";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
// EXPECT_STREQ(output, expect);
|
||||
printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
TEST(PatternReplace, Non_Zero_Terminated)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user