#18495: 404 with non-/ WSGI, script prefix not removed in core/urlresolvers.py:
resolve()
-------------------------+-------------------------------------------------
     Reporter:           |      Owner:  nobody
  django@…               |     Status:  new
         Type:  Bug      |    Version:  1.4
    Component:  Core     |   Keywords:  404, WSGIScriptAlias, wsgi,
  (URLs)                 |  resolve, url, prefix
     Severity:  Normal   |  Has patch:  1
 Triage Stage:           |      UI/UX:  0
  Unreviewed             |
Easy pickings:  0        |
-------------------------+-------------------------------------------------
 Hello,

 I've just had a nasty WSGI bug with Django 1.4 while using a non-/
 WSGIScriptAlias with apache2. I tracked down the bug and found a cause so
 here is a patch for review, if you like.

 Sorry in advance if this is a known and fixed bug, but then please
 consider patching 1.4 stable release?

 === The bug ===
 Let:
 {{{#!python
 urlpatterns = patterns('', r'^bug/$', 'app.views.Bug')
 }}}

 Django runserver => everything OK

 apache2 + WSGIScriptAlias / => everything OK

 apache2 + WSGIScriptAlias /test => breaks

 Details: request URL {{{'/test/bug/'}}}, tried {{{'^bug/$'}}} did not
 match current URL {{{'bug/'}}} - silly right? :) obviously this is not
 reporting the actual URL it was checked against, maybe something to fix to
 have a more meaningful error message.

 A quick debug of WSGI reveals apache is properly giving all the info to
 Django: {{{SCRIPT_NAME='/test'}}}, {{{PATH_INFO='/bug/'}}}.

 === Proposed fix ===
 After a quick search it seems to be a known bug on the interwebs but
 solutions I've seen were ugly (write the prefix statically in the app).

 Diving into Django I found core/urlresolvers.py with:
 {{{#!python
 class RegexURLResolver(LocaleRegexProvider):
 [...]
     def resolve(self, path):
 [...]
             new_path = path[match.end():]
             for pattern in self.url_patterns:
 }}}
 With request path {{{'/test/bug/'}}}, {{{new_path}}} becomes
 {{{'test/bug/'}}}, and this obviously does not match against
 {{{'^bug/$'}}}.

 Proposed fix: if present, remove {{{get_script_prefix()}}} from
 {{{new_path}}} (after applying the same regexp).
 {{{#!python
              new_path = path[match.end():]
 +            # Get script prefix, apply regexp and remove from path if
 present.
 +            prefix = get_script_prefix()
 +            if prefix:
 +                match = self.regex.search(prefix)
 +                if match:
 +                    prefix = prefix[match.end():]
 +                    if new_path.startswith(prefix):
 +                        new_path = new_path[len(prefix):]
              for pattern in self.url_patterns:
 }}}
 (patch attached)

 Now everything works as expected, I hope I did not miss anything.

 Let me know if you have any questions and thanks for providing this great
 framework :)

 Cheers,

 StalkR

-- 
Ticket URL: <https://code.djangoproject.com/ticket/18495>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to