You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Tim Noll <tn...@grapevine.net> on 2002/02/05 13:21:14 UTC

image corruption

I'm attempting to use mod_perl and Template Toolkit to serve up
templates. However, I'm having a problem with the images in those
templates: They're passing through the content handler, and thus getting
corrupted.

My first thought was to return DECLINED from the content handler if the
request is not for text/html content; however, since I'm using a
Location directive, the content_type is always empty since there's no
direct mapping to an actual image file. I could use an Alias to map the
URI to the file, but then I wouldn't have the path_info that I'm using
to call the template.

Since my test code, using path_info, is based on an example from the
Template Toolkit docs, I feel like I'm probably overlooking something
basic. So, I'd appreciate it if someone could show me the error of my
ways. :-)

Here are the relevant chunks of config and code:

from httpd.conf
---------------
...
<Location /tt>
 SetHandler perl-script
 PerlHandler Apache::Test::Mod
 PerlSetVar WEBROOT /usr/local/apache/tt/html
</Location>
...

Apache::Test::Mod
-----------------
...
sub handler {
 my $r = shift;

 # this doesn't work
 #return DECLINED unless $r->content_type eq 'text/html';

 my $WEBROOT = $r->dir_config('WEBROOT')
  or return fail( $r, SERVER_ERROR, "'WEBROOT' not specified" );

 my $file = $r->path_info;

 my $vars = {
  content  => $r->content_type,
 };

 $r->content_type('text/html');
 $r->no_cache(1);

 my $template = Template->new( {
  INCLUDE_PATH => "$WEBROOT:$WEBROOT/include",
  OUTPUT   => $r,
 } );
 $template->process( $file, $vars, $r)
  or return fail( $r, SERVER_ERROR, $template->error );

 $r->send_http_header();
 $r->print( $output );

 return OK;
}
...

index.html (test template)
--------------------------
<html>
 <head>
  <title>test</title>
 </head>
 <body>
  <p>content_type: [% content %]</p>
  <p>image: <img src="images/hello.gif"></p>
 </body>
</html>


Thanks.

-Tim



Re: image corruption

Posted by Tim Noll <tn...@grapevine.net>.
First of all, let me apologize for the hacked-up code I sent with my
original post. I was actually trying to return my current code back to
something that looked more like the TT sample code, but I clearly didn't
do a very good job. (In my actual code, I've moved
$r->send_http_header() until after the $template->process() call so that
I can trap file not found errors.)

In any case, thanks to David Ranney. Adding this to my config file does
the trick:

Alias /tt/images /usr/local/apache/tt/html/images
<Location /tt/images>
    SetHandler default-handler
</Location>

Thanks.

-Tim


----- Original Message -----
From: "Lyle Brooks" <br...@deseret.com>
To: "Tim Noll" <tn...@grapevine.net>
Cc: "Lyle Brooks" <br...@deseret.com>; <mo...@apache.org>
Sent: Tuesday, February 05, 2002 5:54 PM
Subject: Re: image corruption


