Apache Mod-Python - Commented: (MODPYTHON-98) wrong handler supplied to req.add_handler() generates error

This is Interesting: Free IT Magazines  
Home > Archive > Apache Mod-Python > January 2006 > Commented: (MODPYTHON-98) wrong handler supplied to req.add_handler() generates error





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Commented: (MODPYTHON-98) wrong handler supplied to req.add_handler() generates error
Graham Dumpleton (JIRA)

2005-12-18, 2:45 am

[ http://issues.apache.org/jira/brows...action_12360697 ]

Graham Dumpleton commented on MODPYTHON-98:
-------------------------------------------

To summarise the changes in the above into one spot so it is easier to see what is required:

1. If say that exception is the way to go, change documentation of req.add_handler(). Thus change:

Note: There is no checking being done on the validity of the handler name. If you pass this function an invalid handler it will simply be ignored.

to something like:

Note: If you pass this function an invalid handler, an exception will be generated at the time an attempt is made to find the handler.

That or just delete the note altogether.

2. Change code so that exception raised if handler string is empty. Thus in HandleDispatch of apache.py change:

while hlist.handler:

to:

while hlist.handler is not None:

3. Fix segfault when adding handler to empty list. Thus in python_handler() function in mod_python.c, change:

/* create a hahdler list object */
request_obj->hlo = (hlistobject *)MpHList_FromHLEntry(hle);

/* add dynamically registered handlers, if any */
if (dynhle) {
MpHList_Append(request_obj->hlo, dynhle);
}

to:

if (!hle)
{
/* create a handler list object from dynamically registered handlers */
request_obj->hlo = (hlistobject *)MpHList_FromHLEntry(dynhle);
}
else
{
/* create a handler list object */
request_obj->hlo = (hlistobject *)MpHList_FromHLEntry(hle);

/* add dynamically registered handlers, if any */
if (dynhle) {
MpHList_Append(request_obj->hlo, dynhle);
}
}

4. Just in case there was some way it could still be triggered if the above is done. Only allow faulty handler if marked as silent to propagate DECLINED if it is the first and only handler. Thus change in HandlerDispatch of apache.py:

elif hlist.silent:
result = DECLINED

to:

elif hlist.silent:
if result != OK:
result = DECLINED

5. To make intent of code clearer in as much as highlighting that exception will be raised if handler not found, change select lines of code in req_add_handler() of requestobject.c to use NOTSILENT instead of 0. Ie., from:

hlist_append(self->request_rec->pool, self->hlo->head,
handler, dir, 0);

hle = hlist_new(self->request_rec->pool, handler, dir, 0);

hlist_append(self->request_rec->pool, hle, handler, dir, 0);

to:

hlist_append(self->request_rec->pool, self->hlo->head,
handler, dir, NOTSILENT);

hle = hlist_new(self->request_rec->pool, handler, dir, NOTSILENT);

hlist_append(self->request_rec->pool, hle, handler, dir, NOTSILENT);

I know that I should be providing a patch, but right now my mod_python code base has various other changes in it related to the simple GIL issues and I would have to take them back out to get a clean patch. :-(

> wrong handler supplied to req.add_handler() generates error
> -----------------------------------------------------------
>
> Key: MODPYTHON-98
> URL: http://issues.apache.org/jira/browse/MODPYTHON-98
> Project: mod_python
> Type: Bug
> Components: core
> Versions: 3.2, 3.1.4
> Reporter: Graham Dumpleton


>
> The documentation for req.add_handler() states:
> Note: There is no checking being done on the validity of the handler name. If you pass this function an invalid handler it will simply be ignored.
> In other words, get the name of the handler wrong and it is supposed to just ignore it. This is not actually the case, instead it will generate an exception when it goes to process the handler:
> Mod_python error: "PythonHandler example::handler_3"
> Traceback (most recent call last):
> File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/apache.py", line 291, in HandlerDispatch
> arg=req, silent=hlist.silent)
> File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/apache.py", line 538, in resolve_object
> raise AttributeError, s
> AttributeError: module '/Users/grahamd/Sites/add_handler/example.py' contains no 'handler_3'
> This can be seen with .htaccess file of:
> SetHandler mod_python
> PythonAccessHandler example
> PythonHandler example::handler_1
> PythonDebug On
> and example.py file containing:
> from mod_python import apache
> def accesshandler(req):
> apache.log_error("accesshandler")
> req.add_handler("PythonHandler","example::handler_3")
> return apache.OK
> def handler_1(req):
> apache.log_error("handler_1")
> req.content_type = 'text/plain'
> req.write("HELLO")
> return apache.OK
> def handler_2(req):
> apache.log_error("handler_2")
> return apache.OK
> Either the documentation is wrong and an exception is desired, or more likely this is an extension of the prior problem with hlist.silent as described as being a problem in other ways in MODPYTHON-46.
> In that case the logic of SILENT/NOTSILENT was the wrong way around and it was fixed by reversing the definitions of the two. In doing this though, it didn't cover cases where a "silent" flag is passed to hlist_new() and hlist_append() in the req_add_ha

ndler() function of requestobject.c.
> Specfically, there are calls to hlist_new() and hlist_append() in that function:
> hlist_append(self->request_rec->pool, self->hlo->head,
> handler, dir, 0);
> hle = hlist_new(self->request_rec->pool, handler, dir, 0);
> hlist_append(self->request_rec->pool, hle, handler, dir, 0);
> These should be written as:
> hlist_append(self->request_rec->pool, self->hlo->head,
> handler, dir, SILENT);
> hle = hlist_new(self->request_rec->pool, handler, dir, SILENT);
> hlist_append(self->request_rec->pool, hle, handler, dir, SILENT);
> If this change were made, the code would then behaves conformant with the documentation as far as being silent, however it highlights a further issue.
> This further issue is that although it is silent when the handler name is wrong, this results in apache.DECLINED being returned for the handler that couldn't be found. Because apache.DECLINED is returned, Apache will try and interpret the URL again and

if possible serve up a static file etc.
> For the above example code this then means that if "example.py" was used in the URL, the browser gets back a response of:
> HELLOfrom mod_python import apache
> def accesshandler(req):
> apache.log_error("accesshandler")
> req.add_handler("PythonHandler","example::handler_3")
> return apache.OK
> def handler_1(req):
> apache.log_error("handler_1")
> req.content_type = 'text/plain'
> req.write("HELLO")
> return apache.OK
> def handler_2(req):
> apache.log_error("handler_2")
> return apache.OK
> That is, the content as returned by handler_1(), followed by the contents of the example.py file.
> If instead the URL wasn't 'example.py' but say 'other.py' with that not existing, get back:
> HELLO
> OK
> The requested URL /~grahamd/add_handler/foo.py was not found on this server.
> Apache/2.0.51 (Unix) mod_python/3.2.5b Python/2.3 Server at localhost Port 8080
> In some ways, this behaviour suggests that the behaviour whereby it raised an exception was probably a better way of handling the situtation anyway. Thus, maybe the documentation should instead be changed and the code left as is, or at least the 0 argum

ents changed to be NOTSLIENT to make it more obvious what it is doing.
> The other option is to change the code to use SILENT, but then document the strange things that can result if the specified handler doesn't exist.
> Comments??????


Graham Dumpleton

2006-01-13, 9:18 pm


On 12/01/2006, at 11:10 AM, Jim Gallacher wrote:

> Jim Gallacher (JIRA) wrote:
>
> Graham,
>
> I've committed the following unit tests for MODPYTHON-98:
>
> test_req_add_bad_handler
> test_req_add_empty_handler_string
> test_accesshandler_add_handler_to_empty_
hl
>
> Can you take a look and let me know if you think these properly cover
> the issue? If they look ok I'll roll the 3.2.6 beta.


I get a failure on test_req_add_empty_handler_string. I don't know if
this is because of other local hacks I have in place or not. But for it
to pass I need the following change to the test.py:

Index: test.py
========================================
===========================
--- test.py (revision 368329)
+++ test.py (working copy)
@@ -516,8 +516,8 @@
f = open(os.path.join(SERVER_ROOT, "logs/error_log"))
log = f.read()
f.close()
- if log.find("contains no 'handler'") == -1:
- self.fail("""Could not find "contains no 'handler'" in
error_log""")
+ if log.find("No module named") == -1:
+ self.fail("""Could not find "No module named" in
error_log""")

def test_accesshandler_add_handler_to_empty_
hl_conf(self):
# Note that there is no PythonHandler specified in the the
VirtualHost

The messages in the error log being:

[Thu Jan 12 21:39:35 2006] [notice] mod_python: (Re)importing module ''
[Thu Jan 12 21:39:35 2006] [error] [client 127.0.0.1] PythonHandler :
Traceback (most recent call last):
[Thu Jan 12 21:39:35 2006] [error] [client 127.0.0.1] PythonHandler :
File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/site-packages/mod_python/apache.py", line 284, in
HandlerDispatch\n log=debug)
[Thu Jan 12 21:39:35 2006] [error] [client 127.0.0.1] PythonHandler :
File
"/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/site-packages/mod_python/apache.py", line 468, in
import_module\n f, p, d = imp.find_module(parts[i], path)
[Thu Jan 12 21:39:35 2006] [error] [client 127.0.0.1] PythonHandler :
ImportError: No module named

