| 1 | # | ||
| 2 | # Apache::SessionManager.pm - mod_perl module to manage HTTP sessions | ||
| 3 | # | ||
| 4 | # The Apache::SessionManager module is free software; you can redistribute it | ||
| 5 | # and/or modify it under the same terms as Perl itself. | ||
| 6 | # | ||
| 7 | # See 'perldoc Apache::SessionManager' for documentation | ||
| 8 | # | ||
| 9 | |||
| 10 | package Apache::SessionManager; | ||
| 11 | |||
| 12 | require 5.005; | ||
| 13 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | $VERSION = '1.03'; | ||
| 17 | |||
| 18 | 0 | ▶ | use mod_perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use Apache::Session::Flex; | ||
| 20 | use constant MP2 => ($mod_perl::VERSION >= 1.99); | ||
| 21 | |||
| 22 | if (MP2) { | ||
| 23 | @Apache::SessionManager::ISA = qw(Apache::RequestRec); | ||
| 24 | } | ||
| 25 | else { | ||
| 26 | @Apache::SessionManager::ISA = qw(Apache); | ||
| 27 | } | ||
| 28 | |||
| 29 | # Test libapreq modules | ||
| 30 | my $libapreq; | ||
| 31 | BEGIN { | ||
| 32 | # Tests mod_perl version and uses the appropriate components | ||
| 33 | if (MP2) { | ||
| 34 | require Apache::Const; | ||
| 35 | Apache::Const->import(-compile => qw(DECLINED REDIRECT)); | ||
| 36 | require Apache::RequestRec; | ||
| 37 | require Apache::SubRequest; | ||
| 38 | require Apache::RequestUtil; | ||
| 39 | require APR::Pool; # for cleanup_register | ||
| 40 | require APR::URI; | ||
| 41 | require Apache::URI; | ||
| 42 | #require Apache::Connection; # for remote_ip | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | require Apache::Constants; | ||
| 46 | Apache::Constants->import(qw(DECLINED REDIRECT)); | ||
| 47 | require Apache::URI; | ||
| 48 | } | ||
| 49 | # Test libapreq modules | ||
| 50 | eval { require Apache::Cookie; Apache::Cookie->can('bake'); Apache::Cookie->can('fetch') }; | ||
| 51 | if ($@) { | ||
| 52 | require CGI::Cookie; | ||
| 53 | $libapreq = 0; | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | $libapreq = 1; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub handler { | ||
| 61 | my $r = shift; | ||
| 62 | my (%session_config,%session,$session_id,%cookie_options); | ||
| 63 | |||
| 64 | return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) unless $r->is_initial_req; | ||
| 65 | |||
| 66 | my $debug_prefix = '[' . $r->connection->remote_ip . "] SessionManager ($$): "; | ||
| 67 | $session_config{'SessionManagerDebug'} = $r->dir_config('SessionManagerDebug') || 0; | ||
| 68 | foreach ( qw/SessionManagerURITracking SessionManagerTracking SessionManagerEnableModBackhand | ||
| 69 | SessionManagerStoreArgs SessionManagerCookieArgs SessionManagerSetEnv SessionManagerExpire | ||
| 70 | SessionManagerHeaderExclude SessionManagerIPExclude/ ) { | ||
| 71 | $session_config{$_} = $r->dir_config($_); | ||
| 72 | } | ||
| 73 | |||
| 74 | $r->log_error($debug_prefix . '---START REQUEST: ' . $r->uri . ' ---') if $session_config{'SessionManagerDebug'} > 0; | ||
| 75 | #print STDERR "$debug_prefix ---START REQUEST: " . $r->uri . " ---\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 76 | |||
| 77 | # Get and remove session ID from URI | ||
| 78 | if ( $session_config{'SessionManagerURITracking'} eq 'On' ) { | ||
| 79 | $r->log_error($debug_prefix . 'start URI ' . $r->uri) if $session_config{'SessionManagerDebug'} > 0; | ||
| 80 | #print STDERR "$debug_prefix start URI " . $r->uri . "\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 81 | |||
| 82 | # retrieve session ID from URL (or HTTP 'Referer:' header) | ||
| 83 | my (undef, $uri_session_id, $rest) = split /\/+/, $r->uri, 3; | ||
| 84 | |||
| 85 | if ( $uri_session_id =~ /^[0-9a-h]+$/ ) { | ||
| 86 | $session_id = $uri_session_id; | ||
| 87 | # Remove the session from the URI | ||
| 88 | $r->uri("/$rest"); | ||
| 89 | $r->log_error($debug_prefix . 'end URI ' . $r->uri) if $session_config{'SessionManagerDebug'} > 0; | ||
| 90 | #print STDERR "$debug_prefix end URI " . $r->uri . "\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | # declines each request if session manager is off | ||
| 95 | return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) unless ( $session_config{'SessionManagerTracking'} eq 'On' ); | ||
| 96 | |||
| 97 | # declines requests matching IP exclusion list | ||
| 98 | if ( $session_config{'SessionManagerIPExclude'} ) { | ||
| 99 | require Socket; | ||
| 100 | foreach ( split(/\s+/,$session_config{'SessionManagerIPExclude'}) ) { | ||
| 101 | $r->log_error($debug_prefix . '_isInRange(' . $r->connection->remote_ip . ",$_)") if $session_config{'SessionManagerDebug'} >= 5; | ||
| 102 | #print STDERR "$debug_prefix _isInRange(" . $r->connection->remote_ip . ",$_)\n" if $session_config{'SessionManagerDebug'} >= 5; | ||
| 103 | return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) if _isInRange($r->connection->remote_ip,$_); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | # declines requests matching any of exclusions headers | ||
| 108 | foreach my $header ( $r->dir_config->get('SessionManagerHeaderExclude') ) { | ||
| 109 | my ($key,$value) = split(/\s*=\s*>\s*/,$header,2); | ||
| 110 | # Header and its value must exists in order to check it | ||
| 111 | next unless ($r->headers_in->{$key} && $value); | ||
| 112 | if ( $r->headers_in->{$key} =~ /$value/i ) { | ||
| 113 | return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | # Set exclusion extension(s) | ||
| 118 | $session_config{'SessionManagerItemExclude'} = $r->dir_config('SessionManagerItemExclude') || '(\.gif|\.jpe?g|\.png|\.mpe?g|\.css|\.js|\.txt|\.mp3|\.wav|\.swf|\.avi|\.au|\.ra?m)$'; | ||
| 119 | |||
| 120 | # declines requests if resource type is to exlcude | ||
| 121 | return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) if ( $r->uri =~ /$session_config{'SessionManagerItemExclude'}/i ); | ||
| 122 | |||
| 123 | $session_config{'SessionManagerStore'} = $r->dir_config('SessionManagerStore') || 'File'; | ||
| 124 | $session_config{'SessionManagerLock'} = $r->dir_config('SessionManagerLock') || 'Null'; | ||
| 125 | $session_config{'SessionManagerGenerate'} = $r->dir_config('SessionManagerGenerate') || 'MD5'; | ||
| 126 | $session_config{'SessionManagerSerialize'} = $r->dir_config('SessionManagerSerialize') || 'Storable'; | ||
| 127 | $session_config{'SessionManagerExpire'} = | ||
| 128 | $session_config{'SessionManagerExpire'} =~ /^(none|no|disabed)$/i ? 0 | ||
| 129 | : $session_config{'SessionManagerExpire'} !~ /^\d+$/ ? 3600 | ||
| 130 | : $session_config{'SessionManagerExpire'}; | ||
| 131 | $session_config{'SessionManagerInactivity'} = ( $r->dir_config('SessionManagerInactivity') =~ /^\d+$/ ) ? $r->dir_config('SessionManagerInactivity') : undef; | ||
| 132 | $session_config{'SessionManagerName'} = $r->dir_config('SessionManagerName') || 'PERLSESSIONID'; | ||
| 133 | |||
| 134 | # Print SesssionManager configs to error_log | ||
| 135 | if ( $session_config{'SessionManagerDebug'} >= 3 ) { | ||
| 136 | $r->log_error($debug_prefix . 'configuration settings:'); | ||
| 137 | #print STDERR "$debug_prefix configuration settings\n"; | ||
| 138 | foreach (sort keys %session_config) { | ||
| 139 | $r->log_error($debug_prefix . ' ' x 8 . "$_ = $session_config{$_}"); | ||
| 140 | #print STDERR "\t$_ = $session_config{$_}\n"; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | # Get session ID from cookie | ||
| 145 | unless ( $session_config{'SessionManagerURITracking'} eq 'On' ) { | ||
| 146 | |||
| 147 | if ( $libapreq ) { | ||
| 148 | # Test libapreq 1 or 2 version to use correct 'fetch' API | ||
| 149 | my %cookies = $Apache::Request::VERSION >= 2 ? Apache::Cookie->fetch($r) : Apache::Cookie->fetch; | ||
| 150 | $session_id = $cookies{$session_config{'SessionManagerName'}}->value if defined $cookies{$session_config{'SessionManagerName'}}; | ||
| 151 | $r->log_error($debug_prefix . 'Apache::Cookie fetch') if $session_config{'SessionManagerDebug'} >= 5; | ||
| 152 | #print STDERR "$debug_prefix Apache::Cookie fetch\n" if $session_config{'SessionManagerDebug'} >= 5; | ||
| 153 | } | ||
| 154 | # Fetch cookies with CGI::Cookie | ||
| 155 | else { | ||
| 156 | # At this phase (HeaderParser | Translation), no $ENV{'COOKIE'} var is set, so we use CGI::Cookie parse method by passing 'Cookie' HTTP header | ||
| 157 | my %cookies = CGI::Cookie->parse($r->headers_in->{'Cookie'}); | ||
| 158 | $session_id = $cookies{$session_config{'SessionManagerName'}}->value if defined $cookies{$session_config{'SessionManagerName'}}; | ||
| 159 | $r->log_error($debug_prefix . 'CGI::Cookie fetch') if $session_config{'SessionManagerDebug'} >= 5; | ||
| 160 | #print STDERR "$debug_prefix CGI::Cookie fetch\n" if $session_config{'SessionManagerDebug'} >= 5; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | # Prepare Apache::Session::Flex options parameters call | ||
| 165 | my %apache_session_flex_options = ( | ||
| 166 | Store => $session_config{'SessionManagerStore'}, | ||
| 167 | Lock => $session_config{'SessionManagerLock'}, | ||
| 168 | Generate => $session_config{'SessionManagerGenerate'}, | ||
| 169 | Serialize => $session_config{'SessionManagerSerialize'} | ||
| 170 | ); | ||
| 171 | |||
| 172 | # Load session data store specific parameters | ||
| 173 | foreach my $arg ( split(/\s*,\s*/,$session_config{'SessionManagerStoreArgs'}) ) { | ||
| 174 | my ($key,$value) = split(/\s*=\s*>\s*/,$arg); | ||
| 175 | $apache_session_flex_options{$key} = $value; | ||
| 176 | } | ||
| 177 | |||
| 178 | if ( $session_config{'SessionManagerDebug'} >= 5 ) { | ||
| 179 | $r->log_error($debug_prefix . 'Apache::Session::Flex options:'); | ||
| 180 | #print STDERR "$debug_prefix Apache::Session::Flex options\n"; | ||
| 181 | foreach (sort keys %apache_session_flex_options) { | ||
| 182 | $r->log_error($debug_prefix . ' ' x 8 . "$_ = $apache_session_flex_options{$_}"); | ||
| 183 | # print STDERR "\t$_ = $apache_session_flex_options{$_}\n"; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # Support for mod_backhand sticky sessions | ||
| 188 | $session_id = substr($session_id,8) if ( $session_config{'SessionManagerEnableModBackhand'} eq 'On' ); | ||
| 189 | |||
| 190 | # Try to retrieve session object from session ID | ||
| 191 | my $res = _tieSession($r,\%session, $session_id, \%apache_session_flex_options,$session_config{'SessionManagerDebug'},$debug_prefix); | ||
| 192 | |||
| 193 | # Session ID not found or invalid session: a new object session will be create | ||
| 194 | if ($res) { | ||
| 195 | my $res = _tieSession($r,\%session, undef, \%apache_session_flex_options,$session_config{'SessionManagerDebug'},$debug_prefix); | ||
| 196 | $session_id = undef; | ||
| 197 | } | ||
| 198 | |||
| 199 | # for new or invalid session's ID put session start time in special session key '_session_start' | ||
| 200 | $session{'_session_start'} = time if ! defined $session{'_session_start'}; | ||
| 201 | |||
| 202 | # session's expiration date check only for existing sessions | ||
| 203 | if ( $session_id ) { | ||
| 204 | $r->log_error($debug_prefix . "checking TTL session, ID = $session_id ($session{'_session_timestamp'})") if $session_config{'SessionManagerDebug'} > 0; | ||
| 205 | #print STDERR "$debug_prefix checking TTL session, ID = $session_id ($session{'_session_timestamp'})\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 206 | |||
| 207 | # Session TTL expired: a new object session is create | ||
| 208 | if ( ( $session_config{'SessionManagerInactivity'} && | ||
| 209 | (time - $session{'_session_timestamp'}) > $session_config{'SessionManagerInactivity'} ) | ||
| 210 | || | ||
| 211 | ( $session_config{'SessionManagerExpire'} && | ||
| 212 | (time - $session{'_session_start'}) > $session_config{'SessionManagerExpire'} ) ) { | ||
| 213 | $r->log_error($debug_prefix . 'session to delete') if $session_config{'SessionManagerDebug'} > 0; | ||
| 214 | #print STDERR "$debug_prefix session to delete\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 215 | tied(%session)->delete; | ||
| 216 | |||
| 217 | my $res = _tieSession($r,\%session, undef, \%apache_session_flex_options,$session_config{'SessionManagerDebug'},$debug_prefix); | ||
| 218 | |||
| 219 | $session_id = undef; | ||
| 220 | $session{'_session_start'} = time; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | # Update '_session_timpestamp' session value only if required | ||
| 225 | $session{'_session_timestamp'} = time if $session_config{'SessionManagerInactivity'}; | ||
| 226 | |||
| 227 | # store object session reference in pnotes to share it over other handlers | ||
| 228 | $r->pnotes('SESSION_MANAGER_HANDLE' => \%session ); | ||
| 229 | |||
| 230 | # set 'SESSION_MANAGER_SID' env variable to session ID to make it available to CGI/SSI scripts | ||
| 231 | $r->subprocess_env(SESSION_MANAGER_SID => $session{_session_id}) if ($session_config{'SessionManagerSetEnv'} eq 'On'); | ||
| 232 | |||
| 233 | MP2 ? $r->pool->cleanup_register(\&cleanup,$r) : $r->register_cleanup(\&cleanup); | ||
| 234 | |||
| 235 | # Foreach new session we: | ||
| 236 | unless ( $session_id ) { | ||
| 237 | my $session_id = $session{_session_id}; | ||
| 238 | |||
| 239 | # Adjusts session id for mod_backhand | ||
| 240 | if ( $session_config{'SessionManagerEnableModBackhand'} eq 'On' ) { | ||
| 241 | my $hex_addr = join "", map { sprintf "%lx", $_ } unpack('C4', gethostbyname($r->get_server_name)); | ||
| 242 | $session_id = $hex_addr . $session_id; | ||
| 243 | } | ||
| 244 | |||
| 245 | # redirect to embedded session ID URI... | ||
| 246 | if ( $session_config{'SessionManagerURITracking'} eq 'On' ) { | ||
| 247 | $r->log_error($debug_prefix . 'URI redirect...') if $session_config{'SessionManagerDebug'} > 0; | ||
| 248 | #print STDERR "$debug_prefix URI redirect...\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 249 | _redirect($r,$session_id); | ||
| 250 | return MP2 ? Apache::REDIRECT : Apache::Constants::REDIRECT; | ||
| 251 | } | ||
| 252 | # ...or send cookie to browser | ||
| 253 | else { | ||
| 254 | $r->log_error($debug_prefix . 'sending cookie...') if $session_config{'SessionManagerDebug'} > 0; | ||
| 255 | #print STDERR "$debug_prefix sending cookie...\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 256 | |||
| 257 | # Load cookie specific parameters | ||
| 258 | foreach my $arg ( split(/\s*,\s*/,$session_config{'SessionManagerCookieArgs'}) ) { | ||
| 259 | my ($key,$value) = split(/\s*=>\s*/,$arg); | ||
| 260 | $cookie_options{'-' . lc($key)} = $value if $key =~ /^(expires|domain|path|secure)$/i; | ||
| 261 | } | ||
| 262 | |||
| 263 | # Set default cookie path | ||
| 264 | $cookie_options{'-path'} = '/' unless $cookie_options{'-path'}; | ||
| 265 | |||
| 266 | if ( $session_config{'SessionManagerDebug'} >= 5 ) { | ||
| 267 | $r->log_error($debug_prefix . 'Cookie options:'); | ||
| 268 | #print STDERR "$debug_prefix Cookie options\n"; | ||
| 269 | foreach (sort keys %cookie_options) { | ||
| 270 | $r->log_error($debug_prefix . ' ' x 8 . "$_ = $cookie_options{$_}"); | ||
| 271 | #print STDERR "\t$_ = $cookie_options{$_}\n"; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | # Set cookie with Apache::Cookie | ||
| 276 | if ( $libapreq ) { | ||
| 277 | my $cookie = Apache::Cookie->new($r, | ||
| 278 | name => $session_config{'SessionManagerName'}, | ||
| 279 | value => $session_id, | ||
| 280 | %cookie_options | ||
| 281 | ); | ||
| 282 | $cookie->bake; | ||
| 283 | |||
| 284 | $r->log_error($debug_prefix . 'Apache::Cookie bake ' . $cookie->as_string) if $session_config{'SessionManagerDebug'} >= 5; | ||
| 285 | #print STDERR ("$debug_prefix Apache::Cookie bake " . $cookie->as_string . "\n") if $session_config{'SessionManagerDebug'} >= 5; | ||
| 286 | } | ||
| 287 | # Set cookie with CGI::Cookie | ||
| 288 | else { | ||
| 289 | my $cookie = CGI::Cookie->new( | ||
| 290 | -name => $session_config{'SessionManagerName'}, | ||
| 291 | -value => $session_id, | ||
| 292 | %cookie_options | ||
| 293 | ); | ||
| 294 | $r->err_headers_out->{'Set-Cookie'} = "$cookie"; | ||
| 295 | |||
| 296 | $r->log_error($debug_prefix . "CGI::Cookie bake $cookie") if $session_config{'SessionManagerDebug'} >= 5; | ||
| 297 | #print STDERR "$debug_prefix CGI::Cookie bake $cookie\n" if $session_config{'SessionManagerDebug'} >= 5; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | $r->log_error($debug_prefix . '---END REQUEST---') if $session_config{'SessionManagerDebug'} > 0; | ||
| 303 | #print STDERR "$debug_prefix ---END REQUEST---\n" if $session_config{'SessionManagerDebug'} > 0; | ||
| 304 | |||
| 305 | return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED; | ||
| 306 | } | ||
| 307 | |||
| 308 | sub cleanup { | ||
| 309 | my $r = shift; | ||
| 310 | return (MP2 ? Apache::DECLINED : Apache::Constants::DECLINED) unless ( $r->dir_config('SessionManagerTracking') eq 'On' ); | ||
| 311 | my $session = ref $r->pnotes('SESSION_MANAGER_HANDLE') ? $r->pnotes('SESSION_MANAGER_HANDLE') : {}; | ||
| 312 | untie %{$session}; | ||
| 313 | return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub new { | ||
| 317 | my ($class, $r) = @_; | ||
| 318 | my $session = (ref $r->pnotes('SESSION_MANAGER_HANDLE')) ? $r->pnotes('SESSION_MANAGER_HANDLE') : {}; | ||
| 319 | return bless { r => $r, session => $session }, $class; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub get_session_param { | ||
| 323 | my ($r,@args) = @_; | ||
| 324 | if ( ! @args ) { | ||
| 325 | @args = keys %{$r->{session}}; | ||
| 326 | } | ||
| 327 | my @ary; | ||
| 328 | foreach ( @args ) { | ||
| 329 | push @ary, $r->{session}->{$_}; | ||
| 330 | } | ||
| 331 | return wantarray ? @ary: "@ary"; | ||
| 332 | } | ||
| 333 | |||
| 334 | sub set_session_param { | ||
| 335 | my ($r,%args) = @_; | ||
| 336 | foreach ( keys %args ) { | ||
| 337 | # to avoid ovverride session special keys | ||
| 338 | next if /^_session/; | ||
| 339 | $r->{session}->{$_} = $args{$_}; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | sub delete_session_param { | ||
| 344 | my ($r,@args) = @_; | ||
| 345 | foreach ( @args ) { | ||
| 346 | # to avoid ovverride session special keys | ||
| 347 | next if /^_session/; | ||
| 348 | delete $r->{session}->{$_}; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | sub get_session { | ||
| 353 | my $r = shift; | ||
| 354 | return ($r->pnotes('SESSION_MANAGER_HANDLE')) ? $r->pnotes('SESSION_MANAGER_HANDLE') : (); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub destroy_session { | ||
| 358 | my $r = shift; | ||
| 359 | my $session = (ref $r->pnotes('SESSION_MANAGER_HANDLE')) ? $r->pnotes('SESSION_MANAGER_HANDLE') : {}; | ||
| 360 | tied(%{$session})->delete; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub _tieSession { | ||
| 364 | my ($r,$session_ref,$id,$options,$debug,$debug_prefix) = @_; | ||
| 365 | eval { | ||
| 366 | tie %{$session_ref}, 'Apache::Session::Flex', $id, $options; | ||
| 367 | }; | ||
| 368 | $r->log_error($debug_prefix . "Tied session ID = $$session_ref{_session_id}$@") if $debug >= 3; | ||
| 369 | #print STDERR "Tied session ID = $$session_ref{_session_id}\n$@" if $debug >= 3; | ||
| 370 | return $@ if $@; | ||
| 371 | } | ||
| 372 | |||
| 373 | # _redirect function adapted from original redirect sub wrote by Greg Cope | ||
| 374 | sub _redirect { | ||
| 375 | my $r = shift; | ||
| 376 | my $session_id = shift || ''; | ||
| 377 | my ($args, $host, $rest, $redirect); | ||
| 378 | ($host, $rest) = split '/', $r->uri, 2; | ||
| 379 | $args = $r->args || ''; | ||
| 380 | $args = '?' . $args if $args; | ||
| 381 | $r->content_type('text/html'); | ||
| 382 | |||
| 383 | # "suggest by Gerald Richter / Matt Sergeant to add scheme://hostname:port to redirect" (Greg's original note) | ||
| 384 | my $uri = MP2 ? APR::URI->parse($r->pool,$r->construct_url) : Apache::URI->parse($r); | ||
| 385 | |||
| 386 | # hostinfo give port if necessary - otherwise not | ||
| 387 | my $hostinfo = $uri->hostinfo; | ||
| 388 | my $scheme = $uri->scheme . '://'; | ||
| 389 | $session_id .= '/' if $session_id; | ||
| 390 | $redirect = $scheme . $hostinfo . '/'. $session_id . $rest . $args; | ||
| 391 | # if no slash and it's a dir add a slash | ||
| 392 | if ($redirect !~ m#/$# && -d $r->lookup_uri($redirect)->filename) { | ||
| 393 | $redirect .= '/'; | ||
| 394 | } | ||
| 395 | $r->headers_out->{'Location'} = $redirect; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub _isInRange { | ||
| 399 | my ($addr,$base) = @_; | ||
| 400 | my $bits; | ||
| 401 | ($base, $bits) = split /[\/:]/, $base; | ||
| 402 | return ( (4 == grep { $_ < 256 && /^\d+$/ } split(/\./, $base)) | ||
| 403 | && (4 == grep { $_ < 256 && /^\d+$/ } split(/\./, $addr)) | ||
| 404 | && ($bits =~ /^\d*$/ && $bits <= 32) | ||
| 405 | && ((unpack("N", Socket::inet_aton($base)) >> (32 - $bits)) == (unpack ("N", Socket::inet_aton($addr)) >> (32 - $bits))) | ||
| 406 | ) ? 1 : 0; | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; | ||
| 410 | __END__ | ||