Decoding M-Business Client HTTP request headers

Note

Information for select HTTP request headers is available to the M-Business Sync Server through server URL macros, without the need to retrieve or decode it. For more information, see Using server URL macros to customize content.

OK, I retrieved the value of my M-Business Client HTTP headers, and it is all gobbledygook! What is wrong?

This is where it gets a little tricky. Most of the values of M-Business Client 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.

Note that iAnywhere Solutions 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.

#!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 M-Business Client headers # # 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'll only
look for the M-Business Client # 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 Web site
that puts PalmOS users first!\n"; } elsif ($deviceos eq 'WINCE_OS') {
print "<p>The Web site that puts Pocket PC users first!\n"; }
else { print "<p>The Web site that puts non-Palm and Pocket PC users
first!\n"; } print
"</center></body></HTML>\n"; 
# # Copyright ©) 2000, iAnywhere Solutions, Inc., # all rights
reserved. # # IANYWHERE 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. IANYWHERE 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.

<?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 Web site that puts PalmOS users first!\n"); } elseif
($deviceos == 'WINCE_OS') { print ("<p>The Web site that puts Pocket
PC users first!\n"); } else { print ("<p>The Web site that puts
Pocket PC and non- Palm users first!\n"); } ?>
 </center> </body> </html> <?php # #
Copyright ©) 2000, iAnywhere Solutions, Inc., # all rights reserved. # #
IANYWHERE 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. IANYWHERE SHALL NOT BE # LIABLE FOR ANY DAMAGES SUFFERED BY
LICENSEE AS A RESULT # OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR #
ITS DERIVATIVES. ?>