From 752ff20cf5eb72c6b90fef8101400755affd0ab7 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Mon, 25 Feb 2013 10:45:00 -0500 Subject: [PATCH] add a 'missing slash' regular expression for Location patterns in order to account for dropped slash with the i4x:// pattern. Certain redirect cases will cause this 2nd slash to be dropped (I think via Nginx) --- .../xmodule/xmodule/modulestore/__init__.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/__init__.py b/common/lib/xmodule/xmodule/modulestore/__init__.py index a9df6c3504..0ba7e36540 100644 --- a/common/lib/xmodule/xmodule/modulestore/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/__init__.py @@ -23,6 +23,15 @@ URL_RE = re.compile(""" (@(?P[^/]+))? """, re.VERBOSE) +MISSING_SLASH_URL_RE = re.compile(""" + (?P[^:]+):/ + (?P[^/]+)/ + (?P[^/]+)/ + (?P[^/]+)/ + (?P[^@]+) + (@(?P[^/]+))? + """, re.VERBOSE) + # TODO (cpennington): We should decide whether we want to expand the # list of valid characters in a location INVALID_CHARS = re.compile(r"[^\w.-]") @@ -164,12 +173,16 @@ class Location(_LocationBase): if isinstance(location, basestring): match = URL_RE.match(location) if match is None: - log.debug('location is instance of %s but no URL match' % basestring) - raise InvalidLocationError(location) - else: - groups = match.groupdict() - check_dict(groups) - return _LocationBase.__new__(_cls, **groups) + # cdodge: + # check for a dropped slash near the i4x:// element of the location string. This can happen with some + # redirects (e.g. edx.org -> www.edx.org which I think happens in Nginx) + match = MISSING_SLASH_URL_RE.match(location) + if match is None: + log.debug('location is instance of %s but no URL match' % basestring) + raise InvalidLocationError(location) + groups = match.groupdict() + check_dict(groups) + return _LocationBase.__new__(_cls, **groups) elif isinstance(location, (list, tuple)): if len(location) not in (5, 6): log.debug('location has wrong length')