Issue Details (XML | Word | Printable)

Key: MODPYTHON-63
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Graham Dumpleton
Reporter: Kevin Quick
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
mod_python

Handle wildcard in Directory to sys.path transfer

Created: 14/Jul/05 08:35 AM   Updated: 02/Apr/07 11:23 AM
Return to search
Component/s: core
Affects Version/s: 3.1.3
Fix Version/s: 3.3.1

Time Tracking:
Not Specified

Environment: gentoo Linux 2.4.20-gentoo-r8 apache-2.0.54-r8 mod_python-3.1.3-r1

Resolution Date: 25/Aug/06 01:03 AM


 Description  « Hide
Below is a patch that does two things:

1) On module import failures, logfile contains additional information showing
   system paths as well as module name.

2) Support of Python*Handler found in a wildcard-based directory. For example,

<IfModule mod_python.c>
    <Directory /home/*/public_html/python>
        AddHandler mod_python .py
        PythonHandler helloworld
        PythonDebug on
    </Directory>
</IfModule>

which mirrors a corresponding perl setting and would allow the user to
place a mod_python handler in their $HOME/public_html/python directory.

In the current code, the wildcard is not translated, the sys.path update
will be invalid, and the user's module will not be accessible. The attached
patch provides a fix for this.

N.B. There are obvious security issues in using this type of configuration,
but this is very useful for dev environments, and security would implemented
via explicit alternatives anyhow.

Index: apache.py
===================================================================
RCS file: /home/cvspublic/httpd-python/lib/python/mod_python/apache.py,v
retrieving revision 1.83
diff -r1.83 apache.py
33a34,40
> def add_handler_path(hpath):
> import glob
> if hpath:
> for D in glob.glob(hpath):
> if os.path.isdir(D) and D not in sys.path:
> sys.path.insert(0, D)
>
170,171c177
< if filter.dir and (filter.dir not in sys.path):
< sys.path[:0] = [filter.dir]
---
> add_handler_path(filter.dir)
280,282c286
< dir = hlist.directory
< if dir and (dir not in sys.path):
< sys.path[:0] = [dir]
---
> add_handler_path(hlist.directory)
454c458,462
< f, p, d = imp.find_module(parts[i], path)
---
> try:
> f, p, d = imp.find_module(parts[i], path)
> except:
> _apache.log_error("mod_python: import error for module %s with path: %s and sys.path: %s"%(parts[i],path,sys.path))
> raise


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Nicolas Lehuen made changes - 22/Oct/05 04:03 PM
Field Original Value New Value
Component/s core [ 11607 ]
Graham Dumpleton added a comment - 14/Feb/06 09:57 AM
In what will be the grand unified theory for the module importer, there will be no explicit modification of sys.path, so above solution isn't going to work.

The issues of wildcards in paths does need to be looked at and how they do it in mod_perl would be a good guide so still need to keep this issue open.

FWIW though, the above will be able to be done in other ways when MODPYTHON-125 is addressed. One could for example use:

<IfModule mod_python.c>
    <Directory /home/*/public_html/python>
        PythonFixupHandler mod_userdir_dispatch
        PythonDebug on
    </Directory>
</IfModule>

# mod_userdir_dispatch.py

from mod_python import apache

def fixuphandler(req):
  if os.splitext(req.filename)[1] == ".py":
    req.handler = "mod_python"
    user = req.notes["mod_userdir_user"]
    directory = "/home/%s/public_html/python" % user
    req.add_handler("PythonHandler","helloworld",dir)
  return apache.OK

In other words, use a fixup handler to enable mod_python for the content handler phase when extension of mapped file is .py, and also specify actual module in users home directory to be called in that situation.

Personally I wouldn't use "helloworld" in the users directory but something like "_dispatch". That way a user could put in that file:

  # _dispatch.py

  from mod_python.publisher import handler

Thus allowing them to use publisher, but where an access to URL of "_dispatch.py/handler" isn't going to cause a loop as publisher tries to call into it, ie., the underscore protects it from publisher.

At least I think that would work.

