mod_sed is already part of
httpd trunk.
Old version of sources is available at webstack/mod_sed
After the module was accepted into apache trunk, above link is now
unmaintained.
To download mod_sed, either download files from httpd trunk or from
opensolaris website

Friday Aug 22, 2008

Tracking apr calls in Apache
pid provider can be used to trace apr calls in apache. At first step we need
to create probes for the apache processes. Following script will create probes
for a single process.

Friday Aug 15, 2008

Using mod_sed to filter Web Content in apache
mod_sed is a apache module which filters the web content using powerful sed
commands whether is generated by php, jsp or a plain html.
Basic configuration information can been seen from the
README.
In this blog, I will cover how cryptic but powerful sed commands can be used
inside apache.

Using branches "b" to implement if/else type of code
Suppose I want to write

Sed's G command append the hold buffer into the current line (Pattern space).
Inside apache, we can do the same thing using OutputSed directives :

OutputSed "h"
OutputSed "s/Sunday/Monday/"
OutputSed "G"

Multiline expression using hold buffer and commands "N", "x", "h" and "H"
Sed is very powerful to handle multi line text manipulation.
Suppose, I have a condition which says :
'If a line contain "Sunday" and next line contain "Monday" then replace
"Sunday" in first line to "Monday" and replace "Monday" to "Tuesday" in second
line.'
As a example, I have a text :

It is Sunday today.
Tomorrow will be Monday.

The output should look like :

It is Monday today.
Tomorrow will be Tuesday.

So I want to do the following (pseudo code)

search for Sunday in current line
if found then
saveline=curline
Read next line into curline
search for Tuesday in second line
if found then
swap curline and readline
replace Sunday to Monday in curline
swap curline and readline again.
replace Monday to Tuesday in curline
saveline = saveline + curline
curline = saveline
end innerif
end outerif

Next line can be read by "N" command.
swap functionality is provided by "x" sed command.
Appending saveline with curline is provided by "H" command.
replacing "curline" with "saveline" is provided by "g" command.
Overall sed script will look like :

/Sunday/ {
# save the current line in hold buffer
h
# Delete the content of the current line.
s/.\*//
# Read next line.
N
# Delete first new line character (from previous line)
s/\^.//
# Search for Monday in next line.
/Monday/ {
# Exchange hold buffer from current line
x
# Now current line contain 1st line so replace Sunday with Monday.
s/Sunday/Monday/
# Exchange hold buffer from current line
x
# Now current line contain 2nd line so replace Monday with Tuesday.
s/Monday/Tuesday/
# Append hold buffer (1st line) with 2nd line.
H
# Replace hold buffer with current line
g
}
}

Inside apache httpd.conf, I will write the equivalent sed script as following :

Little history behind mod_sed filter module
Sun has donated the "sed" filtering module mod_sed to Apache Software
Foundation. It is not yet part of the Apache Web Server. It is under
consideration with Apache httpd dev community.

In this blog, I will cover the history behind the mod_sed code.
Solaris 10 has two separate "sed" utilities, one in /usr/bin/sed and another
in /usr/ucb/sed. The later one is open sourced under CDDL and available
in opensolaris. Sun Java System Web Server 7.0 initially included sed filter
module. Sun Web Server filter module was derived from "/usr/bin/sed" code and
it was written by Chris Elving

Last year, I took the Sun Web Server code and wrote the mod_sed based on Web
Server code. The difference between Sun Web Server sed filter module and
mod_sed is that mod_sed is derived from /usr/ucb/sed code. Sun Web Server's sed
filtering module uses NSPR for portable API while mod_sed uses APR since it
runs under Apache which uses APR for portability.

Functionality wise "/usr/bin/sed" code was little better than "/usr/ucb/sed"
but I have fixed some of the limitation of "/usr/ucb/sed" in mod_sed e.g max
number of characters in a line or hold buffer.