1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html><head> 3 <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>mod_sed apache filter</title></head><body> 4 <h1>Filtering the content with sed</h1> 5 <a href="#Introduction">1.1 Introduction</a><br> 6 <a href="#Configuration">1.2 Configuration</a><br> 7 <a href="#LoadingModule">1.2.1 8 Loading a module</a><br> 9 <a href="#AddingAFilter">1.2.2 10 Adding a filter</a><br> 11 <a href="#AddingOutputFilter">1.2.2.1 Adding a output filter</a><br> 12 <a href="#AddingInputFilter">1.2.2.2 Adding a input filter</a><br> 13 <a href="#SedScripts">1.2.3 14 Sed scripts</a><br> 15 <a href="#RemovingFilter">1.2.4 16 Removing a filter</a><br> 17 <a href="#Examples">1.2.5 18 Examples</a><br> 19 <a href="#Interfaces">1.3 Interfaces</a><br> 20 <a href="#ExportedInterfaces">1.3.1 21 Exported Interfaces</a><br> 22 <a href="#ApacheVersion">1.3.2 23 Apache version requirement</a><br> 24 <hr size="2" width="100%"> 25 <h2><a name="Introduction"></a>1.1 Introduction</h2> 26 Sed is a line oriented editor. It is commonly used for search 27 and 28 replace functionality. In Apache, traditionally sed is used as an <a href="http://httpd.apache.org/docs/2.0/mod/mod_ext_filter.html">external 29 content filter</a>.<br> 30 <code><br> 31 # mod_ext_filter directive to define a filter which<br> 32 # replaces text in the response<br> 33 ExtFilterDefine external_sed mode=output intype=text/html <span class="indent">cmd="/bin/sed s/california/CA/g"<br> 34 </span> <br> 35 <Location /><br> 36 <span class="indent">SetOutputFilter external_sed<br> 37 </span> </Location><br> 38 <br> 39 </code>In the above example external process /bin/sed is used to 40 replace the string "california" to "CA". For every filter invocation, a 41 new process "/bin/sed" is created which takes input on standard input 42 and produces the filtered content on standard output. The above 43 technique works fine but it doesn't perform well. Process creation is 44 very costly for every request. Process creation on multithreaded 45 process might be even more costlier. Also sed may not be available on 46 all platforms. <br> 47 <br> 48 mod_sed is a in-process content filter. The filters implement the sed 49 edit commands implemented by Solaris 10 sed 50 program as described in <a href="http://docs.sun.com/app/docs/doc/816-5165/sed-1b?a=view">man 51 page</a>. However unlike sed, mod_sed doesn't take data from 52 standard 53 input. Instead filter act on the entity data sent between client and 54 server. mod_sed can be used as a input or output filter. mod_sed is a 55 content filter which means that it can not be used to modify client or 56 server http headers.<br> 57 <br> 58 <span style="font-weight: bold;"></span>mod_sed 59 output filter accept a chunk of data and execute 60 the sed scripts on data and generates the output which is passed to 61 next filter in the filter chain.<br> 62 <br> 63 mod_sed input filter reads the data from next filter in filter chain 64 and 65 executes the sed scripts and returns the generated data to 66 caller filter in the filter chain.<br> 67 <br> 68 Both input and output filter only process the data if new line 69 character is seen in the content. At the end of the data, rest of the 70 data is treated as last line.<br> 71 <h2><a name="Configuration"></a>1.2 72 Configuration</h2> 73 <h3><a name="LoadingModule"></a>1.2.1 Loading 74 a module</h3> 75 Apache modules can be 76 compiled statically 77 into the httpd process or separate dynamic shared object (dso). If 78 mod_sed is compiled as a dso then it needs to be loaded using Apache's <a href="http://httpd.apache.org/docs/2.2/mod/mod_so.html#loadmodule">LoadModule</a> 79 directive before it can be used e.g<br> 80 <span style="font-family: monospace;">LoadModule sed_module 81 modules/mod_sed.so</span> 82 <h3><a name="AddingAFilter"></a>1.2.2 Adding a 83 filter</h3> 84 Sed 85 filter could be inserted by Apache 86 directives <a href="http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addinputfilter">AddInputFilter</a> 87 or <a href="http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addoutputfilter">AddOutputFilter</a> 88 (or by 89 <a href="http://httpd.apache.org/docs/2.2/mod/core.html#setinputfilter">SetInputFilter</a>/<a href="http://httpd.apache.org/docs/2.2/mod/core.html#setoutputfilter">SetOutputFilter</a>) 90 or similar directives. <br> 91 <h4><a name="AddingOutputFilter"></a>1.2.2.1 92 Adding a output filter</h4> 93 Following directive can be used to add a Sed output filter to 94 html files.<br> 95 <span style="font-family: monospace;">AddOutputFilter Sed 96 <extension></span><br> 97 e.g<br> 98 <span style="font-family: monospace;">AddOutputFilter Sed 99 html</span><br> 100 <h4><a name="AddingInputFilter"></a>1.2.2.2 101 Adding a input filter</h4> 102 Following directive can be used to add a Sed input filter to 103 php scripts.<br> 104 <span style="font-family: monospace;">AddInputFilter Sed 105 <extension><br> 106 e.g<br style="font-family: monospace;"> 107 </span><span style="font-family: monospace;">AddInputFilter 108 Sed php</span> 109 <h3><a name="SedScripts"></a>1.2.3 Sed scripts</h3> 110 Sed scripts can be provided for input and output filter by <a href="#SedRequestFilter">InputSed</a> 111 and <a href="#SedResponseFilter">OutputSed</a> 112 mod_sed directives respectively. For example :<br> 113 <span style="font-family: monospace;">OutputSed "script"<br> 114 OutputSed "script"</span><br> 115 InputSed and OutputSed takes single argument. These directives can be 116 used multiple times to specify multiple scripts. These sed scripts will 117 get executed in the order of their appearance in 118 configuration file.<br> 119 <h3><a name="RemovingFilter"></a>1.2.4 120 Removing a filter</h3> 121 Sed filter could be removed by <a href="http://httpd.apache.org/docs/2.2/mod/mod_mime.html#removeoutputfilter">RemoveOutputFilter</a> 122 or <a href="http://httpd.apache.org/docs/2.2/mod/mod_mime.html#removeinputfilter">RemoveInputFilter</a> 123 Apache directives. 124 <h3><a name="Examples"></a>1.2.5 Examples</h3> 125 Adding a output filter :<br> 126 <span style="font-family: monospace;"><Directory 127 "/var/www/docs/sed"></span><br style="font-family: monospace;"> 128 <span style="font-family: monospace;"> 129 AddOutputFilter Sed html</span><br style="font-family: monospace;"> 130 <span style="font-family: monospace;"> OutputSed "s/california/CA/g"<br> 131 OutputSed "s/washington/WA/g"</span><br style="font-family: monospace;"> 132 <span style="font-family: monospace;"></Directory></span><br style="font-family: monospace;"> 133 <br> 134 The above example will replace the string "california" to "CA" and the 135 string "washington" to WA in html document before sending to client.<br> 136 <br> 137 Adding an input filter :<br> 138 <span style="font-family: monospace;"><Directory 139 "/var/www/docs/sed"></span><br style="font-family: monospace;"> 140 <span style="font-family: monospace;"> 141 142 AddInputFilter Sed php</span><br style="font-family: monospace;"> 143 <span style="font-family: monospace;"> 144 InputSed "s/california/CA/gi"</span><br style="font-family: monospace;"> 145 <span style="font-family: monospace;"></Directory></span><br style="font-family: monospace;"> 146 <br> 147 In the above example, the string "california" will be replaced to "CA" 148 in any request data provided to php scripts.<br> 149 <br> 150 Removing a output filter :<br> 151 <span style="font-family: monospace;"><Directory 152 "/var/www/docs/sed/subdir1"></span><br style="font-family: monospace;"> 153 <span style="font-family: monospace;"></span><span style="font-family: monospace;"> 154 RemoveOutputFilter Sed html</span><br style="font-family: monospace;"> 155 <span style="font-family: monospace;"></Directory></span><br> 156 <h2><a name="Interfaces"></a>1.3 Interfaces</h2> 157 <h3><a name="ExportedInterfaces"></a>1.3.1 158 Exported Interfaces </h3> 159 <table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2"> 160 <tbody> 161 <tr> 162 <td style="font-weight: bold;">Interface</td> 163 <td style="font-weight: bold;">Classification</td> 164 <td><span style="font-weight: bold;">Context</span></td> 165 <td style="font-weight: bold;">Description</td> 166 </tr> 167 <tr> 168 <td>Sed</td> 169 <td>Filter name.</td> 170 <td>Filter handling directives</td> 171 <td>Filter can be added using several possible Apache 172 directives. 173 Name of the filter tells Apache which filter to add. It can be used for 174 both input and output filter.</td> 175 </tr> 176 <tr> 177 <td>sed_module</td> 178 <td>Module name</td> 179 <td>-</td> 180 <td>If mod_sed is compiled as a dso, module name must be 181 provided as an argument to <a href="http://httpd.apache.org/docs/2.2/mod/mod_so.html#loadmodule">LoadModule</a></td> 182 </tr> 183 <tr> 184 <td><a name="SedResponseFilter"></a>OutputSed</td> 185 <td>Directive</td> 186 <td>directory</td> 187 <td>Directive to specify the multiple sed scripts to output 188 filter.</td> 189 </tr> 190 <tr> 191 <td><a name="SedRequestFilter"></a>InputSed</td> 192 <td>Directive</td> 193 <td>directory</td> 194 <td>Directive to specify the multiple sed scripts to input 195 filter.</td> 196 </tr> 197 198 199 200 </tbody> 201 </table> 202 <h4><a name="ApacheVersion"></a>1.3.2 Apache 203 version requirement :</h4> 204 Module will work from 205 Apache 2.2.x and above.<br> 206 <br> 207 </body></html> 208