def open_session(self, app, request): s = self.get_signing_serializer(app) if s is None: return None #读取val这个val就是session的值,session_cookie_name这个就是配置文件的session_cookie_name val = request.cookies.get(app.session_cookie_name) if not val: #如果是空的 return self.session_class() #则返回空字典 max_age = total_seconds(app.permanent_session_lifetime) try: #在内部加载,解密,反序列化 data = s.loads(val, max_age=max_age) #又实例化对象 return self.session_class(data) except BadSignature: return self.session_class()
def finalize_request(self, rv, from_error_handler=False): response = self.make_response(rv) try: #执行process_response response = self.process_response(response) request_finished.send(self, response=response) except Exception: if not from_error_handler: raise self.logger.exception('Request finalizing failed with an ' 'error while handling an error') return response
11、执行def process_response(self, response):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def process_response(self, response): ctx = _request_ctx_stack.top bp = ctx.request.blueprint funcs = ctx._after_request_functions if bp is not None and bp in self.after_request_funcs: funcs = chain(funcs, reversed(self.after_request_funcs[bp])) if None in self.after_request_funcs: funcs = chain(funcs, reversed(self.after_request_funcs[None])) for handler in funcs: response = handler(response) if not self.session_interface.is_null_session(ctx.session): #这里执行了save_session self.session_interface.save_session(self, ctx.session, response) return response
# If the session is modified to be empty, remove the cookie. # If the session is empty, return without setting the cookie. if not session: if session.modified: response.delete_cookie( app.session_cookie_name, domain=domain, path=path )
return
# Add a "Vary: Cookie" header if the session was accessed at all. if session.accessed: response.vary.add('Cookie')
if not self.should_set_cookie(app, session): return