Commit 8c037f1f authored by Nick Kew's avatar Nick Kew
Browse files

Commit mod_sed: enable filtering of HTTP Requests and Responses through sed


git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@691418 13f79535-47bb-0310-9956-ffa450edef68
parent 0ec9f88c
Loading
Loading
Loading
Loading
+3 −0
Changes for CHANGES: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,9 @@
Changes with Apache 2.3.0
[ When backported to 2.2.x, remove entry from this file ]

  *) New module mod_sed: filter Request/Response bodies through sed
     [Basant Kumar Kukreja <basant.kukreja sun.com>]

  *) mod_auth_form: Make sure that basic authentication is correctly
     faked directly after login. [Graham Leggett]

+141 −0
Changes for docs/manual/mod/mod_sed.xml: 141 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?xml version="1.0"?>
<!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
<?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<modulesynopsis metafile="mod_sed.xml.meta">

<name>mod_sed</name>
<description>Filtering Input (request) and Output (response) content using sed commands</description>
<status>Experimental</status>
<sourcefile>mod_sed.c sed0.c sed1.c regexp.c regexp.h sed.h</sourcefile>
<identifier>sed_module</identifier>
<compatibility>Available in Apache 2.3 and later</compatibility>

<summary>
<p>
mod_sed is a in-process content filter. mod_sed filter implement the sed edit
commands implemented by Solaris 10 sed
program as described in <a href="http://docs.sun.com/app/docs/doc/816-5165/sed-1b?a=view">man
page</a>. However unlike sed, mod_sed doesn't take data from
standard
input. Instead filter act on the entity data sent between client and
server. mod_sed can be used as a input or output filter. mod_sed is a
content filter which means that it can not be used to modify client or
server http headers.
</p>
<p>
mod_sed output filter accept a chunk of data and execute the sed scripts on data and generates the output which is passed to next filter in the filter chain.
</p>

<p>
mod_sed input filter reads the data from next filter in filter chain and executes the sed scripts and returns the generated data to caller filter in the filter chain.
</p>

<p>
Both input and output filter only process the data if new line character is seen in the content. At the end of the data, rest of the data is treated as last line.
</p>

<p>A tutorial article on mod_sed, and why it is more powerful than simple
string or regular expression search and replace, is available in <a
href="http://blogs.sun.com/basant/entry/using_mod_sed_to_filter">on
the author's blog</a>.</p>

</summary>

<directivesynopsis>
<name>OutputSed</name>
<description>Sed command for filter the response content</description>
<syntax>OutputSed <var>sed-command</var></syntax>
<contextlist><context>directory</context><context>.htaccess</context>
</contextlist>

<usage>
    <p>The <directive>OutputSed</directive> directive specify the sed
    command which will be executed on the response.
    </p>
</usage>
</directivesynopsis>

<directivesynopsis>
<name>InputSed</name>
<description>Sed command to filter the request data (typically post data)</description>
<syntax>InputSed <var>sed-command</var></syntax>
<contextlist><context>directory</context><context>.htaccess</context>
</contextlist>

<usage>
    <p>The <directive>InputSed</directive> directive specify the sed command
    which will be executed on the request data e.g POST data.
    </p>
</usage>
</directivesynopsis>

<section id="sampleconf"><title>Sample Configuration</title>
    <example><title>Adding a output filter </title>
         # In following example, sed filter will replace the string <br />
         # "monday" to "MON" and the string "sunday" to SUN in html document <br />
         # before sending to client. <br />
        <indent>
        &lt;Directory "/var/www/docs/sed"&gt; <br />
           <indent>
           AddOutputFilter Sed html <br />
           OutputSed "s/monday/MON/g" <br />
           OutputSed "s/sunday/SUN/g" <br />
           </indent>
        &lt;/Directory&gt; <br />
        </indent>
    </example>

    <example><title>Adding a input filter </title>
         # In following example, sed filter will replace the string <br />
         # "monday" to "MON" and the string "sunday" to SUN in the POST data <br />
         # sent to php <br />
        <indent>
        &lt;Directory "/var/www/docs/sed"&gt; <br />
           <indent>
           AddInputFilter Sed php <br />
           OutputSed "s/monday/MON/g" <br />
           OutputSed "s/sunday/SUN/g" <br />
           </indent>
        &lt;/Directory&gt; <br />
        </indent>
    </example>
</section>
<section id="sed_commands"><title>Sed Commands</title>
    <p>
    Complete details of the sed command can be found from
   <a href="http://docs.sun.com/app/docs/doc/816-5165/sed-1b?a=view">sed man
page</a>.
    </p>
    <dl>
        <dt><code>b</code></dt>
        <dd>branch to the label specified (Similar to goto)</dd>
        <dt><code>h</code></dt>
        <dd>Copy the current line to hold buffer.</dd>
        <dt><code>H</code></dt>
        <dd>Append the current line to hold buffer.</dd>
        <dt><code>g</code></dt>
        <dd>Copy the hold buffer into the current line</dd>
        <dt><code>G</code></dt>
        <dd>Append the hold buffer into the current line</dd>
        <dt><code>x</code></dt>
        <dd>Swap the content of hold buffer and current line</dd>
    </dl>
