Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix some problems with IPv6 addresses in utils

  • Loading branch information...
commit 6ea6a6dc1381bef813cf597cd48e493607fb08c5 1 parent b713eca
k21 authored
Showing with 25 additions and 7 deletions.
  1. +25 −7 r2/r2/lib/utils/utils.py
32 r2/r2/lib/utils/utils.py
View
@@ -220,7 +220,7 @@ def base_url(url):
res = r_base_url.findall(url)
return (res and res[0]) or url
-r_domain = re.compile("(?i)(?:.+?://)?(?:www[\d]*\.)?([^/:#?]*)")
+r_domain = re.compile("(?i)(?:.+?://)?(?:www[\d]*\.)?(\[[0-9a-fA-F:]+\]|[^/:#?]*)")
def domain(s):
"""
Takes a URL and returns the domain part, minus www., if
@@ -379,7 +379,8 @@ class UrlParser(object):
__slots__ = ['scheme', 'path', 'params', 'query',
'fragment', 'username', 'password', 'hostname',
- 'port', '_url_updates', '_orig_url', '_query_dict']
+ 'port', '_url_updates', '_orig_url', '_query_dict',
+ 'is_ipv6']
valid_schemes = ('http', 'https', 'ftp', 'mailto')
cname_get = "cnameframe"
@@ -389,6 +390,9 @@ def __init__(self, url):
for s in self.__slots__:
if hasattr(u, s):
setattr(self, s, getattr(u, s))
+ self.is_ipv6 = False
+ if getattr(u, 'netloc', '').startswith('['):
+ self.is_ipv6 = True
self._url_updates = {}
self._orig_url = url
self._query_dict = None
@@ -459,8 +463,19 @@ def unparse(self):
q.update(self._url_updates)
q = query_string(q).lstrip('?')
- # make sure the port is not doubly specified
- if self.port and ":" in self.hostname:
+ # if this is ipv6 address, remove brackets from hostname
+ if self.hostname and self.hostname.startswith('[') and ']' in self.hostname:
+ self.is_ipv6 = True
+ self.hostname = self.hostname[1:]
+ self.hostname = self.hostname[:self.hostname.index(']')]
+
+ # if this is marked as ipv6 address but it is not, remove the mark
+ if self.hostname and self.is_ipv6:
+ if not all(c in '0123456789abcdefABCDEF:' for c in self.hostname):
+ self.is_ipv6 = False
+
+ # make sure the port is not doubly specified
+ if self.hostname and ':' in self.hostname and self.port and not self.is_ipv6:
self.hostname = self.hostname.split(':')[0]
# if there is a netloc, there had better be a scheme
@@ -539,7 +554,10 @@ def netloc(self):
if not self.hostname:
return ""
elif getattr(self, "port", None):
- return self.hostname + ":" + str(self.port)
+ if self.is_ipv6:
+ return "[" + self.hostname + "]:" + str(self.port)
+ else:
+ return self.hostname + ":" + str(self.port)
return self.hostname
def mk_cname(self, require_frame = True, subreddit = None, port = None):
@@ -948,8 +966,8 @@ def new_fn(*a,**kw):
def common_subdomain(domain1, domain2):
if not domain1 or not domain2:
return ""
- domain1 = domain1.split(":")[0]
- domain2 = domain2.split(":")[0]
+ domain1 = urlparse(domain1).hostname
+ domain2 = urlparse(domain2).hostname
if len(domain1) > len(domain2):
domain1, domain2 = domain2, domain1

8 comments on commit 6ea6a6d

danry25

Looks like this patch breaks submissions in the current iteration of Reddit from Source :(

k21
Owner

Yes, it has been a long time since this patch was written. I do not have a reddit locally installed right now and because the patch applied cleanly, I do not know what it breaks. Could you please be a bit more specific? Thanks.

danry25

Well, let me try & set it up on our latest reddit install, we migrated from reddit from source on Ubuntu 11.10 to reddit from source on ubuntu 12.04.1 since I last tried this patch. What all do I need to change out file wise by the way?

k21
Owner

If you are going to try applying this patch, it might be a good idea to use commit @2cfd44f instead of this one, which also fixes problems with /domain listings.

Only the following files were modified in this patch:
r2/r2/config/middleware.py
r2/r2/lib/utils/utils.py

danry25

Thanks for the info, I'll try applying the patch from the commit you recomended here in a minute & see how it goes.

danry25

interesting, so it appears to have no effect if I go & insert the changes you made into the latest reddit builds r2/r2/config/middleware.py
& r2/r2/lib/utils/utils.py files, although it doesn't break normal link submission. Submitting an IPv6 url gets reddit to reply with "you should check that url". Maybe reddit is relying on more values to screen urls now?

k21
Owner

I will try to get my local reddit installation running again and find out what the problem is, but I do not have a lot of free time, so I cannot guarantee when (whether) it will be fixed.

danry25

Oh, don't worry about it too much, I'm not particularly pressed to get this added into my reddit install. I'll clone your repository though on a different VPS & see if it works with IPv6 links.

Please sign in to comment.
Something went wrong with that request. Please try again.