[Twisted-web] Sessions with LivePage?

Matt Goodall matt at pollenation.net
Sat Jul 23 05:19:00 MDT 2005

Donovan Preston wrote:
> On Jul 22, 2005, at 12:38 AM, Chris (FeedTagger) wrote:
>> I'm having an issue with sessions and livepage - I can't seem to get
>> the two working together. If I replace livepage.LivePage with
>> rend.Page the session works as expected... I keep refreshing and the
>> same uid appears. Why is this not the case with LivePage and how can I
>> use sessions? Neither of the examples; chatola and liveanimal use
>> sessions with livepage.
> I don't know of any reason why there would be an issue using sessions 
> with livepage. In fact, my "Secret Project" uses them. Just as an 
> experiment, you might want to try wrapping the root resource in a 
> nevow.guard.SessionWrapper so that you get guard's modern sessions,  not
> the old crufty twisted-web one.

There are two problem:

1. If you expect to create a session in the middle of rendering a page
then you *must* set the buffered=True on the Page class.

    class MyPage(rend.Page):
        buffered = True
        docFactory = loader.xmlfile(...)
        def render_something(self, ctx, data):
            session = inevow.ISession(ctx)

Session are identified using a cookie. Cookies are set in the response
headers, but it's too late once any of the page content has been written.

Unfortunately, twisted.web has a bug that masks this problem. t.web
should raise an error really, but changing that now would probably break
applications (although they're arguably broken already).

2. LivePage breaks buffered anyway. LivePage overrides Page's renderHTTP
and the first thing it does is set some headers (no problem) and then
writes some content (agh!). Once that's happened, the buffered attribute
is irrelevant.

As it happens, LivePage also breaks addSlash but it's more likely that
addSlash the thing that's broken really ;-).

- Matt

    /  \__     Matt Goodall, Pollenation Internet Ltd
    \__/  \    w: http://www.pollenation.net
  __/  \__/    e: matt at pollenation.net
 /  \__/  \    t: +44 (0)113 2252500
 \__/  \__/
 /  \	       Any views expressed are my own and do not necessarily
 \__/          reflect the views of my employer.

More information about the Twisted-web mailing list