summaryrefslogtreecommitdiff
path: root/plugin/business/tsg-http/src/pattern_replace.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/business/tsg-http/src/pattern_replace.cpp')
-rw-r--r--plugin/business/tsg-http/src/pattern_replace.cpp78
1 files changed, 77 insertions, 1 deletions
diff --git a/plugin/business/tsg-http/src/pattern_replace.cpp b/plugin/business/tsg-http/src/pattern_replace.cpp
index d9167fd..2f21db5 100644
--- a/plugin/business/tsg-http/src/pattern_replace.cpp
+++ b/plugin/business/tsg-http/src/pattern_replace.cpp
@@ -260,6 +260,53 @@ static char *find_insert_position(char * in)
return insert_from;
}
+int find_remove_position(char *start, char *end)
+{
+ if(end - start <=0)
+ {
+ return 0;
+ }
+
+ char *tags = ALLOC(char, (end - start)+1);
+ memcpy(tags, start, end - start);
+ if(strstr(tags, "<head>") != NULL || strstr(tags, "<body>") != NULL)
+ {
+ free(tags);
+ return 1;
+ }
+
+ free(tags);
+ return 0;
+}
+
+void remove_string_with_tags(char *html)
+{
+ char *start = NULL, *end = NULL;
+
+ while ((start = strstr(html, "<!--")) != NULL)
+ {
+ end = strstr(start, "-->");
+ if (end != NULL)
+ {
+ end += 3;
+ if (find_remove_position(start, end) &&(strstr(start, "<head>") != NULL || strstr(start, "<body>") != NULL))
+ {
+ memmove(start, end, strlen(end) + 1);
+ }
+ else
+ {
+ html = end;
+ }
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ return;
+}
+
size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char *script, const char *type, char** out)
{
char *target=NULL;
@@ -329,7 +376,36 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char
size_t execute_insert_rule(char * in, size_t in_sz, const struct insert_rule * rules, char** out)
{
- return insert_string(in, in_sz, rules->position, rules->script, rules->type, out);
+ size_t out_size=0;
+
+ if (in == NULL || in_sz < 0)
+ {
+ return 0;
+ }
+
+ char*new_in = ALLOC(char, in_sz+1);
+ memcpy(new_in, in, in_sz);
+ remove_string_with_tags(new_in);
+
+ out_size = insert_string(new_in, strlen(new_in), rules->position, rules->script, rules->type, out);
+
+ free(new_in);
+ new_in=NULL;
+
+ return out_size;
+}
+
+size_t simple_insert(char * in, size_t in_sz, const char *insert_on, const char *script, const char *type, char** out)
+{
+ struct insert_rule rules;
+ memset(&rules, 0, sizeof(rules));
+
+ rules.type=(char *)type;
+ rules.script=(char *)script;
+ rules.position=(char *)insert_on;
+ rules.inject_sz=strlen(script);
+
+ return execute_insert_rule(in, in_sz, &rules, out);
}
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz, int options)