def get_root_url(self): """ Returns the root URL of the application (based on environmental variables). All the action module path elements and action names are removed. E.g.: The app is installed in http://127.0.0.1/app/ and it is currently processing http://127.0.0.1/app/user/login then root URL is still http://127.0.0.1/app/ Please note that KonText always normalizes PATH_INFO environment variable to '/' (see public/app.py). """ module, _ = self.environ.get('PATH_INFO').rsplit('/', 1) module = '%s/' % module if module.endswith(self.get_mapping_url_prefix()): action_module_path = module[:-len(self.get_mapping_url_prefix())] else: action_module_path = '' if len( action_module_path ) > 0: # => app is not installed in root path (e.g. http://127.0.0.1/app/) action_module_path = action_module_path[1:] if 'HTTP_X_FORWARDED_PROTO' in self.environ: protocol = self.environ['HTTP_X_FORWARDED_PROTO'] elif 'HTTP_X_FORWARDED_PROTOCOL' in self.environ: protocol = self.environ['HTTP_X_FORWARDED_PROTOCOL'] else: protocol = self.environ['wsgi.url_scheme'] url_items = ('%s://%s' % (protocol, settings.get_str('global', 'http_host', self.environ.get('HTTP_HOST'))), settings.get_str('global', 'action_path_prefix', ''), action_module_path) return '/'.join( filter(lambda x: bool(x), map(lambda x: x.strip('/'), url_items))) + '/'
def get_root_url(self): """ Returns the root URL of the application (based on environmental variables). All the action module path elements and action names are removed. E.g.: The app is installed in http://127.0.0.1/app/ and it is currently processing http://127.0.0.1/app/user/login then root URL is still http://127.0.0.1/app/ Please note that KonText always normalizes PATH_INFO environment variable to '/' (see public/app.py). """ module, _ = self.environ.get('PATH_INFO').rsplit('/', 1) module = '%s/' % module if module.endswith(self.get_mapping_url_prefix()): action_module_path = module[:-len(self.get_mapping_url_prefix())] else: action_module_path = '' if len(action_module_path) > 0: # => app is not installed in root path (e.g. http://127.0.0.1/app/) action_module_path = action_module_path[1:] if 'HTTP_X_FORWARDED_PROTO' in self.environ: protocol = self.environ['HTTP_X_FORWARDED_PROTO'] elif 'HTTP_X_FORWARDED_PROTOCOL' in self.environ: protocol = self.environ['HTTP_X_FORWARDED_PROTOCOL'] else: protocol = self.environ['wsgi.url_scheme'] url_items = ('%s://%s' % (protocol, settings.get_str('global', 'http_host', self.environ.get('HTTP_HOST'))), settings.get_str('global', 'action_path_prefix', ''), action_module_path) return '/'.join(filter(lambda x: bool(x), map(lambda x: x.strip('/'), url_items))) + '/'
def get_root_url(self) -> str: """ Returns the root URL of the application (based on environmental variables). All the action module path elements and action names are removed. E.g.: The app is installed in http://127.0.0.1/app/ and it is currently processing http://127.0.0.1/app/user/login then root URL is still http://127.0.0.1/app/ Please note that KonText always normalizes PATH_INFO environment variable to '/' (see public/app.py). """ module, _ = self._environ.get('PATH_INFO', '').rsplit('/', 1) module = '%s/' % module if module.endswith(self._mapping_url_prefix): action_module_path = module[:-len(self._mapping_url_prefix)] else: action_module_path = '' if len( action_module_path ) > 0: # => app is not installed in root path (e.g. http://127.0.0.1/app/) action_module_path = action_module_path[1:] url_items = ('{}://{}'.format( self._http_protocol, settings.get_str('global', 'http_host', self._environ.get('HTTP_HOST'))), settings.get_str('global', 'action_path_prefix', ''), action_module_path) return '/'.join( [x for x in [x.strip('/') for x in url_items] if bool(x)]) + '/'
def __call__(self, environ, start_response): ui_lang = self.get_lang(environ) translation.activate(ui_lang) environ['REQUEST_URI'] = wsgiref.util.request_uri( environ) # TODO remove? app_url_prefix = settings.get_str('global', 'action_path_prefix', '') if app_url_prefix and environ['PATH_INFO'].startswith(app_url_prefix): environ['PATH_INFO'] = environ['PATH_INFO'][len(app_url_prefix):] sessions = plugins.runtime.SESSIONS.instance request = JSONRequest(environ) sid = request.cookies.get(sessions.get_cookie_name()) if sid is None: request.session = sessions.new() else: request.session = sessions.get(sid) sid_is_valid = True if environ['PATH_INFO'] in ('/', ''): url = environ['REQUEST_URI'].split('?')[0] if not url.endswith('/'): url += '/' status = '303 See Other' headers = [('Location', f'{self._root_url(environ)}query')] body = '' # old-style (CGI version) URLs are redirected to new ones elif '/run.cgi/' in environ['REQUEST_URI']: status = '301 Moved Permanently' headers = [('Location', environ['REQUEST_URI'].replace('/run.cgi/', '/'))] body = '' else: app = self.create_controller(environ['PATH_INFO'], request=request, ui_lang=ui_lang) status, headers, sid_is_valid, body = app.run() response = Response(response=body, status=status, headers=headers) if not sid_is_valid: curr_data = dict(request.session) request.session = sessions.new() request.session.update(curr_data) request.session.modified = True if request.session.should_save: sessions.save(request.session) cookie_path = settings.get_str('global', 'cookie_path_prefix', '/') cookies_same_site = settings.get('global', 'cookies_same_site', None) response.set_cookie(sessions.get_cookie_name(), request.session.sid, path=cookie_path, secure=cookies_same_site is not None, samesite=cookies_same_site) return response(environ, start_response)
def __call__(self, environ, start_response): ui_lang = self.get_lang(environ) translation.activate(ui_lang) environ['REQUEST_URI'] = wsgiref.util.request_uri(environ) # TODO remove? app_url_prefix = settings.get_str('global', 'action_path_prefix', '') if app_url_prefix and environ['PATH_INFO'].startswith(app_url_prefix): environ['PATH_INFO'] = environ['PATH_INFO'][len(app_url_prefix):] sessions = plugins.runtime.SESSIONS.instance request = Request(environ) sid = request.cookies.get(sessions.get_cookie_name()) if sid is None: request.session = sessions.new() else: request.session = sessions.get(sid) sid_is_valid = True if environ['PATH_INFO'] in ('/', ''): url = environ['REQUEST_URI'].split('?')[0] if not url.endswith('/'): url += '/' status = '303 See Other' headers = [('Location', '%sfirst_form' % url)] body = '' # old-style (CGI version) URLs are redirected to new ones elif '/run.cgi/' in environ['REQUEST_URI']: status = '301 Moved Permanently' headers = [('Location', environ['REQUEST_URI'].replace('/run.cgi/', '/'))] body = '' else: controller_class = self.load_controller_class(environ['PATH_INFO']) app = controller_class(request=request, ui_lang=ui_lang) status, headers, sid_is_valid, body = app.run() response = Response(response=body, status=status, headers=headers) if not sid_is_valid: curr_data = dict(request.session) request.session = sessions.new() request.session.update(curr_data) request.session.modified = True if request.session.should_save: sessions.save(request.session) cookie_path = settings.get_str('global', 'cookie_path_prefix', '/') response.set_cookie(sessions.get_cookie_name(), request.session.sid, path=cookie_path) return response(environ, start_response)
def switch_language(self, request): if plugins.runtime.GETLANG.exists: pass # TODO should the plug-in do something here? else: path_prefix = settings.get_str('global', 'action_path_prefix') self._new_cookies['kontext_ui_lang'] = request.form.get('language') self._new_cookies['kontext_ui_lang']['path'] = path_prefix if path_prefix else '/' self._new_cookies['kontext_ui_lang']['expires'] = time.strftime('%a, %d %b %Y %T GMT', time.gmtime(time.time() + 180 * 24 * 3600)) self.redirect(request.form.get('continue')) return {}
def switch_language(self, request): if plugins.runtime.GETLANG.exists: pass # TODO should the plug-in do something here? else: path_prefix = settings.get_str('global', 'action_path_prefix') self._new_cookies['kontext_ui_lang'] = request.form.get('language') self._new_cookies['kontext_ui_lang']['path'] = path_prefix if path_prefix else '/' self._new_cookies['kontext_ui_lang']['expires'] = time.strftime('%a, %d %b %Y %T GMT', time.gmtime(time.time() + 180 * 24 * 3600)) self.redirect( request.environ.get('HTTP_REFERER', self.create_url('first_form', dict(corpname=self.args.corpname)))) return {}
def switch_language(self, request): path_prefix = settings.get_str('global', 'action_path_prefix') self._response.set_cookie( 'kontext_ui_lang', request.form.get('language'), path=path_prefix if path_prefix else '/', expires=time.strftime('%a, %d %b %Y %T GMT', time.gmtime(time.time() + 180 * 24 * 3600))) self.redirect( request.environ.get( 'HTTP_REFERER', self.create_url('query', dict(corpname=self.args.corpname)))) return None
def switch_language(self, request): if plugins.runtime.GETLANG.exists: pass # TODO should the plug-in do something here? else: path_prefix = settings.get_str('global', 'action_path_prefix') self._new_cookies['kontext_ui_lang'] = request.form.get('language') self._new_cookies['kontext_ui_lang'][ 'path'] = path_prefix if path_prefix else '/' self._new_cookies['kontext_ui_lang']['expires'] = time.strftime( '%a, %d %b %Y %T GMT', time.gmtime(time.time() + 180 * 24 * 3600)) self.redirect( request.environ.get( 'HTTP_REFERER', self.create_url('first_form', dict(corpname=self.args.corpname)))) return {}
def test_get_str(self): v = settings.get_str('global', 'emtpy') self.assertEqual(v, '')
def _root_url(environ): protocol = get_protocol(environ) host = settings.get_str('global', 'http_host', environ.get('HTTP_HOST')) app_url_prefix = settings.get_str('global', 'action_path_prefix', '/') return f'{protocol}://{host}{app_url_prefix}/'