/usr/share/perl5/Apache/Session/Wrapper.pm is in libapache-session-wrapper-perl 0.34-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 | package Apache::Session::Wrapper;
use strict;
use vars qw($VERSION);
$VERSION = '0.34';
$VERSION = eval $VERSION;
use base qw(Class::Container);
use Apache::Session 1.81;
use Exception::Class ( 'Apache::Session::Wrapper::Exception::NonExistentSessionID' =>
{ description => 'A non-existent session id was used',
fields => [ 'session_id' ] },
'Apache::Session::Wrapper::Exception::Params' =>
{ description => 'An invalid parameter or set of parameters was given',
alias => 'param_error' },
);
use Params::Validate 0.70;
use Params::Validate qw( validate SCALAR UNDEF BOOLEAN ARRAYREF OBJECT );
Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
use Scalar::Util ();
my $MOD_PERL = _find_mp_version();
sub _find_mp_version
{
return 0 unless $ENV{MOD_PERL};
return
( $ENV{MOD_PERL} =~ /(?:1\.9|2\.\d)/
? 2
: 1
);
}
my @HeaderMethods = qw( err_headers_out headers_out );
my %params =
( always_write =>
{ type => BOOLEAN,
default => 1,
descr => 'Whether or not to force a write before the session goes out of scope' },
allow_invalid_id =>
{ type => BOOLEAN,
default => 1,
descr => 'Whether or not to allow a failure to find an existing session id' },
param_name =>
{ type => SCALAR,
optional => 1,
depends => 'param_object',
descr => 'Name of the parameter to use for session tracking' },
param_object =>
{ type => OBJECT,
optional => 1,
can => 'param',
descr => 'Object which has a "param" method, to be used for getting the session id from a query string or POST argument' },
use_cookie =>
{ type => BOOLEAN,
default => 0,
descr => 'Whether or not to use a cookie to track the session' },
cookie_name =>
{ type => SCALAR,
default => 'Apache-Session-Wrapper-cookie',
descr => 'Name of cookie used by this module' },
cookie_expires =>
{ type => UNDEF | SCALAR,
default => '+1d',
descr => 'Expiration time for cookies' },
cookie_domain =>
{ type => UNDEF | SCALAR,
optional => 1,
descr => 'Domain parameter for cookies' },
cookie_path =>
{ type => SCALAR,
default => '/',
descr => 'Path for cookies' },
cookie_secure =>
{ type => BOOLEAN,
default => 0,
descr => 'Are cookies sent only for SSL connections?' },
cookie_resend =>
{ type => BOOLEAN,
default => 1,
descr => 'Resend the cookie on each request?' },
header_object =>
{ type => OBJECT,
callbacks =>
{ 'has a method to set headers' =>
sub { grep { $_[0]->can($_) } @HeaderMethods } },
optional => 1,
descr => 'An object that can be used to send cookies with' },
class =>
{ type => SCALAR,
descr => 'An Apache::Session class to use for sessions' },
data_source =>
{ type => SCALAR,
optional => 1,
descr => 'The data source when using MySQL or PostgreSQL' },
user_name =>
{ type => UNDEF | SCALAR,
optional => 1,
descr => 'The user name to be used when connecting to a database' },
password =>
{ type => UNDEF | SCALAR,
default => undef,
descr => 'The password to be used when connecting to a database' },
table_name =>
{ type => UNDEF | SCALAR,
optional => 1,
descr => 'The table in which sessions are saved' },
lock_data_source =>
{ type => SCALAR,
optional => 1,
descr => 'The data source when using MySQL or PostgreSQL' },
lock_user_name =>
{ type => UNDEF | SCALAR,
optional => 1,
descr => 'The user name to be used when connecting to a database' },
lock_password =>
{ type => UNDEF | SCALAR,
default => undef,
descr => 'The password to be used when connecting to a database' },
handle =>
{ type => OBJECT,
optional => 1,
descr => 'An existing database handle to use' },
lock_handle =>
{ type => OBJECT,
optional => 1,
descr => 'An existing database handle to use' },
commit =>
{ type => BOOLEAN,
default => 1,
descr => 'Whether or not to auto-commit changes to the database' },
transaction =>
{ type => BOOLEAN,
default => 0,
descr => 'The Transaction flag for Apache::Session' },
directory =>
{ type => SCALAR,
optional => 1,
descr => 'A directory to use when storing sessions' },
lock_directory =>
{ type => SCALAR,
optional => 1,
descr => 'A directory to use for locking when storing sessions' },
file_name =>
{ type => SCALAR,
optional => 1,
descr => 'A DB_File to use' },
store =>
{ type => SCALAR,
optional => 1,
descr => 'A storage class to use with the Flex module' },
lock =>
{ type => SCALAR,
optional => 1,
descr => 'A locking class to use with the Flex module' },
generate =>
{ type => SCALAR,
default => 'MD5',
descr => 'A session generator class to use with the Flex module' },
serialize =>
{ type => SCALAR,
optional => 1,
descr => 'A serialization class to use with the Flex module' },
textsize =>
{ type => SCALAR,
optional => 1,
descr => 'A parameter for the Sybase storage module' },
long_read_len =>
{ type => SCALAR,
optional => 1,
descr => 'A parameter for the Oracle storage module' },
n_sems =>
{ type => SCALAR,
optional => 1,
descr => 'A parameter for the Semaphore locking module' },
semaphore_key =>
{ type => SCALAR,
optional => 1,
descr => 'A parameter for the Semaphore locking module' },
mod_usertrack_cookie_name =>
{ type => SCALAR,
optional => 1,
descr => 'The cookie name used by mod_usertrack' },
save_path =>
{ type => SCALAR,
optional => 1,
descr => 'Path used by Apache::Session::PHP' },
session_id =>
{ type => SCALAR,
optional => 1,
descr => 'Try this session id first when making a session' },
);
# What set of parameters are required for each session class.
# Multiple array refs represent multiple possible sets of parameters
my %ApacheSessionParams =
( Flex => [ [ qw( store lock generate serialize ) ] ],
MySQL => [ [ qw( data_source user_name
lock_data_source lock_user_name ) ],
[ qw( handle lock_handle ) ] ],
Postgres => [ [ qw( data_source user_name commit ) ],
[ qw( handle commit ) ] ],
File => [ [ qw( directory lock_directory ) ] ],
DB_File => [ [ qw( file_name lock_directory ) ] ],
PHP => [ [ qw( save_path ) ] ],
);
@ApacheSessionParams{ qw( Informix Oracle Sybase ) } =
( $ApacheSessionParams{Postgres} ) x 3;
my %OptionalApacheSessionParams =
( MySQL => [ [ qw( table_name password lock_password ) ] ],
Postgres => [ [ qw( table_name password ) ] ],
Informix => [ [ qw( table_name password ) ] ],
Oracle => [ [ qw( long_read_len table_name password ) ] ],
Sybase => [ [ qw( textsize table_name password ) ] ],
);
my %ApacheSessionFlexParams =
( store =>
{ MySQL => [ [ qw( data_source user_name ) ],
[ qw( handle ) ] ],
Postgres => $ApacheSessionParams{Postgres},
File => [ [ qw( directory ) ] ],
DB_File => [ [ qw( file_name ) ] ],
PHP => $ApacheSessionParams{PHP},
},
lock =>
{ MySQL => [ [ qw( lock_data_source lock_user_name ) ],
[ qw( lock_handle ) ] ],
File => [ [ ] ],
Null => [ [ ] ],
Semaphore => [ [ ] ],
},
generate =>
{ MD5 => [ [ ] ],
ModUniqueId => [ [ ] ],
ModUsertrack => [ [ qw( mod_usertrack_cookie_name ) ] ],
},
serialize =>
{ Storable => [ [ ] ],
Base64 => [ [ ] ],
Sybase => [ [ ] ],
UUEncode => [ [ ] ],
PHP => [ [ ] ],
},
);
@{ $ApacheSessionFlexParams{store} }{ qw( Informix Oracle Sybase ) } =
( $ApacheSessionFlexParams{store}{Postgres} ) x 3;
my %OptionalApacheSessionFlexParams =
( store => { map { $_ => $OptionalApacheSessionParams{$_} }
qw( MySQL Postgres Informix Oracle Sybase ) },
);
sub _SetValidParams {
my $class = shift;
my %extra;
for my $hash ( \%ApacheSessionParams,
\%OptionalApacheSessionParams,
@ApacheSessionFlexParams{ qw( store lock generate serialize ) },
@OptionalApacheSessionFlexParams{ qw( store lock generate serialize ) },
)
{
for my $p ( map { @$_ } map { @$_ } values %$hash )
{
my $h;
if ( ref $p ) {
# we assume its a hash of names/parameter specifications
$h = $p;
} elsif (!$params{$p}) {
# its a new parameter defined by a scalar, default to SCALAR value
$h = { $p => { optional => 1, type => SCALAR } };
} else {
# its a scalar option we already know.
next;
}
# now expand the options
foreach my $name (keys %$h) {
next if $params{$name};
$extra{$p} = $h->{$name};
}
}
}
$class->valid_params( %extra, %params );
$class->SetStudlyForms();
}
__PACKAGE__->_SetValidParams();
my %StudlyForm;
sub SetStudlyForms
{
%StudlyForm =
( map { $_ => _studly_form($_) }
map { ref $_ ? @$_ :$_ }
map { @$_ }
( values %ApacheSessionParams ),
( values %OptionalApacheSessionParams ),
( map { values %{ $ApacheSessionFlexParams{$_} } }
keys %ApacheSessionFlexParams ),
( map { values %{ $OptionalApacheSessionFlexParams{$_} } }
keys %OptionalApacheSessionFlexParams ),
);
# why Apache::Session does this I do not know
$StudlyForm{textsize} = 'textsize';
}
sub _studly_form
{
my $string = shift;
$string =~ s/(?:^|_)(\w)/\U$1/g;
return $string;
}
sub RegisterClass {
my $class = shift;
my %p = validate( @_, { name => { type => SCALAR },
required => { type => SCALAR | ARRAYREF, default => [ [ ] ] },
optional => { type => SCALAR | ARRAYREF, default => [ ] },
},
);
$p{name} =~ s/^Apache::Session:://;
$ApacheSessionParams{ $p{name} } =
( ref $p{required}
? $p{required}
: $ApacheSessionParams{ $p{required} }
);
$OptionalApacheSessionParams{ $p{name} } =
( ref $p{optional}
? [ $p{optional} ]
: $OptionalApacheSessionParams{ $p{optional} }
);
$class->_SetValidParams();
}
sub RegisterFlexClass {
my $class = shift;
my %p = validate( @_, { type => { type => SCALAR,
regex => qr/^(?:store|lock|generate|serialize)/,
},
name => { type => SCALAR },
required => { type => SCALAR | ARRAYREF, default => [ [ ] ] },
optional => { type => SCALAR | ARRAYREF, default => [ ] },
},
);
$p{name} =~ s/^Apache::Session:://;
$p{name} =~ s/^\Q$p{type}\E:://i;
$ApacheSessionFlexParams{ $p{type} }{ $p{name} } =
( ref $p{required}
? $p{required}
: $ApacheSessionFlexParams{ $p{type} }{ $p{required} }
);
$OptionalApacheSessionFlexParams{ $p{type} }{ $p{name} } =
( ref $p{optional}
? [ $p{optional} ]
: $OptionalApacheSessionFlexParams{ $p{type} }{ $p{optional} }
);
$class->_SetValidParams();
}
sub new
{
my $class = shift;
my %p = @_;
my $self = $class->SUPER::new(%p);
$self->_check_session_params;
$self->_set_session_params;
if ( $self->{use_cookie} && ! ( $ENV{MOD_PERL} || $self->{header_object} ) )
{
param_error
"The header_object parameter is required in order to use cookies outside of mod_perl";
}
my $session_class = "Apache::Session::$self->{session_class_piece}";
unless ( $session_class->can('TIEHASH') )
{
eval "require $session_class";
die $@ if $@;
}
$self->_make_session( $p{session_id} );
$self->_bake_cookie
if $self->{use_cookie} && ! $self->{cookie_is_baked};
return $self;
}
sub _check_session_params
{
my $self = shift;
$self->{session_class_piece} = $self->{class};
$self->{session_class_piece} =~ s/^Apache::Session:://;
my $sets = $ApacheSessionParams{ $self->{session_class_piece} }
or param_error "Invalid session class: $self->{class}";
$self->_check_sets( $sets, 'session', $self->{class} )
if grep { @$_ } @$sets;
if ( $self->{session_class_piece} eq 'Flex' )
{
foreach my $key ( keys %ApacheSessionFlexParams )
{
my $subclass = $self->{$key};
my $sets = $ApacheSessionFlexParams{$key}{$subclass}
or param_error "Invalid class for $key: $self->{$key}";
$self->_check_sets( $sets, $key, $subclass )
if grep { @$_ } @$sets;
}
}
}
sub _check_sets
{
my $self = shift;
my $sets = shift;
my $type = shift;
my $class = shift;
my @missing;
foreach my $set (@$sets)
{
my @matched = grep { exists $self->{$_} } @$set;
return if @matched == @$set;
@missing = grep { ! exists $self->{$_} } @$set;
}
param_error "Some or all of the required parameters for your chosen $type class ($class) were provided."
. " The following parameters were missing: @missing\n";
}
sub _set_session_params
{
my $self = shift;
my %params;
$self->_sets_to_params
( $ApacheSessionParams{ $self->{session_class_piece} },
\%params );
$self->_sets_to_params
( $OptionalApacheSessionParams{ $self->{session_class_piece} },
\%params );
if ( $self->{session_class_piece} eq 'Flex' )
{
foreach my $key ( keys %ApacheSessionFlexParams )
{
my $subclass = $self->{$key};
$params{ $StudlyForm{$key} } = $subclass;
$self->_sets_to_params
( $ApacheSessionFlexParams{$key}{$subclass},
\%params );
$self->_sets_to_params
( $OptionalApacheSessionFlexParams{$key}{$subclass},
\%params );
}
}
$self->{params} = \%params;
$self->_set_cookie_fields
if $self->{use_cookie};
}
sub _set_cookie_fields
{
my $self = shift;
my $cookie_class;
if ($MOD_PERL)
{
$cookie_class =
$MOD_PERL == 2 ? 'Apache2::Cookie' : 'Apache::Cookie';
eval "require $cookie_class"
unless $cookie_class->can('new');
}
unless ( $cookie_class && $cookie_class->can('new' ) )
{
require CGI::Cookie;
$cookie_class = 'CGI::Cookie';
}
$self->{cookie_class} = $cookie_class;
if ( $self->{cookie_class} eq 'CGI::Cookie' )
{
$self->{new_cookie_args} = [];
$self->{fetch_cookie_args} = [];
}
else
{
$self->{new_cookie_args} =
[ $MOD_PERL == 2
? Apache2::RequestUtil->request
: Apache->request
];
$self->{fetch_cookie_args} =
( $MOD_PERL == 2
? $self->{new_cookie_args}
: []
);
$self->{bake_cookie_args} =
( $MOD_PERL == 2
? $self->{new_cookie_args}
: []
);
}
}
sub _sets_to_params
{
my $self = shift;
my $sets = shift;
my $params = shift;
foreach my $set (@$sets)
{
foreach my $key (@$set)
{
if ( exists $self->{$key} )
{
$params->{ $StudlyForm{$key} } =
$self->{$key};
}
}
}
}
sub _make_session
{
my $self = shift;
my $session_id = shift;
return if
defined $session_id && $self->_try_session_id( $session_id );
my $id = $self->_get_session_id;
return if defined $id && $self->_try_session_id($id);
if ( defined $self->{param_name} )
{
my $id = $self->_get_session_id_from_args;
return if defined $id && $self->_try_session_id($id);
}
if ( $self->{use_cookie} )
{
my $id = $self->_get_session_id_from_cookie;
if ( defined $id && $self->_try_session_id($id) )
{
$self->{cookie_is_baked} = 1
unless $self->{cookie_resend};
return;
}
}
# make a new session id
$self->_try_session_id(undef);
}
# for subclasses
sub _get_session_id { return }
sub _get_session_id_from_args
{
my $self = shift;
return $self->{param_object}->param( $self->{param_name} );
}
sub _get_session_id_from_cookie
{
my $self = shift;
if ( $MOD_PERL == 2 )
{
my $jar = Apache2::Cookie::Jar->new( @{ $self->{fetch_cookie_args} } );
my $c = $jar->cookies( $self->{cookie_name} );
return $c->value if $c;
}
else
{
my %c = $self->{cookie_class}->fetch( @{ $self->{fetch_cookie_args} } );
return $c{ $self->{cookie_name} }->value
if exists $c{ $self->{cookie_name} };
}
return undef;
}
sub _try_session_id
{
my $self = shift;
my $session_id = shift;
return 1 if ( $self->{session} &&
defined $session_id &&
$self->{session_id} eq $session_id );
my %s;
{
local $SIG{__DIE__};
eval
{
tie %s, "Apache::Session::$self->{session_class_piece}",
$session_id, $self->{params};
};
if ( $@ || ! tied %s || ! $s{_session_id} )
{
$self->_handle_tie_error( $@, $session_id );
return;
}
}
untie %{ $self->{session} } if $self->{session};
$self->{session} = \%s;
$self->{session_id} = $s{_session_id};
$self->{cookie_is_baked} = 0;
return 1;
}
sub _handle_tie_error
{
my $self = shift;
my $err = shift;
my $session_id = shift;
if ( $err =~ /Object does not exist/ && defined $session_id )
{
return if $self->{allow_invalid_id};
Apache::Session::Wrapper::Exception::NonExistentSessionID->throw
( error => "Invalid session id: $session_id",
session_id => $session_id );
}
else
{
my $error =
$err ? $err : "Tying to Apache::Session::$self->{session_class_piece} failed but did not throw an exception";
die $error;
}
}
sub _bake_cookie
{
my $self = shift;
my $expires = shift || $self->{cookie_expires};
$expires = undef if defined $expires && $expires =~ /^session$/i;
my $domain = $self->{cookie_domain};
my $cookie =
$self->{cookie_class}->new
( @{ $self->{new_cookie_args} },
-name => $self->{cookie_name},
# Apache2::Cookie will return undef if we pass undef for
# -value.
-value => ( $self->{session_id} || '' ),
( defined $expires
? ( -expires => $expires )
: ()
),
( defined $domain
? ( -domain => $domain )
: ()
),
-path => $self->{cookie_path},
-secure => $self->{cookie_secure},
);
# If not running under mod_perl, CGI::Cookie->bake() will call
# print() to send a cookie header right now, which may not be what
# the user wants.
if ( $cookie->can('bake') && ! $cookie->isa('CGI::Cookie') )
{
$cookie->bake( @{ $self->{bake_cookie_args} } );
}
else
{
my $header_object = $self->{header_object};
for my $meth (@HeaderMethods)
{
if ( $header_object->can($meth) )
{
$header_object->$meth->add( 'Set-Cookie' => $cookie );
last;
}
}
}
# always set this even if we skipped actually setting the cookie
# to avoid resending it. this keeps us from entering this method
# over and over
$self->{cookie_is_baked} = 1
unless $self->{cookie_resend};
}
sub session
{
my $self = shift;
my %p = validate( @_,
{ session_id =>
{ type => SCALAR,
optional => 1,
},
} );
if ( ! $self->{session} || %p )
{
$self->_make_session( $p{session_id} );
$self->_bake_cookie
if $self->{use_cookie} && ! $self->{cookie_is_baked};
}
return $self->{session};
}
sub delete_session
{
my $self = shift;
return unless $self->{session};
my $session = delete $self->{session};
(tied %$session)->delete;
delete $self->{session_id};
$self->_bake_cookie('-1d') if $self->{use_cookie};
}
sub cleanup_session
{
my $self = shift;
if ( $self->{always_write} )
{
if ( $self->{session}->{___force_a_write___} )
{
$self->{session}{___force_a_write___} = 0;
}
else
{
$self->{session}{___force_a_write___} = 1;
}
}
undef $self->{session};
}
sub DESTROY { $_[0]->cleanup_session }
1;
__END__
=head1 NAME
Apache::Session::Wrapper - A simple wrapper around Apache::Session
=head1 SYNOPSIS
my $wrapper =
Apache::Session::Wrapper->new( class => 'MySQL',
handle => $dbh,
cookie_name => 'example-dot-com-cookie',
);
# will get an existing session from a cookie, or create a new session
# and cookie if needed
$wrapper->session->{foo} = 1;
=head1 DESCRIPTION
This module is a simple wrapper around Apache::Session which provides
some methods to simplify getting and setting the session id.
It can uses cookies to store the session id, or it can look in a
provided object for a specific parameter. Alternately, you can simply
provide the session id yourself in the call to the C<session()>
method.
If you're using Mason, you should probably take a look at
C<MasonX::Request::WithApacheSession> first, which integrates this
module directly into Mason.
=head1 METHODS
This class provides the following public methods:
=over 4
=item * new
This method creates a new C<Apache::Session::Wrapper> object.
If the parameters you provide are not correct (wrong type, missing
parameters, etc.), this method throws an
C<Apache::Session::Wrapper::Exception::Params> exception. You can
treat this exception as a string if you want.
=item * session
This method returns a hash tied to the C<Apache::Session> class.
This method accepts an optional "session_id" parameter.
=item * delete_session
This method deletes the existing session from persistent storage. If
you are using the built-in cookie handling, it also deletes the cookie
in the browser.
=back
=head1 CONFIGURATION
This module accepts quite a number of parameters, most of which are
simply passed through to C<Apache::Session>. For this reason, you are
advised to familiarize yourself with the C<Apache::Session>
documentation before attempting to configure this module.
You can also register C<Apache::Session> classes, or the classes used
for doing the work in C<Apache::Session::Flex>. See L<REGISTERING
CLASSES> for details.
=head2 Supported Classes
The following classes are already supported and do not require
registration:
=over 4
=item * Apache::Session::MySQL
=item * Apache::Session::Postgres
=item * Apache::Session::Oracle
=item * Apache::Session::Informix
=item * Apache::Session::Sybase
=item * Apache::Session::File
=item * Apache::Session::DB_File
=item * Apache::Session::PHP
=item * Apache::Session::Flex
=back
The following classes can be used with C<Apache::Session::Flex>:
=over 4
=item * Apache::Session::Store::MySQL
=item * Apache::Session::Store::Postgres
=item * Apache::Session::Store::Informix
=item * Apache::Session::Store::Oracle
=item * Apache::Session::Store::Sybase
=item * Apache::Session::Store::File
=item * Apache::Session::Store::DB_File
=item * Apache::Session::Store::PHP
=item * Apache::Session::Lock::MySQL
=item * Apache::Session::Lock::File
=item * Apache::Session::Lock::Null
=item * Apache::Session::Lock::Semaphore
=item * Apache::Session::Generate::MD5
=item * Apache::Session::Generate::ModUsertrack
=item * Apache::Session::Serialize::Storable
=item * Apache::Session::Serialize::Base64
=item * Apache::Session::Serialize::Sybase
=item * Apache::Session::Serialize::UUEncode
=item * Apache::Session::Serialize::PHP
=back
=head2 Generic Parameters
=over 4
=item * class => class name
The name of the C<Apache::Session> subclass you would like to use.
This module will load this class for you if necessary.
This parameter is required.
=item * always_write => boolean
If this is true, then this module will ensure that C<Apache::Session>
writes the session. If it is false, the default C<Apache::Session>
behavior is used instead.
This defaults to true.
=item * allow_invalid_id => boolean
If this is true, an attempt to create a session with a session id that
does not exist in the session storage will be ignored, and a new
session will be created instead. If it is false, a
C<Apache::Session::Wrapper::Exception::NonExistentSessionID> exception
will be thrown instead.
This defaults to true.
=item * session_id => string
Try this session id first and use it if it exist. If the session does
not exist, it will ignore this parameter and make a new session.
=back
=head2 Cookie-Related Parameters
=over 4
=item * use_cookie => boolean
If true, then this module will use one of C<Apache::Cookie>,
C<Apache2::Cookie> or C<CGI::Cookie> (as appropriate) to set and read
cookies that contain the session id.
=item * cookie_name => name
This is the name of the cookie that this module will set. This
defaults to "Apache-Session-Wrapper-cookie".
Corresponds to the C<Apache::Cookie> "-name" constructor parameter.
=item * cookie_expires => expiration
How long before the cookie expires. This defaults to 1 day, "+1d".
Corresponds to the "-expires" parameter.
As a special case, you can set this value to "session" to have the
"-expires" parameter set to undef, which gives you a cookie that
expires at the end of the session.
=item * cookie_domain => domain
This corresponds to the "-domain" parameter. If not given this will
not be set as part of the cookie.
If it is undefined, then no "-domain" parameter will be given.
=item * cookie_path => path
Corresponds to the "-path" parameter. It defaults to "/".
=item * cookie_secure => boolean
Corresponds to the "-secure" parameter. It defaults to false.
=item * cookie_resend => boolean
By default, this parameter is true, and the cookie will be sent for
I<every request>. If it is false, then the cookie will only be sent
when the session is I<created>. This is important as resending the
cookie has the effect of updating the expiration time.
=item * header_object => object
When running outside of mod_perl, you must provide an object to which
the cookie header can be added. This object must provide an
C<err_headers_out()> or C<headers_out()> method.
Under mod_perl 1, this will default to the object returned by C<<
Apache->request() >>. Under mod_perl 2 we call C<<
Apache2::RequestUtil->request() >>
=back
=head2 Query/POST-Related Parameters
=over 4
=item * param_name => name
If set, then this module will first look for the session id in the
object specified via "param_object". This parameter determines the
name of the parameter that is checked.
If you are also using cookies, then the module checks the param object
I<first>, and then it checks for a cookie.
=item * param_object => object
This should be an object that provides a C<param()> method. This
object will be checked to see if it contains the parameter named in
"params_name". This object will probably be a C<CGI.pm> or
C<Apache::Request> object, but it doesn't have to be.
=back
=head2 Apache::Session-related Parameters
These parameters are simply passed through to C<Apache::Session>.
=over 4
=item * data_source => DSN
Corresponds to the C<DataSource> parameter passed to the DBI-related
session modules.
=item * user_name => user name
Corresponds to the C<UserName> parameter passed to the DBI-related
session modules.
=item * password => password
Corresponds to the C<Password> parameter passed to the DBI-related
session modules. Defaults to undef.
=item * handle => DBI handle
Corresponds to the C<Handle> parameter passed to the DBI-related
session modules. This cannot be set via the F<httpd.conf> file,
because it needs to be an I<actual Perl variable>, not the I<name> of
that variable.
=item * table_name => table name
Corresponds to the C<TableName> paramaeter passed to DBI-related
modules.
=item * lock_data_source => DSN
Corresponds to the C<LockDataSource> parameter passed to
C<Apache::Session::MySQL>.
=item * lock_user_name => user name
Corresponds to the C<LockUserName> parameter passed to
C<Apache::Session::MySQL>.
=item * lock_password => password
Corresponds to the C<LockPassword> parameter passed to
C<Apache::Session::MySQL>. Defaults to undef.
=item * lock_handle => DBI handle
Corresponds to the C<LockHandle> parameter passed to the DBI-related
session modules. As with the C<handle> parameter, this cannot
be set via the F<httpd.conf> file.
=item * commit => boolean
Corresponds to the C<Commit> parameter passed to the DBI-related
session modules.
=item * transaction => boolean
Corresponds to the C<Transaction> parameter.
=item * directory => directory
Corresponds to the C<Directory> parameter passed to
C<Apache::Session::File>.
=item * lock_directory => directory
Corresponds to the C<LockDirectory> parameter passed to
C<Apache::Session::File>.
=item * file_name => file name
Corresponds to the C<FileName> parameter passed to
C<Apache::Session::DB_File>.
=item * store => class
Corresponds to the C<Store> parameter passed to
C<Apache::Session::Flex>.
=item * lock => class
Corresponds to the C<Lock> parameter passed to
C<Apache::Session::Flex>.
=item * generate => class
Corresponds to the C<Generate> parameter passed to
C<Apache::Session::Flex>.
=item * serialize => class
Corresponds to the C<Serialize> parameter passed to
C<Apache::Session::Flex>.
=item * textsize => size
Corresponds to the C<textsize> parameter passed to
C<Apache::Session::Sybase>.
=item * long_read_len => size
Corresponds to the C<LongReadLen> parameter passed to
C<Apache::Session::MySQL>.
=item * n_sems => number
Corresponds to the C<NSems> parameter passed to
C<Apache::Session::Lock::Semaphore>.
=item * semaphore_key => key
Corresponds to the C<SemaphoreKey> parameter passed to
C<Apache::Session::Lock::Semaphore>.
=item * mod_usertrack_cookie_name => name
Corresponds to the C<ModUsertrackCookieName> parameter passed to
C<Apache::Session::Generate::ModUsertrack>.
=item * save_path => path
Corresponds to the C<SavePath> parameter passed to
C<Apache::Session::PHP>.
=back
=head1 HOW COOKIES ARE HANDLED
When run under mod_perl, this module attempts to first use
C<Apache::Cookie> for cookie-handling. Otherwise it uses
C<CGI::Cookie> as a fallback.
If it ends up using C<CGI::Cookie> then you must provide a
"header_object" parameter. This object must have an
C<err_headers_out()> or C<headers_out()> method. It looks for these
methods in that order. The method is expected to return an object with
an API like C<Apache::Table>. It calls C<add()> on the returned method
to add a "Set-Cookie" header.
=head1 REGISTERING CLASSES
In order to support any C<Apache::Session> subclasses, this module
provides a simple registration mechanism.
You can register an C<Apache::Session> subclass, or a class intended
to provide a class that implements something required by
C<Apache::Session::Flex>.
=head2 Registering a Complete Subclass
This is done by calling C<< Apache::Session::Wrapper->RegisterClass() >>:
Apache::Session::Wrapper->RegisterClass
( name => 'MyClass',
required => [ [ qw( param1 param2 ) ],
[ qw( param3 param4 ) ] ],
optional => [ 'optional_p' ],
);
Apache::Session::Wrapper->RegisterClass
( name => 'Apache::Session::MyFile',
required => 'File',
optional => 'File',
);
The C<RegisterClass()> method takes the following options:
=over 4
=item * name
This should be the name of the class you are registering. The actual
class must start with "Apache::Session::", but this part does not need
to be included when registering the class (it's optional).
=item * required
These are the required parameters for this class.
The value of this parameter can either be a string or a reference to
an array of array references.
If it is a string, then it identifies an existing C<Apache::Session>
subclass which is already registered or built-in, like "File" or
"Postgres".
If it an array reference, then I<that reference> should in turn
contain one or more array references. Each of those contained
references represents one set of required parameters. When an
C<Apache::Session::Wrapper> object is constructed, only one of these
sets must be passed in. For example:
required => [ [ qw( p1 p2 ) ],
[ qw( p2 p3 p4 ) ] ]
This says that either "p1" and "p2" must be provided, I<or> "p2",
"p3", and "p4".
If there are no required parameters for this class, then the
"required" parameter can be omitted.
=item * optional
This specifies optional parameters, and should just be a simple array
reference.
=back
=head2 Registering a Subclass for Flex
Registering a subclass that can be used with C<Apache::Session::Flex>
is very similar to registering a complete class:
Apache::Session::Wrapper->RegisterFlexClass
( name => 'MyClass',
type => 'Store',
required => [ [ qw( param1 param2 ) ],
[ qw( param3 param4 ) ] ],
optional => [ 'optional_p' ],
);
Apache::Session::Wrapper->RegisterFlexClass
( name => 'Apache::Session::Store::MyFile',
type => 'store',
required => 'File',
optional => 'File',
);
The C<RegisterFlexClass()> method has the same parameters as
C<RegisterClass()>, but it also requires a "type" parameter. This must
be one of "store", "lock", "generate", or "serialize".
=head1 SUBCLASSING
This class provides a simple hook for subclasses. Before trying to
get a session id from the URL or cookie, it calls a method named
C<_get_session_id()>. In this class, that method is a no-op, but you
can override this in a subclass.
This class is a C<Class::Container> subclass, so if you accept
additional constructor parameters, you should declare them via the
C<valid_params()> method.
=head1 SUPPORT
As can be seen by the number of parameters above, C<Apache::Session>
has B<way> too many possibilities for me to test all of them. This
means there are almost certainly bugs.
Please submit bugs to the CPAN RT system at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Apache%3A%3ASession%3A%3AWrapper
or via email at bug-apache-session-wrapper@rt.cpan.org.
Support questions can be sent to me at my email address, shown below.
=head1 AUTHOR
Dave Rolsky, <autarch@urth.org>
=head1 COPYRIGHT
Copyright (c) 2003-2006 David Rolsky. All rights reserved. This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included
with this module.
=cut
|