* Add leading "/" to url.

* Remove extra "/" in case the database has extra leading or trailing slashes.
* Catch KeyError, not IndexError, since that's what gets thrown in the case I was thinking about.
 - Legacy-Id: 335
This commit is contained in:
Bill Fenner 2007-06-12 17:41:15 +00:00
parent f1251ca2d6
commit 47f60e49d1

View file

@ -10,7 +10,7 @@ def redirect(request, path="", script=""):
redir = Redirect.objects.get(cgi=script) redir = Redirect.objects.get(cgi=script)
except Redirect.DoesNotExist: except Redirect.DoesNotExist:
raise Http404 raise Http404
url = redir.url + "/" url = "/" + redir.url + "/"
(rest, remove) = (redir.rest, redir.remove) (rest, remove) = (redir.rest, redir.remove)
try: try:
cmd = redir.commands.all().get(command=request.REQUEST['command']) cmd = redir.commands.all().get(command=request.REQUEST['command'])
@ -21,7 +21,7 @@ def redirect(request, path="", script=""):
remove = cmd.suffix.remove remove = cmd.suffix.remove
except Command.DoesNotExist: except Command.DoesNotExist:
pass # it's ok, there's no more-specific request. pass # it's ok, there's no more-specific request.
except IndexError: except KeyError:
pass # it's ok, request didn't have 'command'. pass # it's ok, request didn't have 'command'.
try: try:
url += rest % request.REQUEST url += rest % request.REQUEST
@ -29,6 +29,8 @@ def redirect(request, path="", script=""):
# rest had something in it that request didn't have, so just # rest had something in it that request didn't have, so just
# redirect to the root of the tool. # redirect to the root of the tool.
pass pass
# Be generous in what you accept: collapse multiple slashes
url = re.sub(r'/+', '/', url)
if remove: if remove:
url = re.sub(re.escape(remove) + "/?$", "", url) url = re.sub(re.escape(remove) + "/?$", "", url)
return HttpResponseRedirect(url) return HttpResponseRedirect(url)