--------------------- PatchSet 1443 Date: 2001/01/30 15:24:25 Author: rbcollins Branch: rbcollins_filters Tag: (none) Log: minor tidyup Members: src/modules/textreplace/textreplace.c:1.1.2.3->1.1.2.4 Index: squid/src/modules/textreplace/textreplace.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/modules/textreplace/Attic/textreplace.c,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- squid/src/modules/textreplace/textreplace.c 30 Jan 2001 11:33:01 -0000 1.1.2.3 +++ squid/src/modules/textreplace/textreplace.c 30 Jan 2001 15:24:25 -0000 1.1.2.4 @@ -1,6 +1,6 @@ /* - * $Id: textreplace.c,v 1.1.2.3 2001/01/30 11:33:01 rbcollins Exp $ + * $Id: textreplace.c,v 1.1.2.4 2001/01/30 15:24:25 rbcollins Exp $ * * DEBUG: section 83 Content Processing Filters * AUTHOR: Robert Collins @@ -38,21 +38,24 @@ /* The textreplace filter */ typedef struct _onunloadcfg OnUnloadConfig; typedef struct _onunloadstate OnUnloadState; -struct _onunloadstate { - OnUnloadConfig *config; - /* allow for searching across buffer breaks */ - /* not implemented just yet :] */ - char *pos; +struct _onunloadstate +{ + OnUnloadConfig *config; + /* allow for searching across buffer breaks */ + /* not implemented just yet :] */ + char *pos; + size_t seen; }; -struct _onunloadcfg { - char *searchstring, *replace; +struct _onunloadcfg +{ + char *searchstring, *replace; }; -static REMOVEFILTER clientFilterOnUnload_Remove; -static ADDFILTER clientFilterOnUnload_Add; +static REMOVEFILTER clientFilterOnUnload_Remove; +static ADDFILTER clientFilterOnUnload_Add; static FILTERMKSTATE clientFilterOnUnload_MakeState; -static FILTERPARSE clientFilterOnUnload_Parse; +static FILTERPARSE clientFilterOnUnload_Parse; static DATAFILTER clientFilterOnUnload; @@ -60,183 +63,222 @@ static void -clientFilterFreeConfig(void *data) { - OnUnloadConfig *config=data; - xfree(config->searchstring); - config->searchstring=NULL; - xfree(config->replace); - config->replace=NULL; - xfree(config); +clientFilterFreeConfig (void *data) +{ + OnUnloadConfig *config = data; + xfree (config->searchstring); + config->searchstring = NULL; + xfree (config->replace); + config->replace = NULL; + xfree (config); } static void -clientFilterOnUnload_AddInstance(FILTER_instance *instance) { - OnUnloadConfig *config; - instance->Add=clientFilterOnUnload_Add; - instance->MakeState=clientFilterOnUnload_MakeState; - instance->Parse =clientFilterOnUnload_Parse; - instance->data=xmalloc(sizeof(OnUnloadConfig)); - /* Set defaults, if sensible defaults exist */ - config=instance->data; - config->searchstring=NULL; - config->replace=NULL; +clientFilterOnUnload_AddInstance (FILTER_instance * instance) +{ + OnUnloadConfig *config; + instance->Add = clientFilterOnUnload_Add; + instance->MakeState = clientFilterOnUnload_MakeState; + instance->Parse = clientFilterOnUnload_Parse; + instance->data = xmalloc (sizeof (OnUnloadConfig)); + /* Set defaults, if sensible defaults exist */ + config = instance->data; + config->searchstring = NULL; + config->replace = NULL; } static void -clientFilterOnUnload_RemInstance(FILTER_instance *instance) { - instance->Add=NULL; - instance->MakeState=NULL; - clientFilterFreeConfig(instance->data); - instance->data=NULL; +clientFilterOnUnload_RemInstance (FILTER_instance * instance) +{ + instance->Add = NULL; + instance->MakeState = NULL; + clientFilterFreeConfig (instance->data); + instance->data = NULL; } static void -clientFilterOnUnload_Add(dlink_list *filters, void *filter_config) { - FILTER_list *temp_filter; - temp_filter=xmalloc(sizeof(FILTER_list)); - temp_filter->filter=clientFilterOnUnload; - temp_filter->Remove=clientFilterOnUnload_Remove; - temp_filter->data=filter_config; - /* cbDataLock(http); ? */ - dlinkAddTail(temp_filter, &temp_filter->node, filters); +clientFilterOnUnload_Add (dlink_list * filters, void *filter_config) +{ + FILTER_list *temp_filter; + temp_filter = xmalloc (sizeof (FILTER_list)); + temp_filter->filter = clientFilterOnUnload; + temp_filter->Remove = clientFilterOnUnload_Remove; + temp_filter->data = filter_config; + /* cbDataLock(http); ? */ + dlinkAddTail (temp_filter, &temp_filter->node, filters); } static void * -clientFilterOnUnload_MakeState(void * data) { - OnUnloadState *state; - OnUnloadConfig *config=data; - if (!config) - return NULL; - /* it's up to the filter writer wether to refcount the config, - * or copy it. For now, to save learning cbdata, I copy it - */ - state=xmalloc(sizeof(OnUnloadState)); - state->config=xmalloc(sizeof(OnUnloadConfig)); - state->config->searchstring=xstrdup(config->searchstring); - state->config->replace=xstrdup(config->replace); - return state; +clientFilterOnUnload_MakeState (void *data) +{ + OnUnloadState *state; + OnUnloadConfig *config = data; + if (!config) + return NULL; + /* it's up to the filter writer wether to refcount the config, + * or copy it. For now, to save learning cbdata, I copy it + */ + state = xmalloc (sizeof (OnUnloadState)); + state->config = xmalloc (sizeof (OnUnloadConfig)); + state->config->searchstring = xstrdup (config->searchstring); + state->config->replace = xstrdup (config->replace); + state->seen = 0; + return state; } static void -clientFilterOnUnload_Remove(FILTER_list *filters, dlink_list *filter_list, void *data) { - OnUnloadState *state=data; - dlinkDelete(&filters->node, filter_list); - xfree(filters); - clientFilterFreeConfig(state->config); - state->config=NULL; - xfree(state); +clientFilterOnUnload_Remove (FILTER_list * filters, dlink_list * filter_list, + void *data) +{ + OnUnloadState *state = data; + dlinkDelete (&filters->node, filter_list); + xfree (filters); + clientFilterFreeConfig (state->config); + state->config = NULL; + xfree (state); } static void -clientFilterOnUnload_Parse(void *data, const char *namestr, char *param_str) { - OnUnloadConfig *config=data; - if (strcasecmp(param_str, "replace") == 0) { - parse_eol(&config->replace); - } else if (strcasecmp(param_str, "search") == 0) { - parse_eol(&config->searchstring); - } else { - debug(33, 0) ("unrecognised parameter '%s' for filter %s\n", param_str,namestr); +clientFilterOnUnload_Parse (void *data, const char *namestr, char *param_str) +{ + OnUnloadConfig *config = data; + if (strcasecmp (param_str, "replace") == 0) + { + parse_eol (&config->replace); + rfc1738_unescape (config->replace); + } + else if (strcasecmp (param_str, "search") == 0) + { + parse_eol (&config->searchstring); + rfc1738_unescape (config->searchstring); + } + else + { + debug (33, 0) ("unrecognised parameter '%s' for filter %s\n", param_str, + namestr); } } void -mod_install_textreplace(const char *namestr) { - /* Register as a potential client_side reply filter */ - filterRegisterModule(namestr,clientFilterOnUnload_AddInstance, - clientFilterOnUnload_RemInstance); +mod_install_textreplace (const char *namestr) +{ + /* Register as a potential client_side reply filter */ + filterRegisterModule (namestr, clientFilterOnUnload_AddInstance, + clientFilterOnUnload_RemInstance); } char * -strnchr(const char *str, char chr, size_t len) { - size_t pos=0; - while (posconfig; - char *chr, *searchstring=config->searchstring; - const char *startpos,*pos; - size_t replacelen=strlen(config->replace), searchlen=strlen(config->searchstring); - - /* this looks for browser DOM handlers of the type onunload="..." and - * removes them from the data passed to the client - * caveats and problems: - * 1. theres probably a set of library functions for matching patterns at arbitrary - * places in a datastream (ie handles buffer edge issues, optimum search patterns - * and the like. Well this is a proof of concept filter - live with it :-] - * 2. Some code may not quote the handler properly. I haven't tried to build - * a full blown parser here - see 1. - * 3. this will match patterns in jpegs etc as well at this point. It needs a - * acl or content type check in clientSendMoreData before adding the filter to the - * list. - * - * 4. I'm making use of the properties of the fixed search string to simplify - * the search. IT IS NOT GENERAL PURPOSE - */ - - temp_filter=filters->node.next->data; - - chr=searchstring; - pos=0; - startpos=buf; - - while (startpos) { - startpos=strnchr(startpos, *chr, len-(startpos-buf)); - - if (startpos) { - /* possibly present in this buffer */ - chr++; - pos=startpos+1; - while (pos) { - if (tolower(*pos)==tolower(*chr)) { - chr++; - pos++; - } - if (pos-buf>=len){ - /* end of data */ - temp_filter->filter(buf,len,filter_list, temp_filter, flags, temp_filter->data); - return; - } - if (*chr=='\0') { - /* end of search string - we've got a match */ - /* send the first set of data */ - if (startpos-buf) - temp_filter->filter(buf,startpos-buf,filter_list, temp_filter, flags, temp_filter->data); - /* send the filter set of data */ - temp_filter->filter(config->replace,replacelen,filter_list, temp_filter, flags, temp_filter->data); - /* and the rest */ - temp_filter->filter(startpos+searchlen,len-(startpos-buf)-searchlen,filter_list, temp_filter, flags,temp_filter->data); - return; - } - if(tolower(*pos)!=tolower(*chr)) { - /* reset the search criteria to after the corrent position */ - startpos=pos; - pos=NULL; - chr=searchstring; - } - } - } - else { - temp_filter->filter(buf,len,filter_list, temp_filter, flags, temp_filter->data); - return; + FILTER_list *temp_filter; + OnUnloadState *state = data; + OnUnloadConfig *config = state->config; + char *chr, *searchstring = config->searchstring; + const char *startpos, *pos, *lastmatch; + size_t replacelen = strlen (config->replace), searchlen = + strlen (config->searchstring); + + /* this looks for browser DOM handlers of the type onunload="..." and + * removes them from the data passed to the client + * caveats and problems: + * 1. theres probably a set of library functions for matching patterns at arbitrary + * places in a datastream (ie handles buffer edge issues, optimum search patterns + * and the like. Well this is a proof of concept filter - live with it :-] + * 2. Some code may not quote the handler properly. I haven't tried to build + * a full blown parser here - see 1. + * + * 3. I'm making use of the properties of the fixed search string to simplify + * the search. IT IS NOT GENERAL PURPOSE + */ + + temp_filter = filters->node.next->data; + state->seen += len; + if (state->seen == len) + { + /* skip the header packet - we don't care about it */ + debug (33, 8) ("clientFilterOnUnload: skipping header block\n"); + temp_filter->filter (buf, len, filter_list, temp_filter, flags, + temp_filter->data); + return; } + startpos=buf; + chr = searchstring; + pos = 0; + lastmatch = startpos; + + while (startpos) { + startpos = strnchr (startpos, *chr, len - (startpos - buf)); + + if ((pos=startpos)) { + while (pos) { + if (tolower (*pos) == tolower (*chr)) { + chr++; + pos++; + } + if (pos - buf >= len) { + /* end of data */ + temp_filter->filter (lastmatch, len - (lastmatch - buf), + filter_list, temp_filter, flags, + temp_filter->data); + return; + } + if (*chr == '\0') { + /* end of search string - we've got a match */ + /* send the first set of data */ + if (startpos - lastmatch) + temp_filter->filter (lastmatch, startpos - lastmatch, + filter_list, temp_filter, flags, + temp_filter->data); + /* send the filter set of data */ + temp_filter->filter (config->replace, replacelen, + filter_list, temp_filter, flags, + temp_filter->data); + lastmatch = startpos + searchlen; + /* update the pointers */ + startpos = pos; + pos = NULL; + chr = searchstring; + +#if 0 + /* and the rest */ + temp_filter->filter (startpos + searchlen, + len - (startpos - buf) - searchlen, + filter_list, temp_filter, flags, + temp_filter->data); + return; +#endif + } + else if (tolower (*pos) != tolower (*chr)) + { + /* reset the search criteria to after the corrent position */ + startpos = pos; + pos = NULL; + chr = searchstring; + } + } + } } - temp_filter->filter(buf,len,filter_list, temp_filter, flags, temp_filter->data); + if (len- (lastmatch-buf)) + /* found nothing */ + temp_filter->filter(lastmatch,len - (lastmatch-buf) ,filter_list, temp_filter, flags, temp_filter->data); } - - -