| 1 | #============================================================================== | ||||||||||||||||||||||||||||||||||
| 2 | # LibWeb::CGI -- Extra cgi supports for libweb applications. | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | package LibWeb::CGI; | ||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2000 Colin Kong | ||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||
| 8 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||
| 9 | # modify it under the terms of the GNU General Public License | ||||||||||||||||||||||||||||||||||
| 10 | # as published by the Free Software Foundation; either version 2 | ||||||||||||||||||||||||||||||||||
| 11 | # of the License, or (at your option) any later version. | ||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||
| 13 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||
| 16 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||||||||
| 18 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||
| 19 | # along with this program; if not, write to the Free Software | ||||||||||||||||||||||||||||||||||
| 20 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||||||||||||||||||||||||||||||||
| 21 | #============================================================================= | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | # $Id: CGI.pm,v 1.4 2000/07/18 06:33:30 ckyc Exp $ | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | #-############################# | ||||||||||||||||||||||||||||||||||
| 26 | # Use standard library. | ||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use vars qw(@ISA $VERSION $AutoloadClass); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | #-############################# | ||||||||||||||||||||||||||||||||||
| 31 | # Use custom library. | ||||||||||||||||||||||||||||||||||
| 32 | require LibWeb::Class; | ||||||||||||||||||||||||||||||||||
| 33 | require LibWeb::Core; | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | $VERSION = '0.02'; | ||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | #-############################# | ||||||||||||||||||||||||||||||||||
| 38 | # Inheritance. | ||||||||||||||||||||||||||||||||||
| 39 | # Require CGI.pm version > 2.66. | ||||||||||||||||||||||||||||||||||
| 40 | require CGI; | ||||||||||||||||||||||||||||||||||
| 41 | @ISA = qw( LibWeb::Class CGI ); | ||||||||||||||||||||||||||||||||||
| 42 | # This variable tells CGI what type of default object to create when | ||||||||||||||||||||||||||||||||||
| 43 | # called in the function-oriented manner. | ||||||||||||||||||||||||||||||||||
| 44 | $CGI::DefaultClass = __PACKAGE__; | ||||||||||||||||||||||||||||||||||
| 45 | # This tells the CGI autoloader where to look for functions that are | ||||||||||||||||||||||||||||||||||
| 46 | # not defined. If you wish to override CGI's autoloader, set this to | ||||||||||||||||||||||||||||||||||
| 47 | # the name of your own package. | ||||||||||||||||||||||||||||||||||
| 48 | $AutoloadClass = 'CGI'; | ||||||||||||||||||||||||||||||||||
| 49 | # Avoid denial of service attacks. | ||||||||||||||||||||||||||||||||||
| 50 | $CGI::POST_MAX = 1024 * 100; # Default: Max 100K posts. | ||||||||||||||||||||||||||||||||||
| 51 | $CGI::DISABLE_UPLOADS = 1; # Default: No uploads. | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | #-############################# | ||||||||||||||||||||||||||||||||||
| 54 | # Methods. | ||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=27.1
| |||||||||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||||||||
| 57 | # Params: [ -post_max=>, -disable_uploads=>, -auto_escape=> ] | ||||||||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||||||||
| 59 | # Pre: | ||||||||||||||||||||||||||||||||||
| 60 | # - -post_max is the ceiling on the size of POSTings, in bytes. | ||||||||||||||||||||||||||||||||||
| 61 | # The default for LibWeb::CGI is 100 Kilobytes. | ||||||||||||||||||||||||||||||||||
| 62 | # - -disable_uploads, if non-zero, will disable file uploads completely | ||||||||||||||||||||||||||||||||||
| 63 | # which is the default for LibWeb::CGI. | ||||||||||||||||||||||||||||||||||
| 64 | # - -auto_escape determines whether the text and labels that you provide | ||||||||||||||||||||||||||||||||||
| 65 | # for form elements are escaped according to HTML rules. Non-zero value | ||||||||||||||||||||||||||||||||||
| 66 | # will enable auto escape, and undef will disable auto escape (default | ||||||||||||||||||||||||||||||||||
| 67 | # for LibWeb::CGI). | ||||||||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||||||||
| 69 | 1 | my ($class, $Class, $self, $rc, $post_max, $disable_uploads, $auto_escape); | |||||||||||||||||||||||||||||||||
| 70 | 1 | $class = shift; | |||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | $Class = ref($class) || $class; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 1 | ($rc, $post_max, $disable_uploads, $auto_escape) = | |||||||||||||||||||||||||||||||||
| 74 | $Class->rearrange( ['RC', 'POST_MAX', 'DISABLE_UPLOADS', 'AUTO_ESCAPE'], @_ ); | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | # Set up base class: CGI accordingly. | ||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $CGI::POST_MAX = $post_max if defined($post_max); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $CGI::DISABLE_UPLOADS = $disable_uploads if defined($disable_uploads); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | # Inherit instance variables from the base class. | ||||||||||||||||||||||||||||||||||
| 81 | 0 | $self = $Class->CGI::new(); | |||||||||||||||||||||||||||||||||
| 82 | 0 | bless($self, $Class); | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | # This doesn't work. Some HTMLs still printed | ||||||||||||||||||||||||||||||||||
| 85 | # out as escaped. I don't know why. | ||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | ($auto_escape) ? $self->autoEscape( $auto_escape ) : | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | $self->autoEscape( undef ); | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | # Any necessary initialization. | ||||||||||||||||||||||||||||||||||
| 90 | #$self->_init($rc); | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | # Returns a reference to this object. | ||||||||||||||||||||||||||||||||||
| 93 | 0 | return $self; | |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | #sub _init { | ||||||||||||||||||||||||||||||||||
| 97 | # # | ||||||||||||||||||||||||||||||||||
| 98 | # # Params: $rc | ||||||||||||||||||||||||||||||||||
| 99 | # # | ||||||||||||||||||||||||||||||||||
| 100 | # # Pre: | ||||||||||||||||||||||||||||||||||
| 101 | # # - $rc is absolute path to the rc file for LibWeb. | ||||||||||||||||||||||||||||||||||
| 102 | # # | ||||||||||||||||||||||||||||||||||
| 103 | # # Initialization whenever an object of this class is created. | ||||||||||||||||||||||||||||||||||
| 104 | # # Put site customizations here to override several CGI.pm's variables. | ||||||||||||||||||||||||||||||||||
| 105 | # # | ||||||||||||||||||||||||||||||||||
| 106 | # my ($self, $key, $value); | ||||||||||||||||||||||||||||||||||
| 107 | # $self = shift; | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | # # Instance variables for this class. | ||||||||||||||||||||||||||||||||||
| 110 | # $self->{__PACKAGE__.'.core'} = new LibWeb::Core(shift); | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | # # A work-around to inherit LibWeb.pm instance variables without doing MI. | ||||||||||||||||||||||||||||||||||
| 113 | # while ( ($key,$value) = each (%LibWeb::Core::RC) ) { | ||||||||||||||||||||||||||||||||||
| 114 | # $self->{__PACKAGE__.$key} = $value | ||||||||||||||||||||||||||||||||||
| 115 | # unless exists $self->{__PACKAGE__.$key}; | ||||||||||||||||||||||||||||||||||
| 116 | # } | ||||||||||||||||||||||||||||||||||
| 117 | #} | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub DESTROY {} | ||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub header { | ||||||||||||||||||||||||||||||||
|
Subroutine
header: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 123 | #$self->delete_all(); | ||||||||||||||||||||||||||||||||||
| 124 | #$self->autoEscape(undef); | ||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (@_) { return $self->SUPER::header(@_); } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $crlf = $LibWeb::Core::RC{CRLF} || "\n\n"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$LibWeb::Core::RC{'CRLF'} || "\n\n"
MC/DC0%0/1
$LibWeb::Core::RC{'CRLF'} || "\n\n"
$LibWeb::Core::RC{'CRLF'}
Truth table0%0/2
A
A= $LibWeb::Core::RC{'CRLF'}
| |||||||||||||||||||||||||||||||||||
| 128 | 0 | return "Content-Type: text/html$crlf$crlf"; | |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub is_param_not_null { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_param_not_null: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 133 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return ( defined($_[0]) && ($_[0] ne "") && ($_[0] ne " ") ); | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
defined $_[0] && $_[0] ne '' && $_[0] ne ' '
defined $_[0] and $_[0] ne ""
MC/DC0%0/3
defined $_[0] && $_[0] ne '' && $_[0] ne ' '
defined $_[0]
$_[0] ne ""
$_[0] ne ' '
Truth table0%0/4
A and B && C
A= defined $_[0]B= $_[0] ne ""C= $_[0] ne ' '
| |||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub parameter { | ||||||||||||||||||||||||||||||||
|
Subroutine
parameter: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||
| 138 | # | ||||||||||||||||||||||||||||||||||
| 139 | # Sample usage: $this->parameter(cgi_parameter). | ||||||||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||||||||
| 141 | # Pre: | ||||||||||||||||||||||||||||||||||
| 142 | # 1. cgi_parameter is the parameter passed by either `GET' or `POST'. | ||||||||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||||||||
| 144 | # Post: | ||||||||||||||||||||||||||||||||||
| 145 | # 1. If cgi_parameter is a mandatory form value (the ones without `.' as prefix | ||||||||||||||||||||||||||||||||||
| 146 | # in the parameter's name) and it is null, print an error message and abort | ||||||||||||||||||||||||||||||||||
| 147 | # the program. | ||||||||||||||||||||||||||||||||||
| 148 | # 2. Return the value of the parameter. | ||||||||||||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||||||||||||
| 150 | 0 | my ( $self, $key, $value, $param_is_not_null ); | |||||||||||||||||||||||||||||||||
| 151 | 0 | $self = shift; | |||||||||||||||||||||||||||||||||
| 152 | 0 | $key = shift; | |||||||||||||||||||||||||||||||||
| 153 | 0 | $value = $self->CGI::param($key); | |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | # Check for denial of service attacks. | ||||||||||||||||||||||||||||||||||
| 156 | # CGI::cgi_error() is available since CGI 2.47. | ||||||||||||||||||||||||||||||||||
| 157 | # Where is CGI::cgi_error()?? It's supported pre CGI3, but seems to be | ||||||||||||||||||||||||||||||||||
| 158 | # disappeared in new release of CGI.pm 3.01 alpha (24/04/2000). | ||||||||||||||||||||||||||||||||||
| 159 | # Need to apply patch here if CGI version is < 2.47 or >= 3.01 alpha. | ||||||||||||||||||||||||||||||||||
| 160 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 161 | $self->fatal( -msg => 'Invalid post. Post too large.', | ||||||||||||||||||||||||||||||||||
| 162 | -alertMsg => "413 POST too large for CGI param: $key", | ||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | -helpMsg => $self->{HHTML}->post_too_large() ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $value and $self->cgi_error
MC/DC0%0/2
not $value and $self->cgi_error
not $value
$self->cgi_error
Truth table0%0/3
not(A) and B
A= not $valueB= $self->cgi_error
| |||||||||||||||||||||||||||||||||||
| 164 | if ( !$value && $self->cgi_error() ); | ||||||||||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | # Check to see if mandatory cgi form values are non-null. | ||||||||||||||||||||||||||||||||||
| 169 | 0 | $param_is_not_null = $self->is_param_not_null($value); | |||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | unless ($key =~ m:^[.].*$:) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | unless ( $param_is_not_null ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 172 | 0 | $key =~ s:[_]+: :g; | |||||||||||||||||||||||||||||||||
| 173 | $self->fatal(-msg => ucfirst($key)." not entered.", | ||||||||||||||||||||||||||||||||||
| 174 | -alertMsg => "$key not entered.", | ||||||||||||||||||||||||||||||||||
| 175 | -helpMsg => $LibWeb::Core::RC{HHTML}->hit_back_and_edit() | ||||||||||||||||||||||||||||||||||
| 176 | 0 | ); | |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | # Return undef for non-mandatory cgi parameter's value if | ||||||||||||||||||||||||||||||||||
| 181 | # it's not entered by user. | ||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | return undef unless($param_is_not_null); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | # Sanitize all html tags. | ||||||||||||||||||||||||||||||||||
| 185 | #$value = $self->sanitize(-html => $value) if defined($value); | ||||||||||||||||||||||||||||||||||
| 186 | 0 | return $value; | |||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub redirect { | ||||||||||||||||||||||||||||||||
|
Subroutine
redirect: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 190 | # | ||||||||||||||||||||||||||||||||||
| 191 | # Params: -url=> [, -cookie=> ]. | ||||||||||||||||||||||||||||||||||
| 192 | # e.g. 'http://www.your_site.org/help.html' or '/help.html'. | ||||||||||||||||||||||||||||||||||
| 193 | # | ||||||||||||||||||||||||||||||||||
| 194 | # Post: | ||||||||||||||||||||||||||||||||||
| 195 | # 1. Redirect the client Web browser to the specified page. | ||||||||||||||||||||||||||||||||||
| 196 | # | ||||||||||||||||||||||||||||||||||
| 197 | 0 | my ($self, $url, $cookie); | |||||||||||||||||||||||||||||||||
| 198 | 0 | $self = shift; | |||||||||||||||||||||||||||||||||
| 199 | 0 | ($url, $cookie) = $self->rearrange( ['URL', 'COOKIE'], @_ ); | |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||
| 201 | # Append 'http://' to the url to make sure redirect work. | ||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | unless ($url =~ m"^http://") { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 203 | # remove front slash of url. | ||||||||||||||||||||||||||||||||||
| 204 | #$url =~ s:^[/]::; | ||||||||||||||||||||||||||||||||||
| 205 | 0 | chop $self->{URL_ROOT}; | |||||||||||||||||||||||||||||||||
| 206 | 0 | $url = $self->{URL_ROOT} . $url; | |||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | 0 | print $self->SUPER::redirect( -url => $url, -cookie => $cookie ); | |||||||||||||||||||||||||||||||||
| 210 | # $self->send_cookie($cookie) if defined($cookie); | ||||||||||||||||||||||||||||||||||
| 211 | # print "Status: 302 Moved\nLocation: $url\n\n" if defined($url); | ||||||||||||||||||||||||||||||||||
| 212 | # #print "Content-Type: text/html\n\n"; | ||||||||||||||||||||||||||||||||||
| 213 | 0 | exit(0); | |||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub send_cookie { | ||||||||||||||||||||||||||||||||
|
Subroutine
send_cookie: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 217 | 0 | shift; | |||||||||||||||||||||||||||||||||
| 218 | 0 | LibWeb::Core->new()->send_cookie(@_); | |||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | sub fatal { | ||||||||||||||||||||||||||||||||
|
Subroutine
fatal: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 222 | 0 | shift; | |||||||||||||||||||||||||||||||||
| 223 | 0 | LibWeb::Core->new()->fatal(@_); | |||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | sub sanitize { | ||||||||||||||||||||||||||||||||
|
Subroutine
sanitize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 227 | 0 | shift; | |||||||||||||||||||||||||||||||||
| 228 | 0 | LibWeb::Core->new()->sanitize(@_); | |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||
| 231 | 1; | ||||||||||||||||||||||||||||||||||
| 232 | __DATA__ | ||||||||||||||||||||||||||||||||||