Repository Revision Date User Message
ASF #392692 Sun Apr 09 08:58:35 UTC 2006 grahamd The PythonImport directive makes use of the handler list data structure
even though it specifically is for a different purpose. The handler list
data structure needs to be changed a bit in order to support changes
required to address MODPYTHON-63 and MODPYTHON-126. These changes may
make it unusable in the way that the PythonImport directive was using it,
or at least it will not be a good match. The PythonImport directive has
therefore been modified to use a standard Apache table object thereby
allowing arbitrary changes to the handler list data structure to be made as
necessary.

Note that although the current Apache table implementation probably results
in module imports occuring in the order they appear in the Apache
configuration file, if how it is implemented is changed, this may no longer
be the case. This is not seen as an issue as the documentation for the
PythonImport directive does not give any guarantees about ordering of
imports within the context of a specific interpreter and it would be bad
practice for modules to rely on mod_python importing them in a specific
order. If there are ordering requirements, it should be dealt with internal
to the module being imported so that it is self contained.
Files Changed
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h
MODIFY /httpd/mod_python/trunk/src/mod_python.c
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/__init__.py
MODIFY /httpd/mod_python/trunk/src/include/mpversion.h
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h.in

Graham Dumpleton made changes - 09/Apr/06 02:18 PM
Assignee Graham Dumpleton [ grahamd ]
Graham Dumpleton made changes - 09/Apr/06 02:18 PM
Status Open [ 1 ] In Progress [ 3 ]
Repository Revision Date User Message
ASF #394455 Sun Apr 16 10:49:39 UTC 2006 grahamd When Python*Handler or Python*Filter directive is used inside of a Files
directive container, the handler/filter directory value will now correctly
resolve to the directory corresponding to any parent Directory directive or
the location of the .htaccess file the Files directive is contained in.
(MODPYTHON-126])

The patch also lays the ground work for being able to support Directory
directive with glob style wildcards or Directory/DirectoryMatch directive
with a regular expression. (MODPYTHON-63)
Files Changed
MODIFY /httpd/mod_python/trunk/test/htdocs/tests.py
MODIFY /httpd/mod_python/trunk/src/hlist.c
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h
MODIFY /httpd/mod_python/trunk/test/httpdconf.py
MODIFY /httpd/mod_python/trunk/test/test.py
MODIFY /httpd/mod_python/trunk/src/mod_python.c
MODIFY /httpd/mod_python/trunk/src/requestobject.c
MODIFY /httpd/mod_python/trunk/Doc/appendixc.tex
MODIFY /httpd/mod_python/trunk/src/include/hlist.h
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h.in

Repository Revision Date User Message
ASF #395571 Thu Apr 20 12:04:23 UTC 2006 grahamd Debian backported ap_regex_t to Apache 2.0 and thus made official version
checking break for this feature. Use explicit check for AP_REG_EXTENDED
in addition to version check. (MODPYTHON-63) (MODPYTHON-126)
Files Changed
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/__init__.py
MODIFY /httpd/mod_python/trunk/src/include/mpversion.h
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h.in

Graham Dumpleton made changes - 07/May/06 07:32 AM
Status In Progress [ 3 ] Open [ 1 ]
Graham Dumpleton made changes - 30/Jul/06 10:55 AM
Status Open [ 1 ] In Progress [ 3 ]
Repository Revision Date User Message
ASF #428972 Sat Aug 05 07:38:17 UTC 2006 grahamd (MODPYTHON-63) Fixes to code previously set down for handling <Directory ~>
and <DirectoryMatch> as it wasn't decoding configuration correctly and in
case of <DirectoryMatch> could cause Apache to crash.
Files Changed
MODIFY /httpd/mod_python/trunk/src/mod_python.c
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/__init__.py
MODIFY /httpd/mod_python/trunk/src/include/mpversion.h

Graham Dumpleton made changes - 13/Aug/06 07:52 AM
Fix Version/s 3.3 [ 12310101 ]
Graham Dumpleton added a comment - 14/Aug/06 12:08 PM
Committed code to complete changes such that handler directory is set correctly when handler directive is used with context of Directory or DirectoryMatch directive with a pattern. Forgot to cross reference to this report in commit message. Link to changes is:

  http://svn.apache.org/viewvc?view=rev&revision=431327

Graham Dumpleton made changes - 25/Aug/06 01:03 AM
Status In Progress [ 3 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Graham Dumpleton made changes - 02/Apr/07 11:23 AM
Status Resolved [ 5 ] Closed [ 6 ]