File Coverage

blib/lib/AxKit/XSP/WebUtils.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # $Id: WebUtils.pm,v 1.9 2003/07/10 09:43:20 matt Exp $
2              
3             # Original Code and comments from Steve Willer.
4              
5             package AxKit::XSP::WebUtils;
6              
7             $VERSION = "1.6";
8              
9             # taglib stuff
10 1     1   8494 use AxKit 1.4;
  0            
  0            
11             use Apache;
12             use Apache::Constants qw(OK);
13             use Apache::Util;
14             use Apache::Request;
15             use Apache::URI;
16             use Apache::AxKit::Language::XSP::TaglibHelper;
17             sub parse_char { Apache::AxKit::Language::XSP::TaglibHelper::parse_char(@_); }
18             sub parse_start { Apache::AxKit::Language::XSP::TaglibHelper::parse_start(@_); }
19             sub parse_end { Apache::AxKit::Language::XSP::TaglibHelper::parse_end(@_); }
20              
21             $NS = 'http://axkit.org/NS/xsp/webutils/v1';
22              
23             @EXPORT_TAGLIB = (
24             'env_param($name)',
25             'path_info()',
26             'query_string()',
27             'request_uri()',
28             'request_host()',
29             'server_root()',
30             'redirect($uri;$host,$secure,$use_refresh)',
31             'url_encode($string)',
32             'url_decode($string)',
33             'header($name;$value)',
34             'return_code($code)',
35             'username()',
36             'password()',
37             'request_parsed_uri(;$omit)',
38             'request_prev_parsed_uri(;$omit)',
39             'request_prev_uri()',
40             'request_prev_query_string()',
41             'request_prev_param($name)',
42             'match_useragent($name)',
43             'is_https()',
44             'is_initial_req()',
45             'variant_list():as_xml=true',
46             'error_notes()',
47             'server_admin()',
48             );
49              
50             @ISA = qw(Apache::AxKit::Language::XSP);
51              
52             use strict;
53              
54             sub env_param ($) {
55             my ($name) = @_;
56              
57             return $ENV{$name};
58             }
59              
60             sub path_info () {
61             my $Request = AxKit::Apache->request;
62             return $Request->path_info;
63             }
64              
65             sub query_string () {
66             my $Request = AxKit::Apache->request;
67             return $Request->query_string;
68             }
69              
70             sub request_uri () {
71             my $Request = AxKit::Apache->request;
72             return $Request->uri;
73             }
74              
75             sub server_root () {
76             my $Request = AxKit::Apache->request;
77             return $Request->document_root;
78             }
79              
80             sub request_host () {
81             my $hostname = Apache->header_in('Via');
82             $hostname =~ s/^[0-9.]+ //g;
83             $hostname =~ s/ .*//g;
84             $hostname ||= $ENV{HTTP_HOST};
85             $hostname ||= Apache->header_in('Host');
86             return $hostname;
87             }
88              
89             sub redirect ($;$$$) {
90             my ($uri, $host, $secure, $use_refresh) = @_;
91            
92             if (lc($secure) eq 'yes') { $secure = 1 }
93             elsif (lc($secure) eq 'no') { $secure = 0 }
94             if (lc($use_refresh) eq 'yes') { $use_refresh = 1 }
95             elsif (lc($use_refresh) eq 'no') { $use_refresh = 0 }
96            
97             my $myhost = $host;
98              
99             my $Request = AxKit::Apache->request;
100              
101             if ($uri !~ m|^https?://|oi) {
102             if ($uri !~ m#^/#) {
103             $uri = "./$uri" if $uri =~ /^\./;
104              
105             # relative path, so let's resolve the path ourselves
106             my $base = $Request->uri;
107             $base =~ s{[^/]*$}{};
108             $uri = "$base$uri";
109             $uri =~ s{//+}{/}g;
110             $uri =~ s{/.(/|$)}{/}g; # embedded ./
111             1 while ($uri =~ s{[^/]+/\.\.(/|$)}{}g); # embedded ../
112             $uri =~ s{^(/\.\.)+(/|$)}{/}g; # ../ off of "root"
113             }
114              
115             if (not defined $host) {
116             $myhost = $Request->header_in("Host");
117              
118             # if we're going through a proxy, the virtual host is rewritten; yuck
119             if ($myhost !~ /[a-zA-Z]/) {
120             my $Server = $Request->server;
121             $myhost = $Server->server_hostname;
122             my $port = $Server->port;
123             $myhost .= ":$port" if $port != 80;
124             }
125             }
126            
127             my $scheme = 'http';
128             $scheme = 'https' if $secure; # Hmm, might break if $port was set above...
129             if ($use_refresh) {
130             $Request->header_out("Refresh" => "0; url=${scheme}://${myhost}${uri}");
131             $Request->content_type("text/html");
132             $Request->status(200);
133             }
134             else {
135             $Request->header_out("Location" => "${scheme}://${myhost}${uri}");
136             $Request->status(302);
137             }
138             }
139             else {
140             if ($use_refresh) {
141             $Request->header_out("Refresh" => "0; url=$uri");
142             $Request->content_type("text/html");
143             $Request->status(200);
144             }
145             else {
146             $Request->header_out("Location" => $uri);
147             $Request->status(302);
148             }
149             }
150            
151             $Request->send_http_header;
152            
153             Apache::exit();
154             }
155              
156             sub header ($;$) {
157             my $name = shift;
158             my $r = AxKit::Apache->request;
159            
160             if (@_) {
161             return $r->header_out($name, $_[0]);
162             }
163             else {
164             return $r->header_in($name);
165             }
166             }
167              
168             sub url_encode ($) {
169             return Apache::Util::escape_uri(shift);
170             }
171              
172             sub url_decode ($) {
173             return Apache::Util::unescape_uri(shift);
174             }
175              
176             sub return_code ($) {
177             my $code = shift;
178              
179             my $Request = AxKit::Apache->request;
180              
181             $Request->status($code);
182            
183             $Request->send_http_header;
184            
185             Apache::exit();
186             }
187              
188             sub username () {
189             my $r = AxKit::Apache->request;
190            
191             return $r->connection->user;
192             }
193              
194             sub password () {
195             my $r = AxKit::Apache->request;
196            
197             my ($res, $pwd) = $r->get_basic_auth_pw;
198             if ($res == OK) {
199             return $pwd;
200             }
201             return;
202             }
203              
204             sub request_parsed_uri ($) {
205             my $omit = shift;
206             my $r = AxKit::Apache->request;
207             my $uri = Apache::URI->parse($r);
208              
209             if ($omit eq 'path') {
210             $uri->path(undef);
211             $uri->query(undef); # we don't want a query without a path
212             }
213             elsif ($omit eq 'path_info' or $omit eq 'query') {
214             $uri->$omit(undef);
215             }
216              
217             return $uri->unparse;
218             }
219              
220             sub request_prev_parsed_uri ($) {
221             my $omit = shift;
222             my $r = AxKit::Apache->request;
223             my $uri = Apache::URI->parse($r->prev||$r);
224              
225             if ($omit eq 'path') {
226             $uri->path(undef);
227             $uri->query(undef); # we don't want a query without a path
228             }
229             elsif ($omit eq 'path_info' or $omit eq 'query') {
230             $uri->$omit(undef);
231             }
232              
233             return $uri->unparse;
234             }
235              
236             sub request_prev_uri () {
237             my $r = AxKit::Apache->request;
238             return ($r->prev||$r)->uri;
239             }
240              
241             sub request_prev_query_string () {
242             my $r = AxKit::Apache->request;
243             return ($r->prev||$r)->query_string;
244             }
245              
246             sub request_prev_param ($) {
247             my $name = shift;
248             my $apr = Apache::Request->instance((AxKit::Apache->request->prev||AxKit::Apache->request));
249              
250             return $apr->param($name);
251             }
252              
253             sub match_useragent ($) {
254             my $name = shift;
255             my $r = AxKit::Apache->request;
256              
257             return $r->header_in('User-Agent') =~ $name;
258             }
259              
260             sub is_https () {
261             my $r = AxKit::Apache->request;
262             return 1 if $r->subprocess_env('https');
263             }
264              
265             sub is_initial_req () {
266             my $r = AxKit::Apache->request;
267             return $r->is_initial_req;
268             }
269              
270             sub variant_list () {
271             my $r = AxKit::Apache->request;
272             my $variant_list = ($r->prev||$r)->notes('variant-list');
273              
274             $variant_list =~ s/([^:>])\n/$1<\/li>\n/g; # tidy up single li-tags because
275             # mod_negotiation's list is not
276             # well-balanced up to Apache 1.3.28
277              
278             return $variant_list;
279             }
280              
281             sub error_notes () {
282             my $r = AxKit::Apache->request;
283             return ($r->prev||$r)->notes('error-notes');
284             }
285              
286             sub server_admin () {
287             my $r = AxKit::Apache->request;
288             return $r->server->server_admin;
289             }
290              
291              
292             1;
293              
294             __END__