Loading ...
Sorry, an error occurred while loading the content.

[mp2] Declining from response handler bypasses other handlers

Expand Messages
  • Matthew Westcott
    I m using a PerlResponseHandler to control access to selected directories of a site, and I ve encountered a similar problem to that described in
    Message 1 of 4 , Oct 2, 2003
    • 0 Attachment
      I'm using a PerlResponseHandler to control access to selected
      directories of a site, and I've encountered a similar problem to that
      described in
      http://marc.theaimsgroup.com/?l=apache-modperl&m=106141216914801&w=2
      where returning a 'declined' status is skipping other handlers and
      going straight to Apache's default handler.

      My code is:

      package MyApache::Permissions;

      use strict;
      use warnings;
      use Apache::RequestRec ();
      use Apache::Const -compile => qw(OK DECLINED);

      my %protected_dirs = (
      'foo' => 1,
      'bar/baz' => 1,
      );

      sub handler {
      my $r = shift;

      my $requested_dir = $r->filename;
      $requested_dir =~ s|^/home/www/html/my_site/(.*)/[^\/]*$|$1|;

      return Apache::DECLINED unless exists $protected_dirs{$requested_dir};

      $r->content_type('text/plain');
      print "mod_perl has taken over the $requested_dir directory...\n";

      return Apache::OK;
      }
      1;

      and this is active over the whole site, using these directives in
      httpd.conf:
      <Location />
      SetHandler perl-script
      PerlResponseHandler MyApache::Permissions
      </Location>

      Blocking the named directories works fine, but for the unblocked
      directories (the ones for which I return Apache::DECLINED) the
      existing (non-Perl) response handlers aren't taking effect, so my
      ColdFusion scripts are being sent through unparsed, and directory
      indexes no longer work (/something/index.html works, /something/
      returns a 404).

      I suspect the problem is on the Apache side; the SetHandler is
      overriding all other handlers. Is there any way around this?

      I'm using Apache/2.0.47 and mod_perl/1.99_10, on Linux.

      - Matthew
    • Geoffrey Young
      ... with SetHandler there are no other handlers to call - it s the one you choose (perl-script in the below case) or default-handler, that s it. ... yup. both
      Message 2 of 4 , Oct 2, 2003
      • 0 Attachment
        Matthew Westcott wrote:
        > I'm using a PerlResponseHandler to control access to selected
        > directories of a site, and I've encountered a similar problem to that
        > described in
        > http://marc.theaimsgroup.com/?l=apache-modperl&m=106141216914801&w=2
        > where returning a 'declined' status is skipping other handlers and
        > going straight to Apache's default handler.

        with SetHandler there are no other handlers to call - it's the one you
        choose (perl-script in the below case) or default-handler, that's it.

        >
        > and this is active over the whole site, using these directives in
        > httpd.conf:
        > <Location />
        > SetHandler perl-script
        > PerlResponseHandler MyApache::Permissions
        > </Location>
        >

        > I suspect the problem is on the Apache side; the SetHandler is
        > overriding all other handlers.

        yup. both your issue and the one in the link provided are expected behaviors.

        > Is there any way around this?

        it really depends on what you want to do. if you are requesting
        /foo/bar.php and want php to process the page, just alter your <Location>
        settings as appropriate.

        if you want to post-process that output with mod_perl, then you need to look
        into output filters.

        if you want to dynamically decide who should serve the page - mod_perl if
        some directory is found, mod_php otherwise, then you can use your own
        PerlTypeHandler or PerlFixupHandler to set $r->handler for the request based
        on your own criteria.

        HTH

        --Geoff
      • Matthew Westcott
        ... Many thanks - a PerlFixupHandler has done the job nicely. The fixup handler makes the decision, turning on a custom PerlResponseHandler if so. For the
        Message 3 of 4 , Oct 2, 2003
        • 0 Attachment
          On 2 Oct 2003 at 9:15, Geoffrey Young wrote:

          > if you want to dynamically decide who should serve the page - mod_perl if
          > some directory is found, mod_php otherwise, then you can use your own
          > PerlTypeHandler or PerlFixupHandler to set $r->handler for the request based
          > on your own criteria.

          Many thanks - a PerlFixupHandler has done the job nicely. The fixup
          handler makes the decision, turning on a custom PerlResponseHandler
          if so. For the record, here's the code I arrived at:

          sub handler {
          my $r = shift;

          my $requested_dir = $r->filename;
          $requested_dir =~ s|^/home/www/html/my_site/(.*)/[^\/]*$|$1|;

          return Apache::OK unless exists $permission{$requested_dir};

          $r->handler('perl-script');
          $r->set_handlers(PerlResponseHandler => \&restricted_response);
          return Apache::OK;
          }

          sub restricted_response {
          my $r = shift;

          my $requested_dir = $r->filename;
          $requested_dir =~ s|^/home/www/html/my_site/(.*)/[^\/]*$|$1|;

          $r->content_type('text/plain');
          print "mod_perl has taken over the $requested_dir directory...\n";

          return Apache::OK;
          }
          1;

          invoked with

          <Location />
          PerlFixupHandler MyApache::Permissions
          </Location>



          - Matthew
        • HLiu@ap.org
          My suggestion are: 1. print $requested_dir; to see what you got. 2 changing your code return Apache::DECLINED unless exists $protected_dirs{$requested_dir};
          Message 4 of 4 , Oct 2, 2003
          • 0 Attachment
            My suggestion are:

            1. print $requested_dir; to see what you got.

            2 changing your code
            return Apache::DECLINED unless exists $protected_dirs{$requested_dir};
            to
            return Apache::DECLINED if $protected_dirs{$requested_dir};

            Hope it helpful.

            Willy



            "Matthew
            Westcott" To: modperl@...
            <matthew@torchbox cc:
            .com> Subject: [mp2] Declining from response handler bypasses other handlers

            10/02/2003 08:47
            AM






            I'm using a PerlResponseHandler to control access to selected
            directories of a site, and I've encountered a similar problem to that
            described in
            http://marc.theaimsgroup.com/?l=apache-modperl&m=106141216914801&w=2
            where returning a 'declined' status is skipping other handlers and
            going straight to Apache's default handler.

            My code is:

            package MyApache::Permissions;

            use strict;
            use warnings;
            use Apache::RequestRec ();
            use Apache::Const -compile => qw(OK DECLINED);

            my %protected_dirs = (
            'foo' => 1,
            'bar/baz' => 1,
            );

            sub handler {
            my $r = shift;

            my $requested_dir = $r->filename;
            $requested_dir =~ s|^/home/www/html/my_site/(.*)/[^\/]*$|$1|;

            return Apache::DECLINED unless exists $protected_dirs
            {$requested_dir};

            $r->content_type('text/plain');
            print "mod_perl has taken over the $requested_dir directory...\n";

            return Apache::OK;
            }
            1;

            and this is active over the whole site, using these directives in
            httpd.conf:
            <Location />
            SetHandler perl-script
            PerlResponseHandler MyApache::Permissions
            </Location>

            Blocking the named directories works fine, but for the unblocked
            directories (the ones for which I return Apache::DECLINED) the
            existing (non-Perl) response handlers aren't taking effect, so my
            ColdFusion scripts are being sent through unparsed, and directory
            indexes no longer work (/something/index.html works, /something/
            returns a 404).

            I suspect the problem is on the Apache side; the SetHandler is
            overriding all other handlers. Is there any way around this?

            I'm using Apache/2.0.47 and mod_perl/1.99_10, on Linux.

            - Matthew
          Your message has been successfully submitted and would be delivered to recipients shortly.