def head(self): title = self.title() return tags.head( ( title and tags.title(title) or None ), ( tags.script('', src = script) for script in self.scripts() ), ( tags.link('', href = style, rel = 'stylesheet', type = 'text/css') for style in self.styles() ), '' )
def process(self): """Process HTTP Requests for session mode XML snippets. The client uses discrete and frequent HttpRequests to aquire new messages and update their prompt and form.""" if debug_lag: print 'begin lag' from time import sleep sleep(debug_lag) print 'end lag' factory = self.channel.factory # get a session # the request's service is a SessionService session_service = self.service try: # if a session has been specified, attempt to aquire it if self.args.has_key('session'): key = self.args['session'][0] if key != '': session = session_service[key] if not session.modes: session = session_service.generateSession() else: session = session_service.generateSession() else: session = session_service.generateSession() # if the request specified a session which has expired or # or did not exist in the first place: except KeyError: # create a fresh session and notify the user that # their session has been recreated. Use a priority # message so that it gets sent before the welcome message. session = session_service.generateSession() session.priorityMessage( 'The <b>server</b> has begun a new <b>session</b> with you.' ) # record that this is the last time that the session # was accessed, so as to avoid expiration until the next # period of the factory's timeout. lastAccess = time() # if the client polls too frequently, give them a # service temporarily unavailable error if ( False and # todo: fix this and turn it back on session.lastAccess is not None and lastAccess - session.lastAccess < factory.sessionPollInterval ): self.setResponseCode(503) # Service Unavailable self.finish() print "Greedy session: %s, %s" % (session.key, self.getHost()) del session_service[session.key] return session.lastAccess = lastAccess try: if self.args.has_key('command'): # execute any commands that were sent in the URL or POST body. commands = self.args['command'] for command in commands: session.command(command) # run the optional user defined HttpRequest process handler session.tick() session.process(self) except SessionEndedError: session = session_service.generateSession() # send any queued cookies for key, value in session.cookies: self.addCookie(key, value) del session.cookies[:] # send an XML response including all new messages and updates # for the client mode, including the current session key # for future reference. self.setResponseCode(200) # OK self.setHeader('Content-type', 'text/xml') envelope = tags.envelope( tags.session(session.key), tags.pollInterval(factory.session_service.pollInterval), tags.prompt(session.prompt), tags.silent(session.silent and 'yes' or 'no'), tags.title(session.title), tags.messages( tags.message(message) for message in session.priorityMessages + session.messages ), tags.overlays( tags.overlay(overlay) for name, overlay in session.overlays.items() ), ) # this code helps observe that XML HTTP Request responses # are limited to 8KB in Firefox. # if len(envelope.xml) > 400: # print len(envelope.xml) # eventually, we'll have to trim messages down to discrete # patches to send to the user document xml = envelope.xml if len(xml) > 8 * 1024: print "Message longer than 8KB (%sKB)." % (len(xml) / 1024) envelope.writexml(self) # remove the messages that have been sent from their queues del session.messages[:] del session.priorityMessages[:] session.overlays.clear() self.finish()