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