the CGI::Application documentation said, feature requests should be
sent to this mailing list, so that's why I'm posting here. Sorry if I
dont't know about recent topics here (is there a web archive
available anywhere?)

We stepped into memory problems using CGI::Application, as our
application had to deliver a file to the customer after doing some
calculations and access control. It worked well until our customer
started to serve *really* huge files, wich were slurped into memory
by the run mode handler and then returned for delivery.

As far as I could see there is no way supplied in CGI::Application to
stream a file directly from the file system to the user agent.

I fixed the problem with the attached patch, that allows to return not
only a string or string reference, but also a file handle. It should
be compatible with the previous version as far as I can see.

Using this patch I can now use the following code to deliver the
file, without having it in memory:

sub myhandler {
  my $self = shift;


    -type                  => 'application/octet-stream',
    '-Content-length'      => -s $filename,
    -Application           => $filename,
    '-Content-Disposition' => "attachment; filename=\"$filename\"",

  open my $fh, "</home/hoeni/Muenchen_10685x9166.jpg"
    || die;
  return $fh

The file is not given to cgiapp_postrun() with would make no sense

It would be great to get the patch implemenented in the module or be
shown another way to work around this.

Thank you,

Tobias "hoeni" Henoeckl

If you implement this, it would be handy for the user of the lib to
have kind of $self->stream_file($filename, $optionalmimetype) method
which would do the above things and open and return a filehandle so
the above code could be reduced to:

return($self->stream_file($filename, 'application/octet-stream'));

Tobias Henoeckl                                            SpaceNet AG
[EMAIL PROTECTED]      http://www.space.net/   Joseph-Dollinger-Bogen 14
+49 89 32356-215     Fax: +49 89 32356-297       80807 Munich, Germany
--- CGI-Application-3.2/lib/CGI/Application.pm  Wed Feb  4 05:02:47 2004
+++ CGI-Application-3.2-patched/lib/CGI/Application.pm  Thu Dec  2 14:50:45 2004
@@ -161,29 +161,38 @@
     # Make sure that $body is not undefined (supress 'uninitialized value' 
     $body = "" unless defined $body;
+    my $bodyref;
-    # Support scalar-ref for body return
-    my $bodyref = (ref($body) eq 'SCALAR') ? $body : \$body;
-    # Call cgiapp_postrun() hook
-    $self->cgiapp_postrun($bodyref);
+        if (ref($body) ne 'GLOB') {
+            # Support scalar-ref for body return
+            $bodyref = (ref($body) eq 'SCALAR') ? $body : \$body;
+            # Call cgiapp_postrun() hook
+            $self->cgiapp_postrun($bodyref);
+        }
        # Set up HTTP headers
        my $headers = $self->_send_headers();
-       # Build up total output
-       my $output  = $headers.$$bodyref;
        # Send output to browser (unless we're in serious debug mode!)
        unless ($ENV{CGI_APP_RETURN_ONLY}) {
-               print $output;
-       }
+            print $headers;
+            if (ref($body) eq 'GLOB') {
+                while(my $block=read($body, my $buf, 32768)) {
+                    print $buf;
+                }
+            } else {
+                print $$bodyref;
+           }
+        }
        # clean up operations
-       return $output;
+        if (ref($body) ne 'GLOB') {
+           return $headers . $$bodyref;
+        }

Web Archive:  http://www.mail-archive.com/cgiapp@lists.erlbaum.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to