| Jim Gallacher 2005-05-28, 5:45 pm |
| Nicolas Lehuen wrote:
> In the Java (i.e. servlets) world, when you want a session, you just
> ask for it on the HttpRequest object. If a session exist, you get it,
> if not, it is created. If you never ask for a session, then it is not
> created, this way a session is not mandatory for pages which do not
> require it.
>
> I really think the current way of building Session instances in
> mod_python is weird. First, because it opens a whole bunch of bugs due
> to multiple Session instantiation (i.e. deadlocks). Second, because
> the Session hosting infrastructure is up to the developer (which can
> choose between MemorySession, DBMSession or FileSession) whereas I
> think it should be up to the hoster.
>
> The session infrastructure should be defined in httpd.conf or a
> .htaccess file, with something like :
>
> PythonOption SessionInstantiation "FileSession(directory='/tmp/sessions')"
>
> Then, there would be a get_session() method on the request object,
> that would check if a session is already present and if not would
> execute the code defined in SessionInstantiation.
Here is my first attempt at get_session(). I've attached a patch if
anyone wants to give it a try.
Currently the dict from req.get_options() is passed to new_session(),
but I'm thinking this could be a dict passed to req.get_session() to
allow the user to over-ride some directives in httpd.conf.
src/requestobject.c
-------------------
static PyObject *req_get_session(requestobject *self)
{
PyObject *m;
PyObject *options;
if (!self->session) {
options = req_get_options(self, NULL);
m = PyImport_ImportModule("mod_python.SessionTest");
// Does PyObject_CallMethod do the Py_INCREF? That's my
understanding.
self->session = PyObject_CallMethod(m, "new_session", "(OO)",
self, options);
if (self->session == NULL)
return NULL;
}
Py_INCREF(self->session);
return self->session;
}
At this point mod_python.SessionTest.new_session is just a test harness
and always returns a FileSession instance.
lib/python/mod_python/SessionTest.py
------------------------------------
import Session
def new_session(req,options):
sess = Session.FileSession(req)
return sess
Comments are welcome. Just don't blame me if it eats your server. 
Regards,
Jim
|