December 24, 2005

Cookie splitting with Apache XSSI

Reference : http://httpd.apache.org/docs/2.0/mod/mod_include.html


Introduction

Cookies can be split up pretty simply using XSSI's 'if expr' statement and the Perl compatible regular expressions (PCRE) it is capable of.

Simple string testing

Simply testing the cookie for contents, which is just a string like any other when it comes to testing, is just the case of seeing if it contains a value.


<!--#if expr="$HTTP_COOKIE = /BBCNewsAudience=Domestic/" -->
<!--#set var="version" value="Domestic" -->
<!--#endif -->

You will note the '/'s, this is how we test a string for part of its contents, it also enables the regular expressions syntax, so you need to be a little careful with what you are looking for.

If you are trying to test to see if a string IS a certain value, and not contain it then we would use the single quote model. For this to be relavant to our cookie model, first we need to separate the cookie string into its component value pairs (this = that).

Splitting up the cookie

Reference - http://www.bbc.co.uk/opensource/projects/apache/ssi_setsplitvars/docu.shtml


We have a simple, but effective, tool for splitting up strings with patterns of separators and delimiters. What are separators/delimiters you may ask? Of a set of value pairs, e.g. a=4&b=6, '=' is the separator of the pair, and '&' is the delimiter between the pair and the next pair.

The tool is 'setsplitvars', as referenced above, which is only available on BBC servers rather than in the Apache core (but available as an open source module). It is used in this case as follows, where in a cookie string '=' is the separator and ';' is the delimiter in cookies.

<!--#setsplitvars delimiter="; " separator="=" value="$HTTP_COOKIE" -->

So taking an example cookie string of:


BBCNewsUI=2-1-n-5-%5E; BBCNewsAudience=Domestic; BBCNewsAudcWght=-99; BBCMediaSelector=m%3Arm%26b%3Abb%26st%3A

We can split it as above and get the varibles, 'BBCNewsUI' equal to '2-1-n-5-%5E', and so on.

Now at this point we could just test the content of the resulting variables

<!--#if expr="$BBCNewsAudience = 'Domestic'" -->
<!--#set var="Domestic" value="true" -->
<!--#endif -->

Of course that's not so useful, given that we had that at stage 1, but you should note that this time we are testing for a specific value, by using single quotes (') rather than '/'. So the former model could match 'BBCNewsAudience=Domesticfoo.bar.baz', but this model will only match 'Domestic' alone as a discrete value.

Patterned Data

So we've worked out how to see if a variable has a specific value, but what if we don't know what that value is. In this case we use Regular expressions. Regular expressions


Refernce Cookie Data
BBC-UID=2453a833b3c9e20423088438713c91413aef9dddb0f06093eb9aec287bc5ddaa0Mozilla%2f5%2e0%20%28Windows%3b%20U%3b%20Windows%20NT%205%2e0%3b%20en%2dUS%3b%20rv%3a1%2e7%2e12%29%20Gecko%2f20050915%20Firefox%2f1%2e0%2e7; BBCpostcoder=PSTCM17 0DL:QRYCM17 0DL:LEA881:TVR4164.4228:WEA4585:CCI22:CON300:CTY11:CRM:DSTCM17:EUR6:LAU22UJ:LST32:NHSQ03:TWN675:RAD52273:WRD22UJGA:TIM:WIL14:MATpostcode; BBCNewsUI=2-1-n-5-%5E; BBCNewsAudience=Domestic; BBCNewsAudcWght=-99; BBCMediaSelector=m%3Arm%26b%3Abb%26st%3A

Posted by nickh at 02:17 PM | Comments - 0

May 11, 2005

SSI - Regular Expressions

Since the advent of Apache 2.0.x you have been able to do regular expressions in apache ssi. Meaning less reliance for me at least on .htaccess files and mod_rewrite.

This has lead me to be able to quickly make template files that pass the query string in multiple parts to the page to dynamically change the content.

Hence we have the Benjamin Gallery and examples of the query string in use (bits echoed in red)-

http://www.cight.com/benjamin/gallery/august/ tmpl.shtml?src=big/big_img_2421.jpg; comm=Flashdance%20Benjamin

Using code as follows

<!--#if expr="$QUERY_STRING = /src=([^;]*)/" --><!--#set var="picture" value="$1" --><!--#endif -->

- find the src= bit find and capture what is after it, until you find a ; then set that as a variable called picture.

<!--#if expr="$picture = /([0-9][0-9][0-9][0-9])/" --><!--#set var="number" value="$1" --><!--#endif -->

- take the pciture variable now created, and look in it for 4 digits, save that as a variable called number.

<!--#if expr="$QUERY_STRING_UNESCAPED = /comm=([^;]*)/" --><!--#set var="comment" value="$1" --><!--#endif -->

- find the comm= bit find and capture what is after it, until you find a ; then set that as a variable called comment. Unfortunately if you use punctuation this ends up escaped if you then echo it, meaning ? becomes \?. And a weirdness in apache won't let you get rid of the \.

Then you can use the variables you have to put comments on the page, link to the larger version (like in the example) and back without chnaging template.

Also it makes the gallery page very easy to create, you just put in the urls of the images and wrap template code to each one (go view source)

Posted by nickh at 04:44 PM | Comments - 0

Tutorials?

i think I'm going to start putting some example code here for doing Apache server-side include and .htaccess manipulation.

Hand hint no. one - printenv is your friend.

<pre>
<!--#printenv -->
</pre>

Bascially, putting this on a page will show the current environment variables on the page (at the part of the page at which it is placed). The 'pre' tag will ensure when rendered in the browser each line of environment will appear on its own line.

Posted by nickh at 12:10 AM | Comments - 0
s