Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

mod_perl - separate PERL interpreter for each LocationMatch

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


uttamhoode at live

Sep 12, 2011, 12:22 AM

Post #1 of 6 (939 views)
Permalink
mod_perl - separate PERL interpreter for each LocationMatch

Hi All, I have written a mod_perl proxy server which redirects the HTTP traffic to a webserver. I have pasted the snippet below. In the apache worker thread, is enable. In the httpd.conf two <LocationMatch> are configured http://ipaddress/path1/http://ipaddress/path2/ After starting the http server if i open any URL from the browser, same “TEST_VAR” is being printedfor both the URLs. If I call path1 first, then for both the URLs 100 is printed.If I call path2 first, then for both the URLs 200 is printed.(that is its taking the first called Path, after httpd is started) #----------------------------Startup.pl --------------------------------------------##startup.pl use lib qw(/home/test1/libs); 1; #--------------------------------- #---------------------------- httpd.conf---------------------------------------------# # httpd.conf PerlRequire /home/test1/startup.pl #http://ipaddress/path1/<LocationMatch /path1/> SetEnvIf Request_URI "/" TEST_VAR=100 SetHandler perl-script PerlResponseHandler Module::Test</LocationMatch> #http://ipaddress/path2/<LocationMatch /path2/> SetEnvIf Request_URI "/" TEST_VAR=200 SetHandler perl-script PerlResponseHandler Module::Test</LocationMatch> #-------------------------------------------------------------------------------# #---------------------------- Test.pm ---------------------------------------------# # Test.pm package Module::Test; use strict; use Apache2::Const qw(:methods :http :common);use Apache2::Log ();use Apache2::URI (); my $param = env_variable(); sub handler{ Apache2::ServerRec::warn($param); print "Value".$param; return Apache2::Const::OK;} sub env_variable{ # Configuration loading from a file during startup my $endpointURL = $ENV{'TEST_VAR'}; return $endpointURL;} 1; #-------------------------------------------------------------------------------- But I tried with <VirtualHost> option and it is working fine as PerlOptions +Parent is present.But since the port numbers are different firefox will now allow simulataneous access toboth URL from same webpage. #http://ipaddress:8080/path1/<VirtualHost *:8080> SetEnvIf Request_URI "/" TEST_VAR=100 PerlRequire /home/test1/startup.pl PerlOptions +Parent <LocationMatch /path2/> SetHandler perl-script PerlResponseHandler Module::Test </LocationMatch></VirtualHost> #http://ipaddress:8085/path2/<VirtualHost *:8085> SetEnvIf Request_URI "/" TEST_VAR=200 PerlRequire /home/test1/startup.pl PerlOptions +Parent <LocationMatch /path2/> SetHandler perl-script PerlResponseHandler Module::Test </LocationMatch></VirtualHost> Please help me with this, Thanks in advance.Regards,uttam


torsten.foertsch at gmx

Sep 12, 2011, 1:18 AM

Post #2 of 6 (915 views)
Permalink
Re: mod_perl - separate PERL interpreter for each LocationMatch [In reply to]

