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

[Cheetahtemplate-discuss] Re: mod_python.publisher and Cheetah

Expand Messages
  • Graham Dumpleton
    ... I would strongly recommend against changing apache.DECLINED to apache.HTTP_NOT_FOUND, it is there for a very specific reason. By using apache.DECLINED, it
    Message 1 of 12 , Mar 15, 2005
    • 0 Attachment
      On 15/03/2005, at 10:28 PM, Stephane Bortzmeyer wrote:

      > On Mon, Mar 14, 2005 at 06:25:50PM -0500,
      > Graham Dumpleton <grahamd@...> wrote
      >> target = req.filename + ".py"
      >>
      >> if not os.path.exists(target):
      >> return apache.DECLINED
      >
      > I changed it (personal preference and I have only Python content) to:
      >
      > if req.filename[:-3] != ".py":
      > target = req.filename + ".py"
      > else:
      > target = req.filename
      >
      > if not os.path.exists(target):
      > raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND

      I would strongly recommend against changing apache.DECLINED to
      apache.HTTP_NOT_FOUND, it is there for a very specific reason.

      By using apache.DECLINED, it is possible to put other types of
      files such as images, plain text etc into the same directory
      and Apache will serve them up without you having to go to
      contortions in the Apache configuration to have it only use
      mod_python for ".py" or some other extension. It is also not
      really good practice to use ".py" extension in a URL anyway as
      that is then exposing to a user of your web site what you are
      using to implement the web site. This can open up security
      issues in as much as they have a better idea of what potential
      security holes to probe. Using ".py" extension also makes it
      harder to change internally how pages are implemented to some
      other Python based toolkit as you are more likely to have to
      change the URL, thus invalidating bookmarks, search engine
      databases etc. Thus, you are better off using REST style of no
      extension, or use ".html" if it truly is HTML content.

      For example, to use ".html" extension you might to the extent
      of having:

      # Only interpret ".html" requests.

      target,extn = os.path.splitext(req.filename)

      if extn != ".html":
      return apache.DECLINED

      # We only want to treat request as being a possible
      # request for a Cheetah generated template file if
      # there exists both a ".tmpl" and ".py" file.

      target_tmpl = target + ".tmpl"
      target_py = target + ".py"

      if not os.path.exists(target_tmpl) and not os.path.exists(target_py):
      return apache.DECLINED

      This will allow ".html" requests for virtual pages generated from
      Cheetah templates, presuming ".tmpl" is in same directory, or a
      static ".html" constructed by hand residing in the same directory.
      The latter will work because DECLINED is returned, which gives Apache
      a second chance at it, instead of HTTP_NOT_FOUND, which gives it no
      chance.

      Graham



      -------------------------------------------------------
      SF email is sponsored by - The IT Product Guide
      Read honest & candid reviews on hundreds of IT Products from real users.
      Discover which products truly live up to the hype. Start reading now.
      http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Stephane Bortzmeyer
      On Wed, Mar 16, 2005 at 08:03:29AM +1100, Graham Dumpleton wrote ... OK, I went back to DECLINED. I modified the publisher again to have
      Message 2 of 12 , Mar 25, 2005
      • 0 Attachment
        On Wed, Mar 16, 2005 at 08:03:29AM +1100,
        Graham Dumpleton <grahamd@...> wrote
        a message of 67 lines which said:

        > I would strongly recommend against changing apache.DECLINED to
        > apache.HTTP_NOT_FOUND, it is there for a very specific reason.

        OK, I went back to DECLINED.

        I modified the publisher again to have both Cheetah-generated files
        and "classic" mod_python scripts in the same directory.
      • Graham Dumpleton
        Sorry, not sure where to send this as can t work out email of person who updated: http://wiki.cheetahtemplate.org/cheetahrecipes.html To say: After a lot
        Message 3 of 12 , Apr 4, 2005
        • 0 Attachment
          Sorry, not sure where to send this as can't work out email of person
          who updated:

          http://wiki.cheetahtemplate.org/cheetahrecipes.html

          To say:

          After a lot research (I thought I'd screwed up my Apache .conf, or
          something like that), I started debugging, and found two problems.

          req.filename had the .py extension already appended, so the script
          was trying import mptest.py.py

          This may have been caused by my allowing .py, .tmpl, etc. file
          extensions to not be blocked. But even when I left .py off the URL,
          it was appended by the time req.filename was passed to the script.

          With a code change of:

          #target = req.filename + ".py"
          target = req.filename

          This was all in relation to mod_python handler code for Cheetah I
          sent back in March.

          Anyway, my guess as to why you had a problem here may well be because
          you have MultiViews turned on. If you have MultiViews turned on, when a
          URL has "mptest" but there is no file of that name, Apache will do some
          matching to find a file with the same basename and internally add an
          extension which makes it match a physical file. This would have result
          in the code screwing up as you found.

          When using mod_python it is highly recommended that you ensure that
          MultiViews is turned for directories managed by mod_python, otherwise
          Apache will screw with the URLs and things may not work reliably or
          as expected.

          In relation to the code change from:

          module = apache.import_module(module_name,[directory]) #1

          to:

          module = apache.import_module(module_name, req, [directory]) #2

          Strictly speaking, the original (#1) is actually wrong in that it should
          have been:

          module = apache.import_module(module_name,path=[directory]) #3

          If this was done, then adding "req" as an argument in (#2) may not have
          been required. All the "req" argument seems to do is possibly push the
          positional parameter for the path to the correct place for mod_python
          2.7.

          The actual prototype for apache.import_module() in mod_python 3.1 is:

          import_module(module_name[, autoreload=1, log=0, path=None])

          Someone might like to try the code instead with (#3) and see if it works
          for both mod_python 2.7 and 3.1.

          If the suggestions work out, maybe someone can update the Wiki page
          appropriately.

          Graham


          On Tuesday, March 15, 2005, at 10:28 PM, Stephane Bortzmeyer wrote:

          > On Mon, Mar 14, 2005 at 06:25:50PM -0500,
          > Graham Dumpleton <grahamd@...> wrote
          > a message of 200 lines which said:
          >
          >> Here is a better version of a handler which allows use of Cheetah
          >> templates with mod_python.
          >
          > It seems to work fine. Many thanks.
          >
          >> target = req.filename + ".py"
          >>
          >> if not os.path.exists(target):
          >> return apache.DECLINED
          >
          > I changed it (personal preference and I have only Python content) to:
          >
          > if req.filename[:-3] != ".py":
          > target = req.filename + ".py"
          > else:
          > target = req.filename
          >
          > if not os.path.exists(target):
          > raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND



          -------------------------------------------------------
          SF email is sponsored by - The IT Product Guide
          Read honest & candid reviews on hundreds of IT Products from real users.
          Discover which products truly live up to the hype. Start reading now.
          http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
          _______________________________________________
          Cheetahtemplate-discuss mailing list
          Cheetahtemplate-discuss@...
          https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
        Your message has been successfully submitted and would be delivered to recipients shortly.