RE: HTTP POST: parameters empty when using ModPerl::Registry(okay when using ModPerl:PerlRun)...

2003-08-14 Thread Steve Bannerman
Perrin,

Thanks for your response...my replies below:
--
   Steve Bannerman
   [EMAIL PROTECTED]
   44.(0)1865.273866


 -Original Message-
 From: Perrin Harkins [mailto:[EMAIL PROTECTED]
 Sent: 06 August 2003 20:40
 To: [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: RE: HTTP POST: parameters empty when using
 ModPerl::Registry(okay when using ModPerl:PerlRun)...

 ...snip...

 I believe I see the source of your troubles in the code that you
 posted.  You are creating a closure by using a lexical variable and then
 accessing it from within a sub.  This is a no-no with any long-running
 system like mod_perl.  You can get away with it in a standard CGI
 environment (or PerlRun) because it just exits after each request
 instead of running the same code again.

 Here is the offending section:

 my $cgi = new CGI;
 saveFile();

 sub saveFile {
   my $inputfile = $cgi-param('file');
 ... etc ...
 }

 Change it to this:

 my $cgi = new CGI;
 saveFile($cgi);

 sub saveFile {
   my $cgi = shift;
   my $inputfile = $cgi-param('file');
 ... etc ...
 }

 I think that will do it.

You're correct...that made it work.

So with respect to your explanation about the long running perl system, am
I to understand that the old version of the saveFile() subroutine uses a
reference to a different $cgi instance that the $cgi instance in the main
body of the script?

As I said, I'm new to perl but that seems to be an awfully strange behavior
of the language...if true, shouldn't the compilation (of the subroutine)
fail because it references an undeclared variable ($cgi)?

Cheers


 - Perrin



RE: HTTP POST: parameters empty when usingModPerl::Registry(okay when using ModPerl:PerlRun)...

2003-08-14 Thread Steve Bannerman
Perrin,

Thanks...your explanation makes sense.

I was thinking of the subroutine as a method on a class and that the objects
in the class had a cgi instance associated with them.  I was thinking in the
object paradigm rather than in the procedural paradigm.

Cheers
--
   Steve Bannerman
   [EMAIL PROTECTED]
   44.(0)1865.273866


 -Original Message-
 From: Perrin Harkins [mailto:[EMAIL PROTECTED]
 Sent: 07 August 2003 19:10
 To: [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: RE: HTTP POST: parameters empty when
 usingModPerl::Registry(okay when using ModPerl:PerlRun)...


 On Thu, 2003-08-07 at 03:36, Steve Bannerman wrote:
  So with respect to your explanation about the long running
 perl system, am
  I to understand that the old version of the saveFile() subroutine uses a
  reference to a different $cgi instance that the $cgi instance
 in the main
  body of the script?

 It uses a reference to the $cgi variable that was in scope when
 saveFile() was compiled.

  As I said, I'm new to perl but that seems to be an awfully
 strange behavior
  of the language...if true, shouldn't the compilation (of the subroutine)
  fail because it references an undeclared variable ($cgi)?

 But it doesn't reference an undeclared variable; it references the
 original $cgi that was available when the sub was compiled.

 Closures are a feature of Perl.  You can read about them in general in
 perlfaq7 and the perlref man page:
 http://www.perldoc.com/perl5.8.0/pod/perlfaq7.html#What's-a-closure-
 http://www.perldoc.com/perl5.8.0/pod/perlref.html

 Note that those both talk a lot about anonymous subs, but any sub can be
 a closure if it refers to a lexical variable defined in an enclosing
 scope.

 There is some mod_perl specific stuff on this here:
 http://perl.apache.org/docs/general/perl_reference/perl_reference.
html#my___Scoped_Variable_in_Nested_Subroutines

If you had warnings on, you would have received a message about $cgi not
staying shared.

In brief terms, what happens is that your program creates a lexical
called $cgi, then saveFile() refers to it, locking in that variable as
the $cgi that will always be referenced by saveFile().  At the end of
the script $cgi goes out of scope and disappears, but saveFile() keeps
referencing it.

In a CGI program this is not a problem, because Perl exits and the
process quits.  In mod_perl, the code gets run again and saveFile()
still refers to the original $cgi.

There are a number of ways to solve this problem, but I prefer the one I
showed you.  Explicitly passing all arguments to subs is well
established as a best practice in programming.  What you were doing with
$cgi before is basically treating it as a global.  So, I'd suggest you
turn on warnings, turn on strict, and embrace the good practice of
passing variables to your subs.

- Perrin



RE: HTTP POST: parameters empty when using ModPerl::Registry (okay when using ModPerl:PerlRun)...

2003-08-14 Thread Steve Bannerman
Stas,

Replies below:
--
   Steve Bannerman
   [EMAIL PROTECTED]
   44.(0)1865.273866


 -Original Message-
 From: Stas Bekman [mailto:[EMAIL PROTECTED]
 Sent: 05 August 2003 18:07
 To: [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: Re: HTTP POST: parameters empty when using ModPerl::Registry
 (okay when using ModPerl:PerlRun)...


  ...snip...
 

 The docs need work, this is just a copy of mp1 registry docs, which need
 adjustments. However most things work the same way. The
 differences between
 Registry and PerlRun are easily summarizes with this diff:

 ModPerl-Registry diff -u lib/ModPerl/Registry.pm lib/ModPerl/PerlRun.pm
 --- lib/ModPerl/Registry.pm 2003-03-22 20:52:24.0 -0800
 +++ lib/ModPerl/PerlRun.pm  2003-03-22 20:52:24.0 -0800
 @@ -1,4 +1,4 @@
 -package ModPerl::Registry;
 +package ModPerl::PerlRun;

   use strict;
   use warnings FATAL = 'all';
 @@ -30,11 +30,11 @@
   make_namespace  = 'make_namespace',
   namespace_root  = 'namespace_root',
   namespace_from  = 'namespace_from_filename',
 -is_cached   = 'is_cached',
 -should_compile  = 'should_compile_if_modified',
 -flush_namespace = 'NOP',
 +is_cached   = 'FALSE',
 +should_compile  = 'TRUE',
 +flush_namespace = 'flush_namespace_normal',
   cache_table = 'cache_table_common',
 -cache_it= 'cache_it',
 +cache_it= 'NOP',
   read_script = 'read_script',
   rewrite_shebang = 'rewrite_shebang',
   set_script_name = 'set_script_name',
 @@ -53,17 +53,10 @@

 PerlRun doesn't cache the script on each request and it flushes
 the script's
 namespace on each request. You can see the actual functions in
 lib/ModPerl/RegistryCooker.pm.

Thanks, that's helpful...it shows me why PerlRun works.

However, it doesn't really explain why the root problem exists.  The way I
think about it, the creation of a new CGI object should create a new set
of slots for instance data.  Then, each request's parameters would be
stored in a slot of the new CGI instance rather than in the global set of
slots for the class of CGI instances.

Maybe I don't understand the object paradigm in perl correctly; however, I
do understand it very well in general.  Thus, it seems like a defect in
either perl (the language) or CGI.pm.  I'm guessing there's some
justification for it in performance...however, it just doesn't seem right.

Thoughts?

 If you can try to take it from
 here and see
 what the problem is (your code/registry?), that would be cool. Thanks.


Unfortunately, I don't really know how to take it from here.  I'm pretty
new to perl and very new to mod_perl.  Thus I'm reaching out to you guys
to find out if anybody has solved this problem...unfortunately,
Christopher's suggestion didn't work (unless I implemented it incorrectly).

 Also make sure you are using the latest CGI.pm (2.93 or higher is good).

I'm using CGI.pm-2.98.

Cheers


 __
 Stas BekmanJAm_pH -- Just Another mod_perl Hacker
 http://stason.org/ mod_perl Guide --- http://perl.apache.org
 mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
 http://modperlbook.org http://apache.org   http://ticketmaster.com




HTTP POST: parameters empty when using ModPerl::Registry (okay when using ModPerl:PerlRun)...

2003-08-14 Thread Steve Bannerman
All,

I apologize if this has already been covered...I looked at the archives
since May but couldn't see anything covering this (there were related items
but their solutions didn't solve this problem).

Here an explanation of the problem:

We want to post experiment results to an upload server which is running
Apache HTTP Server (2.0.46) and mod_perl (1.99_09).  When we post a sequence
of files to the server, some of them are written to the local disk and some
are not.  That is, the test fails when using ModPerl::Registry but it
succeeds when using ModPerl::PerlRun.

In analyzing which ones work and which ones do not, I wrote a quick test to
see why the transfer is not working.  From the looks of the results, it
appears that the first request handled by a particular Apache process/thread
works and that any subsequent requests handled by that thread fail.
Works means that the file in the test gets saved to disk and fail means that
a file of size 0 gets written to disk.

Below are the httpd.conf segments (working and failing), the test client
(test_client.pl) and the test server (test_server.pl which is accessible
from the /cpdn/cgi-bin location).

Any suggestions?  Thanks in advance...

Also, does it matter if I use ModPerl::PerlRun instead of ModPerl::Registry
(I have read some about this at
http://apache.perl.org/docs/2.0/api/ModPerl/Registry.html but the
documentation there is a little light).

--
Working httpd.conf
--
Location /cpdn/cgi-bin
  AllowOverride None
  SetHandler perl-script
  PerlResponseHandler ModPerl::PerlRun
  PerlOptions +ParseHeaders
  Options +ExecCGI
  Allow from All
/Location

--
Failing httpd.conf
--
Location /cpdn/cgi-bin
  AllowOverride None
  SetHandler perl-script
  PerlResponseHandler ModPerl::Registry
  PerlOptions +ParseHeaders
  Options +ExecCGI
  Allow from All
/Location

--
test_client.pl
--
#!/usr/bin/perl
use strict;

use LWP::UserAgent;
use HTTP::Request::Common;

my $postUrl = $ARGV[0];
my $file = $ARGV[1];

my $postType = 'form-data';

my $ua = new LWP::UserAgent;
my $req = POST($postUrl,
   Content_Type = $postType,
   Content = [ file = [$file] ]);

my $res = $ua-request($req);
if ($res-is_success()) {
  print POST test successful\n;
  print $res-content();
} else {
  print STDERR POST test failed;
  print STDERR code:  . $res-code() . \n;
  print STDERR message:  . $res-message() . \n;
}

--
test_server.pl
--
use strict;
use CGI qw(:standard);

my $cgi = new CGI;
saveFile();

sub saveFile {
  my $inputfile = $cgi-param('file');
  my $outputfile = /tmp/file- . $$ . - . time();
  open(FILE,$outputfile) || printError();
  my $buffer;
  while (read($inputfile,$buffer,2096)) {
print FILE $buffer;
  }
  close(FILE);
  undef $buffer;
}

sub printError {
  print header();
  print Content-type: text/plain\n;
  print Status: 500$\n;
  print Message: Internal Error\n;
  exit;
}

Cheers
--
   Steve Bannerman
   [EMAIL PROTECTED]
   44.(0)1865.273866



RE: HTTP POST: parameters empty when using ModPerl::Registry (okay when using ModPerl:PerlRun)...

2003-08-06 Thread Steve Bannerman
Christopher,

Thanks for the suggestion; unfortunately, it doesn't work.  I made the
change you suggested (inserting CGI-initialize_globals(); just before
creating an instance of CGI) and restarted apache/httpd.  The same
result...the first time the script executes it saves the file
properly...after that, a file is created with 0 size.

Besides, as you (and others prescribing the use of initialize_globals())
described it, shouldn't subsequent executions of the script write the same
file as the first execution.  That is, if the parameters of the CGI
instances are actually global, wouldn't the same array of bytes still be in
the global 'file' parameter?

Cheers
--
   Steve Bannerman
   [EMAIL PROTECTED]
   44.(0)1865.273866


 -Original Message-
 From: Christopher Knight [mailto:[EMAIL PROTECTED]
 Sent: 05 August 2003 18:20
 To: Stas Bekman; [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: RE: HTTP POST: parameters empty when using ModPerl::Registry
 (okay when using ModPerl:PerlRun)...


 try
 CGI-initialize_globals();
 at the begining of the script but before you use params

 if you are depending on the 'use CGI' statement to initialize
 your params (like a command line script), it will cause
 problems in Registry.  Thats becuase it is initialized once on
 the initial 'use CGI' and it stays in memory for the life
 of the webserver.  So each time you use a script, you have to
 initialize the CGI params to your current request.

 -Original Message-
 From: Stas Bekman [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, August 05, 2003 12:07 PM
 To: [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: Re: HTTP POST: parameters empty when using ModPerl::Registry
 (okay when using ModPerl:PerlRun)...


 Steve Bannerman wrote:
  All,
 
  I apologize if this has already been covered...I looked at the archives
  since May but couldn't see anything covering this (there were
 related items
  but their solutions didn't solve this problem).
 
  Here an explanation of the problem:
 
  We want to post experiment results to an upload server which
 is running
  Apache HTTP Server (2.0.46) and mod_perl (1.99_09).  When we
 post a sequence
  of files to the server, some of them are written to the local
 disk and some
  are not.  That is, the test fails when using ModPerl::Registry but it
  succeeds when using ModPerl::PerlRun.
 
  In analyzing which ones work and which ones do not, I wrote a
 quick test to
  see why the transfer is not working.  From the looks of the results, it
  appears that the first request handled by a particular Apache
 process/thread
  works and that any subsequent requests handled by that thread fail.
  Works means that the file in the test gets saved to disk and
 fail means that
  a file of size 0 gets written to disk.
 
  Below are the httpd.conf segments (working and failing), the test client
  (test_client.pl) and the test server (test_server.pl which is accessible
  from the /cpdn/cgi-bin location).
 
  Any suggestions?  Thanks in advance...
 
  Also, does it matter if I use ModPerl::PerlRun instead of
 ModPerl::Registry
  (I have read some about this at
  http://apache.perl.org/docs/2.0/api/ModPerl/Registry.html but the
  documentation there is a little light).

 The docs need work, this is just a copy of mp1 registry docs, which need
 adjustments. However most things work the same way. The
 differences between
 Registry and PerlRun are easily summarizes with this diff:

 ModPerl-Registry diff -u lib/ModPerl/Registry.pm lib/ModPerl/PerlRun.pm
 --- lib/ModPerl/Registry.pm 2003-03-22 20:52:24.0 -0800
 +++ lib/ModPerl/PerlRun.pm  2003-03-22 20:52:24.0 -0800
 @@ -1,4 +1,4 @@
 -package ModPerl::Registry;
 +package ModPerl::PerlRun;

   use strict;
   use warnings FATAL = 'all';
 @@ -30,11 +30,11 @@
   make_namespace  = 'make_namespace',
   namespace_root  = 'namespace_root',
   namespace_from  = 'namespace_from_filename',
 -is_cached   = 'is_cached',
 -should_compile  = 'should_compile_if_modified',
 -flush_namespace = 'NOP',
 +is_cached   = 'FALSE',
 +should_compile  = 'TRUE',
 +flush_namespace = 'flush_namespace_normal',
   cache_table = 'cache_table_common',
 -cache_it= 'cache_it',
 +cache_it= 'NOP',
   read_script = 'read_script',
   rewrite_shebang = 'rewrite_shebang',
   set_script_name = 'set_script_name',
 @@ -53,17 +53,10 @@

 PerlRun doesn't cache the script on each request and it flushes
 the script's
 namespace on each request. You can see the actual functions in
 lib/ModPerl/RegistryCooker.pm. If you can try to take it from here and see
 what the problem is (your code/registry?), that would be cool. Thanks.

 Also make sure you are using the latest CGI.pm (2.93 or higher is good).

 __
 Stas BekmanJAm_pH -- Just Another mod_perl Hacker
 http://stason.org/ mod_perl Guide --- http