On Monday, 12 September 2011 09:22:57 utham hoode wrote:
> Hi All, I have written a mod_perl proxy server which redirects
> the HTTP traffic to a webserver. I have pasted the snippet below.
> In the apache worker thread, is enable. In the httpd.conf two
> <LocationMatch> are configured
> http://ipaddress/path1/http://ipaddress/path2/ After starting the
> http server if i open any URL from the browser, same “TEST_VAR” is
> being printedfor both the URLs. If I call path1 first, then for both
> the URLs 100 is printed.If I call path2 first, then for both the URLs
> 200 is printed.(that is its taking the first called Path, after httpd
> is started) #----------------------------Startup.pl
> --------------------------------------------##startup.pl use lib
> qw(/home/test1/libs); 1; #---------------------------------
> #----------------------------
> httpd.conf---------------------------------------------# #
> httpd.conf PerlRequire /home/test1/startup.pl
> #http://ipaddress/path1/<LocationMatch /path1/> SetEnvIf
> Request_URI "/" TEST_VAR=100 SetHandler perl-script
> PerlResponseHandler Module::Test</LocationMatch>
> #http://ipaddress/path2/<LocationMatch /path2/> SetEnvIf
> Request_URI "/" TEST_VAR=200 SetHandler perl-script
> PerlResponseHandler Module::Test</LocationMatch>
> #--------------------------------------------------------------------
> -----------# #---------------------------- Test.pm
> ---------------------------------------------# # Test.pm package
> Module::Test; use strict; use Apache2::Const qw(:methods :http
> :common);use Apache2::Log ();use Apache2::URI (); my $param =
> env_variable(); sub handler{ Apache2::ServerRec::warn($param);
> print "Value".$param; return Apache2::Const::OK;} sub
> env_variable{ # Configuration loading from a file during
> startup my $endpointURL = $ENV{'TEST_VAR'}; return
> $endpointURL;} 1;
> #--------------------------------------------------------------------
> ------------ But I tried with <VirtualHost> option and it is working
> fine as PerlOptions +Parent is present.But since the port numbers are
> different firefox will now allow simulataneous access toboth URL from
> same webpage. #http://ipaddress:8080/path1/<VirtualHost *:8080>
> SetEnvIf Request_URI "/" TEST_VAR=100 PerlRequire
> /home/test1/startup.pl PerlOptions +Parent <LocationMatch
> /path2/> SetHandler perl-script PerlResponseHandler
> Module::Test </LocationMatch></VirtualHost>
> #http://ipaddress:8085/path2/<VirtualHost *:8085> SetEnvIf
> Request_URI "/" TEST_VAR=200 PerlRequire /home/test1/startup.pl
> PerlOptions +Parent <LocationMatch /path2/> SetHandler
> perl-script PerlResponseHandler Module::Test
> </LocationMatch></VirtualHost> Please help me with this, Thanks in
> advance.Regards,uttam

Could you please resend the mail with proper line breaks? Otherwise, it's
indecipherable, at least for me.

Torsten Förtsch

--
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net


uttamhoode at live

Sep 12, 2011, 2:04 AM

Post #3 of 6 (917 views)
Permalink
RE: mod_perl - separate PERL interpreter for each LocationMatch [In reply to]

Hi All,

I have written a mod_perl proxy server which redirects the HTTP traffic to
a webserver.
In the apache worker thread is enable. In the httpd.conf two
are configured
http://ipaddress/path1/
http://ipaddress/path2/
After starting the http server if i open any URL from the browser same
TEST_VAR is being printed
for both the URLs.
If I call path1 first then for both the URLs 100 is printed.
If I call path2 first then for both the URLs 200 is printed.

#startup.pl
use lib qw(/home/test1/libs);
1;

#---------------------------------

# httpd.conf
PerlRequire /home/test1/startup.pl
#http://ipaddress/path1/

SetEnvIf Request_URI "/" TEST_VAR=100
SetHandler perl-script
PerlResponseHandler Module::Test

#http://ipaddress/path2/

SetEnvIf Request_URI "/" TEST_VAR=200
SetHandler perl-script
PerlResponseHandler Module::Test


#Test.pm
#--------------------------------------------------------------------
package Module::Test;
use strict;
use Apache2::Const qw(:methods :http :common);
use Apache2::Log ();
use Apache2::URI ();

my $param = env_variable();
sub handler
{
Apache2::ServerRec::warn($param);
print "Value".$param;
return Apache2::Const::OK;
}

sub env_variable
{
# Configuration loading from a file during startup
my $endpointURL = $ENV{'TEST_VAR'};
return $endpointURL;
}
1;
#--------------------------------------------------------------------------------

