def get_cookie_domain(self, app): """Returns the domain that should be set for the session cookie. Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise falls back to detecting the domain based on ``SERVER_NAME``. Once detected (or if not set at all), ``SESSION_COOKIE_DOMAIN`` is updated to avoid re-running the logic. """ rv = app.config['SESSION_COOKIE_DOMAIN'] # set explicitly, or cached from SERVER_NAME detection # if False, return None if rv is not None: return rv if rv else None rv = app.config['SERVER_NAME'] # server name not set, cache False to return none next time if not rv: app.config['SESSION_COOKIE_DOMAIN'] = False return None # chop off the port which is usually not supported by browsers # remove any leading '.' since we'll add that later rv = rv.rsplit(':', 1)[0].lstrip('.') if '.' not in rv: # Chrome doesn't allow names without a '.' # this should only come up with localhost # hack around this by not setting the name, and show a warning warnings.warn( '"{rv}" is not a valid cookie domain, it must contain a ".".' ' Add an entry to your hosts file, for example' ' "{rv}.localdomain", and use that instead.'.format(rv=rv) ) app.config['SESSION_COOKIE_DOMAIN'] = False return None ip = is_ip(rv) if ip: warnings.warn( 'The session cookie domain is an IP address. This may not work' ' as intended in some browsers. Add an entry to your hosts' ' file, for example "localhost.localdomain", and use that' ' instead.' ) # if this is not an ip and app is mounted at the root, allow subdomain # matching by adding a '.' prefix if self.get_cookie_path(app) == '/' and not ip: rv = '.' + rv app.config['SESSION_COOKIE_DOMAIN'] = rv return rv
def get_cookie_domain(self, app): """Returns the domain that should be set for the session cookie. Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise falls back to detecting the domain based on ``SERVER_NAME``. Once detected (or if not set at all), ``SESSION_COOKIE_DOMAIN`` is updated to avoid re-running the logic. """ rv = app.config['SESSION_COOKIE_DOMAIN'] # set explicitly, or cached from SERVER_NAME detection # if False, return None if rv is not None: return rv if rv else None rv = app.config['SERVER_NAME'] # database name not set, cache False to return none next time if not rv: app.config['SESSION_COOKIE_DOMAIN'] = False return None # chop off the port which is usually not supported by browsers # remove any leading '.' since we'll add that later rv = rv.rsplit(':', 1)[0].lstrip('.') if '.' not in rv: # Chrome doesn't allow names without a '.' # this should only come up with localhost # hack around this by not setting the name, and show a warning warnings.warn( '"{rv}" is not a valid cookie domain, it must contain a ".".' ' Add an entry to your hosts file, for example' ' "{rv}.localdomain", and use that instead.'.format(rv=rv) ) app.config['SESSION_COOKIE_DOMAIN'] = False return None ip = is_ip(rv) if ip: warnings.warn( 'The session cookie domain is an IP address. This may not work' ' as intended in some browsers. Add an entry to your hosts' ' file, for example "localhost.localdomain", and use that' ' instead.' ) # if this is not an ip and app is mounted at the root, allow subdomain # matching by adding a '.' prefix if self.get_cookie_path(app) == '/' and not ip: rv = '.' + rv app.config['SESSION_COOKIE_DOMAIN'] = rv return rv
def get_cookie_domain(self, app): """ 获取cookie应该被设置的域名 获取优先级:app配置SESSION_COOKIE_DOMAIN->SERVER_NAME 一旦侦测到SESSION_COOKIE_DOMAIN就会被更新,以免重复运行 :param app: :return: """ rv = app.config['SESSION_COOKIE_DOMAIN'] # set explicitly, or cached from SERVER_NAME detection # if False, return None if rv is not None: # 这样写要对照下面的SERVER_NAME # 防止重复运行 return rv if rv else None rv = app.config['SERVER_NAME'] # server name not set, cache False to return none next time if not rv: app.config['SESSION_COOKIE_DOMAIN'] = False return None # 移除端口号:浏览器通常不支持 # 一处开头的.,因为稍后会加上 rv = rv.rsplit(':', 1)[0].lstrip('.') if '.' not in rv: # Chrome浏览器不允许没有. # this should only come up with localhost # hack around this by not setting the name, and show a warning warnings.warn( '"{rv}" is not a valid cookie domain, it must contain a ".".' ' Add an entry to your hosts file, for example' ' "{rv}.localdomain", and use that instead.'.format(rv=rv)) app.config['SESSION_COOKIE_DOMAIN'] = False return None ip = is_ip(rv) if ip: warnings.warn( 'The session cookie domain is an IP address. This may not work' ' as intended in some browsers. Add an entry to your hosts' ' file, for example "localhost.localdomain", and use that' ' instead.') # if this is not an ip and app is mounted at the root, allow subdomain # matching by adding a '.' prefix if self.get_cookie_path(app) == '/' and not ip: rv = '.' + rv app.config['SESSION_COOKIE_DOMAIN'] = rv return rv