def core(self, value, path):
     """Do it."""
     value = String.core(self, value, path)
     scheme_source, authority_source, path, query, fragment = urlsplit(value)
     scheme = self.lower_string(scheme_source)
     authority = self.lower_string(authority_source)
     if self.inlist(scheme, self.cut_schemes) and self.inlist(authority, self.cut_sites) and (
             scheme is not None or authority is not None):
         scheme = None
         authority = None
         if not path and not query and not fragment:
             path = '/'
     if not self.inlist(scheme, self.allow_schemes) or not self.inlist(authority, self.allow_sites):
         raise IncorrectException(self, value)
     if scheme is None:
         check_scheme = 'http'
     else:
         check_scheme = scheme
     if self.inlist(scheme, self.local_schemes) and self.inlist(authority, self.local_sites):
         if self.verify_local is True:
             if not path and query is not None:
                 raise IncorrectException(self, value)
             if path and not local_check(path, query):
                 raise IncorrectException(self, value)
         elif self.verify_local is not False:
             check = urljoin(check_scheme, self.verify_local, path, query, fragment)
             if not remote_check(check):
                 raise IncorrectException(self, value)
     elif self.inlist(scheme, self.verify_schemes) and self.inlist(authority, self.verify_sites):
         if authority is not None:
             check = urljoin(check_scheme, authority, path, query, fragment)
             if not remote_check(check):
                 raise IncorrectException(self, value)
     value = urljoin(scheme, authority, path, query, fragment)
     return value
Esempio n. 2
0
 def url(self, value):
     """
     Return valid url or empty string.
     """
     resolver = RegexURLResolver(r'^/', settings.ROOT_URLCONF)
     checked = []
     try:
         while value not in checked:
             checked.append(value)
             if MACRO_RE.match(value):
                 if MACRO_RE.sub(MACRO_REPL, value):
                     raise ReplaceDone
                 else:
                     raise ReplaceFailed
             scheme, authority, path, query, fragment = urlsplit(value)
             if not scheme and not authority and not path:
                 raise ReplaceDone
             if (scheme and scheme.lower() not in self.check_schemes) or\
                 (authority and authority.lower() not in self.check_sites):
                 raise ReplaceDone
             try:
                 callback, callback_args, callback_kwargs = resolver.resolve(path)
             except Exception:
                 raise ReplaceFailed
             try:
                 try:
                     setting = self.views[callback]
                 except KeyError:
                     if self.check_unregistered:
                         data_from_view(path, query)
                     raise ReplaceDone
                 if setting.get('disable', False):
                     raise ReplaceFailed
                 obj = object_from_view(path, query, setting['context'])
                 path = macro(obj)
                 if setting.get('remove_query', False):
                     query = None
                 value = urljoin(None, None, path, query, fragment)
                 raise ReplaceDone
             except ReplaceRedirect, redirect:
                 value = redirect.target
                 scheme, authority, path, query, fragment = urlsplit(value)
                 if scheme == 'http' and authority == 'testserver':
                     value = urljoin(None, None, path, query, fragment)
         else: