Mailing List Archive

Dynamically generating file for download
Hi,

I have an Embperl 2.0 based website with lots of epl files in the root
directory to build the page templates, etc.

I have a need to dynamically generate a data file for users to download
based on form input.

I am not sure how I can generate a response to form submission that does not
include the page template html automatically added by the epl files in the
root directory. I also need to return the HTTP response with an appropriate
MIME content type, etc.

Now, I could generate a file somewhere on disk and then send a 302 redirect
to it, or ust use a CGI to generate the file based on an embperl form but
I'd rather do it all dynamically with one page in Embperl.

Does anyone have any suggestions how I can approach this?

Thanks.
Re: Dynamically generating file for download [ In reply to ]
Lelton schrieb:
> Does anyone have any suggestions how I can approach this?
>
> Thanks.
>
%http_headers_out is what you are looking for:

$http_headers_out{'Content-Type'} = '' the type of your data";
my $binary_data = collect_the_data(%params);
print OUT $binary_data;
exit;

--
mit freundlichem Gruß,

Frank Wesemann
Fotofinder GmbH USt-IdNr. DE812854514
Software Entwicklung Web: http://www.fotofinder.net/
Potsdamer Str. 96 Tel: +49 30 25 79 28 90
10785 Berlin Fax: +49 30 25 79 28 999

Sitz: Berlin
Amtsgericht Berlin Charlottenburg (HRB 73099)
Geschäftsführer: Ali Paczensky




---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
I think he's also wanting to either use EmbPerl instead of EmbPerlObject
for that page, or put it in a directory with a null base.epo (or
equivalent).

I haven't attempted to bypass that in EmbPerlObject yet, so what Frank
said may actually be sufficient. However, it sounds a lot like what
I've used to bypass EmbPerl additions, and I think you'd need to also
bypass the EmbPerlObject stuff - unless Gerald has been working his
magic again.

Ed

On Thu, 26 Jul 2007, Frank Wesemann wrote:

> Lelton schrieb:
> > Does anyone have any suggestions how I can approach this?
> >
> > Thanks.
> >
> %http_headers_out is what you are looking for:
>
> $http_headers_out{'Content-Type'} = '' the type of your data";
> my $binary_data = collect_the_data(%params);
> print OUT $binary_data;
> exit;
>
> --
> mit freundlichem Gruß,
>
> Frank Wesemann
> Fotofinder GmbH USt-IdNr. DE812854514
> Software Entwicklung Web: http://www.fotofinder.net/
> Potsdamer Str. 96 Tel: +49 30 25 79 28 90
> 10785 Berlin Fax: +49 30 25 79 28 999
>
> Sitz: Berlin
> Amtsgericht Berlin Charlottenburg (HRB 73099)
> Geschäftsführer: Ali Paczensky
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail: embperl-help@perl.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
On 7/26/07, Frank Wesemann <f.wesemann@fotofinder.net> wrote:
>
> %http_headers_out is what you are looking for:
>
> $http_headers_out{'Content-Type'} = '' the type of your data";
> my $binary_data = collect_the_data(%params);
> print OUT $binary_data;
> exit;

Thanks for the reply but that does not solve the problem. The
content-type is the easy part! With that code in a [- -] I still get
the inherited HTML templates which gets stuck in with the contents of
$binary_data in the rendered output.

As Ed mentioned, I need some way of preventing Embperl from processing
any of the inherited templates in base.epl within a particular branch
within the program logic.

Thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
On Fri, 27 Jul 2007, Lelton wrote:

> On 7/26/07, Frank Wesemann <f.wesemann@fotofinder.net> wrote:
>>
>> %http_headers_out is what you are looking for:
>>
>> $http_headers_out{'Content-Type'} = '' the type of your data";
>> my $binary_data = collect_the_data(%params);
>> print OUT $binary_data;
>> exit;
>
> Thanks for the reply but that does not solve the problem. The
> content-type is the easy part! With that code in a [- -] I still get
> the inherited HTML templates which gets stuck in with the contents of
> $binary_data in the rendered output.
>
> As Ed mentioned, I need some way of preventing Embperl from processing
> any of the inherited templates in base.epl within a particular branch
> within the program logic.

The way you word this seems to me ambiguous. Are you needing assistance
in how to do the two options I mentioned?

To have EmbPerl process it instead of EmbPerlObject, you'd basically
define a different file extension in apache, and configure it to use
EmbPerl for that extension.

To have an empty base.epo, you'd simply copy either /dev/null or nul
(depending on your OS) to base.epo into a subdirectory specifically for
pages without templates.

