@@ -1336,7 +1336,7 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
|||||||
if (tfe_http_in_request(events))
|
if (tfe_http_in_request(events))
|
||||||
{
|
{
|
||||||
rewrite_uri_sz = execute_replace_rule(in_req_spec->uri, strlen(in_req_spec->uri),
|
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);
|
kZoneRequestUri, rep_ctx->rule, rep_ctx->n_rule, &rewrite_uri, 1);
|
||||||
if(rewrite_uri_sz>0) rep_ctx->actually_replaced=1;
|
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,
|
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);
|
rewrite_uri_sz >0 ? rewrite_uri : in_req_spec->uri);
|
||||||
@@ -1371,7 +1371,7 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
|||||||
rewrite_buff = NULL;
|
rewrite_buff = NULL;
|
||||||
rewrite_sz = 0;
|
rewrite_sz = 0;
|
||||||
rewrite_sz=execute_replace_rule(in_header_value,
|
rewrite_sz=execute_replace_rule(in_header_value,
|
||||||
strlen(in_header_value), zone, rep_ctx->rule, rep_ctx->n_rule, &rewrite_buff);
|
strlen(in_header_value), zone, rep_ctx->rule, rep_ctx->n_rule, &rewrite_buff, 1);
|
||||||
|
|
||||||
if(rewrite_sz>0) rep_ctx->actually_replaced=1;
|
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);
|
tfe_http_field_write(rep_ctx->replacing, &in_header_field, rewrite_sz>0? rewrite_buff : in_header_value);
|
||||||
@@ -1395,6 +1395,7 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
|||||||
|
|
||||||
if ((events & EV_HTTP_REQ_BODY_END) || (events & EV_HTTP_RESP_BODY_END))
|
if ((events & EV_HTTP_REQ_BODY_END) || (events & EV_HTTP_RESP_BODY_END))
|
||||||
{
|
{
|
||||||
|
int options = 0;
|
||||||
char * __http_body = (char *) evbuffer_pullup(rep_ctx->http_body, -1);
|
char * __http_body = (char *) evbuffer_pullup(rep_ctx->http_body, -1);
|
||||||
size_t __http_body_len = evbuffer_get_length(rep_ctx->http_body);
|
size_t __http_body_len = evbuffer_get_length(rep_ctx->http_body);
|
||||||
|
|
||||||
@@ -1403,8 +1404,13 @@ void http_replace(const struct tfe_stream * stream, const struct tfe_http_sessio
|
|||||||
rewrite_buff = NULL;
|
rewrite_buff = NULL;
|
||||||
rewrite_sz = 0;
|
rewrite_sz = 0;
|
||||||
|
|
||||||
|
if (in_resp_spec->content_type != NULL && strstr(in_resp_spec->content_type, "charset=utf-8"))
|
||||||
|
{
|
||||||
|
options = 1;
|
||||||
|
}
|
||||||
|
|
||||||
rewrite_sz = execute_replace_rule(__http_body, __http_body_len, r_zone,
|
rewrite_sz = execute_replace_rule(__http_body, __http_body_len, r_zone,
|
||||||
rep_ctx->rule, rep_ctx->n_rule, &rewrite_buff);
|
rep_ctx->rule, rep_ctx->n_rule, &rewrite_buff, options);
|
||||||
|
|
||||||
if (rewrite_sz >0 )
|
if (rewrite_sz >0 )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ size_t select_replace_rule(enum replace_zone zone, const struct replace_rule * r
|
|||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t replace_string(const char * in, size_t in_sz, const struct replace_rule * zone, char** out)
|
size_t replace_string(const char * in, size_t in_sz, const struct replace_rule * zone, char** out, int options)
|
||||||
{
|
{
|
||||||
assert(strlen(zone->find) != 0);
|
assert(strlen(zone->find) != 0);
|
||||||
|
|
||||||
@@ -198,8 +198,9 @@ size_t replace_string(const char * in, size_t in_sz, const struct replace_rule *
|
|||||||
const PCRE2_SPTR pattern = (PCRE2_SPTR)zone->find;
|
const PCRE2_SPTR pattern = (PCRE2_SPTR)zone->find;
|
||||||
const PCRE2_SPTR subject = (PCRE2_SPTR)in;
|
const PCRE2_SPTR subject = (PCRE2_SPTR)in;
|
||||||
const PCRE2_SPTR replacement = (PCRE2_SPTR)zone->replace_with;
|
const PCRE2_SPTR replacement = (PCRE2_SPTR)zone->replace_with;
|
||||||
|
uint32_t pcre2_options = options ? PCRE2_UTF : 0;
|
||||||
|
|
||||||
pcre2_code *re = pcre2_compile(pattern, strlen(zone->find), PCRE2_UTF, &error, &erroffset, 0);
|
pcre2_code *re = pcre2_compile(pattern, strlen(zone->find), pcre2_options, &error, &erroffset, 0);
|
||||||
if (!re)
|
if (!re)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@@ -237,7 +238,7 @@ not_enough_mem_retry:
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t execute_replace_rule(const char * in, size_t in_sz,
|
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)
|
enum replace_zone zone, const struct replace_rule * rules, size_t n_rule, char** out, int options)
|
||||||
{
|
{
|
||||||
const struct replace_rule * todo[n_rule];
|
const struct replace_rule * todo[n_rule];
|
||||||
size_t n_todo = 0, i = 0, interator_sz=0, pre_out_sz=0;
|
size_t n_todo = 0, i = 0, interator_sz=0, pre_out_sz=0;
|
||||||
@@ -253,7 +254,7 @@ size_t execute_replace_rule(const char * in, size_t in_sz,
|
|||||||
interator_sz = in_sz;
|
interator_sz = in_sz;
|
||||||
for (i = 0; i < n_todo; i++)
|
for (i = 0; i < n_todo; i++)
|
||||||
{
|
{
|
||||||
output_size = replace_string(interator, interator_sz, todo[i], &new_out);
|
output_size = replace_string(interator, interator_sz, todo[i], &new_out, options);
|
||||||
if (output_size == 0)
|
if (output_size == 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@@ -287,7 +288,12 @@ static char *find_insert_position(char * in)
|
|||||||
{
|
{
|
||||||
char *insert_from = NULL;
|
char *insert_from = NULL;
|
||||||
char *script_local = NULL;
|
char *script_local = NULL;
|
||||||
|
|
||||||
|
if (in == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
insert_from = strstr(in, "jquery");
|
insert_from = strstr(in, "jquery");
|
||||||
if (insert_from != NULL)
|
if (insert_from != NULL)
|
||||||
{
|
{
|
||||||
@@ -369,14 +375,14 @@ size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * r
|
|||||||
return insert_string(in, in_sz, rules->position, rules->script, rules->type, out);
|
return insert_string(in, in_sz, rules->position, rules->script, rules->type, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz)
|
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz, int options)
|
||||||
{
|
{
|
||||||
char* exec_para=NULL;
|
char* exec_para=NULL;
|
||||||
asprintf(&exec_para,"zone=http_resp_body;substitute=/%s/%s", find, replacement);
|
asprintf(&exec_para,"zone=http_resp_body;substitute=/%s/%s", find, replacement);
|
||||||
size_t n_got_rule=0;
|
size_t n_got_rule=0;
|
||||||
struct replace_rule rules[16];
|
struct replace_rule rules[16];
|
||||||
n_got_rule=format_replace_rule(exec_para, rules, sizeof(rules)/sizeof(rules[0]));
|
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);
|
*output_sz=execute_replace_rule(input, strlen(input), kZoneResponseBody, rules, n_got_rule, output, options);
|
||||||
free(exec_para);
|
free(exec_para);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ struct insert_rule
|
|||||||
//@parm exec_para example input:
|
//@parm exec_para example input:
|
||||||
//zone=http_req_body; substitute=/中華民國/中华人民共和国;zone=http_resp_header; substitute=/Content-Type:\btext\/html/Content-Type:\bvideo\/mp4
|
//zone=http_req_body; substitute=/中華民國/中华人民共和国;zone=http_resp_header; substitute=/Content-Type:\btext\/html/Content-Type:\bvideo\/mp4
|
||||||
//@return formated rule number.
|
//@return formated rule number.
|
||||||
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);
|
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, int options);
|
||||||
size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * rules, char** out);
|
size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * rules, char** out);
|
||||||
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *stype, const char *type, char** out);
|
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *stype, const char *type, char** out);
|
||||||
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz);
|
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz, int options);
|
||||||
|
|
||||||
enum replace_zone zone_name_to_id(const char * name);
|
enum replace_zone zone_name_to_id(const char * name);
|
||||||
char *execute_read_file(const char *filename, size_t *filelen);
|
char *execute_read_file(const char *filename, size_t *filelen);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ int main(int argc, char ** argv)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
printf("Subject: %s\n", input);
|
printf("Subject: %s\n", input);
|
||||||
printf("Subsitute: %s\n", output);
|
printf("Subsitute: %s\n", output);
|
||||||
free(output);
|
free(output);
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ TEST(PatternReplace, Grouping1)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
// EXPECT_STREQ(output, expect);
|
// EXPECT_STREQ(output, expect);
|
||||||
printf("%s\n", output);
|
printf("%s\n", output);
|
||||||
@@ -31,7 +31,7 @@ TEST(PatternReplace, Grouping2)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, "中央空调"));
|
EXPECT_TRUE(NULL==strstr(output, "中央空调"));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, "中央Air conditioner"));
|
EXPECT_TRUE(NULL!=strstr(output, "中央Air conditioner"));
|
||||||
@@ -68,7 +68,7 @@ TEST(PatternReplace, Non_Zero_Terminated)
|
|||||||
|
|
||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
simple_replace(find, replacement, input, input_sz, &output, &output_sz);
|
simple_replace(find, replacement, input, input_sz, &output, &output_sz, 1);
|
||||||
|
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==memmem(output, output_sz, find, strlen(find)));
|
EXPECT_TRUE(NULL==memmem(output, output_sz, find, strlen(find)));
|
||||||
@@ -89,7 +89,7 @@ TEST(PatternReplace, ASCII)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, find));
|
EXPECT_TRUE(NULL==strstr(output, find));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||||
@@ -107,7 +107,7 @@ TEST(PatternReplace, UTF8)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, find));
|
EXPECT_TRUE(NULL==strstr(output, find));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||||
@@ -124,7 +124,7 @@ TEST(PatternReplace, CaseInsensitive)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, find));
|
EXPECT_TRUE(NULL==strstr(output, find));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||||
@@ -148,7 +148,7 @@ TEST(PatternReplace, CaseInsensitiveRussian)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, find));
|
EXPECT_TRUE(NULL==strstr(output, find));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||||
@@ -165,7 +165,7 @@ TEST(PatternReplace, QueryAdd)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, find));
|
EXPECT_TRUE(NULL==strstr(output, find));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||||
@@ -184,7 +184,7 @@ TEST(PatternReplace, QueryDel)
|
|||||||
char* output=NULL;
|
char* output=NULL;
|
||||||
size_t output_sz=0;
|
size_t output_sz=0;
|
||||||
|
|
||||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
simple_replace(find, replacement, input, strlen(input),&output, &output_sz, 1);
|
||||||
EXPECT_TRUE(output_sz>0);
|
EXPECT_TRUE(output_sz>0);
|
||||||
EXPECT_TRUE(NULL==strstr(output, find));
|
EXPECT_TRUE(NULL==strstr(output, find));
|
||||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||||
|
|||||||
@@ -424,6 +424,16 @@ static void delete_http_req_spec(struct tfe_http_req_spec *req_spec)
|
|||||||
free((char *)req_spec->url);
|
free((char *)req_spec->url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void delete_http_resp_spec(struct tfe_http_resp_spec *resp_spec)
|
||||||
|
{
|
||||||
|
if (resp_spec->content_encoding)
|
||||||
|
free((char *)resp_spec->content_encoding);
|
||||||
|
if (resp_spec->content_type)
|
||||||
|
free((char *)resp_spec->content_type);
|
||||||
|
if (resp_spec->content_length)
|
||||||
|
free((char *)resp_spec->content_length);
|
||||||
|
}
|
||||||
|
|
||||||
void delete_stream_half_data(struct tfe_h2_half_private **data,
|
void delete_stream_half_data(struct tfe_h2_half_private **data,
|
||||||
int body_flag, enum tfe_conn_dir dir)
|
int body_flag, enum tfe_conn_dir dir)
|
||||||
{
|
{
|
||||||
@@ -446,6 +456,12 @@ void delete_stream_half_data(struct tfe_h2_half_private **data,
|
|||||||
struct tfe_http_req_spec *req_spec = &((*data)->half_public.req_spec);
|
struct tfe_http_req_spec *req_spec = &((*data)->half_public.req_spec);
|
||||||
delete_http_req_spec(req_spec);
|
delete_http_req_spec(req_spec);
|
||||||
}
|
}
|
||||||
|
if (dir == CONN_DIR_UPSTREAM)
|
||||||
|
{
|
||||||
|
struct tfe_http_resp_spec *resp_spec = &((*data)->half_public.resp_spec);
|
||||||
|
delete_http_resp_spec(resp_spec);
|
||||||
|
}
|
||||||
|
|
||||||
if((*data)->event_cb_user_deleter != NULL)
|
if((*data)->event_cb_user_deleter != NULL)
|
||||||
(*data)->event_cb_user_deleter((*data)->event_cb_user);
|
(*data)->event_cb_user_deleter((*data)->event_cb_user);
|
||||||
free(*data);
|
free(*data);
|
||||||
@@ -1378,15 +1394,15 @@ fill_resp_spec_from_handle(struct tfe_h2_half_private *half_private)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strncmp((char *)(h2_field->nv.name), "content-type", strlen("content-type"))){
|
if (!strncmp((char *)(h2_field->nv.name), "content-type", strlen("content-type"))){
|
||||||
resp_spec->content_type = (const char *)(h2_field->nv.value);
|
resp_spec->content_type = tfe_strdup((const char *)(h2_field->nv.value));;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strncmp((char *)(h2_field->nv.name), "content-encoding", strlen("content-encoding"))){
|
if (!strncmp((char *)(h2_field->nv.name), "content-encoding", strlen("content-encoding"))){
|
||||||
resp_spec->content_encoding = (const char *)(h2_field->nv.value);
|
resp_spec->content_encoding = tfe_strdup((const char *)(h2_field->nv.value));;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!strncmp((char *)(h2_field->nv.name), "content-length", strlen("content-length"))){
|
if (!strncmp((char *)(h2_field->nv.name), "content-length", strlen("content-length"))){
|
||||||
resp_spec->content_length = (const char *)(h2_field->nv.value);
|
resp_spec->content_length = tfe_strdup((const char *)(h2_field->nv.value));;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user