Graham


Nicolas Lehuen

2006-01-13, 9:18 pm

Hi all,

I'm back after a few days without Internet access...

I also have the same test failure as Graham. Here is my error log :

[Thu Jan 12 20:11:24 2006] [error] [client 127.0.0.1]
req_add_empty_handler_string
[Thu Jan 12 20:11:24 2006] [notice] mod_python: (Re)importing module ''
[Thu Jan 12 20:11:24 2006] [error] [client 127.0.0.1] PythonHandler :
Traceback (most recent call last):
[Thu Jan 12 20:11:24 2006] [error] [client 127.0.0.1] PythonHandler :=20
File "E:\\Python24\\Lib\\site-packages\\mod_python\\apache.py", line
287, in HandlerDispatch\n log=3Ddebug)
[Thu Jan 12 20:11:24 2006] [error] [client 127.0.0.1] PythonHandler :=20
File "E:\\Python24\\Lib\\site-packages\\mod_python\\apache.py", line
461, in import_module\n f, p, d =3D imp.find_module(parts[i], path)
[Thu Jan 12 20:11:24 2006] [error] [client 127.0.0.1] PythonHandler :
ImportError: No module named
[Thu Jan 12 20:11:25 2006] [notice] mod_python: (Re)importing module 'tests=
'
[Thu Jan 12 20:11:25 2006] [error] [client 127.0.0.1]
accesshandler_add_handler_to_empty_hl

