summaryrefslogtreecommitdiff
path: root/plugin/business/pangu-http/src/pattern_replace.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/business/pangu-http/src/pattern_replace.cpp')
-rw-r--r--plugin/business/pangu-http/src/pattern_replace.cpp95
1 files changed, 65 insertions, 30 deletions
diff --git a/plugin/business/pangu-http/src/pattern_replace.cpp b/plugin/business/pangu-http/src/pattern_replace.cpp
index b81da0b..a627c31 100644
--- a/plugin/business/pangu-http/src/pattern_replace.cpp
+++ b/plugin/business/pangu-http/src/pattern_replace.cpp
@@ -78,23 +78,55 @@ strtok_r_esc(char * s, const char delim, char ** save_ptr)
return s;
}
-char *rt_read_file(const char* filename, size_t *input_sz)
+char *execute_read_file(const char *filename, size_t *filelen)
{
- FILE* fp=NULL;
- struct stat file_info;
- stat(filename, &file_info);
- *input_sz=file_info.st_size;
-
- fp=fopen(filename,"r");
- if(fp==NULL)
- {
- return NULL;
- }
- char* data=(char*)malloc((*input_sz));
- fread(data,1,*input_sz,fp);
- fclose(fp);
-
- return data;
+ FILE *file = NULL;
+ long length = 0;
+ char *content = NULL;
+ size_t read_chars = 0;
+
+ file = fopen(filename, "rb");
+ if (file == NULL)
+ {
+ goto cleanup;
+ }
+ if (fseek(file, 0, SEEK_END) != 0)
+ {
+ goto cleanup;
+ }
+ length = ftell(file);
+ if (length < 0)
+ {
+ goto cleanup;
+ }
+ if (fseek(file, 0, SEEK_SET) != 0)
+ {
+ goto cleanup;
+ }
+
+ /* allocate content buffer */
+ content = (char*)malloc((size_t)length + sizeof(""));
+ if (content == NULL)
+ {
+ goto cleanup;
+ }
+
+ /* read the file into memory */
+ read_chars = fread(content, sizeof(char), (size_t)length, file);
+ if ((long)read_chars != length)
+ {
+ free(content);
+ content = NULL;
+ goto cleanup;
+ }
+ *filelen = read_chars;
+ content[read_chars] = '\0';
+cleanup:
+ if (file != NULL)
+ {
+ fclose(file);
+ }
+ return content;
}
size_t __attribute__((__unused__))
format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace)
@@ -262,14 +294,14 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char
if (0==strcasecmp(type, "css"))
{
- target_size = in_sz+strlen(stype)+css_type_len;
- target = ALLOC(char, target_size);
+ target_size = in_sz+strlen(stype)+1+css_type_len;
+ target = ALLOC(char, target_size + 1);
}
if (0==strcasecmp(type, "js"))
{
- target_size = in_sz+strlen(stype)+js_type_len;
- target = ALLOC(char, target_size);
+ target_size = in_sz+strlen(stype)+1+js_type_len;
+ target = ALLOC(char, target_size + 1);
}
if (insert_on != NULL && 0==strcasecmp(insert_on, "after-page-load"))
@@ -283,22 +315,24 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char
size_t style_len = 0; char *style_msg = NULL;
if (0==strcasecmp(type, "js"))
{
- style_len = strlen(stype)+js_type_len+1;
+ style_len = strlen(stype)+1+js_type_len;
style_msg = ALLOC(char, style_len);
snprintf(style_msg, style_len, "<script type=\"text/javascript\" class=\"RQ_SCRIPT\">%s</script>", stype);
}
if (0==strcasecmp(type, "css"))
{
- style_len = strlen(stype)+css_type_len+1;
+ style_len = strlen(stype)+1+css_type_len;
style_msg = ALLOC(char, style_len);
- snprintf(style_msg, style_len, "<style type=\"text/css\" class=\"RQ_SCRIPT\">%s</style>\n", stype);
+ snprintf(style_msg, style_len, "<style type=\"text/css\" class=\"RQ_SCRIPT\">%s</style>", stype);
}
- strncat(target, style_msg, target_size);
+
+ strncat(target, style_msg, MIN(style_len, target_size));
free(style_msg);
style_msg = NULL;
- strncat(target, head_string, target_size);
+ strncat(target, head_string, (target_size - (head_string-in) - style_len));
+ target[target_size-1] = '\0';
+ outlen = target_size;
*out = target;
- outlen = strlen(target) + 1;
}else
{
free(target);
@@ -315,12 +349,13 @@ size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * r
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz)
{
- size_t n_got_rule=1;
+ 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];
- rules[0].zone = kZoneResponseBody;
- rules[0].find = tfe_strdup(find);
- rules[0].replace_with = tfe_strdup(replacement);
+ 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;
}