def header(self, headers=None, response=None): '''Put up the appropriate header. ''' if headers is None: headers = {'Content-Type':'text/html; charset=utf-8'} if response is None: response = self.response_code # update with additional info headers.update(self.additional_headers) if headers.get('Content-Type', 'text/html') == 'text/html': headers['Content-Type'] = 'text/html; charset=utf-8' headers = headers.items() for ((path, name), (value, expire)) in self.add_cookies.items(): cookie = "%s=%s; Path=%s;"%(name, value, path) if expire is not None: cookie += " expires=%s;"%Cookie._getdate(expire) headers.append(('Set-Cookie', cookie)) self._socket_op(self.request.start_response, headers, response) self.headers_done = 1 if self.debug: self.headers_sent = headers
def getSession(self, create=1, path=None, domain=None, secure=None): ''' returns the session associated with this request. If create, will create a new session if there is none. ''' # permit sessions to be turned off by leaving the session store null, # but raise an exception if someone tries to access a session if Configuration.SessionStore is None: raise SessionError, "no session store enabled" try: sess= self.__userSession except AttributeError: pass else: if sess: return sess DEBUG(SESSIONHANDLER, "session is None") # session is None id=self.getSessionID(create) if not id: DEBUG(SESSIONHANDLER, "id is false for create: %s" % create) return None sess=self.__userSession=Session(id) sesskey=Configuration.SessionIDKey # test session - is it too old? if sess.age() >= Configuration.SessionTimeout: DEBUG(SESSIONHANDLER, "session is too old") sess.delete() del self.__userSession if self.requestCookie.has_key(sesskey): self.responseCookie[sesskey]="" self.responseCookie[sesskey]['expires']=Cookie._getdate(-10000000) del self.__sessionID id=self.getSessionID(create) if not id: return None sess=self.__userSession=Session(id) if (not self.requestCookie.has_key(sesskey)) or \ [x for x in (path, domain, secure) if x is not None]: self.responseCookie[sesskey]=id morsel=self.responseCookie[sesskey] if path is not None: morsel['path']=path if domain is not None: morsel['domain']=domain if secure is not None: morsel['secure']=secure return self.__userSession
def getSession(self, create=0, path=None, domain=None, secure=None): ''' returns the session associated with this request. If create, will create a new session if there is none. ''' # permit sessions to be turned off by leaving the session store null, # but raise an exception if someone tries to access a session if Configuration.SessionStore is None: raise SessionError, "no session store enabled" try: sess = self.__userSession except AttributeError: pass else: if sess: return sess DEBUG(SESSIONHANDLER, "session is None") # session is None id = self.getSessionID(create) if not id: DEBUG(SESSIONHANDLER, "id is false for create: %s" % create) return None sess = self.__userSession = Session(id) sesskey = Configuration.SessionIDKey # test session - is it too old? if sess.age() >= Configuration.SessionTimeout: DEBUG(SESSIONHANDLER, "session is too old") sess.delete() del self.__userSession if self.requestCookie.has_key(sesskey): self.responseCookie[sesskey] = "" self.responseCookie[sesskey]['expires'] = Cookie._getdate( -10000000) del self.__sessionID id = self.getSessionID(create) if not id: return None sess = self.__userSession = Session(id) if (not self.requestCookie.has_key(sesskey)) or \ [x for x in (path, domain, secure) if x is not None]: self.responseCookie[sesskey] = id morsel = self.responseCookie[sesskey] if path is not None: morsel['path'] = path if domain is not None: morsel['domain'] = domain if secure is not None: morsel['secure'] = secure return self.__userSession
def _send_head(self): if self._new_session_uri!=None: import Cookie cookie = Cookie.SmartCookie() TTL = 3600*24*10000 # time to live in seconds (a long time) cookie['Redfoot_session'] = self._new_session_uri cookie['Redfoot_session']['path'] = "/" cookie['Redfoot_session']['Version'] = "1" cookie['Redfoot_session']['expires'] = Cookie._getdate(TTL) output = cookie.output() # Warning: Assuming there is only one header in output (name, value) = output.split(": ", 1) self.set_header(name, value)
def _send_head(self): self.write("%s %s %s\r\n" % ("HTTP/1.1", "200", "OK")) for key in self._header.keys(): self.write("%s: %s\r\n" % (key, self._header[key])) if self._new_session_uri!=None: import Cookie cookie = Cookie.SmartCookie() TTL = 3600*24*10000 # time to live in seconds (a long time) cookie['Redfoot_session'] = self._new_session_uri cookie['Redfoot_session']['path'] = "/" cookie['Redfoot_session']['Version'] = "1" cookie['Redfoot_session']['expires'] = Cookie._getdate(TTL) self.write(cookie.output()) self.write("\r\n")
def handle(self): """Make us really anonymous - nuke the cookie too.""" # log us out self.client.make_user_anonymous() # construct the logout cookie now = Cookie._getdate() self.client.additional_headers['Set-Cookie'] = \ '%s=deleted; Max-Age=0; expires=%s; Path=%s;' % ( self.client.cookie_name, now, self.client.cookie_path) # Let the user know what's going on self.client.ok_message.append(self._('You are logged out')) # reset client context to render tracker home page # instead of last viewed page (may be inaccessibe for anonymous) self.client.classname = None self.client.nodeid = None self.client.template = None
def removeSession(self): ''' clears and removes any active session. ''' DEBUG(SESSIONHANDLER, "in removeSession()") self.getSession(0) try: sess = self.__userSession except AttributeError: pass else: if sess: sess.delete() del self.__userSession self.__sessionID = None sesskey = Configuration.SessionIDKey if self.requestCookie.has_key(sesskey): self.responseCookie[sesskey] = "" self.responseCookie[sesskey]['expires'] = Cookie._getdate(-10000000)
def removeSession(self): ''' clears and removes any active session. ''' DEBUG(SESSIONHANDLER, "in removeSession()") self.getSession(0) try: sess=self.__userSession except AttributeError: pass else: if sess: sess.delete() del self.__userSession self.__sessionID=None sesskey=Configuration.SessionIDKey if self.requestCookie.has_key(sesskey): self.responseCookie[sesskey]="" self.responseCookie[sesskey]['expires']=Cookie._getdate(-10000000)
def cookie(self): c = Cookie.SimpleCookie() # XXX There is is a bug in the base class implementation fixed here c[self.cookie_name] = self.cookie_value().strip().replace('\n', '') for k, v in self.cookie_params.items(): if k not in ['path', 'expires']: c[self.cookie_name][k] = v # path and secure are handled differently to keep it consistent with # the base class API if not self.cookie_params.has_key('path'): c[self.cookie_name]['path'] = '/' else: c[self.cookie_name]['path'] = self.cookie_params['path'] if self.cookie_params.has_key('expires'): time = Cookie._getdate(float(self.cookie_params['expires'])) log.info(time) c[self.cookie_name]['expires'] = time if self.secure: c[self.cookie_name]['secure'] = 'true' return c
def get_static_file(path, dir=None, max_age=10): if not path: raise Http404NotFound if dir is None: dir = options.STATIC_DIR if dir is None: if pony.MAIN_DIR is None: raise Http404NotFound dir = os.path.join(pony.MAIN_DIR, 'static') for component in path: if not path_re.match(component): raise Http404NotFound fname = os.path.join(dir, *path) if not os.path.isfile(fname): if path == [ 'favicon.ico' ]: return get_static_file(path, pony_static_dir, 30*60) raise Http404NotFound method = local.request.method if method not in ('GET', 'HEAD'): raise Http405MethodNotAllowed ext = os.path.splitext(path[-1])[1] headers = local.response.headers headers['Content-Type'] = httputils.guess_type(ext) if max_age <= 60: headers['Expires'] = '0' else: headers['Expires'] = Cookie._getdate(max_age) headers['Cache-Control'] = 'max-age=%d' % max_age headers['Content-Length'] = str(os.path.getsize(fname)) if method == 'HEAD': return '' return file(fname, 'rb')
def _add_usertracking_cookie(conn, sessionDict): if Configuration.usertrackingOn: cookiename=Configuration.usertrackingCookieName if not _verify_cookie(conn, cookiename): f=Configuration.usertrackingGenUIDFunc if f is None: conn.responseCookie[cookiename]=uuid() else: conn.responseCookie[cookiename]=f(conn) morsel=conn.responseCookie[cookiename] for c, a in _config_attrs: if a=='expires': # special case if not Configuration.usertrackingExpiresAbsolute: v=getattr(Configuration, c) if v is not None: morsel[a]=Cookie._getdate(v) continue v=getattr(Configuration, c) if v is not None: morsel[a]=v DEBUG(USERTRACKING, str(morsel)) DEBUG(USERTRACKING, str(conn.responseCookie[cookiename]))
def get_static_file(path, dir=None, max_age=10): if not path: raise Http404NotFound if dir is None: dir = options.STATIC_DIR if dir is None: if pony.MAIN_DIR is None: raise Http404NotFound dir = os.path.join(pony.MAIN_DIR, 'static') for component in path: if not path_re.match(component): raise Http404NotFound fname = os.path.join(dir, *path) if not os.path.isfile(fname): if path == ['favicon.ico']: return get_static_file(path, pony_static_dir, 30 * 60) raise Http404NotFound method = local.request.method if method not in ('GET', 'HEAD'): raise Http405MethodNotAllowed ext = os.path.splitext(path[-1])[1] headers = local.response.headers headers['Content-Type'] = httputils.guess_type(ext) if max_age <= 60: headers['Expires'] = '0' else: headers['Expires'] = Cookie._getdate(max_age) headers['Cache-Control'] = 'max-age=%d' % max_age headers['Content-Length'] = str(os.path.getsize(fname)) if method == 'HEAD': return '' return file(fname, 'rb')