Here are the examples of the python api django.utils.six.moves.urllib.parse.urlparse taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
68 Examples
3
Example 1
def purge(self, url):
url_parsed = urlparse(url)
distribution_id = None
if isinstance(self.cloudfront_distribution_id, dict):
host = url_parsed.hostname
if host in self.cloudfront_distribution_id:
distribution_id = self.cloudfront_distribution_id.get(host)
else:
logger.info(
"Couldn't purge '%s' from CloudFront. Hostname '%s' not found in the DISTRIBUTION_ID mapping",
url, host)
else:
distribution_id = self.cloudfront_distribution_id
if distribution_id:
path = url_parsed.path
self._create_invalidation(distribution_id, path)
3
Example 2
def __init__(self, location, *args, **kwargs):
super(LocationHeaderMixin, self).__init__(*args, **kwargs)
parsed = urlparse(location)
if parsed.scheme and parsed.scheme not in self.allowed_schemes:
raise SuspiciousOperation(
"Unsafe redirect to URL with protocol '%s'" % parsed.scheme
)
self['Location'] = iri_to_uri(location)
3
Example 3
Project: PyClassLessons Source File: feedgenerator.py
def get_tag_uri(url, date):
"""
Creates a TagURI.
See http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
"""
bits = urlparse(url)
d = ''
if date is not None:
d = ',%s' % datetime_safe.new_datetime(date).strftime('%Y-%m-%d')
return 'tag:%s%s:%s/%s' % (bits.hostname, d, bits.path, bits.fragment)
3
Example 4
def __init__(self, application, base_dir=None):
self.application = application
if base_dir:
self.base_dir = base_dir
else:
self.base_dir = self.get_base_dir()
self.base_url = urlparse(self.get_base_url())
super(StaticFilesHandler, self).__init__()
3
Example 5
def get_base_url(request=None):
base_url = getattr(settings, 'WAGTAILAPI_BASE_URL', request.site.root_url if request else None)
if base_url:
# We only want the scheme and netloc
base_url_parsed = urlparse(base_url)
return base_url_parsed.scheme + '://' + base_url_parsed.netloc
3
Example 6
Project: django-sudo Source File: utils.py
def _is_safe_url(url, host):
# Chrome considers any URL with more than two slashes to be absolute, but
# urlparse is not so flexible. Treat any url with three slashes as unsafe.
if url.startswith('///'):
return False
url_info = urlparse(url)
# Forbid URLs like http:///example.com - with a scheme, but without a hostname.
# In that URL, example.com is not the hostname but, a path component. However,
# Chrome will still consider example.com to be the hostname, so we must not
# allow this syntax.
if not url_info.netloc and url_info.scheme:
return False
# Forbid URLs that start with control characters. Some browsers (like
# Chrome) ignore quite a few control characters at the start of a
# URL and might consider the URL as scheme relative.
if unicodedata.category(url[0])[0] == 'C':
return False
return ((not url_info.netloc or url_info.netloc == host) and
(not url_info.scheme or url_info.scheme in ('http', 'https')))
3
Example 7
Project: hue Source File: client.py
def head(self, path, data={}, **extra):
"Construct a HEAD request."
parsed = urlparse(path)
query_string = urlencode(data, doseq=True) or force_str(parsed[4])
if six.PY3:
query_string = query_string.encode('utf-8').decode('iso-8859-1')
r = {
'PATH_INFO': self._get_path(parsed),
'QUERY_STRING': query_string,
'REQUEST_METHOD': str('HEAD'),
}
r.update(extra)
return self.request(**r)
3
Example 8
def same_origin(url1, url2):
"""
Checks if two URLs are 'same-origin'
"""
PROTOCOL_TO_PORT = {
'http': 80,
'https': 443,
}
p1, p2 = urlparse(url1), urlparse(url2)
try:
o1 = (p1.scheme, p1.hostname, p1.port or PROTOCOL_TO_PORT[p1.scheme])
o2 = (p2.scheme, p2.hostname, p2.port or PROTOCOL_TO_PORT[p2.scheme])
return o1 == o2
except (ValueError, KeyError):
return False
3
Example 9
Project: django-cms Source File: urlutils.py
def is_media_request(request):
"""
Check if a request is a media request.
"""
parsed_media_url = urlparse(settings.MEDIA_URL)
if request.path_info.startswith(parsed_media_url.path):
if parsed_media_url.netloc:
if request.get_host() == parsed_media_url.netloc:
return True
else:
return True
return False
3
Example 10
Project: PyClassLessons Source File: views.py
def redirect_to_login(next, login_url=None,
redirect_field_name=REDIRECT_FIELD_NAME):
"""
Redirects the user to the login page, passing the given 'next' page
"""
resolved_url = resolve_url(login_url or settings.LOGIN_URL)
login_url_parts = list(urlparse(resolved_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urlunparse(login_url_parts))
3
Example 11
def _is_safe_url(url, host):
# Chrome considers any URL with more than two slashes to be absolute, but
# urlparse is not so flexible. Treat any url with three slashes as unsafe.
if url.startswith('///'):
return False
url_info = urlparse(url)
# Forbid URLs like http:///example.com - with a scheme, but without a hostname.
# In that URL, example.com is not the hostname but, a path component. However,
# Chrome will still consider example.com to be the hostname, so we must not
# allow this syntax.
if not url_info.netloc and url_info.scheme:
return False
# Forbid URLs that start with control characters. Some browsers (like
# Chrome) ignore quite a few control characters at the start of a
# URL and might consider the URL as scheme relative.
if unicodedata.category(url[0])[0] == 'C':
return False
return ((not url_info.netloc or url_info.netloc == host) and
(not url_info.scheme or url_info.scheme in ['http', 'https']))
3
Example 12
def __init__(self, customer, stored_request):
super(EmulateHttpRequest, self).__init__()
parsedurl = urlparse(stored_request.get('absolute_base_uri'))
self.path = self.path_info = parsedurl.path
self.environ = {}
self.META['PATH_INFO'] = parsedurl.path
self.META['SCRIPT_NAME'] = ''
self.META['HTTP_HOST'] = parsedurl.netloc
self.META['HTTP_X_FORWARDED_PROTO'] = parsedurl.scheme
self.META['QUERY_STRING'] = parsedurl.query
self.META['HTTP_USER_AGENT'] = stored_request.get('user_agent')
self.META['REMOTE_ADDR'] = stored_request.get('remote_ip')
self.method = 'GET'
self.LANGUAGE_CODE = self.COOKIES['django_language'] = stored_request.get('language')
self.customer = customer
self.user = customer.is_anonymous() and AnonymousUser or customer.user
self.current_page = None
3
Example 13
Project: hue Source File: client.py
def get(self, path, data={}, **extra):
"Construct a GET request."
parsed = urlparse(path)
query_string = urlencode(data, doseq=True) or force_str(parsed[4])
if six.PY3:
query_string = query_string.encode('utf-8').decode('iso-8859-1')
r = {
'PATH_INFO': self._get_path(parsed),
'QUERY_STRING': query_string,
'REQUEST_METHOD': str('GET'),
}
r.update(extra)
return self.request(**r)
3
Example 14
Project: drf-json-api Source File: renderers.py
def url_to_template(self, view_name, request, template_name):
resolver = urlresolvers.get_resolver(None)
info = resolver.reverse_dict[view_name]
path_template = info[0][0][0]
# FIXME: what happens when URL has more than one dynamic values?
# e.g. nested relations: manufacturer/%(id)s/cars/%(card_id)s
path = path_template % {info[0][0][1][0]: '{%s}' % template_name}
parsed_url = urlparse(request.build_absolute_uri())
return urlunparse(
[parsed_url.scheme, parsed_url.netloc, path, '', '', '']
)
3
Example 15
Project: reviewboard Source File: hg.py
@classmethod
def check_repository(cls, path, username=None, password=None,
local_site_name=None):
"""Performs checks on a repository to test its validity."""
result = urlparse(path)
if result.scheme == 'ssh':
raise SCMError('Mercurial over SSH is not supported.')
super(HgTool, cls).check_repository(path, username, password,
local_site_name)
# Create a client. This will fail if the repository doesn't exist.
if result.scheme in ('http', 'https'):
HgWebClient(path, username, password)
else:
HgClient(path, local_site_name)
3
Example 16
def assertURLEqual(self, url, expected, parse_qs=False):
"""
Given two URLs, make sure all their components (the ones given by
urlparse) are equal, only comparing components that are present in both
URLs.
If `parse_qs` is True, then the querystrings are parsed with QueryDict.
This is useful if you don't want the order of parameters to matter.
Otherwise, the query strings are compared as-is.
"""
fields = ParseResult._fields
for attr, x, y in zip(fields, urlparse(url), urlparse(expected)):
if parse_qs and attr == 'query':
x, y = QueryDict(x), QueryDict(y)
if x and y and x != y:
self.fail("%r != %r (%s doesn't match)" % (url, expected, attr))
3
Example 17
Project: django-revproxy Source File: views.py
def get_upstream(self, path):
upstream = self.upstream
if not getattr(self, '_parsed_url', None):
self._parsed_url = urlparse(upstream)
if self._parsed_url.scheme not in ('http', 'https'):
raise InvalidUpstream(ERRORS_MESSAGES['upstream-no-scheme'] %
upstream)
if path and upstream[-1] != '/':
upstream += '/'
return upstream
3
Example 18
def same_origin(url1, url2):
"""
Checks if two URLs are 'same-origin'
"""
p1, p2 = urlparse(url1), urlparse(url2)
try:
return (p1.scheme, p1.hostname, p1.port) == (p2.scheme, p2.hostname, p2.port)
except ValueError:
return False
3
Example 19
def same_origin(url1, url2):
"""
Checks if two URLs are 'same-origin'
"""
p1, p2 = urlparse(url1), urlparse(url2)
try:
o1 = (p1.scheme, p1.hostname, p1.port or PROTOCOL_TO_PORT[p1.scheme])
o2 = (p2.scheme, p2.hostname, p2.port or PROTOCOL_TO_PORT[p2.scheme])
return o1 == o2
except (ValueError, KeyError):
return False
3
Example 20
Project: browsercompat Source File: views.py
def clean_url(self):
data = self.cleaned_data['url']
scheme, netloc, path, params, query, fragment = urlparse(data)
if '%' not in path:
path = quote(path)
cleaned = urlunparse((scheme, netloc, path, '', '', ''))
validate_mdn_url(cleaned)
return cleaned
3
Example 21
def __init__(self, redirect_to, *args, **kwargs):
parsed = urlparse(force_text(redirect_to))
if parsed.scheme and parsed.scheme not in self.allowed_schemes:
raise DisallowedRedirect("Unsafe redirect to URL with protocol '%s'" % parsed.scheme)
super(HttpResponseRedirectBase, self).__init__(*args, **kwargs)
self['Location'] = iri_to_uri(redirect_to)
0
Example 22
Project: nodewatcher Source File: decorators.py
def user_test_required(test_func, message_func=None, message_level_func=lambda u: messages.INFO, redirect_url_func=lambda u: None, redirect_field_name=auth.REDIRECT_FIELD_NAME, decorator_id=None):
"""
Decorator for views that checks that the user passes the given test, redirecting if necessary (by default to the profile page).
The test should be a callable that takes the user object and returns True if the user passes. It can use optional message
argument to tell the user the reason why she has be redirected, which should also be a callable that takes the user object as
an argument.
It maintains `decorators` attribute on wrapped function with list of all ids of used decorators, where the first one is the first
one used.
"""
if decorator_id is None:
decorator_id = id(user_test_required)
def decorator(view_func):
@functools.wraps(view_func, assigned=decorators.available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
resolved_login_url = shortcuts.resolve_url(redirect_url_func(request.user) or settings.LOGIN_REDIRECT_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = parse.urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = parse.urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and (not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
if message_func:
messages.add_message(request, message_level_func(request.user), message_func(request.user), fail_silently=True)
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(path, resolved_login_url, redirect_field_name)
_wrapped_view.decorators = []
if hasattr(view_func, 'decorators'):
_wrapped_view.decorators += view_func.decorators
_wrapped_view.decorators.append(decorator_id)
return _wrapped_view
return decorator
0
Example 23
Project: reviewboard Source File: test_github.py
def test_get_change(self):
"""Testing GitHub get_change implementation"""
commit_sha = '1c44b461cebe5874a857c51a4a13a849a4d1e52d'
parent_sha = '44568f7d33647d286691517e6325fea5c7a21d5e'
tree_sha = '56e25e58380daf9b4dfe35677ae6043fe1743922'
commits_api_response = json.dumps([
{
'commit': {
'author': {'name': 'David Trowbridge'},
'committer': {'date': '2013-06-25T23:31:22Z'},
'message': 'Move .clearfix to defs.less',
},
'sha': commit_sha,
'parents': [{'sha': parent_sha}],
},
])
compare_api_response = json.dumps({
'base_commit': {
'commit': {
'tree': {'sha': tree_sha},
},
},
'files': [
{
'sha': '4344b3ad41b171ea606e88e9665c34cca602affb',
'filename': 'reviewboard/static/rb/css/defs.less',
'status': 'modified',
'patch': dedent("""\
@@ -182,4 +182,23 @@
}
+/* Add a rule for clearing floats, */
+.clearfix {
+ display: inline-block;
+
+ &:after {
+ clear: both;
+ content: \".\";
+ display: block;
+ height: 0;
+ visibility: hidden;
+ }
+}
+
+/* Hides from IE-mac \\*/
+* html .clearfix {height: 1%;}
+.clearfix {display: block;}
+/* End hide from IE-mac */
+
+
// vim: set et ts=2 sw=2:"""),
},
{
'sha': '8e3129277b018b169cb8d13771433fbcd165a17c',
'filename': 'reviewboard/static/rb/css/reviews.less',
'status': 'modified',
'patch': dedent("""\
@@ -1311,24 +1311,6 @@
.border-radius(8px);
}
-/* Add a rule for clearing floats, */
-.clearfix {
- display: inline-block;
-
- &:after {
- clear: both;
- content: \".\";
- display: block;
- height: 0;
- visibility: hidden;
- }
-}
-
-/* Hides from IE-mac \\*/
-* html .clearfix {height: 1%;}
-.clearfix {display: block;}
-/* End hide from IE-mac */
-
/cuem************************************************
* Issue Summary"""),
},
]
})
trees_api_response = json.dumps({
'tree': [
{
'path': 'reviewboard/static/rb/css/defs.less',
'sha': '830a40c3197223c6a0abb3355ea48891a1857bfd',
},
{
'path': 'reviewboard/static/rb/css/reviews.less',
'sha': '535cd2c4211038d1bb8ab6beaed504e0db9d7e62',
},
],
})
# This has to be a list to avoid python's hinky treatment of scope of
# variables assigned within a closure.
step = [1]
def _http_get(service, url, *args, **kwargs):
parsed = urlparse(url)
if parsed.path == '/repos/myuser/myrepo/commits':
self.assertEqual(step[0], 1)
step[0] += 1
query = parsed.query.split('&')
self.assertIn(('sha=%s' % commit_sha), query)
return commits_api_response, None
elif parsed.path.startswith('/repos/myuser/myrepo/compare/'):
self.assertEqual(step[0], 2)
step[0] += 1
revs = parsed.path.split('/')[-1].split('...')
self.assertEqual(revs[0], parent_sha)
self.assertEqual(revs[1], commit_sha)
return compare_api_response, None
elif parsed.path.startswith('/repos/myuser/myrepo/git/trees/'):
self.assertEqual(step[0], 3)
step[0] += 1
self.assertEqual(parsed.path.split('/')[-1], tree_sha)
return trees_api_response, None
else:
print(parsed)
self.fail('Got an unexpected GET request')
account = self._get_hosting_account()
account.data['authorization'] = {'token': 'abc123'}
service = account.service
self.spy_on(service.client.http_get, call_fake=_http_get)
repository = Repository(hosting_account=account)
repository.extra_data = {
'repository_plan': 'public',
'github_public_repo_name': 'myrepo',
}
change = service.get_change(repository, commit_sha)
self.assertTrue(service.client.http_get.called)
self.assertEqual(change.message, 'Move .clearfix to defs.less')
self.assertEqual(md5(change.diff.encode('utf-8')).hexdigest(),
'0dd1bde0a60c0a7bb92c27b50f51fcb6')
0
Example 24
Project: django-ratelimit-backend Source File: views.py
@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name='registration/login.html',
redirect_field_name=REDIRECT_FIELD_NAME,
authentication_form=AuthenticationForm,
current_app=None, extra_context=None):
"""
Displays the login form and handles the login action.
"""
redirect_to = request.POST.get(redirect_field_name,
request.GET.get(redirect_field_name, ''))
if request.method == "POST":
form = authentication_form(data=request.POST, request=request)
if form.is_valid():
netloc = urlparse(redirect_to)[1]
# Use default setting if redirect_to is empty
if not redirect_to:
redirect_to = settings.LOGIN_REDIRECT_URL
# Heavier security check -- don't allow redirection to a different
# host.
elif netloc and netloc != request.get_host():
redirect_to = settings.LOGIN_REDIRECT_URL
# Okay, security checks complete. Log the user in.
auth_login(request, form.get_user())
return redirect(redirect_to)
else:
form = authentication_form(request)
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
if extra_context is not None:
context.update(extra_context)
request.current_app = current_app
return TemplateResponse(request, template_name, context)
0
Example 25
Project: cgstudiomap Source File: csrf.py
def process_view(self, request, callback, callback_args, callback_kwargs):
if getattr(request, 'csrf_processing_done', False):
return None
try:
csrf_token = _sanitize_token(
request.COOKIES[settings.CSRF_COOKIE_NAME])
# Use same token next time
request.META['CSRF_COOKIE'] = csrf_token
except KeyError:
csrf_token = None
# Wait until request.META["CSRF_COOKIE"] has been manipulated before
# bailing out, so that get_token still works
if getattr(callback, 'csrf_exempt', False):
return None
# Assume that anything not defined as 'safe' by RFC2616 needs protection
if request.method not in ('GET', 'HEAD', 'OPTIONS', 'TRACE'):
if getattr(request, '_dont_enforce_csrf_checks', False):
# Mechanism to turn off CSRF checks for test suite.
# It comes after the creation of CSRF cookies, so that
# everything else continues to work exactly the same
# (e.g. cookies are sent, etc.), but before any
# branches that call reject().
return self._accept(request)
if request.is_secure():
# Suppose user visits http://example.com/
# An active network attacker (man-in-the-middle, MITM) sends a
# POST form that targets https://example.com/detonate-bomb/ and
# submits it via JavaScript.
#
# The attacker will need to provide a CSRF cookie and token, but
# that's no problem for a MITM and the session-independent
# nonce we're using. So the MITM can circuemvent the CSRF
# protection. This is true for any HTTP connection, but anyone
# using HTTPS expects better! For this reason, for
# https://example.com/ we need additional protection that treats
# http://example.com/ as completely untrusted. Under HTTPS,
# Barth et al. found that the Referer header is missing for
# same-domain requests in only about 0.2% of cases or less, so
# we can use strict Referer checking.
referer = force_text(
request.META.get('HTTP_REFERER'),
strings_only=True,
errors='replace'
)
if referer is None:
return self._reject(request, REASON_NO_REFERER)
referer = urlparse(referer)
# Make sure we have a valid URL for Referer.
if '' in (referer.scheme, referer.netloc):
return self._reject(request, REASON_MALFORMED_REFERER)
# Ensure that our Referer is also secure.
if referer.scheme != 'https':
return self._reject(request, REASON_INSECURE_REFERER)
# If there isn't a CSRF_COOKIE_DOMAIN, assume we need an exact
# match on host:port. If not, obey the cookie rules.
if settings.CSRF_COOKIE_DOMAIN is None:
# request.get_host() includes the port.
good_referer = request.get_host()
else:
good_referer = settings.CSRF_COOKIE_DOMAIN
server_port = request.META['SERVER_PORT']
if server_port not in ('443', '80'):
good_referer = '%s:%s' % (good_referer, server_port)
# Here we generate a list of all acceptable HTTP referers,
# including the current host since that has been validated
# upstream.
good_hosts = list(settings.CSRF_TRUSTED_ORIGINS)
good_hosts.append(good_referer)
if not any(is_same_domain(referer.netloc, host) for host in good_hosts):
reason = REASON_BAD_REFERER % referer.geturl()
return self._reject(request, reason)
if csrf_token is None:
# No CSRF cookie. For POST requests, we insist on a CSRF cookie,
# and in this way we can avoid all CSRF attacks, including login
# CSRF.
return self._reject(request, REASON_NO_CSRF_COOKIE)
# Check non-cookie token for match.
request_csrf_token = ""
if request.method == "POST":
try:
request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
except IOError:
# Handle a broken connection before we've completed reading
# the POST data. process_view shouldn't raise any
# exceptions, so we'll ignore and serve the user a 403
# (assuming they're still listening, which they probably
# aren't because of the error).
pass
if request_csrf_token == "":
# Fall back to X-CSRFToken, to make things easier for AJAX,
# and possible for PUT/DELETE.
request_csrf_token = request.META.get(settings.CSRF_HEADER_NAME, '')
if not constant_time_compare(request_csrf_token, csrf_token):
return self._reject(request, REASON_BAD_TOKEN)
return self._accept(request)
0
Example 26
def generic(self, method, path, data='',
content_type='application/octet-stream', secure=False,
**extra):
"""Constructs an arbitrary HTTP request."""
parsed = urlparse(force_str(path))
data = force_bytes(data, settings.DEFAULT_CHARSET)
r = {
'PATH_INFO': self._get_path(parsed),
'REQUEST_METHOD': str(method),
'SERVER_PORT': str('443') if secure else str('80'),
'wsgi.url_scheme': str('https') if secure else str('http'),
}
if data:
r.update({
'CONTENT_LENGTH': len(data),
'CONTENT_TYPE': str(content_type),
'wsgi.input': FakePayload(data),
})
r.update(extra)
# If QUERY_STRING is absent or empty, we want to extract it from the URL.
if not r.get('QUERY_STRING'):
query_string = force_bytes(parsed[4])
# WSGI requires latin-1 encoded strings. See get_path_info().
if six.PY3:
query_string = query_string.decode('iso-8859-1')
r['QUERY_STRING'] = query_string
return self.request(**r)
0
Example 27
Project: reviewboard Source File: core.py
@classmethod
def get_auth_from_uri(cls, path, username):
"""Return the username and hostname from the given repository path.
This is used to separate out a username and a hostname from a path,
given a string containing ``username@hostname``.
Subclasses do not need to provide this in most cases. It's used as
a convenience method for :py:meth:`check_repository`. Subclasses that
need special parsing logic will generally just replace the behavior
in that method.
Args:
path (unicode):
The repository path to parse.
username (unicode):
The existing username provided in the repository configuration.
Returns:
tuple:
A tuple containing 2 string items: The username, and the hostname.
"""
url = urlparse(path)
if '@' in url[1]:
netloc_username, hostname = url[1].split('@', 1)
else:
hostname = url[1]
netloc_username = None
if netloc_username:
return netloc_username, hostname
else:
return username, hostname
0
Example 28
Project: hue Source File: admin_urls.py
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False):
opts = context.get('opts')
preserved_filters = context.get('preserved_filters')
parsed_url = list(urlparse(url))
parsed_qs = dict(parse_qsl(parsed_url[4]))
merged_qs = dict()
if opts and preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters))
match_url = '/%s' % url.partition(get_script_prefix())[2]
try:
match = resolve(match_url)
except Resolver404:
pass
else:
current_url = '%s:%s' % (match.app_name, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if changelist_url == current_url and '_changelist_filters' in preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))
merged_qs.update(preserved_filters)
if popup:
from django.contrib.admin.options import IS_POPUP_VAR
merged_qs[IS_POPUP_VAR] = 1
merged_qs.update(parsed_qs)
parsed_url[4] = urlencode(merged_qs)
return urlunparse(parsed_url)
0
Example 29
Project: reviewboard Source File: service.py
@classmethod
def get_repository_fields(cls, username, hosting_url, plan, tool_name,
field_vars):
if not cls.supports_repositories:
raise NotImplementedError
# Grab the list of fields for population below. We have to do this
# differently depending on whether or not this hosting service has
# different repository plans.
fields = cls._get_field(plan, 'repository_fields')
new_vars = field_vars.copy()
new_vars['hosting_account_username'] = username
if cls.self_hosted:
new_vars['hosting_url'] = hosting_url
new_vars['hosting_domain'] = urlparse(hosting_url)[1]
results = {}
assert tool_name in fields
for field, value in six.iteritems(fields[tool_name]):
try:
results[field] = value % new_vars
except KeyError as e:
logging.error('Failed to generate %s field for hosting '
'service %s using %s and %r: Missing key %s'
% (field, six.text_type(cls.name), value,
new_vars, e),
exc_info=1)
raise KeyError(
_('Internal error when generating %(field)s field '
'(Missing key "%(key)s"). Please report this.') % {
'field': field,
'key': e,
})
return results
0
Example 30
Project: django-two-factor-auth Source File: test_utils.py
def assertEqualUrl(self, lhs, rhs):
"""
Asserts whether the URLs are canonically equal.
"""
if six.PY2:
# See those Chinese characters above? Those are quite difficult
# to match against the generated URLs in portable code. True,
# this solution is not the nicest, but it works. And it's test
# code after all.
lhs = lhs.encode('utf8')
lhs = urlparse(lhs)
rhs = urlparse(rhs)
self.assertEqual(lhs.scheme, rhs.scheme)
self.assertEqual(lhs.netloc, rhs.netloc)
self.assertEqual(lhs.path, rhs.path)
self.assertEqual(lhs.fragment, rhs.fragment)
# We used parse_qs before, but as query parameter order became
# significant with Microsoft Authenticator and possibly other
# authenticator apps, we've switched to parse_qsl.
self.assertEqual(parse_qsl(lhs.query), parse_qsl(rhs.query))
0
Example 31
def generic(self, method, path, data='',
content_type='application/octet-stream', secure=False,
**extra):
"""Constructs an arbitrary HTTP request."""
parsed = urlparse(path)
data = force_bytes(data, settings.DEFAULT_CHARSET)
r = {
'PATH_INFO': self._get_path(parsed),
'REQUEST_METHOD': str(method),
'SERVER_PORT': str('443') if secure else str('80'),
'wsgi.url_scheme': str('https') if secure else str('http'),
}
if data:
r.update({
'CONTENT_LENGTH': len(data),
'CONTENT_TYPE': str(content_type),
'wsgi.input': FakePayload(data),
})
r.update(extra)
# If QUERY_STRING is absent or empty, we want to extract it from the URL.
if not r.get('QUERY_STRING'):
query_string = force_bytes(parsed[4])
# WSGI requires latin-1 encoded strings. See get_path_info().
if six.PY3:
query_string = query_string.decode('iso-8859-1')
r['QUERY_STRING'] = query_string
return self.request(**r)
0
Example 32
def is_ignorable_request(self, request, uri, domain, referer):
"""
Return True if the given request *shouldn't* notify the site managers
according to project settings or in three specific situations:
- If the referer is empty.
- If a '?' in referer is identified as a search engine source.
- If the referer is equal to the current URL, ignoring the scheme
(assumed to be a poorly implemented bot).
"""
if not referer:
return True
if not self.is_internal_request(domain, referer) and '?' in referer:
return True
parsed_referer = urlparse(referer)
if parsed_referer.netloc in ['', domain] and parsed_referer.path == uri:
return True
return any(pattern.search(uri) for pattern in settings.IGNORABLE_404_URLS)
0
Example 33
Project: reviewboard Source File: test_gitlab.py
def test_get_change(self):
"""Testing GitLab get_change implementation"""
commit_id = 'ed899a2f4b50b4370feeea94676502b42383c746'
commit_api_response = json.dumps(
{
'author_name': 'Chester Li',
'id': commit_id,
'created_at': '2015-03-10T11:50:22+03:00',
'message': 'Replace sanitize with escape once',
'parent_ids': ['ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba']
}
)
path_api_response = json.dumps(
{
'path_with_namespace': 'username/project_name'
}
)
diff = dedent(b'''\
---
f1 | 1 +
f2 | 1 +
2 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/f1 b/f1
index 11ac561..3ea0691 100644
--- a/f1
+++ b/f1
@@ -1 +1,2 @@
this is f1
+add one line to f1
diff --git a/f2 b/f2
index c837441..9302ecd 100644
--- a/f2
+++ b/f2
@@ -1 +1,2 @@
this is f2
+add one line to f2 with Unicode\xe2\x9d\xb6
''')
def _http_get(service, url, *args, **kwargs):
self.assertTrue(url.startswith(account.hosting_url))
parsed = urlparse(url)
if parsed.path.startswith(
'/api/v3/projects/123456/repository/commits'):
# If the url is commit_api_url.
return commit_api_response, None
elif parsed.path == '/api/v3/projects/123456':
# If the url is path_api_url.
return path_api_response, None
elif parsed.path.endswith('.diff'):
# If the url is diff_url.
return diff, None
else:
print(parsed)
self.fail('Got an unexpected GET request')
account = self._get_hosting_account(use_url=True)
account.data['private_token'] = encrypt_password('abc123')
service = account.service
repository = Repository(hosting_account=account)
repository.extra_data = {'gitlab_project_id': 123456}
self.spy_on(service.client.http_get, call_fake=_http_get)
commit = service.get_change(repository, commit_id)
self.assertTrue(service.client.http_get.called)
self.assertEqual(commit.date, '2015-03-10T11:50:22+03:00')
self.assertEqual(commit.diff, diff)
self.assertNotEqual(commit.parent, '')
0
Example 34
Project: reviewboard Source File: cvs.py
@classmethod
def parse_hostname(cls, path):
"""Parses a hostname from a repository path."""
return urlparse(path)[1] # netloc
0
Example 35
Project: hue Source File: decorators.py
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
# urlparse chokes on lazy objects in Python 3, force to str
resolved_login_url = force_str(
resolve_url(login_url or settings.LOGIN_URL))
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return _wrapped_view
return decorator
0
Example 36
Project: PyClassLessons Source File: admin_urls.py
@register.simple_tag(takes_context=True)
def add_preserved_filters(context, url, popup=False, to_field=None):
opts = context.get('opts')
preserved_filters = context.get('preserved_filters')
parsed_url = list(urlparse(url))
parsed_qs = dict(parse_qsl(parsed_url[4]))
merged_qs = dict()
if opts and preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters))
match_url = '/%s' % url.partition(get_script_prefix())[2]
try:
match = resolve(match_url)
except Resolver404:
pass
else:
current_url = '%s:%s' % (match.app_name, match.url_name)
changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name)
if changelist_url == current_url and '_changelist_filters' in preserved_filters:
preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters']))
merged_qs.update(preserved_filters)
if popup:
from django.contrib.admin.options import IS_POPUP_VAR
merged_qs[IS_POPUP_VAR] = 1
if to_field:
from django.contrib.admin.options import TO_FIELD_VAR
merged_qs[TO_FIELD_VAR] = to_field
merged_qs.update(parsed_qs)
parsed_url[4] = urlencode(merged_qs)
return urlunparse(parsed_url)
0
Example 37
def is_safe_url(url, host=None):
"""
Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
a different host and uses a safe scheme).
Always returns ``False`` on an empty url.
"""
if not url:
return False
url = url.strip()
# Chrome treats \ completely as /
url = url.replace('\\', '/')
# Chrome considers any URL with more than two slashes to be absolute, but
# urlparse is not so flexible. Treat any url with three slashes as unsafe.
if url.startswith('///'):
return False
url_info = urlparse(url)
# Forbid URLs like http:///example.com - with a scheme, but without a hostname.
# In that URL, example.com is not the hostname but, a path component. However,
# Chrome will still consider example.com to be the hostname, so we must not
# allow this syntax.
if not url_info.netloc and url_info.scheme:
return False
return ((not url_info.netloc or url_info.netloc == host) and
(not url_info.scheme or url_info.scheme in ['http', 'https']))
0
Example 38
Project: ava Source File: decorators.py
def access_check(test_func, view_func=None, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that should pass a test before allowing access.
:param test_func: A callable that returns True if access is allowed, or
False if the user should be sent to the login page.
:param test_func: A callable that returns a HTTP response. This should only
be set if used in a 'urls.py' file. If the decorator is used
on a view function, this parameter should be set to None.
:param login_url: The URL of the login page. If set to None, the default
login page is used.
:param redirect_field_name: The name of the URL field that should contain
the page to display after login.
"""
def decorator(decorated_view_func):
@wraps(decorated_view_func, assigned=available_attrs(decorated_view_func))
def _wrapped_view(request, *args, **kwargs):
# If the access test is passed, display the page.
if test_func(request):
return decorated_view_func(request, *args, **kwargs)
# Get the path to the current page and the login page.
path = request.build_absolute_uri()
resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
# Redirect to the login page.
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return _wrapped_view
if view_func:
return decorator(view_func)
else:
return decorator
0
Example 39
def is_safe_url(url, host=None):
"""
Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
a different host and uses a safe scheme).
Always returns ``False`` on an empty url.
"""
if url is not None:
url = url.strip()
if not url:
return False
# Chrome treats \ completely as /
url = url.replace('\\', '/')
# Chrome considers any URL with more than two slashes to be absolute, but
# urlparse is not so flexible. Treat any url with three slashes as unsafe.
if url.startswith('///'):
return False
url_info = urlparse(url)
# Forbid URLs like http:///example.com - with a scheme, but without a hostname.
# In that URL, example.com is not the hostname but, a path component. However,
# Chrome will still consider example.com to be the hostname, so we must not
# allow this syntax.
if not url_info.netloc and url_info.scheme:
return False
# Forbid URLs that start with control characters. Some browsers (like
# Chrome) ignore quite a few control characters at the start of a
# URL and might consider the URL as scheme relative.
if unicodedata.category(url[0])[0] == 'C':
return False
return ((not url_info.netloc or url_info.netloc == host) and
(not url_info.scheme or url_info.scheme in ['http', 'https']))
0
Example 40
Project: dj-libcloud Source File: storage.py
def url(self, name):
provider_type = self.provider['type'].lower()
obj = self._get_object(name)
if not obj:
return None
try:
# currently only Cloudfiles supports it
url = self.driver.get_object_cdn_url(obj)
except NotImplementedError as e:
object_path = '%s/%s' % (self.bucket, obj.name)
if 's3' in provider_type:
base_url = 'http://%s' % self.driver.connection.host
url = urljoin(base_url, object_path)
elif 'google' in provider_type:
url = urljoin('//storage.googleapis.com', object_path)
elif 'azure' in provider_type:
base_url = ('http://%s.blob.core.windows.net' %
self.provider['user'])
url = urljoin(base_url, object_path)
else:
raise e
if self.secure:
if 'cloudfiles' in provider_type:
parsed_url = urlparse(url)
if parsed_url.scheme != 'http':
return url
split_netloc = parsed_url.netloc.split('.')
split_netloc[1] = 'ssl'
url = urlunparse(
'https',
'.'.join(split_netloc),
parsed_url.path,
parsed_url.params, parsed_url.query,
parsed_url.fragment
)
if ('s3' in provider_type or
'google' in provider_type or
'azure' in provider_type):
url = url.replace('http://', 'https://')
return url
0
Example 41
Project: django-redis-cache Source File: utils.py
def parse_connection_kwargs(server, db=None, **kwargs):
"""
Return a connection pool configured from the given URL.
For example::
redis://[:password]@localhost:6379/0
rediss://[:password]@localhost:6379/0
unix://[:password]@/path/to/socket.sock?db=0
Three URL schemes are supported:
redis:// creates a normal TCP socket connection
rediss:// creates a SSL wrapped TCP socket connection
unix:// creates a Unix Domain Socket connection
There are several ways to specify a database number. The parse function
will return the first specified option:
1. A ``db`` querystring option, e.g. redis://localhost?db=0
2. If using the redis:// scheme, the path argument of the url, e.g.
redis://localhost/0
3. The ``db`` argument to this function.
If none of these options are specified, db=0 is used.
Any additional querystring arguments and keyword arguments will be
passed along to the ConnectionPool class's initializer. In the case
of conflicting arguments, querystring arguments always win.
NOTE: taken from `redis.ConnectionPool.from_url` in redis-py
"""
kwargs['unix_socket_path'] = ''
if '://' in server:
url = server
url_string = url
url = urlparse(url)
qs = ''
# in python2.6, custom URL schemes don't recognize querystring values
# they're left as part of the url.path.
if '?' in url.path and not url.query:
# chop the querystring including the ? off the end of the url
# and reparse it.
qs = url.path.split('?', 1)[1]
url = urlparse(url_string[:-(len(qs) + 1)])
else:
qs = url.query
url_options = {}
for name, value in parse_qs(qs).items():
if value and len(value) > 0:
url_options[name] = value[0]
# We only support redis:// and unix:// schemes.
if url.scheme == 'unix':
url_options.update({
'password': url.password,
'unix_socket_path': url.path,
})
else:
url_options.update({
'host': url.hostname,
'port': int(url.port or 6379),
'password': url.password,
})
# If there's a path argument, use it as the db argument if a
# querystring value wasn't specified
if 'db' not in url_options and url.path:
try:
url_options['db'] = int(url.path.replace('/', ''))
except (AttributeError, ValueError):
pass
if url.scheme == 'rediss':
url_options['connection_class'] = SSLConnection
# last shot at the db value
url_options['db'] = int(url_options.get('db', db or 0))
# update the arguments from the URL values
kwargs.update(url_options)
# backwards compatability
if 'charset' in kwargs:
warnings.warn(DeprecationWarning(
'"charset" is deprecated. Use "encoding" instead'))
kwargs['encoding'] = kwargs.pop('charset')
if 'errors' in kwargs:
warnings.warn(DeprecationWarning(
'"errors" is deprecated. Use "encoding_errors" instead'))
kwargs['encoding_errors'] = kwargs.pop('errors')
else:
unix_socket_path = None
if ':' in server:
host, port = server.rsplit(':', 1)
try:
port = int(port)
except (ValueError, TypeError):
raise ImproperlyConfigured(
"{0} from {1} must be an integer".format(
repr(port),
server
)
)
else:
host, port = None, None
unix_socket_path = server
kwargs.update(
host=host,
port=port,
unix_socket_path=unix_socket_path,
db=db,
)
return kwargs
0
Example 42
def __init__(self, application):
self.application = application
self.base_url = urlparse(self.get_base_url())
super(StaticFilesHandler, self).__init__()
0
Example 43
Project: Misago Source File: utils.py
def get_thread_id_from_url(request, url):
try:
clean_url = six.text_type(url).strip()
bits = urlparse(clean_url)
except:
return None
if bits.netloc and bits.netloc != request.get_host():
return None
if bits.path.startswith(request.get_host()):
clean_path = bits.path.lstrip(request.get_host())
else:
clean_path = bits.path
try:
wsgi_alias = request.path[:len(request.path_info) * -1]
resolution = resolve(clean_path[len(wsgi_alias):])
except:
return None
if not resolution.namespaces:
return None
url_name = '{}:{}'.format(':'.join(resolution.namespaces), resolution.url_name)
kwargname = SUPPORTED_THREAD_ROUTES.get(url_name)
if not kwargname:
return None
try:
return int(resolution.kwargs.get(kwargname))
except (TypeError, ValueError):
return None
0
Example 44
Project: froide Source File: utils.py
def _is_safe_url(url, allowed_hosts, require_https=False):
# Chrome considers any URL with more than two slashes to be absolute, but
# urlparse is not so flexible. Treat any url with three slashes as unsafe.
if url.startswith('///'):
return False
url_info = urlparse(url)
# Forbid URLs like http:///example.com - with a scheme, but without a hostname.
# In that URL, example.com is not the hostname but, a path component. However,
# Chrome will still consider example.com to be the hostname, so we must not
# allow this syntax.
if not url_info.netloc and url_info.scheme:
return False
# Forbid URLs that start with control characters. Some browsers (like
# Chrome) ignore quite a few control characters at the start of a
# URL and might consider the URL as scheme relative.
if unicodedata.category(url[0])[0] == 'C':
return False
scheme = url_info.scheme
# Consider URLs without a scheme (e.g. //example.com/p) to be http.
if not url_info.scheme and url_info.netloc:
scheme = 'http'
valid_schemes = ['https'] if require_https else ['http', 'https']
return ((not url_info.netloc or url_info.netloc in allowed_hosts) and
(not scheme or scheme in valid_schemes))
0
Example 45
def __init__(self, params):
location_url_parsed = urlparse(params.pop('LOCATION'))
self.cache_scheme = location_url_parsed.scheme
self.cache_netloc = location_url_parsed.netloc
0
Example 46
def purge(self, url):
url_parsed = urlparse(url)
host = url_parsed.hostname
# Append port to host if it is set in the original URL
if url_parsed.port:
host += (':' + str(url_parsed.port))
request = PurgeRequest(
url=urlunparse([
self.cache_scheme,
self.cache_netloc,
url_parsed.path,
url_parsed.params,
url_parsed.query,
url_parsed.fragment
]),
headers={
'Host': host,
'User-Agent': 'Wagtail-frontendcache/' + __version__
}
)
try:
urlopen(request)
except HTTPError as e:
logger.error("Couldn't purge '%s' from HTTP cache. HTTPError: %d %s", url, e.code, e.reason)
except URLError as e:
logger.error("Couldn't purge '%s' from HTTP cache. URLError: %s", url, e.reason)
0
Example 47
Project: molo Source File: elasticsearch.py
def __init__(self, params):
super(ElasticsearchSearchBackend, self).__init__(params)
# Get settings
self.hosts = params.pop('HOSTS', None)
self.index_name = params.pop('INDEX', 'wagtail')
self.timeout = params.pop('TIMEOUT', 10)
if params.pop('ATOMIC_REBUILD', False):
self.rebuilder_class = self.atomic_rebuilder_class
else:
self.rebuilder_class = self.basic_rebuilder_class
# If HOSTS is not set, convert URLS setting to HOSTS
es_urls = params.pop('URLS', ['http://localhost:9200'])
if self.hosts is None:
self.hosts = []
for url in es_urls:
parsed_url = urlparse(url)
use_ssl = parsed_url.scheme == 'https'
port = parsed_url.port or (443 if use_ssl else 80)
http_auth = None
if parsed_url.username is not None and parsed_url.password is not None:
http_auth = (parsed_url.username, parsed_url.password)
self.hosts.append({
'host': parsed_url.hostname,
'port': port,
'url_prefix': parsed_url.path,
'use_ssl': use_ssl,
'verify_certs': use_ssl,
'http_auth': http_auth,
})
# Get Elasticsearch interface
# Any remaining params are passed into the Elasticsearch constructor
self.es = Elasticsearch(
hosts=self.hosts,
timeout=self.timeout,
**params)
0
Example 48
Project: PyClassLessons Source File: decorators.py
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func))
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
return view_func(request, *args, **kwargs)
path = request.build_absolute_uri()
resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
# If the login url is the same scheme and net location then just
# use the path as the "next" url.
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
current_scheme, current_netloc = urlparse(path)[:2]
if ((not login_scheme or login_scheme == current_scheme) and
(not login_netloc or login_netloc == current_netloc)):
path = request.get_full_path()
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(
path, resolved_login_url, redirect_field_name)
return _wrapped_view
return decorator
0
Example 49
Project: reviewboard Source File: forms.py
def save(self):
"""Save the form."""
server = self.cleaned_data['server']
if "://" not in server:
# urlparse doesn't properly handle URLs without a scheme. It
# believes the domain is actually the path. So we apply a prefix.
server = "http://" + server
url_parts = urlparse(server)
domain_method = url_parts[0]
domain_name = url_parts[1]
if domain_name.endswith("/"):
domain_name = domain_name[:-1]
site = Site.objects.get_current()
site.domain = domain_name
site.save()
self.siteconfig.set("site_domain_method", domain_method)
cache_type = self.cleaned_data['cache_type']
if cache_type != 'custom':
if cache_type == 'locmem':
# We want to specify a "reviewboard" location to keep items
# separate from those in other caches.
location = 'reviewboard'
else:
location_field = self.CACHE_LOCATION_FIELD_MAP[cache_type]
location = self.cleaned_data[location_field]
if cache_type == 'memcached':
# memcached allows a list of servers, rather than just a
# string representing one.
location = location.split(';')
self.siteconfig.set('cache_backend', {
DEFAULT_FORWARD_CACHE_ALIAS: {
'BACKEND': self.CACHE_BACKENDS_MAP[cache_type],
'LOCATION': location,
}
})
super(GeneralSettingsForm, self).save()
# Reload any important changes into the Django settings.
load_site_config()
0
Example 50
@staticmethod
def normalise_path(url):
# Strip whitespace
url = url.strip()
# Parse url
url_parsed = urlparse(url)
# Path must start with / but not end with /
path = url_parsed[2]
if not path.startswith('/'):
path = '/' + path
if path.endswith('/') and len(path) > 1:
path = path[:-1]
# Parameters must be sorted alphabetically
parameters = url_parsed[3]
parameters_components = parameters.split(';')
parameters = ';'.join(sorted(parameters_components))
# Query string components must be sorted alphabetically
query_string = url_parsed[4]
query_string_components = query_string.split('&')
query_string = '&'.join(sorted(query_string_components))
if parameters:
path = path + ';' + parameters
# Add query string to path
if query_string:
path = path + '?' + query_string
return path