You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Gunnar Wolf <gw...@gwolf.org> on 2006/08/02 19:50:55 UTC

Delivering binary content from a Embperl application

Hi,

I have seen this question asked a couple of times in this list over
five years ago... And the answer was 'wait for 2.0, as 1.x is not able
of sending data containing NUL characters'. I have seen no newer
questions in this regard - But I could not either find this
documented.

Ok, here goes the question: My applications have to deliver binary
content stored in a database or generated on the fly. How can I do
this from within my Embperl structure (I'm using the Embperl object
application), without having to delegate this to a traditional CGI or
something like that?

Thank you very much,

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

Re: Delivering binary content from a Embperl application

Posted by Frank Wesemann <f....@fotofinder.net>.
This is what finally worked for me:
     binmode ( STDOUT );
     my @blob = $image->ImageToBlob(); ( This means my $var = 
$file->getData() in your terms )
     print $blob[0];
     exit;

I use it to manipulate Imagedata ( jpegs )

-- 
mit freundlichem Gruß

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

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


RE: Delivering binary content from a Embperl application

Posted by Gerald Richter <ri...@ecos.de>.
Hi,

>From an Embperl perspective this doesn't makes a difference. So use what
better works for you. I agree that the first one is easier to read and has
less problems with withspaces.

Gerald
  

> -----Original Message-----
> From: Gunnar Wolf [mailto:gwolf@gwolf.org] 
> Sent: Wednesday, August 09, 2006 7:03 PM
> To: Gerald Richter
> Cc: 'Frank Wesemann'; embperl@perl.apache.org
> Subject: Re: Delivering binary content from a Embperl application
> 
> Gerald Richter dijo [Mon, Aug 07, 2006 at 03:59:30PM +0200]:
> > This might work with Apache 1.3 by chance, but it will not 
> with Apache 
> > 2.0, because $image->write('-') writes to STDOUT which is 
> never send 
> > to the client, at least not by default.
> > 
> > The best would be to do something like
> > 
> > $imgdata = $image -> <get_image> () ; # I don't know the correct 
> > ImageMagik function to use, # but it should return the 
> image data # -> 
> > no exit here -][+ $imgdata +]
> > 
> > # Make sure you have no extra spaces here or newlines in 
> your source.
> 
> Hmh... I followed Torsten's advice, and did this:
> 
> ----------------------------------------------------------------------
> [-
>  # Omitting some irrelevant bits regarding my app's logic  
> $file = Comas::File->new(-db => $epreq->{db}, -id => $fdat{file_id});
> 
>  if (defined($file) and $file->get_proposal_id == 
> $fdat{proposal_id}) {
>      my $filename = $file->get_filename;
> 
>      my $mime = MIME::Types->new or die "Could not query MIME type";
>      $http_headers_out{'Content-Type'} = $mime->mimeTypeOf($filename)
>       or 'application/octet-stream';
> 
>      $http_headers_out{'Content-Disposition'} = "attachment; 
> filename=".
>          $filename;
>      print OUT $file->get_data;
> 
>      # Exit in order not to break binary content (don't 
> worry, this is 
>      # Embperl-safe)
>      exit 0;
>  }
> -]
> Could not open requested file
> ----------------------------------------------------------------------
> 
> Note that I'm using Apache 2.0, and it works correctly. I 
> like keeping this all inside a single block, but... Would you 
> recommend something like this instead?
> 
> ----------------------------------------------------------------------
> [- $file = Comas::File->new(-db => $epreq->{db}, -id => 
> $fdat{file_id}); -] [$ if (defined($file) and 
> $file->get_proposal_id == $fdat{proposal_id}) $]
> [- (... Rest of logic ...)
>   $file_data = $file->get_data -]
> [+ $file_data +]
> [$ else $]
> Could not open requested file
> [$ endif $]
> ----------------------------------------------------------------------
> 
> IMHO, the first way is much clearer, and less prone for some 
> whitespace or wrongly appended thing end up modifying the sent binary.
> 
> 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
> 
>  
> ** Virus checked by BB-5000 Mailfilter ** 
> !DSPAM:44da2bc0115051971813638!
> 