>
> Ok, a couple of things...
>
> 1) You want to move the $r->send_http_header; call up before calling
>    $template->process();
>
> 2) Modify $template->process( $file, $vars, $r) to
>           $template->process( $file, $vars) since you specify OUTPUT
=> $r
>    when you create the Template object (so it's re-dundant).
>
> 3) get rid of the $r->print( $output ) line as well,
$template->process()
> is going to send the output to Apache the way you have it setup.
>
>
> 4) As David Ranney pointed out in a previous post, you might want to
> put your images somewhere else, or adjust your URL
>
> You get a path_info part only for the virtual component of your URL
> (ie. there's no filesystem component beyond /tt ), but your URL for
> images/hello.gif is relative to /tt, which means your handler for
> <Location /tt> has got to fix things up.
>
> ...or..
>
> If you make the URL hello.gif to resolve to something outside /tt,
> then Apache should serve it up as a regular file, which I suspect is
> what you want.
>
> HTH
>
> Lyle
>
>
>
>
> Quoting Tim Noll (tnoll@grapevine.net):
> > Whoops, I hacked up my example a little to make it easier it to
read,
>
> > and I accidentally removed the line:
> > $file =~ s{^/}{};
> > But, it was in the original. Really. :-)
> >
> > -Tim
> >
> >
> > ----- Original Message -----
> > From: "Lyle Brooks" <br...@deseret.com>
> > To: "Tim Noll" <tn...@grapevine.net>
> > Cc: <mo...@apache.org>
> > Sent: Tuesday, February 05, 2002 4:42 PM
> > Subject: Re: image corruption
> >
> >
> > > When I try this example, I find that this line
> > >
> > > >  my $file = $r->path_info;
> > >
> > > will set $file to "/index.html" when I request the URL
/tt/index.html
> > >
> > > which leads to an error message that says,
> > >
> > > reason: file error - /index.html: absolute paths are not allowed
(set
> > ABSOLUTE option)
> > >
> > > You may want to clip off the leading slash or set the Template
Toolkit
> > > option ABSOLUTE, depending on which suits your needs.
> > >
> > > Quoting Tim Noll (tnoll@grapevine.net):
> > > > I'm attempting to use mod_perl and Template Toolkit to serve up
> > > > templates. However, I'm having a problem with the images in
those
> > > > templates: They're passing through the content handler, and thus
> > getting
> > > > corrupted.
> > > >
> > > > My first thought was to return DECLINED from the content handler
if
> > the
> > > > request is not for text/html content; however, since I'm using a
> > > > Location directive, the content_type is always empty since
there's
> > no
> > > > direct mapping to an actual image file. I could use an Alias to
map
> > the
> > > > URI to the file, but then I wouldn't have the path_info that I'm
> > using
> > > > to call the template.
> > > >
> > > > Since my test code, using path_info, is based on an example from
the
> > > > Template Toolkit docs, I feel like I'm probably overlooking
> > something
> > > > basic. So, I'd appreciate it if someone could show me the error
of
> > my
> > > > ways. :-)
> > > >
> > > > Here are the relevant chunks of config and code:
> > > >
> > > > from httpd.conf
> > > > ---------------
> > > > ...
> > > > <Location /tt>
> > > >  SetHandler perl-script
> > > >  PerlHandler Apache::Test::Mod
> > > >  PerlSetVar WEBROOT /usr/local/apache/tt/html
> > > > </Location>
> > > > ...
> > > >
> > > > Apache::Test::Mod
> > > > -----------------
> > > > ...
> > > > sub handler {
> > > >  my $r = shift;
> > > >
> > > >  # this doesn't work
> > > >  #return DECLINED unless $r->content_type eq 'text/html';
> > > >
> > > >  my $WEBROOT = $r->dir_config('WEBROOT')
> > > >   or return fail( $r, SERVER_ERROR, "'WEBROOT' not specified" );
> > > >
> > > >  my $file = $r->path_info;
> > > >
> > > >  my $vars = {
> > > >   content  => $r->content_type,
> > > >  };
> > > >
> > > >  $r->content_type('text/html');
> > > >  $r->no_cache(1);
> > > >
> > > >  my $template = Template->new( {
> > > >   INCLUDE_PATH => "$WEBROOT:$WEBROOT/include",
> > > >   OUTPUT   => $r,
> > > >  } );
> > > >  $template->process( $file, $vars, $r)
> > > >   or return fail( $r, SERVER_ERROR, $template->error );
> > > >
> > > >  $r->send_http_header();
> > > >  $r->print( $output );
> > > >
> > > >  return OK;
> > > > }
> > > > ...
> > > >
> > > > index.html (test template)
> > > > --------------------------
> > > > <html>
> > > >  <head>
> > > >   <title>test</title>
> > > >  </head>
> > > >  <body>
> > > >   <p>content_type: [% content %]</p>
> > > >   <p>image: <img src="images/hello.gif"></p>
> > > >  </body>
> > > > </html>
> > > >
> > > >
> > > > Thanks.
> > > >
> > > > -Tim
> > > >
> > > >
> > >
> >
>


Re: image corruption

Posted by Lyle Brooks <br...@deseret.com>.
Ok, a couple of things...

1) You want to move the $r->send_http_header; call up before calling
   $template->process();

2) Modify $template->process( $file, $vars, $r) to
          $template->process( $file, $vars) since you specify OUTPUT => $r
   when you create the Template object (so it's re-dundant).

3) get rid of the $r->print( $output ) line as well, $template->process()
is going to send the output to Apache the way you have it setup.


4) As David Ranney pointed out in a previous post, you might want to
put your images somewhere else, or adjust your URL

