[mod_python] Patch to 2.7.5 for internal_redirect

Tom Emerson tree at basistech.com
Tue Jul 31 15:31:26 EST 2001


Attached is a small patch to mod_python 2.7.5 which provides support
for doing an internal_redirect. I've only done minimal testing on this
but it appears to work.

I added a new method to the request object, internal_redirect. It
takes a single string argument which is the local URI to redirect
to. After calling this method your handler should just return
apache.OK.

  from mod_python import apache
 
  def handler(req):
      req.internal_redirect("/redirected.html")
      return apache.OK

I also added an exception class, InternalRedirect, to
mod_python.apache. It takes a single argument which is the local URI
to redirect to. This is used by a modification to Publisher which
allows you to perform an internal redirect simply by raising an
InternalRedirect exception:

  from mod_python import apache
 
  def test(req):
      raise apache.InternalRedirect("/redirected.html")

The request object's "main" member is None if the current request is
the "main" request.

Share and enjoy.

-------------- next part --------------
*** ./lib/python/mod_python/publisher.py.orig	Mon May 14 18:17:21 2001
--- ./lib/python/mod_python/publisher.py	Tue Jul 31 15:46:33 2001
***************
*** 168,174 ****
              if name not in expected:
                  del args[name]
  
!         result = apply(object, (), args)
  
      if result:
          result = str(result)
--- 168,178 ----
              if name not in expected:
                  del args[name]
  
!         try:
!             result = apply(object, (), args)
!         except apache.InternalRedirect, e:
!             req.internal_redirect(e.uri)
!             return apache.OK
  
      if result:
          result = str(result)
*** ./lib/python/mod_python/apache.py.orig	Tue Jul 31 16:00:17 2001
--- ./lib/python/mod_python/apache.py	Tue Jul 31 15:44:16 2001
***************
*** 53,58 ****
--- 53,63 ----
  import imp
  import types
  import _apache
+ import exceptions
+ 
+ class InternalRedirect (exceptions.Exception):
+     def __init__(self, uri):
+         self.uri = uri
  
  # a small hack to improve PythonPath performance. This
  # variable stores the last PythonPath in raw (unevaled) form.
*** ./src/requestobject.c.orig	Tue May 22 22:49:43 2001
--- ./src/requestobject.c	Tue Jul 31 15:08:21 2001
***************
*** 438,443 ****
--- 438,462 ----
  }
  
  /**
+  ** request.internal_redirect(request self, string uri)
+  **
+  *    An interface to the ap_internal_redirect function.
+  *    Tom Emerson, <tree at basistech.com>, 2001-07-31
+  */
+ static PyObject * req_internal_redirect(requestobject *self, PyObject *args)
+ {
+     char *uri;
+ 
+     if (! PyArg_ParseTuple(args, "s", &uri))
+         return NULL;
+ 
+     ap_internal_redirect(uri, self->request_rec);
+     
+     Py_INCREF(Py_None);
+     return Py_None;
+ }
+ 
+ /**
   ** request.get_options(request self)
   **
   */
***************
*** 792,797 ****
--- 811,817 ----
      {"get_dirs",             (PyCFunction) req_get_dirs,             METH_VARARGS},
      {"get_remote_host",      (PyCFunction) req_get_remote_host,      METH_VARARGS},
      {"get_options",          (PyCFunction) req_get_options,          METH_VARARGS},
+     {"internal_redirect",    (PyCFunction) req_internal_redirect,    METH_VARARGS},
      {"read",                 (PyCFunction) req_read,                 METH_VARARGS},
      {"readline",             (PyCFunction) req_readline,             METH_VARARGS},
      {"register_cleanup",     (PyCFunction) req_register_cleanup,     METH_VARARGS},
-------------- next part --------------

-- 
Tom Emerson                                          Basis Technology Corp.
Sr. Sinostringologist                              http://www.basistech.com
  "Beware the lollipop of mediocrity: lick it once and you suck forever"


More information about the Mod_python mailing list