File: Util.pm

package info (click to toggle)
libapache-mod-perl 1.21.20000309-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,632 kB
  • ctags: 1,295
  • sloc: perl: 7,049; ansic: 5,214; sh: 356; makefile: 171
file content (162 lines) | stat: -rw-r--r-- 3,817 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package Apache::Util;

use strict;
use mod_perl ();
use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS);

use Exporter ();
use DynaLoader ();

*unescape_uri = \&Apache::unescape_url;
*unescape_uri_info = \&Apache::unescape_url_info;

*import = \&Exporter::import;

@EXPORT_OK = qw(escape_html escape_uri unescape_uri unescape_uri_info 
		parsedate ht_time size_string validate_password);
%EXPORT_TAGS = (all => \@EXPORT_OK);
$VERSION = '1.02';

__PACKAGE__->mod_perl::boot($VERSION);

1;
__END__


=head1 NAME

Apache::Util - Interface to Apache C util functions

=head1 SYNOPSIS

  use Apache::Util qw(:all);

=head1 DESCRIPTION

This module provides a Perl interface to some of the C utility functions
available in Perl.  The same functionality is avaliable in libwww-perl, but
the C versions are faster:

    use Benchmark;
    timethese(1000, {
        C => sub { my $esc = Apache::Util::escape_html($html) },
        Perl => sub { my $esc = HTML::Entities::encode($html) },
    });  

    Benchmark: timing 1000 iterations of C, Perl...
            C:  0 secs ( 0.17 usr  0.00 sys =  0.17 cpu)
         Perl: 15 secs (15.06 usr  0.04 sys = 15.10 cpu) 

    use Benchmark;
    timethese(10000, {
        C => sub { my $esc = Apache::Util::escape_uri($uri) },
        Perl => sub { my $esc = URI::Escape::uri_escape($uri) },
    }); 

    Benchmark: timing 10000 iterations of C, Perl...
            C:  0 secs ( 0.55 usr  0.01 sys =  0.56 cpu)
         Perl:  2 secs ( 1.78 usr  0.01 sys =  1.79 cpu) 

=head1 FUNCTIONS

=over 4

=item escape_html

This routine replaces unsafe characters in $string with their entity
representation.

 my $esc = Apache::Util::escape_html($html);

=item escape_uri

This function replaces all unsafe characters in the $string with their
escape sequence and returns the result.

 my $esc = Apache::Util::escape_uri($uri);

=item unescape_uri

This function decodes all %XX hex escape sequences in the given URI.

 my $unescaped = Apache::Util::unescape_uri($safe_uri); 

=item unescape_uri_info

This function is similar to unescape_uri() but is specialized to remove
escape sequences from the query string portion of the URI. The main
difference is that it translates the ``+'' character into spaces  as well
as recognizing and translating the hex escapes.

Example:

 $string = $r->uri->query;
 my %data = map { Apache::Util::unescape_uri_info($_) }
              split /[=&]/, $string, -1;

 This would correctly translate the query string
 ``name=Fred+Flintstone&town=Bedrock'' into the hash: 

 data => 'Fred Flintstone',
 town => 'Bedrock'

=item parsedate

Parses an HTTP date in one of three standard forms:

 Sun, 06 Nov 1994 08:49:37 GMT  ; RFC 822, updated by RFC 1123

 Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036

 Sun Nov  6 08:49:37 1994       ; ANSI C's asctime() format       

Example:

 my $secs = Apache::Util::parsedate($date_str);

=item ht_time

Format a time string.

Examples:

 my $str = Apache::Util::ht_time(time);

 my $str = Apache::Util::ht_time(time, "%d %b %Y %T %Z");

 my $str = Apache::Util::ht_time(time, "%d %b %Y %T %Z", 0);

=item size_string

Converts the given file size into a formatted string. The size
given in the string will be in units of bytes, kilobytes, or
megabytes, depending on the size.

 my $size = Apache::Util::size_string -s $r->finfo;

=item validate_password

Validate a plaintext password against a smashed one.  Use either
crypt() (if available), ap_MD5Encode() or ap_SHA1Encode depending 
upon the format of the smashed input password.

Returns true if they match, false otherwise.

 if (Apache::Util::validate_password("slipknot", "aXYx4GnaCrDQc")) {
     print "password match\n";
 }
 else {
     print "password mismatch\n";
 }

=back

=head1 AUTHOR

Doug MacEachern

=head1 SEE ALSO

perl(1).

=cut