You get a path_info part only for the virtual component of your URL
(ie. there's no filesystem component beyond /tt ), but your URL for
images/hello.gif is relative to /tt, which means your handler for
<Location /tt> has got to fix things up.

...or..

If you make the URL hello.gif to resolve to something outside /tt,
then Apache should serve it up as a regular file, which I suspect is
what you want.

HTH

Lyle
 
                                                                                
                                                                                
                                                                                
Quoting Tim Noll (tnoll@grapevine.net):
> Whoops, I hacked up my example a little to make it easier it to read,

> and I accidentally removed the line:
> $file =~ s{^/}{};
> But, it was in the original. Really. :-)
> 
> -Tim
> 
> 
> ----- Original Message -----
> From: "Lyle Brooks" <br...@deseret.com>
> To: "Tim Noll" <tn...@grapevine.net>
> Cc: <mo...@apache.org>
> Sent: Tuesday, February 05, 2002 4:42 PM
> Subject: Re: image corruption
> 
> 
> > When I try this example, I find that this line
> >
> > >  my $file = $r->path_info;
> >
> > will set $file to "/index.html" when I request the URL /tt/index.html
> >
> > which leads to an error message that says,
> >
> > reason: file error - /index.html: absolute paths are not allowed (set
> ABSOLUTE option)
> >
> > You may want to clip off the leading slash or set the Template Toolkit
> > option ABSOLUTE, depending on which suits your needs.
> >
> > Quoting Tim Noll (tnoll@grapevine.net):
> > > I'm attempting to use mod_perl and Template Toolkit to serve up
> > > templates. However, I'm having a problem with the images in those
> > > templates: They're passing through the content handler, and thus
> getting
> > > corrupted.
> > >
> > > My first thought was to return DECLINED from the content handler if
> the
> > > request is not for text/html content; however, since I'm using a
> > > Location directive, the content_type is always empty since there's
> no
> > > direct mapping to an actual image file. I could use an Alias to map
> the
> > > URI to the file, but then I wouldn't have the path_info that I'm
> using
> > > to call the template.
> > >
> > > Since my test code, using path_info, is based on an example from the
> > > Template Toolkit docs, I feel like I'm probably overlooking
> something
> > > basic. So, I'd appreciate it if someone could show me the error of
> my
> > > ways. :-)
> > >
> > > Here are the relevant chunks of config and code:
> > >
> > > from httpd.conf
> > > ---------------
> > > ...
> > > <Location /tt>
> > >  SetHandler perl-script
> > >  PerlHandler Apache::Test::Mod
> > >  PerlSetVar WEBROOT /usr/local/apache/tt/html
> > > </Location>
> > > ...
> > >
> > > Apache::Test::Mod
> > > -----------------
> > > ...
> > > sub handler {
> > >  my $r = shift;
> > >
> > >  # this doesn't work
> > >  #return DECLINED unless $r->content_type eq 'text/html';
> > >
> > >  my $WEBROOT = $r->dir_config('WEBROOT')
> > >   or return fail( $r, SERVER_ERROR, "'WEBROOT' not specified" );
> > >
> > >  my $file = $r->path_info;
> > >
> > >  my $vars = {
> > >   content  => $r->content_type,
> > >  };
> > >
> > >  $r->content_type('text/html');
> > >  $r->no_cache(1);
> > >
> > >  my $template = Template->new( {
> > >   INCLUDE_PATH => "$WEBROOT:$WEBROOT/include",
> > >   OUTPUT   => $r,
> > >  } );
> > >  $template->process( $file, $vars, $r)
> > >   or return fail( $r, SERVER_ERROR, $template->error );
> > >
> > >  $r->send_http_header();
> > >  $r->print( $output );
> > >
> > >  return OK;
> > > }
> > > ...
> > >
> > > index.html (test template)
> > > --------------------------
> > > <html>
> > >  <head>
> > >   <title>test</title>
> > >  </head>
> > >  <body>
> > >   <p>content_type: [% content %]</p>
> > >   <p>image: <img src="images/hello.gif"></p>
> > >  </body>
> > > </html>
> > >
> > >
> > > Thanks.
> > >
> > > -Tim
> > >
> > >
> >
> 

Re: image corruption

Posted by Tim Noll <tn...@grapevine.net>.
Whoops, I hacked up my example a little to make it easier it to read,
and I accidentally removed the line:
$file =~ s{^/}{};
But, it was in the original. Really. :-)

-Tim


----- Original Message -----
From: "Lyle Brooks" <br...@deseret.com>
To: "Tim Noll" <tn...@grapevine.net>
Cc: <mo...@apache.org>
Sent: Tuesday, February 05, 2002 4:42 PM
Subject: Re: image corruption