Ed

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
On 7/28/07, Ed Grimm <edgrimm@dsblade00.wat.us.ray.com> wrote:

> The way you word this seems to me ambiguous. Are you needing assistance
> in how to do the two options I mentioned?

I'm not sure how I can articulate it clearer. I have a working embperl
based website with lots of included epl files that build page
templates, etc. Every subsequent html file contains text/code which
sits inside each page template which is automatically generated and
then served.

What I want to do for ONE particular page is have it generate a file
that can be downloaded. Now, the user visits the particular html page
(which is rendered with all the templates), fills in some details,
clicks submit (which is a FORM action back to the same page) and then
the HTTP response is the file to download.

Now, this can be easily achieved by some [$if defined..] statements to
check if the form variables have been filled in and then response back
the the file to download.

The problem is that the file download would include the HTML templates
that are provided as part of base.epl, etc thus making the file
download littered with HTML garbage. Now, I could circumvent that by
placing it all in a new directory and have a null base.epl however
that does not solve my problem because I want the templates rendered
in the first place when the form variables need to be specified!

So if that's not clear enough, here's some pseudocode:

$ cat generate.html

[$if !defined(form variables)$]
<!-- stuff in base.epl with Execute, etc will be automaticall included here -->
Text:
<FORM ACTION="generate.html">
....
</FORM>
Text
<!-- and here -->
[$else$]
call_some_function_that_prevents_any_templates_from_being_processed();
[-
# perl to generate the file.
$header{'content_type'} = 'text/text';
print OUT ...
-]
[$endif]

Is that at all possible with Embperl?

Thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
Hello Lelton,

I usually do it the same way as Ed suggested. Use a different file
extension for Embperl pages and pure perl pages. Here's an exerpt from
my apache.conf:


Options +ExecCGI
AddHandler cgi-script .pl
<FilesMatch ".*\.html$">
SetHandler perl-script
PerlHandler HTML::EmbperlObject
</FilesMatch>

So .html files are handles by Embperl and .pl files are just plainly
called. If performance would be important for this website I could have
used the mod_perl registry for the .pl files.

The form (in your Embperl code) then looks like:

<form action="download.pl">
<input name="whatever"/>
</form>

And my stub for download.pl:

#!/usr/bin/env perl
use CGI;
my $cgi=new CGI;
my $whatever=$cgi->param('whatever');
my $c=magic_function_to_generate_content_into_scalar($whatever);
print "Content-Type: image/png\n\n";
print $c;
exit 0;

--
---> Dirk Jagdmann ^ doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
Lelton dijo [Fri, Jul 27, 2007 at 09:28:03AM +1000]:
> Thanks for the reply but that does not solve the problem. The
> content-type is the easy part! With that code in a [- -] I still get
> the inherited HTML templates which gets stuck in with the contents of
> $binary_data in the rendered output.
>
> As Ed mentioned, I need some way of preventing Embperl from processing
> any of the inherited templates in base.epl within a particular branch
> within the program logic.

Hi,

You might want to take a look at a little system I wrote, implementing
a simple SMB-to-web read-only gateway using Embperl. It includes the
answer to this point you are raising, although I don't know it's done
in the cleanest way - but it works perfectly :)

http://www.gwolf.org/soft/smbgate/SmbGate-1.0.tar.gz

There are still some references to my workplace sprinkled through the
code - If you want to use it, look for any occurrences of the 'iiec'
string :)

Greetings,

--
Gunnar Wolf - gwolf@gwolf.org - (+52-55)5623-0154 / 1451-2244
PGP key 1024D/8BB527AF 2001-10-23
Fingerprint: 0C79 D2D1 2C4E 9CE4 5973 F800 D80E F35A 8BB5 27AF

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: Dynamically generating file for download [ In reply to ]
On Sat, 28 Jul 2007, Lelton wrote:

> On 7/28/07, Ed Grimm <edgrimm@dsblade00.wat.us.ray.com> wrote:
>
> > The way you word this seems to me ambiguous. Are you needing assistance
> > in how to do the two options I mentioned?
>
> I'm not sure how I can articulate it clearer. I have a working embperl
> based website with lots of included epl files that build page
> templates, etc. Every subsequent html file contains text/code which
> sits inside each page template which is automatically generated and
> then served.
>
> What I want to do for ONE particular page is have it generate a file
> that can be downloaded. Now, the user visits the particular html page
> (which is rendered with all the templates), fills in some details,
> clicks submit (which is a FORM action back to the same page) and then
> the HTTP response is the file to download.

