AMFSwitcher.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 187 CC 16 SCAR 43.8
CC
16
Cov
0%
handler
182.0
loadConfigFile
20.0
BEGIN
2.0
CRAP 79.8
1 #file:Apache2/AMFSwitcher.pm;
2 #--------------------------------
3  
4 #
5 # Created by Idel Fuschini
6 # Date: 15/12/09
7 # Site: http://www.idelfuschini.it
8 # Mail: idel.fuschini@gmail.com
9  
10  
11  
12 package Apache2::AMFSwitcher;
13
14 use strict;
15 use warnings;
16 use Apache2::AMFCommonLib ();
17
18 use Apache2::RequestRec ();
19 use Apache2::RequestUtil ();
20 use Apache2::SubRequest ();
21 use Apache2::Log;
22 use Apache2::Filter ();
23 use APR::Table ();
24 use LWP::Simple;
25 use Apache2::Const -compile => qw(OK REDIRECT DECLINED);
26 use IO::Uncompress::Unzip qw(unzip $UnzipError) ;
27 use constant BUFF_LEN => 1024;
28 use vars qw($VERSION);
29 $VERSION= "2.21";
30 #
31 # Define the global environment
32 #
33 my $CommonLib = new Apache2::AMFCommonLib ();
34 my $mobileversionurl="none";
35 my $fullbrowserurl="none";
36 my $redirecttranscoder="true";
37 my $redirecttranscoderurl="none";
38 my %ArrayPath;
39 $ArrayPath{1}='none';
40 $ArrayPath{2}='none';
41 $ArrayPath{3}='none';
42 $CommonLib->printLog("---------------------------------------------------------------------------");
43 $CommonLib->printLog("AMFSwitcher Version $VERSION");
44 if ($ENV{MOBILE_HOME}) {
45 } else {
46 $CommonLib->printLog("MOBILE_HOME not exist. Please set the variable MOBILE_HOME into httpd.conf");
47 $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated");
48 ModPerl::Util::exit();
49 }
50 if ($ENV{LoadWebPatch}) {
51 if ($ENV{LoadWebPatch} eq 'true') {
52 &loadConfigFile();
53 } else {
54 $CommonLib->printLog("LoadWebPatch not exist. Please set the variable LoadWebPatch must be set with true value");
55 $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated");
56 ModPerl::Util::exit();
57 }
58 } else {
59 $CommonLib->printLog("LoadWebPatch must be set. Please set the variable LoadWebPatch into httpd.conf with boolean value (true o false)");
60 $CommonLib->printLog("Pre-Requisite: WURFLFilter must be activated");
61 ModPerl::Util::exit();
62 }
63 sub loadConfigFile {
64 my $null="";
65 my $null2="";
66 my $null3="";
67 my $val;
68 my $capability;
69 my $r_id;
70 my $dummy;
71 $CommonLib->printLog("AMFSwitcher: Start read configuration from httpd.conf");
72 if ($ENV{MobileVersionUrl}) {
73 $mobileversionurl=$ENV{MobileVersionUrl};
74 $ArrayPath{1}=$ENV{MobileVersionUrl};
75 $CommonLib->printLog("MobileVersionUrl is: $mobileversionurl");
76 }
77 if ($ENV{FullBrowserUrl}) {
78 $fullbrowserurl=$ENV{FullBrowserUrl};
79 $ArrayPath{2}=$ENV{FullBrowserUrl};
80 $CommonLib->printLog("FullBrowserUrl is: $fullbrowserurl");
81 }
82 if ($ENV{RedirectTranscoderUrl}) {
83 $redirecttranscoderurl=$ENV{RedirectTranscoderUrl};
84 $ArrayPath{3}=$ENV{RedirectTranscoderUrl};
85 $redirecttranscoder="true";
86 $CommonLib->printLog("RedirectTranscoderUrl is: $redirecttranscoderurl");
87 }
88 $CommonLib->printLog("Finish loading parameter");
89 }
90 sub handler {
91 my $f = shift;
92 my $capability2;
93 my $device_claims_web_support="null";
94 my $is_wireless_device="null";
95 my $is_transcoder="null";
96 my $location="none";
97 my $return_value=Apache2::Const::DECLINED;
98 my $device_type=1;
99 my $no_redirect=1;
100 my $uri=$f->uri();
101 if ($f->pnotes('device_claims_web_support')) {
102 $device_claims_web_support=$f->pnotes('device_claims_web_support')
103 }
104 if ($f->pnotes('is_wireless_device')) {
105 $is_wireless_device=$f->pnotes('is_wireless_device');
106 }
107 if ($f->pnotes('is_transcoder')) {
108 $is_transcoder=$f->pnotes('is_transcoder');
109 }
110 if ($device_claims_web_support eq 'true' && $is_wireless_device eq 'false') {
111 if ($fullbrowserurl ne 'none') {
112 $location=$fullbrowserurl;
113 }
114 $device_type=2;
115 } else {
116 if ($mobileversionurl ne 'none') {
117 $location=$mobileversionurl;
118 }
119 $device_type=1;
120 }
121 if ($is_transcoder eq 'true') {
122 if ($redirecttranscoderurl ne 'none') {
123 $location=$redirecttranscoderurl;
124 }
125 $device_type=3;
126 }
127 if ($ArrayPath{$device_type} eq substr($uri,0,length($ArrayPath{$device_type}))) {
128 $no_redirect=0;
129 }
130 if ($location ne "none" ) {
131 if (substr ($location,0,5) eq "http:") {
132 $f->headers_out->set(Location => $location);
133 $f->status(Apache2::Const::REDIRECT);
134 $return_value=Apache2::Const::REDIRECT;
135 } else {
136 if ($no_redirect==1) {
137 $f->headers_out->set(Location => $location);
138 $f->status(Apache2::Const::REDIRECT);
139 $return_value=Apache2::Const::REDIRECT;
140 }
141 }
142 }
143 return $return_value;
144 }
145  
146 1;
147 =head1 NAME
148  
149 Apache2::AMFSwitcher - Used to switch the device to the apropriate content (mobile, fullbrowser or for transcoder)
150  
151  
152 =head1 COREQUISITES
153  
154 Apache2::RequestRec
155  
156 Apache2::RequestUtil
157  
158 Apache2::SubRequest
159  
160 Apache2::Log
161  
162 Apache2::Filter
163  
164 APR::Table
165  
166 LWP::Simple
167  
168 Apache2::Const
169  
170  
171  
172 =head1 DESCRIPTION
173  
174 This module has the scope to manage with WURFLFilter.pm module the group of device (MobileDevice, PC and transcoder).
175  
176 To work AMFSwitcher has need WURFLFilter configured.
177  
178 For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html
179  
180 An example of how to set the httpd.conf is below:
181  
182 =over 4
183  
184 =item C<PerlSetEnv MOBILE_HOME server_root/MobileFilter>
185  
186 This indicate to the filter where you want to redirect the specific family of devices:
187  
188 =item C<PerlSetEnv FullBrowserUrl http://www.versionforpc.com>
189  
190 =item C<PerlSetEnv MobileVersionUrl http://www.versionformobile.com>
191  
192 =item C<PerlSetEnv PerlSetEnv RedirectTranscoderUrl http://www.versionfortrasncoder.com>
193  
194 =item C<PerlTransHandler +Apache2::AMFSwitcher>
195  
196 =back
197  
198 NOTE: this software need wurfl.xml you can download it directly from this site: http://wurfl.sourceforge.net or you can set the filter to download it directly.
199  
200 =head1 SEE ALSO
201  
202 For more details: http://www.idelfuschini.it/apache-mobile-filter-v2x.html
203  
204 Mobile Demo page of the filter: http://apachemobilefilter.nogoogle.it (thanks Ivan alias sigmund)
205  
206 Demo page of the filter: http://apachemobilefilter.nogoogle.it/php_test.php (thanks Ivan alias sigmund)
207  
208 =head1 AUTHOR
209  
210 Idel Fuschini (idel.fuschini [at] gmail [dot] com)
211  
212 =cut