> When I try this example, I find that this line
>
> >  my $file = $r->path_info;
>
> will set $file to "/index.html" when I request the URL /tt/index.html
>
> which leads to an error message that says,
>
> reason: file error - /index.html: absolute paths are not allowed (set
ABSOLUTE option)
>
> You may want to clip off the leading slash or set the Template Toolkit
> option ABSOLUTE, depending on which suits your needs.
>
> Quoting Tim Noll (tnoll@grapevine.net):
> > I'm attempting to use mod_perl and Template Toolkit to serve up
> > templates. However, I'm having a problem with the images in those
> > templates: They're passing through the content handler, and thus
getting
> > corrupted.
> >
> > My first thought was to return DECLINED from the content handler if
the
> > request is not for text/html content; however, since I'm using a
> > Location directive, the content_type is always empty since there's
no
> > direct mapping to an actual image file. I could use an Alias to map
the
> > URI to the file, but then I wouldn't have the path_info that I'm
using
> > to call the template.
> >
> > Since my test code, using path_info, is based on an example from the
> > Template Toolkit docs, I feel like I'm probably overlooking
something
> > basic. So, I'd appreciate it if someone could show me the error of
my
> > ways. :-)
> >
> > Here are the relevant chunks of config and code:
> >
> > from httpd.conf
> > ---------------
> > ...
> > <Location /tt>
> >  SetHandler perl-script
> >  PerlHandler Apache::Test::Mod
> >  PerlSetVar WEBROOT /usr/local/apache/tt/html
> > </Location>
> > ...
> >
> > Apache::Test::Mod
> > -----------------
> > ...
> > sub handler {
> >  my $r = shift;
> >
> >  # this doesn't work
> >  #return DECLINED unless $r->content_type eq 'text/html';
> >
> >  my $WEBROOT = $r->dir_config('WEBROOT')
> >   or return fail( $r, SERVER_ERROR, "'WEBROOT' not specified" );
> >
> >  my $file = $r->path_info;
> >
> >  my $vars = {
> >   content  => $r->content_type,
> >  };
> >
> >  $r->content_type('text/html');
> >  $r->no_cache(1);
> >
> >  my $template = Template->new( {
> >   INCLUDE_PATH => "$WEBROOT:$WEBROOT/include",
> >   OUTPUT   => $r,
> >  } );
> >  $template->process( $file, $vars, $r)
> >   or return fail( $r, SERVER_ERROR, $template->error );
> >
> >  $r->send_http_header();
> >  $r->print( $output );
> >
> >  return OK;
> > }
> > ...
> >
> > index.html (test template)
> > --------------------------
> > <html>
> >  <head>
> >   <title>test</title>
> >  </head>
> >  <body>
> >   <p>content_type: [% content %]</p>
> >   <p>image: <img src="images/hello.gif"></p>
> >  </body>
> > </html>
> >
> >
> > Thanks.
> >
> > -Tim
> >
> >
>


Re: image corruption

Posted by David Ranney <dr...@e-perception.com>.
Another obvious option would be to put your images in a different directory,
e.g. /images rather than /tt/images.

Or, add another <Location> directive for /tt/images, and set the handler to
the default handler.

-Dave


Re: image corruption

Posted by Lyle Brooks <br...@deseret.com>.
When I try this example, I find that this line

>  my $file = $r->path_info;

will set $file to "/index.html" when I request the URL /tt/index.html

which leads to an error message that says,

reason: file error - /index.html: absolute paths are not allowed (set ABSOLUTE option)

You may want to clip off the leading slash or set the Template Toolkit
option ABSOLUTE, depending on which suits your needs.
 
Quoting Tim Noll (tnoll@grapevine.net):
> I'm attempting to use mod_perl and Template Toolkit to serve up
> templates. However, I'm having a problem with the images in those
> templates: They're passing through the content handler, and thus getting
> corrupted.
> 
> My first thought was to return DECLINED from the content handler if the
> request is not for text/html content; however, since I'm using a
> Location directive, the content_type is always empty since there's no
> direct mapping to an actual image file. I could use an Alias to map the
> URI to the file, but then I wouldn't have the path_info that I'm using
> to call the template.
> 
> Since my test code, using path_info, is based on an example from the
> Template Toolkit docs, I feel like I'm probably overlooking something
> basic. So, I'd appreciate it if someone could show me the error of my
> ways. :-)
> 
> Here are the relevant chunks of config and code:
> 
> from httpd.conf
> ---------------
> ...
> <Location /tt>
>  SetHandler perl-script
>  PerlHandler Apache::Test::Mod
>  PerlSetVar WEBROOT /usr/local/apache/tt/html
> </Location>
> ...
> 
> Apache::Test::Mod
> -----------------
> ...
> sub handler {
>  my $r = shift;
> 
>  # this doesn't work
>  #return DECLINED unless $r->content_type eq 'text/html';
> 
>  my $WEBROOT = $r->dir_config('WEBROOT')
>   or return fail( $r, SERVER_ERROR, "'WEBROOT' not specified" );
> 
>  my $file = $r->path_info;
> 
>  my $vars = {
>   content  => $r->content_type,
>  };
> 
>  $r->content_type('text/html');
>  $r->no_cache(1);
> 
>  my $template = Template->new( {
>   INCLUDE_PATH => "$WEBROOT:$WEBROOT/include",
>   OUTPUT   => $r,
>  } );
>  $template->process( $file, $vars, $r)
>   or return fail( $r, SERVER_ERROR, $template->error );
> 
>  $r->send_http_header();
>  $r->print( $output );
> 
>  return OK;
> }
> ...
> 
> index.html (test template)
> --------------------------
> <html>
>  <head>
>   <title>test</title>
>  </head>
>  <body>
>   <p>content_type: [% content %]</p>
>   <p>image: <img src="images/hello.gif"></p>
>  </body>
> </html>
> 
> 
> Thanks.
> 
> -Tim
> 
>