Regards,
Nicolas
2006/1/12, Jim Gallacher <jpg@jgassociates.ca>:
> Graham Dumpleton wrote:
>
> This test was of some concern, as I thought the error log message might
> be inconsistent.
>
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D[vbcol=seagreen]
>
> Your error message actually makes more sense, as the last line in my
> error_log for this exception mentions PIL. I have *no* idea where that
> comes from but was hoping that the final line would at least always
> contain the string "contains no 'handler'.
>
> Here is my error_log:
>
> [Thu Jan 12 08:25:13 2006] [error] [client 127.0.0.1]
> req_add_empty_handler_string
> [Thu Jan 12 08:25:13 2006] [notice] mod_python: (Re)importing module ''
> [Thu Jan 12 08:25:13 2006] [error] [client 127.0.0.1] PythonHandler :
> Traceback (most recent call last):
> [Thu Jan 12 08:25:13 2006] [error] [client 127.0.0.1] PythonHandler :
> File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 291,
> in HandlerDispatch\n arg=3Dreq, silent=3Dhlist.silent)
> [Thu Jan 12 08:25:13 2006] [error] [client 127.0.0.1] PythonHandler :
> File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 531,
> in resolve_object\n raise AttributeError, s
> [Thu Jan 12 08:25:13 2006] [error] [client 127.0.0.1] PythonHandler :
> AttributeError: module
> '/usr/lib/python2.3/site-packages/PIL/__init__.pyc' contains no 'handler'
>
> Perhaps some other people could check out trunk, run the tests and let
> me know what shows up in the error_log after the
> req_add_empty_hander_string line.
>
> Thanks,
> Jim
>


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com