| 1 | #!/usr/local/bin/perl | ||
| 2 | #=============================================================================== | ||
| 3 | # | ||
| 4 | # FILE: wd_shell.pl | ||
| 5 | # | ||
| 6 | # DESCRIPTION: shell script for WebDAO project | ||
| 7 | # AUTHOR: Aliaksandr P. Zahatski (Mn), <zag@cpan.org> | ||
| 8 | #=============================================================================== | ||
| 9 | package WebDAO::Shell::Writer; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my $self = bless( ( $#_ == 0 ) ? shift : {@_}, ref($class) || $class ); | ||
| 14 | } | ||
| 15 | sub write { print $_[1] } | ||
| 16 | sub close { } | ||
| 17 | sub headers { } | ||
| 18 | |||
| 19 | package main; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Carp; | ||
| 23 | use WebDAO; | ||
| 24 | use WebDAO::SessionSH; | ||
| 25 | use WebDAO::CV; | ||
| 26 | use Data::Dumper; | ||
| 27 | use WebDAO::Lex; | ||
| 28 | use Getopt::Long; | ||
| 29 | use Pod::Usage; | ||
| 30 | use WebDAO::Util; | ||
| 31 | use MIME::Base64; | ||
| 32 | |||
| 33 | my ( $help, $man, $sess_id, $dump_headers ); | ||
| 34 | my %opt = ( help => \$help, man => \$man, sid => \$sess_id, d=>\$dump_headers ); | ||
| 35 | my @urls = (); | ||
| 36 | GetOptions( \%opt, 'help|?', 'man', 'd', 'f=s', 'wdEngine|M=s', 'wdEnginePar=s', 'c=s', 'u=s', | ||
| 37 | 'sid|s=s', '<>' => sub { push @urls, shift } ) | ||
| 38 | or pod2usage(2); | ||
| 39 | pod2usage(1) if $help; | ||
| 40 | pod2usage( -exitstatus => 0, -verbose => 2 ) if $man; | ||
| 41 | |||
| 42 | if ($opt{u}) { | ||
| 43 | $ENV{"HTTP_AUTHORIZATION"} = | ||
| 44 | " " . encode_base64($opt{u}); | ||
| 45 | } | ||
| 46 | my $evl_file = shift @urls; | ||
| 47 | pod2usage( -exitstatus => 2, -message => 'No path give or non exists ' ) | ||
| 48 | unless $evl_file; | ||
| 49 | |||
| 50 | #cut params from command line | ||
| 51 | ( $ENV{PATH_INFO}, $ENV{QUERY_STRING} ) = split( /\?/, $evl_file ); | ||
| 52 | $evl_file = $ENV{PATH_INFO}; | ||
| 53 | |||
| 54 | foreach my $sname ('__DIE__') { | ||
| 55 | $SIG{$sname} = sub { | ||
| 56 | return if ( caller(1) )[3] =~ /eval/; | ||
| 57 | push @_, "STACK:" . Dumper( [ map { [ caller($_) ] } ( 1 .. 3 ) ] ); | ||
| 58 | print STDERR "PID: $$ $sname: @_"; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | $ENV{wdEngine} ||= $opt{wdEngine} || 'WebDAO::Engine'; | ||
| 63 | $ENV{wdEnginePar} ||= $opt{wdEnginePar}; | ||
| 64 | #overwrite wdEnginePar with config=... | ||
| 65 | if ( $opt{c} ) { | ||
| 66 | $ENV{wdEnginePar}='config='.$opt{c}; | ||
| 67 | } | ||
| 68 | $ENV{wdSession} ||= 'WebDAO::SessionSH'; | ||
| 69 | $ENV{wdShell} = 1; | ||
| 70 | my $ini = WebDAO::Util::get_classes( __env => \%ENV, __preload => 1 ); | ||
| 71 | |||
| 72 | #Make Session object | ||
| 73 | my $cv = WebDAO::CV->new( | ||
| 74 | env => \%ENV, | ||
| 75 | writer => sub { | ||
| 76 | my $fd = new WebDAO::Shell::Writer:: | ||
| 77 | status => $_[0]->[0], | ||
| 78 | headers => $_[0]->[1]; | ||
| 79 | my $str; | ||
| 80 | if ( $dump_headers ) { | ||
| 81 | while (my ($h, $v) = splice (@{$_[0]->[1]}, 0, 2 ) ) { | ||
| 82 | $str .= "$h: $v\n" | ||
| 83 | } | ||
| 84 | $str.="\n\n"; | ||
| 85 | $fd->write($str) | ||
| 86 | } | ||
| 87 | return $fd; | ||
| 88 | } | ||
| 89 | ); | ||
| 90 | |||
| 91 | my $sess = "$ini->{wdSession}"->new( | ||
| 92 | %{ $ini->{wdSessionPar} }, | ||
| 93 | cv => $cv, | ||
| 94 | ); | ||
| 95 | |||
| 96 | $sess->U_id($sess_id); | ||
| 97 | |||
| 98 | my $filename = exists $opt{f} ? $opt{f} : $ENV{wdIndexFile}; | ||
| 99 | |||
| 100 | my %engine_args = (); | ||
| 101 | if ( $filename && $filename ne '-' ) { | ||
| 102 | unless ( -r $filename && -f $filename ) { | ||
| 103 | warn <<TXT; | ||
| 104 | ERR:: file not found or can't access (wdIndexFile): $filename | ||
| 105 | check -f option or env variable wdIndexFile; | ||
| 106 | TXT | ||
| 107 | exit 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | open FH, "<$filename" or die $!; | ||
| 111 | my $content = ''; | ||
| 112 | { | ||
| 113 | local $/ = undef; | ||
| 114 | $content = <FH>; | ||
| 115 | } | ||
| 116 | close FH; | ||
| 117 | my $lex = new WebDAO::Lex:: tmpl => $content; | ||
| 118 | $engine_args{lex} = $lex; | ||
| 119 | } | ||
| 120 | my $eng = "$ini->{wdEngine}"->new( | ||
| 121 | %{ $ini->{wdEnginePar} }, | ||
| 122 | session => $sess, | ||
| 123 | %engine_args | ||
| 124 | ); | ||
| 125 | |||
| 126 | $sess->ExecEngine( $eng, $evl_file ); | ||
| 127 | $sess->destroy; | ||
| 128 | croak STDERR $@ if $@; | ||
| 129 | print "\n"; | ||
| 130 | |||
| 131 | =head1 NAME | ||
| 132 | |||
| 133 | wd_shell.pl - command line tool for developing and debuging | ||
| 134 | |||
| 135 | =head1 SYNOPSIS | ||
| 136 | |||
| 137 | wd_shell.pl [options] /some/url/query | ||
| 138 | wd_shell.pl [options] '/some/url/query?param1=1' | ||
| 139 | |||
| 140 | options: | ||
| 141 | |||
| 142 | -help - print help message | ||
| 143 | -man - print man page | ||
| 144 | -f file - set root [x]html file | ||
| 145 | -d - dump HTTP headers | ||
| 146 | -u login:password - set HTTP_AUTHORIZATION variable | ||
| 147 | |||
| 148 | examples: | ||
| 149 | |||
| 150 | wd_shell.pl -wdEngine Test -wdEnginePar config=../test.ini /some/url/query | ||
| 151 | wd_shell.pl -M Test -c ../test.ini /some/url/query #the same | ||
| 152 | |||
| 153 | |||
| 154 | =head1 OPTIONS | ||
| 155 | |||
| 156 | =over 8 | ||
| 157 | |||
| 158 | =item B<-help> | ||
| 159 | |||
| 160 | Print a brief help message and exits | ||
| 161 | |||
| 162 | =item B<-man> | ||
| 163 | |||
| 164 | Prints manual page and exits | ||
| 165 | |||
| 166 | =item B<-f> L<filename> | ||
| 167 | |||
| 168 | Set L<filename> set root [x]html file for load domain | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =head1 DESCRIPTION | ||
| 173 | |||
| 174 | B<wd_shell.pl> - tool for debug . | ||
| 175 | |||
| 176 | =head1 SEE ALSO | ||
| 177 | |||
| 178 | http://sourceforge.net/projects/webdao, WebDAO | ||
| 179 | |||
| 180 | =head1 AUTHOR | ||
| 181 | |||
| 182 | Zahatski Aliaksandr, E<lt>zag@cpan.orgE<gt> | ||
| 183 | |||
| 184 | =head1 COPYRIGHT AND LICENSE | ||
| 185 | |||
| 186 | Copyright 2000-2013 by Zahatski Aliaksandr | ||
| 187 | |||
| 188 | This library is free software; you can redistribute it and/or modify | ||
| 189 | it under the same terms as Perl itself. | ||
| 190 | |||
| 191 | =cut | ||
| 192 |