Hi all!
Our company has a backoffice project written on C++ ( Qt ) for Linux
by my department. But nessesity appear's to make WEB interface, with all
functions which our C++ program does. Now I have to choose among a lot
of languages and technologies of WEB development.
I have a lot of experience with perl, and we already have an Apache
2 WEB server, so first, I look at mod_perl. Reading documentation and
making some practice with mod_perl, I didn't find ( or understand ) how
to solve some, for my point of view, standart tasks.
1. Reading form parameters ( i.e. POST data of request header ).
I have already read 'perldoc Apache2::RequestRec' about bucket
brigades to get POST data, but I think that it's to difficult for such
simple task, and may be there is another solution ? Here is mine, which
I wrote, before I found "bucket brigades" variant:
======= Part of httpd.conf BEGIN =======
PerlModule Promtelecom::PostDataParser
<Location />
PerlHeaderParserHandler Promtelecom::PostDataParser
</Location>
======= Part of httpd.conf END =======
======= PostDataParser.pm BEGIN =======
package Promtelecom::PostDataParser;
use strict;
use Apache2::Const -compile => qw( OK );
my %post_data = ();
sub handler {
my $r = shift;
if( $r->method eq "POST" ) {
my $content = "";
$r->read( $content, $r->headers_in()->get( 'Content-length' ) );
foreach my $pair ( split( /[&;]/, $content ) ) {
my ( $key, $value ) = split( '=', $pair, 2 );
$Promtelecom::PostDataParser::post_data{ $key } = $value;
}
}
return Apache2::Const::OK;
}
1;
======= PostDataParser.pm END =======
Here I parse POST data into "post_data" hash. So, I can access to
values of this hash from other handlers like this:
$Promtelecom::PostDataParser::post_data{"key"}
So, the question is: am I right ? This solution works, but I don't
know if its enough right for mod_perl programming style and architecture.
2. Result of first question :) Sessions.
As I understand, I need session to save ( restore ) user data
between requests. What for ? Now I'll try to explain.
Here is my another try to work with mod_perl :) Simple
Authentication handle, which takes login user and password and try to
make a Database connection. If connection was established - then
Authentication passed,else - not.
======= PostDataParser.pm BEGIN =======
package Promtelecom::Authentication;
use strict;
use Apache2::Const -compile => qw(OK HTTP_UNAUTHORIZED);
$Apache::DBI::DEBUG = 1;
my $user = '';
my $password = '';
sub handler {
my $r = shift;
my $status = 0;
( $status, $Promtelecom::Authentication::password ) =
$r->get_basic_auth_pw;
return $status unless $status == Apache2::Const::OK;
$Promtelecom::Authentication::user = $r->user;
my $dbh = DBI->connect( 'dbi:Pg:dbname=database;host=database',
$Promtelecom::Authentication::user,
$Promtelecom::Authentication::password, { AutoCommit => 0 } );
return Apache2::Const::OK if defined $dbh;
return Apache2::Const::HTTP_UNAUTHORIZED;
}
1;
======= PostDataParser.pm END =======
So, why I need session's or pnote's for passing data between handlers if
I can make vars like $Promtelecom::Authentication::password and
$Promtelecom::Authentication::login and access them from other handlers?
3. Database persistant connection.
As I understand, such possibility I can get only using Apache::DBI
module. So, here is my httpd.conf:
======= Part of httpd.conf BEGIN =======
PerlModule Apache::DBI
PerlModule Promtelecom::Authentication
PerlModule Promtelecom::PostDataParser
<Location />
PerlAuthenHandler Promtelecom::Authentication
AuthType Basic
AuthName "Promtelecom Network"
Require valid-user
PerlHeaderParserHandler Promtelecom::PostDataParser
</Location>
PerlModule Promtelecom::Debitor
<Location /debitor>
SetHandler perl-script
PerlHandler Promtelecom::Debitor
</Location>
======= Part of httpd.conf END =======
You have already seen my Promtelecom::Authentication module ( question 2
), so here is a simple code of Promtelecom::Debitor:
======= PostDataParser.pm BEGIN =======
package Promtelecom::Debitor;
use strict;
use Apache2::Const -compile => qw( OK );
sub handler {
my $r = shift;
my $dbh = DBI->connect( 'dbi:Pg:dbname=database;host=database',
$Promtelecom::Authentication::user,
$Promtelecom::Authentication::password, { AutoCommit => 0 } );
return Apache2::Const::OK;
}
1;
======= PostDataParser.pm END =======
So, here is what I'm waiting from mod_perl:
1. Client open browser and enter address http://myserver/debitor
2. Apache ( on myserver ) get the request, and call's PerlAuthenHandler
Promtelecom::Authentication
3. Client get then window with login and password line edits.
4. Client enters login and password.
5. Promtelecom::Authentication gets client's login and password and try
to set database connection using these datas.
In /var/log/apache2/error_log I see something like:
11836 Apache::DBI new connect to
'dbname=database;host=databaseuserpasswordAutoCommit=0PrintError=1Username=user'
6. If connection established, Apache calls PerlHandler Promtelecom::Debitor
7. Here I think that Apache::DBI will see that
Promtelecom::Authentication already established database connection with
same parameters and returns me this connection, but I see new string in
err_log here:
11838 Apache::DBI new connect to
'dbname=database;host=databaseuserpasswordAutoCommit=0PrintError=1Username=user'
Where is a mistake ?
Best regards,
Vladimir S. Tikhonjuk
Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.