---------------------------------------------------------------------------
Gerald Richter            ecos electronic communication services gmbh
IT-Securitylösungen * Webapplikationen mit Apache/Perl/mod_perl/Embperl

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


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


Re: Delivering binary content from a Embperl application

Posted by Gunnar Wolf <gw...@gwolf.org>.
Gerald Richter dijo [Mon, Aug 07, 2006 at 03:59:30PM +0200]:
> This might work with Apache 1.3 by chance, but it will not with Apache 2.0,
> because $image->write('-') writes to STDOUT which is never send to the
> client, at least not by default.
> 
> The best would be to do something like
> 
> $imgdata = $image -> <get_image> () ; 
> # I don't know the correct ImageMagik function to use, 
> # but it should return the image data
> # -> no exit here
> -][+ $imgdata +]
> 
> # Make sure you have no extra spaces here or newlines in your source.

Hmh... I followed Torsten's advice, and did this:

----------------------------------------------------------------------
[-
 # Omitting some irrelevant bits regarding my app's logic
 $file = Comas::File->new(-db => $epreq->{db}, -id => $fdat{file_id});

 if (defined($file) and $file->get_proposal_id == $fdat{proposal_id}) {
     my $filename = $file->get_filename;

     my $mime = MIME::Types->new or die "Could not query MIME type";
     $http_headers_out{'Content-Type'} = $mime->mimeTypeOf($filename)
      or 'application/octet-stream';

     $http_headers_out{'Content-Disposition'} = "attachment; filename=".
         $filename;
     print OUT $file->get_data;

     # Exit in order not to break binary content (don't worry, this is 
     # Embperl-safe)
     exit 0;
 }
-]
Could not open requested file
----------------------------------------------------------------------

Note that I'm using Apache 2.0, and it works correctly. I like keeping
this all inside a single block, but... Would you recommend something
like this instead?

----------------------------------------------------------------------
[- $file = Comas::File->new(-db => $epreq->{db}, -id => $fdat{file_id}); -]
[$ if (defined($file) and $file->get_proposal_id == $fdat{proposal_id}) $]
[- (... Rest of logic ...)
  $file_data = $file->get_data -]
[+ $file_data +]
[$ else $]
Could not open requested file
[$ endif $]
----------------------------------------------------------------------

IMHO, the first way is much clearer, and less prone for some
whitespace or wrongly appended thing end up modifying the sent binary.

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: Delivering binary content from a Embperl application

Posted by Gerald Richter <ri...@ecos.de>.
> 
> Try putting the following before the write:
> 
>    tie *OUT, 'Embperl::Out';
>    select(OUT);
> 

This will redirect the Perl STDOUT to Embperl's output, but not the C one
and I guess the output from ImageMagik is done by it's C code

Gerald



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


Re: Delivering binary content from a Embperl application

Posted by Torsten Luettgert <t....@cbxnet.de>.
On Mon, 2006-08-07 at 11:20 +0200, Frank Wesemann wrote:
> Torsten Luettgert schrieb:
> > Don't know about Embperl object, but for me, it "just works".
> > Here is some (reduced) code I use for delivering jpeg images:
> > 
> Interesting enough this doesn't work ( it outputs nothing! ):
> 
> > [-
> >    # some permission checking and obtaining $imgpath omitted
> >    if( -f $imgpath && $permission ) {
> >       $http_headers_out{'Content-Length'} = -s $imgpath;
> >       $http_headers_out{'Content-Type'} = 'image/jpeg';
> >       $http_headers_out{'Cache-Control'} = 'no-cache';
> >       $http_headers_out{'Pragma'} = 'no-cache';
> >       $http_headers_out{'Expires'} = '0';
> 	my $image = Image::Magick->new();
> 	$image->read($imgpath);
> 	$image->do_something_or_not();
> 	$image->write('-')
> >       exit 0;
> >    }
> > -]
> 
> and this is, err, strange behavior, I think.

Your $imgpath does contain a valid image?

