Developers
AvantGo Channel Developer Guide

TOC PREV NEXT INDEX


Decoding AvantGo Client HTTP request headers

OK, I retrieved the value of AvantGo HTTP headers, and it is all gobbledygook! What is wrong?

This is where it gets a little tricky. Most of the values of AvantGo headers are Base64-encoded before they are sent across. So you might find that your X-AvantGo-Screensize header has a value equal to something like MTUweDE1MA==. You will need to decode these headers before you can actually deal with them in a reasonable manner.

Decoding a Base64 Encoded string is generally pretty easy, albeit a little obscure. It is a little different depending on what you are using to serve up pages:

  • If you are using Perl, include a use MIME::Base64 line at the beginning of your script to use Perl's built-in Base64 library. Then you can use the decode_base64() function to decode whatever string you would like.
  • If you are using PHP, you can use the base64_decode() function.
  • If you are using Java Servlets, Sun has a javax.commerce.util package that includes a Base64Decoder class — you can find more information about it at:
  • http://java.sun.com/

  • If you are using ColdFusion, some readers have sent in useful suggestions for decoding Base64 headers. You can find these at:
  • http://www.avantgo.com/doc/developer/channels/creating/advanced/coldfusion.html

Please note that AvantGo cannot provide support for the various third-party libraries. You are better off contacting these third parties directly.

For example, here is a Perl script that prints a custom front page based on the user's mobile device. It analyzes the bit depth and chooses a logo based on that value. Then it analyzes the device's operating system and prints out some text based on that value.

Figure 6-1 Perl script to taylor page to user device

#!D:\Perl\bin\perl.exe

use CGI qw/:standard/;

use MIME::Base64;

#      devicecontext.pl

#

# Todd's sample code for changing images and text

# based on what AvantGo headers we receive

#

# Note that I am using Perl on a Apache Server on an

# NT machine, hence the weird first line.

  

# Just print out the beginning...

  

print "Content-type: text/html\n";

print "Cache-Control: max-age=7200\n";

  

print <<END_of_Start;

<HTML>

   <HEAD>

   <TITLE>Greetings</TITLE>

   <META Name="HandheldFriendly" content="True">

   </HEAD>

   <BODY>

END_of_Start

  

# And now process the headers.

#

# We're only going to look for the AvantGo headers

# we're interested in (COLORDEPTH and DEVICEOS)

#

# They need to be base64-decoded before we can do

# anything useful to them so I'm using the decode_base64

# function that's included in Perl's MIME:Base64 library.

  

$colordepth = decode_base64(http('HTTP_X_AVANTGO_COLORDEPTH'));

$deviceos = decode_base64(http('HTTP_X_AVANTGO_DEVICEOS'));

  

print "<center><b>Greetings!</b><p>And welcome

to...<\p>\n";

  

  

if ($colordepth > 2) {

# If the colordepth is greater than 2, print the color logo

print "<img src=\"/colorlogo.gif\" alt=\"MyCompany.com\">\n ";,

  

} else {

  # Otherwise, we'll go with the black and white logo

  print "<img src=\"/bwlogo.gif\"

  alt=\"MyCompany.com\">\n";

  

}

  

if ($deviceos eq 'PALM_OS') {

  print "<p>The website that puts PalmOS users first!\n";

} elsif ($deviceos eq 'WINCE_OS') {

  print "<p>The website that puts Pocket PC users

    first!\n";

} else {

  print "<p>The website that puts non-Palm and Pocket PC

    users first!\n";

}

  

print "</center></body></HTML>\n";

  

#

# Copyright (c) 2000, AvantGo, Inc., all rights reserved.

#

# AVANTGO MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT

# THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR

# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED

# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR

# PURPOSE, OR NON-INFRINGEMENT. AVANTGO SHALL NOT BE

# LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT

# OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR

# ITS DERIVATIVES.

  

Here is the same page in PHP.

Figure 6-2 PHP script to taylor page to user device

<?php   header("Cache-Control: max-age=7200");

?>

<HTML>

<HEAD>

   <TITLE>Greetings</TITLE>

   <META Name="HandheldFriendly" Content="TRUE">

</HEAD>

<BODY>

   <center>

   <b>Greetings!</b>

   <p>And welcome to...</p>

  

<?php

$colordepth=base64_decode(getenv("HTTP_X_AVANTGO_COLORDEPTH"));

$deviceos = base64_decode(getenv("HTTP_X_AVANTGO_DEVICEOS"));

   if ($colordepth > 2) {

   print( "<img src=\"/colorlogo.gif\"

alt=\"MyCompany.com\">\n");

   } else {

   print( "<img src=\"\bwlogo.gif\"

alt=\"MyCompany.com\">\n");

   }

   if ($deviceos == 'PALM_OS') {

   print ("<p>The website that puts PalmOS users

      first!\n");

   } elseif ($deviceos == 'WINCE_OS') {

   print ("<p>The website that puts Pocket PC users

      first!\n");

   } else {

   print ("<p>The website that puts Pocket PC and non-

      Palm users first!\n");

   }

?>

  

   </center>

</body>

</html>

  

<?php

#

# Copyright (c) 2000, AvantGo, Inc., all rights reserved.

#

# AVANTGO MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT

# THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR

# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED

# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR

# PURPOSE, OR NON-INFRINGEMENT. AVANTGO SHALL NOT BE

# LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT

# OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR

# ITS DERIVATIVES.

?>

  



TOC PREV NEXT INDEX