But I tried with option and it is working fine as PerlOptions
+Parent is present.
But since the port numbers are different firefox will now allow simulataneous
access to
both URL from same webpage.

#http://ipaddress:8080/path1/

SetEnvIf Request_URI "/" TEST_VAR=100
PerlRequire /home/test1/startup.pl
PerlOptions +Parent

SetHandler perl-script
PerlResponseHandler Module::Test



#http://ipaddress:8085/path2/

SetEnvIf Request_URI "/" TEST_VAR=200
PerlRequire /home/test1/startup.pl
PerlOptions +Parent

SetHandler perl-script
PerlResponseHandler Module::Test



Regards,
uttam


----------------------------------------
> From: torsten.foertsch [at] gmx
> To: modperl [at] perl
> Subject: Re: mod_perl - separate PERL interpreter for each LocationMatch
> Date: Mon, 12 Sep 2011 10:18:59 +0200
> CC: uttamhoode [at] live
>
> On Monday, 12 September 2011 09:22:57 utham hoode wrote:
> > Hi All, I have written a mod_perl proxy server which redirects
> > the HTTP traffic to a webserver. I have pasted the snippet below.
> > In the apache worker thread, is enable. In the httpd.conf two
> > are configured
> > http://ipaddress/path1/http://ipaddress/path2/ After starting the
> > http server if i open any URL from the browser, same “TEST_VAR” is
> > being printedfor both the URLs. If I call path1 first, then for both
> > the URLs 100 is printed.If I call path2 first, then for both the URLs
> > 200 is printed.(that is its taking the first called Path, after httpd
> > is started) #----------------------------Startup.pl
> > --------------------------------------------##startup.pl use lib
> > qw(/home/test1/libs); 1; #---------------------------------
> > #----------------------------
> > httpd.conf---------------------------------------------# #
> > httpd.conf PerlRequire /home/test1/startup.pl
> > #http://ipaddress/path1/ SetEnvIf
> > Request_URI "/" TEST_VAR=100 SetHandler perl-script
> > PerlResponseHandler Module::Test
> > #http://ipaddress/path2/ SetEnvIf
> > Request_URI "/" TEST_VAR=200 SetHandler perl-script
> > PerlResponseHandler Module::Test
> > #--------------------------------------------------------------------
> > -----------# #---------------------------- Test.pm
> > ---------------------------------------------# # Test.pm package
> > Module::Test; use strict; use Apache2::Const qw(:methods :http
> > :common);use Apache2::Log ();use Apache2::URI (); my $param =
> > env_variable(); sub handler{ Apache2::ServerRec::warn($param);
> > print "Value".$param; return Apache2::Const::OK;} sub
> > env_variable{ # Configuration loading from a file during
> > startup my $endpointURL = $ENV{'TEST_VAR'}; return
> > $endpointURL;} 1;
> > #--------------------------------------------------------------------
> > ------------ But I tried with option and it is working
> > fine as PerlOptions +Parent is present.But since the port numbers are
> > different firefox will now allow simulataneous access toboth URL from
> > same webpage. #http://ipaddress:8080/path1/
> > SetEnvIf Request_URI "/" TEST_VAR=100 PerlRequire
> > /home/test1/startup.pl PerlOptions +Parent > /path2/> SetHandler perl-script PerlResponseHandler
> > Module::Test
> > #http://ipaddress:8085/path2/ SetEnvIf
> > Request_URI "/" TEST_VAR=200 PerlRequire /home/test1/startup.pl
> > PerlOptions +Parent SetHandler
> > perl-script PerlResponseHandler Module::Test
> > Please help me with this, Thanks in
> > advance.Regards,uttam
>
> Could you please resend the mail with proper line breaks? Otherwise, it's
> indecipherable, at least for me.
>
> Torsten Förtsch
>
> --
> Need professional modperl support? Hire me! (http://foertsch.name)
>
> Like fantasy? http://kabatinte.net


torsten.foertsch at gmx

Sep 12, 2011, 2:52 AM

Post #4 of 6 (909 views)
Permalink
Re: mod_perl - separate PERL interpreter for each LocationMatch [In reply to]

On Monday, 12 September 2011 11:04:35 utham hoode wrote:
> I have written a mod_perl proxy server which redirects the HTTP
> traffic to a webserver.
> In the apache worker thread is enable. In the httpd.conf two
> are configured
> http://ipaddress/path1/
> http://ipaddress/path2/
> After starting the http server if i open any URL from the browser same
> TEST_VAR is being printed
> for both the URLs.
> If I call path1 first then for both the URLs 100 is printed.
> If I call path2 first then for both the URLs 200 is printed.

I am still a bit at a loss what you are trying to do here. The phrase
"modperl proxy server that redirects" is quite ambiguous:

- a modperl handler that works in the response phase, mangles the request
a bit before sending it to a backend server. On the way back the response
from the backend is again a bit mangled perhaps and sent to the client

- on the other hand it may be a server that sends out HTTP redirects to
point the browser to the correct server using HTTP 3xx codes.

- and thirdly it may be a piece of code working in a phase between
maptostorage and fixup that modifies the request configuration to have it
handled by mod_proxy.

- perhaps there are a few more possible meanings

Generally, apache creates an individual configuration for each request.
At first it looks up the virtual server config. Then it merges into that
one all the <Location> <Directory> <Files> blocks that are applicable.

So, you can set a certain environment variable for one request to value1
and for another to value2. But you have to have a distinguishing mark.

> #startup.pl
> use lib qw(/home/test1/libs);
> 1;
>
> #---------------------------------
>
> # httpd.conf
> PerlRequire /home/test1/startup.pl
> #http://ipaddress/path1/
>
> SetEnvIf Request_URI "/" TEST_VAR=100
> SetHandler perl-script
> PerlResponseHandler Module::Test
>
> #http://ipaddress/path2/
>
> SetEnvIf Request_URI "/" TEST_VAR=200
> SetHandler perl-script
> PerlResponseHandler Module::Test

In the snippet above I do not see any such mark. You set TEST_VAR to 100
and to 200 if the request URI is /. That does not make sense.

>
> #Test.pm
> #--------------------------------------------------------------------
> package Module::Test;
> use strict;
> use Apache2::Const qw(:methods :http :common);
> use Apache2::Log ();
> use Apache2::URI ();
>
> my $param = env_variable();

Here you use a global variable that is set only once for the lifetime of
the interpreter when the module is compiled. $param is not set for each
request.

> sub handler
> {
> Apache2::ServerRec::warn($param);
> print "Value".$param;
> return Apache2::Const::OK;
> }
>
> sub env_variable
> {
> # Configuration loading from a file during startup
> my $endpointURL = $ENV{'TEST_VAR'};
> return $endpointURL;
> }
> 1;
> #---------------------------------------------------------------------
> -----------
>
> But I tried with option and it is working fine as PerlOptions
> +Parent is present.
> But since the port numbers are different firefox will now allow
> simulataneous access to
> both URL from same webpage.
>
> #http://ipaddress:8080/path1/
>
> SetEnvIf Request_URI "/" TEST_VAR=100
> PerlRequire /home/test1/startup.pl
> PerlOptions +Parent
>
> SetHandler perl-script
> PerlResponseHandler Module::Test
>
>
>
> #http://ipaddress:8085/path2/
>
> SetEnvIf Request_URI "/" TEST_VAR=200
> PerlRequire /home/test1/startup.pl
> PerlOptions +Parent
>
> SetHandler perl-script
> PerlResponseHandler Module::Test

+Parent is a VHost level option like:

<VirtualHost ...>
PerlOptions +Parent
...
</VirtualHost>

The virtual host then gets it's own perl interpreter. It has been
requested a few times to make it possible to change the interpreter per
request configuration but nobody has got around to do the work yet.
Something along these lines would be good:

<PerlInterpreter MyInterpreter>
PerlSwitches ...
PerlRequire ...
PerlModule ...
</PerlInterpreter>

<PerlInterpreter OtherInterpreter>
PerlSwitches ...
PerlRequire ...
PerlModule ...
</PerlInterpreter>

<Location /...>
PerlUseInterpreter MyInterpreter
</Location>

Torsten Förtsch

--
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net


uttamhoode at live

Sep 12, 2011, 4:03 AM

Post #5 of 6 (921 views)
Permalink
RE: mod_perl - separate PERL interpreter for each LocationMatch [In reply to]

----------------------------------------
> From: torsten.foertsch [at] gmx
> To: modperl [at] perl
> Subject: Re: mod_perl - separate PERL interpreter for each LocationMatch
> Date: Mon, 12 Sep 2011 11:52:57 +0200
> CC: uttamhoode [at] live
>
> On Monday, 12 September 2011 11:04:35 utham hoode wrote:
> > I have written a mod_perl proxy server which redirects the HTTP
> > traffic to a webserver.
> > In the apache worker thread is enable. In the httpd.conf two
> > are configured
> > http://ipaddress/path1/
> > http://ipaddress/path2/
> > After starting the http server if i open any URL from the browser same
> > TEST_VAR is being printed
> > for both the URLs.
> > If I call path1 first then for both the URLs 100 is printed.
> > If I call path2 first then for both the URLs 200 is printed.
>
> I am still a bit at a loss what you are trying to do here. The phrase
> "modperl proxy server that redirects" is quite ambiguous:

Thanks for the quick response.
Sorry for not providing all the details. Actual proxy program is working fine withsingle <LocationMatch>. I wrote Test.pm just to demonstrate the issue.

>
> - a modperl handler that works in the response phase, mangles the request
> a bit before sending it to a backend server. On the way back the response
> from the backend is again a bit mangled perhaps and sent to the client
>
> - on the other hand it may be a server that sends out HTTP redirects to
> point the browser to the correct server using HTTP 3xx codes.
>
> - and thirdly it may be a piece of code working in a phase between
> maptostorage and fixup that modifies the request configuration to have it
> handled by mod_proxy.
>
> - perhaps there are a few more possible meanings
>
> Generally, apache creates an individual configuration for each request.
> At first it looks up the virtual server config. Then it merges into that
> one all the <Location> <Directory> <Files> blocks that are applicable.
>
> So, you can set a certain environment variable for one request to value1
> and for another to value2. But you have to have a distinguishing mark.
>
> > #startup.pl
> > use lib qw(/home/test1/libs);
> > 1;
> >
> > #---------------------------------
> >
> > # httpd.conf
> > PerlRequire /home/test1/startup.pl
> > #http://ipaddress/path1/
> >
> > SetEnvIf Request_URI "/" TEST_VAR=100
> > SetHandler perl-script
> > PerlResponseHandler Module::Test
> >
> > #http://ipaddress/path2/
> >
> > SetEnvIf Request_URI "/" TEST_VAR=200
> > SetHandler perl-script
> > PerlResponseHandler Module::Test
>
> In the snippet above I do not see any such mark. You set TEST_VAR to 100
> and to 200 if the request URI is /. That does not make sense.

I tried this but did not work.
SetEnvIf Request_URI "/path1/" TEST_VAR=100
and 
SetEnvIf Request_URI "/path2/" TEST_VAR=200


>
> >
> > #Test.pm
> > #--------------------------------------------------------------------
> > package Module::Test;
> > use strict;
> > use Apache2::Const qw(:methods :http :common);
> > use Apache2::Log ();
> > use Apache2::URI ();
> >
> > my $param = env_variable();
>
> Here you use a global variable that is set only once for the lifetime of
> the interpreter when the module is compiled. $param is not set for each
> request.


Here is the problem. Both path1 and path2 are sharing same global variable $param.Is there any way to avoid that?. Because In the actual proxy program I am reading aconfiguration file and storing it in a global variable. For path1 and path2 configuration file is different. I can place the variable inside sub handler {}so that for  each request program loads the configuration file.I think this will reduce the performance.

>
> > sub handler
> > {
> > Apache2::ServerRec::warn($param);
> > print "Value".$param;
> > return Apache2::Const::OK;
> > }
> >
> > sub env_variable
> > {
> > # Configuration loading from a file during startup
> > my $endpointURL = $ENV{'TEST_VAR'};
> > return $endpointURL;
> > }
> > 1;
> > #---------------------------------------------------------------------
> > -----------
> >
> > But I tried with option and it is working fine as PerlOptions
> > +Parent is present.
> > But since the port numbers are different firefox will now allow
> > simulataneous access to
> > both URL from same webpage.
> >
> > #http://ipaddress:8080/path1/
> >
> > SetEnvIf Request_URI "/" TEST_VAR=100
> > PerlRequire /home/test1/startup.pl
> > PerlOptions +Parent
> >
> > SetHandler perl-script
> > PerlResponseHandler Module::Test
> >
> >
> >
> > #http://ipaddress:8085/path2/
> >
> > SetEnvIf Request_URI "/" TEST_VAR=200
> > PerlRequire /home/test1/startup.pl
> > PerlOptions +Parent
> >
> > SetHandler perl-script
> > PerlResponseHandler Module::Test
>
> +Parent is a VHost level option like:
>
> <VirtualHost ...>
> PerlOptions +Parent
> ...
> </VirtualHost>
>
> The virtual host then gets it's own perl interpreter. It has been
> requested a few times to make it possible to change the interpreter per
> request configuration but nobody has got around to do the work yet.
> Something along these lines would be good:
    I wish below feature is supported.

>
> <PerlInterpreter MyInterpreter>
> PerlSwitches ...
> PerlRequire ...
> PerlModule ...
> </PerlInterpreter>
>
> <PerlInterpreter OtherInterpreter>
> PerlSwitches ...
> PerlRequire ...
> PerlModule ...
> </PerlInterpreter>
>
> <Location /...>
> PerlUseInterpreter MyInterpreter
> </Location>
>
> Torsten Förtsch
>
> --
> Need professional modperl support? Hire me! (http://foertsch.name)
>
> Like fantasy? http://kabatinte.net


torsten.foertsch at gmx

Sep 12, 2011, 4:36 AM

Post #6 of 6 (911 views)
Permalink
Re: mod_perl - separate PERL interpreter for each LocationMatch [In reply to]

On Monday, 12 September 2011 13:03:30 utham hoode wrote:
> Here is the problem. Both path1 and path2 are sharing same global
> variable $param.Is there any way to avoid that?. Because In the
> actual proxy program I am reading aconfiguration file and storing it
> in a global variable. For path1 and path2 configuration file is
> different. I can place the variable inside sub handler {}so that for
> each request program loads the configuration file.I think this will
> reduce the performance.

how about

=httpd.conf=========================================
<LocationMatch "^/path1">
SetEnv CFG /path/to/config1.conf
</LocationMatch>

<LocationMatch "^/path2">
SetEnv CFG /path/to/config2.conf
</LocationMatch>
====================================================

=the handler========================================
my %config;

sub handler {
my ($r)=@_;

my $cfg=$r->subprocess_env->{CFG} or
die "No config file configured for ".$r->uri;

$cfg=($config{$cfg}||=read_cfg $cfg);
...
}
====================================================

Thus, the global %config is used as a cache for the configurations of the
various URIs. The actual file is read only on the first request.

All the configurations can even be pre-populated at startup time.

Note however, that since the config is read only once it can be changed
only by starting up a new interpreter. If that's a problem you can
monitor file modification times for example. If I were you I'd use an
MMapDB object.

Torsten Förtsch

--
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.