If so, probably it doesn't work with printing to STDOUT,
like you did with $image->write('-');

Try putting the following before the write:

   tie *OUT, 'Embperl::Out';
   select(OUT);

Perhaps this helps.

Regards,
Torsten



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


RE: Delivering binary content from a Embperl application

Posted by Gerald Richter <ri...@ecos.de>.
> 
> Torsten Luettgert schrieb:
> > Don't know about Embperl object, but for me, it "just works".
> > Here is some (reduced) code I use for delivering jpeg images:
> > 
> Interesting enough this doesn't work ( it outputs nothing! ):
> 
> > [-
> >    # some permission checking and obtaining $imgpath omitted
> >    if( -f $imgpath && $permission ) {
> >       $http_headers_out{'Content-Length'} = -s $imgpath;
> >       $http_headers_out{'Content-Type'} = 'image/jpeg';
> >       $http_headers_out{'Cache-Control'} = 'no-cache';
> >       $http_headers_out{'Pragma'} = 'no-cache';
> >       $http_headers_out{'Expires'} = '0';
> 	my $image = Image::Magick->new();
> 	$image->read($imgpath);
> 	$image->do_something_or_not();
> 	$image->write('-')
> >       exit 0;
> >    }
> > -]
> 
> and this is, err, strange behavior, I think.
> regards
> 

This might work with Apache 1.3 by chance, but it will not with Apache 2.0,
because $image->write('-') writes to STDOUT which is never send to the
client, at least not by default.

The best would be to do something like

$imgdata = $image -> <get_image> () ; 
# I don't know the correct ImageMagik function to use, 
# but it should return the image data
# -> no exit here
-][+ $imgdata +]

# Make sure you have no extra spaces here or newlines in your source.

Gerald



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


Re: Delivering binary content from a Embperl application

Posted by Frank Wesemann <f....@fotofinder.net>.
Torsten Luettgert schrieb:
> Don't know about Embperl object, but for me, it "just works".
> Here is some (reduced) code I use for delivering jpeg images:
> 
Interesting enough this doesn't work ( it outputs nothing! ):

> [-
>    # some permission checking and obtaining $imgpath omitted
>    if( -f $imgpath && $permission ) {
>       $http_headers_out{'Content-Length'} = -s $imgpath;
>       $http_headers_out{'Content-Type'} = 'image/jpeg';
>       $http_headers_out{'Cache-Control'} = 'no-cache';
>       $http_headers_out{'Pragma'} = 'no-cache';
>       $http_headers_out{'Expires'} = '0';
	my $image = Image::Magick->new();
	$image->read($imgpath);
	$image->do_something_or_not();
	$image->write('-')
>       exit 0;
>    }
> -]

and this is, err, strange behavior, I think.
regards

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


-- 
mit freundlichem Gruß

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

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


Re: Delivering binary content from a Embperl application

Posted by Torsten Luettgert <t....@pressestimmen.de>.
On Wed, 2006-08-02 at 12:50 -0500, Gunnar Wolf wrote:
> Ok, here goes the question: My applications have to deliver binary
> content stored in a database or generated on the fly. How can I do
> this from within my Embperl structure (I'm using the Embperl object
> application), without having to delegate this to a traditional CGI or
> something like that?

Don't know about Embperl object, but for me, it "just works".
Here is some (reduced) code I use for delivering jpeg images:

[-
   # some permission checking and obtaining $imgpath omitted
   if( -f $imgpath && $permission ) {
      $http_headers_out{'Content-Length'} = -s $imgpath;
      $http_headers_out{'Content-Type'} = 'image/jpeg';
      $http_headers_out{'Cache-Control'} = 'no-cache';
      $http_headers_out{'Pragma'} = 'no-cache';
      $http_headers_out{'Expires'} = '0';
      open(I, '<:raw', $imgpath);
      undef $/;
      print OUT <I>;
      $/ = "\n";
      close(I);
      exit 0;
   }
-]
(display error message)

What's important is that there's no white space or anything else before
the first [- or you'll have that at the top of the output.

Regards,
Torsten


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