</section>
</modulesynopsis>
+3 −0
Changes for modules/filters/config.m4: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@ APACHE_MODULE(include, Server Side Includes, , , yes)
APACHE_MODULE(filter, Smart Filtering, , , yes)
APACHE_MODULE(substitute, response content rewrite-like filtering, , , most)

sed_obj="mod_sed.lo sed0.lo sed1.lo regexp.lo"
APACHE_MODULE(sed, filter request and/or response bodies through sed, $sed_obj)

if test "$ac_cv_ebcdic" = "yes"; then
# mod_charset_lite can be very useful on an ebcdic system,
#   so include it by default
+175 −0
Changes for modules/filters/libsed.h: 175 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2005, 2008 Sun Microsystems, Inc. All Rights Reserved.
 * Use is subject to license terms.
 *
 *	Copyright (c) 1984 AT&T
 *	  All Rights Reserved  	
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  http://www.apache.org/licenses/LICENSE-2.0. 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 
 * or implied. 
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

#ifndef LIBSED_H
#define LIBSED_H

#ifdef __cplusplus
extern "C" {
#endif

#include <limits.h>

#include "apr_file_io.h"
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif

#define SED_NLINES 256
#define SED_DEPTH 20
#define SED_LABSIZE 50
#define SED_ABUFSIZE 20

typedef struct sed_reptr_s sed_reptr_t;

struct sed_reptr_s {
    sed_reptr_t *next;
    char        *ad1;
    char        *ad2;
    char        *re1;
    sed_reptr_t *lb1;
    char        *rhs;
    int         findex;
    char        command;
    int         gfl;
    char        pfl;
    char        negfl;
    int         nrep;
};

typedef struct sed_label_s sed_label_t;

struct sed_label_s {
    char        asc[9];
    sed_reptr_t *chain;
    sed_reptr_t *address;
};

typedef void (sed_err_fn_t)(void *data, const char *error);
typedef void (sed_write_fn_t)(void *ctx, char *buf, int sz);

typedef struct sed_commands_s sed_commands_t;
#define NWFILES 11 /* 10 plus one for standard output */

struct sed_commands_s {
    sed_err_fn_t *errfn;
    void         *data;

    unsigned     lsize;
    char         *linebuf;
    char         *lbend;
    const char   *saveq;

    char         *cp;
    char         *lastre;
    char         *respace;
    char         sseof;
    char         *reend;
    const char   *earg;
    int          eflag;
    int          gflag;
    int          nflag;
    apr_int64_t  tlno[SED_NLINES];
    int          nlno;
    int          depth;

    char         *fname[NWFILES];
    int          nfiles;

    sed_label_t  ltab[SED_LABSIZE];
    sed_label_t  *labtab;
    sed_label_t  *lab;
    sed_label_t  *labend;

    sed_reptr_t  **cmpend[SED_DEPTH];
    sed_reptr_t  *ptrspace;
    sed_reptr_t  *ptrend;
    sed_reptr_t  *rep;
    int          nrep;
    apr_pool_t   *pool;
    int          canbefinal;
};

typedef struct sed_eval_s sed_eval_t;

struct sed_eval_s {
    sed_err_fn_t   *errfn;
    sed_write_fn_t *writefn;
    void           *data;

    sed_commands_t *commands;

    apr_int64_t    lnum;
    void           *fout;

    unsigned       lsize;
    char           *linebuf;
    char           *lspend;

    unsigned       hsize;
    char           *holdbuf;
    char           *hspend;

    unsigned       gsize;
    char           *genbuf;
    char           *lcomend;

    apr_file_t    *fcode[NWFILES];
    sed_reptr_t    *abuf[SED_ABUFSIZE];
    sed_reptr_t    **aptr;
    sed_reptr_t    *pending;
    unsigned char  *inar;
    int            nrep;

    int            dolflag;
    int            sflag;
    int            jflag;
    int            delflag;
    int            lreadyflag;
    int            quitflag;
    int            finalflag;
    int            numpass;
    int            nullmatch;
    int            col;
    apr_pool_t     *pool;
};

apr_status_t sed_init_commands(sed_commands_t *commands, sed_err_fn_t *errfn, void *data,
                               apr_pool_t *p);
apr_status_t sed_compile_string(sed_commands_t *commands, const char *s);
apr_status_t sed_compile_file(sed_commands_t *commands, apr_file_t *fin);
char* sed_get_finalize_error(const sed_commands_t *commands, apr_pool_t* pool);
int sed_canbe_finalized(const sed_commands_t *commands);
void sed_destroy_commands(sed_commands_t *commands);

apr_status_t sed_init_eval(sed_eval_t *eval, sed_commands_t *commands,
                           sed_err_fn_t *errfn, void *data,
                           sed_write_fn_t *writefn, apr_pool_t *p);
apr_status_t sed_reset_eval(sed_eval_t *eval, sed_commands_t *commands, sed_err_fn_t *errfn, void *data);
apr_status_t sed_eval_buffer(sed_eval_t *eval, const char *buf, int bufsz, void *fout);
apr_status_t sed_eval_file(sed_eval_t *eval, apr_file_t *fin, void *fout);
apr_status_t sed_finalize_eval(sed_eval_t *eval, void *f);
void sed_destroy_eval(sed_eval_t *eval);

#ifdef __cplusplus
}
#endif

#endif /* LIBSED_H */
+437 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading