Some standart and simple tasts with MP2 HOWTO

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=0PrintE rror=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=0PrintE rror=1Username=user'

Where is a mistake ?

Best regards,
Vladimir S. Tikhonjuk
vst [ Sa, 29 Juli 2006 11:42 ] [ ID #1409766 ]

Re: Some standart and simple tasts with MP2 HOWTO

Vladimir S. Tikhonjuk wrote:

>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 ).
>
>
>

Use CGI.pm or libapreq also known as Apache2::Request.

http://search.cpan.org/~lds/CGI.pm-3.20/
http://search.cpan.org/~joesuf/libapreq2-2.07/

<http://search.cpan.org/%7Ejoesuf/libapreq2-2.07/glue/perl/lib/Apache2/Request.pm>

> 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.
>
>
>
Use Apache::Session or CGI::Session

http://search.cpan.org/~cwest/Apache-Session-1.81/
http://search.cpan.org/~markstos/CGI-Session-4.14/

>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?
>
>
>
Because when storing informations in global variables they presist
between requests made to this very apache child.
Pnotes only stay for this single request. In short if you restore this
information when making the next request which maybe
totally unrelated to the first one this information is still there.
Please see below why you cannot use this to restore
persistent information.

> 3. Database persistant connection.
>
> As I understand, such possibility I can get only using Apache::DBI
>module. So, here is my httpd.conf:
>
>
>
>
If I'm not complete mistaken but others may know better Apache::DBI
persists connections on a per-child base
so as long as you hit other children when requesting pages (you don't
have control over that because this is done by).
This is also the reason why it won't help you to restore information in
this global scope variables.

Please search the archives for a discussion about request handling by
apache.

http://mail-archives.apache.org/mod_mbox/perl-modperl/200504 .mbox/%3C426E73E8.9060600 [at] gmx.at%3E

>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=0Print Error=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=0Print Error=1Username=user'
>
>Where is a mistake ?
>
>Best regards,
>Vladimir S. Tikhonjuk
>
>
Tom
Tom Schindl [ Sa, 29 Juli 2006 13:21 ] [ ID #1409767 ]

Re: Some standart and simple tasts with MP2 HOWTO

>>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 ).
>>
>Use CGI.pm or libapreq also known as Apache2::Request.
>
>http://search.cpan.org/~lds/CGI.pm-3.20/
>http://search.cpan.org/~joesuf/libapreq2-2.07/
>
><http://search.cpan.org/%7Ejoesuf/libapreq2-2.07/glue/perl/lib/Apache2/Request.pm>
>
>
Thanks a lot. This modules really helped me.

>> 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.
>>
>>

Well, can you post me short example of how to use Apache::Session with
Apache::DBI.
I wan't make FirstHandle, which will be executed when user come to
"/debitor" path. Connection
to database will established in this handle. Then, when user go to the
"/debitor/result" path,
the SecondHadle will be executed, with the same ( as in FirstHandle )
database connection.
vst [ So, 30 Juli 2006 13:33 ] [ ID #1410530 ]

Re: Some standart and simple tasts with MP2 HOWTO

Vladimir S. Tikhonjuk schrieb:
>>> 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 ).
>>>
>> Use CGI.pm or libapreq also known as Apache2::Request.
>>
>> http://search.cpan.org/~lds/CGI.pm-3.20/
>> http://search.cpan.org/~joesuf/libapreq2-2.07/
>>
>> <http://search.cpan.org/%7Ejoesuf/libapreq2-2.07/glue/perl/lib/Apache2/Request.pm>
>>
>>
> Thanks a lot. This modules really helped me.
>
>>> 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.
>>>
>>>
>
> Well, can you post me short example of how to use Apache::Session with
> Apache::DBI.
> I wan't make FirstHandle, which will be executed when user come to
> "/debitor" path. Connection
> to database will established in this handle. Then, when user go to the
> "/debitor/result" path,
> the SecondHadle will be executed, with the same ( as in FirstHandle )
> database connection.

Load Apache::DBI like shown in the POD:
http://search.cpan.org/~pgollucci/Apache-DBI-1.01/lib/Apache /DBI.pm

This way the DBI-methods DBI->connect() and DBI->disconnect() are
overloaded. Afterwards you can call DBI->connect() and DBI->disconnect()
like you would in any perl-script. The magic is handled in the background.

Another thing I would think about is: Do you really need Physical
database users and not always connect using same user/pwd e.g.
apache/mypass and handle the rest of the magic in you application
otherwise Apache::DBI has to connect for every every user in every child:

10 different user + 30 forked children => 300 Opened connections

Tom
Tom Schindl [ Mo, 31 Juli 2006 07:04 ] [ ID #1411599 ]

Re: Some standart and simple tasts with MP2 HOWTO

> http://search.cpan.org/~pgollucci/Apache-DBI-1.01/lib/Apache /DBI.pm
http://search.cpan.org/dist/Apache-DBI-1.01/lib/Apache/DBI.p m

In case anyone cares, now that its 1.01 and not 1.00_01 (dev). CPAN indexes this correctly.
The previous cause redirects....

Thanks for answering Tom.


------------------------------------------------------------ ------------
Philip M. Gollucci (pgollucci [at] p6m7g8.com) 323.219.4708
Consultant / http://p6m7g8.net/Resume/resume.shtml
Senior Software Engineer - TicketMaster - http://ticketmaster.com
1024D/A79997FA F357 0FDD 2301 6296 690F 6A47 D55A 7172 A799 97F

"In all that I've done wrong I know I must have done something right to
deserve a hug every morning and butterfly kisses at night."
pgollucci [ Mo, 31 Juli 2006 08:28 ] [ ID #1411600 ]
Webserver » gmane.comp.apache.mod-perl » Some standart and simple tasts with MP2 HOWTO

Vorheriges Thema: How to detect if a module is loaded from the master apache
Nächstes Thema: Help: ENV shared between requests???