You may have gathered this from the other people who have responded to
this, but there is no easy way to do this in EmbPerlObject. A single
file will always either load the template or not, depending on its
applicable <Location>, <Directory>, and <FilesMatch> settings, and the
template file associated with its directory location.

You *could* make the base.epo file be smart enough that it recognized
when a particular page is loaded *and* has form data, to not show any
template. I think that would be unwise, but it could be done.

What could be a bit simpler and more clever, while not straying too far
from your 'pure' state, would be to have a link to the file with the
same name except for the extension - something like

% ls index.*
-rw-r--r-- 1 eg other 5503 Jul 31 2007 index.html
lrw-r--r-- 1 eg other 9 Jul 31 2007 index.txt -> index.html
%

Now, have the action link to the other extension name.


That having been said - I used to do web pages that had the same script
generate the form and process the form. I had enough problems with
doing that in various template environments that I decided, for testing
purposes, I'd stop fighting it temporarily and have a different file to
process the action from the one to generate the form - and it was so
much easier to work with it that way that the temporary became
permanent; I would now resist any effort to make me go back to that.

It's a coding style that has been given in examples by many books and
HOWTO documents, but it does not seem to me to be actually good for
everyday use. Since I stopped using that design, I had the opportunity
to talk to one book author who had used it - who apologized for having
lead me astray; he admitted that it wasn't normally good coding design,
he had only used it in the book because it let him fit the text in page
boundaries better. I cannot say that the other authors were using it
for that reason, but the experience reminded me that I should always
think before adopting someone's practices, and try to determine if there
is any reason why their practice might not be appropriate for me.

Ed

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
RE: Re: Dynamically generating file for download [ In reply to ]
Hi,

There are already a lot of good replies.

What I actualy would do, would be to use an Embperl Application Object
(see
http://perl.apache.org/embperl/pod/intro/IntroEmbperl2.-page-3-.htm),
that let you separate logic and layout.

The init method is able to tweak serveral parameters of the request and
to inspect %fdat, before any redering is done. So you can simply do a
redirect to another page, before your base.epl come into play.

Gerald


------------------------------------------------------------------------
--
Gerald Richter ECOS electronic communication services GmbH
******************* SECURING YOUR NETWORK ********************

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 939-122
WWW: http://www.BB-5000.info Fax: +49 6133 939-333

Sitz der Gesellschaft: Dienheim; AG Mainz HRB 6889; GF: W.Heck,
G.Richter
------------------------------------------------------------------------
--



> -----Original Message-----
> From: Lelton [mailto:lelton@gmail.com]
> Sent: Saturday, July 28, 2007 4:52 AM
> To: embperl@perl.apache.org
> Subject: Re: Dynamically generating file for download
>
> On 7/28/07, Ed Grimm <edgrimm@dsblade00.wat.us.ray.com> wrote:
>
> > The way you word this seems to me ambiguous. Are you needing
> > assistance in how to do the two options I mentioned?
>
> I'm not sure how I can articulate it clearer. I have a
> working embperl based website with lots of included epl files
> that build page templates, etc. Every subsequent html file
> contains text/code which sits inside each page template which
> is automatically generated and then served.
>
> What I want to do for ONE particular page is have it generate
> a file that can be downloaded. Now, the user visits the
> particular html page (which is rendered with all the
> templates), fills in some details, clicks submit (which is a
> FORM action back to the same page) and then the HTTP response
> is the file to download.
>
> Now, this can be easily achieved by some [$if defined..]
> statements to check if the form variables have been filled in
> and then response back the the file to download.
>
> The problem is that the file download would include the HTML
> templates that are provided as part of base.epl, etc thus
> making the file download littered with HTML garbage. Now, I
> could circumvent that by placing it all in a new directory
> and have a null base.epl however that does not solve my
> problem because I want the templates rendered in the first
> place when the form variables need to be specified!
>
> So if that's not clear enough, here's some pseudocode:
>
> $ cat generate.html
>
> [$if !defined(form variables)$]
> <!-- stuff in base.epl with Execute, etc will be automaticall
> included here -->
> Text:
> <FORM ACTION="generate.html">
> ....
> </FORM>
> Text
> <!-- and here -->
> [$else$]
> call_some_function_that_prevents_any_templates_from_being_processed();
> [-
> # perl to generate the file.
> $header{'content_type'} = 'text/text';
> print OUT ...
> -]
> [$endif]
>
> Is that at all possible with Embperl?
>
> Thanks.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail: embperl-help@perl.apache.org
>
>
> ** Virus checked by BB-5000 Mailfilter **
> !DSPAM:416,46aab134266951798513471!
>
>

** Virus checked by BB-5000 Mailfilter **

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org