#12464: Empty PATH_INFO causes blank SCRIPT_NAME
------------------------------------+---------------------------------------
          Reporter:  chrisw         |         Owner:  nobody
            Status:  new            |     Milestone:        
         Component:  Uncategorized  |       Version:  1.1   
        Resolution:                 |      Keywords:        
             Stage:  Unreviewed     |     Has_patch:  0     
        Needs_docs:  0              |   Needs_tests:  0     
Needs_better_patch:  0              |  
------------------------------------+---------------------------------------
Changes (by ramiro):

  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0

Old description:

> Take the following Apache config snippet:
>
> RewriteEngine On
> WSGIScriptAlias /project /django/project/bin/django.wsgi
>
> This results in mod_rewrite setting the SCRIPT_URL environment variable.
>
> The code at
> http://code.djangoproject.com/browser/django/tags/releases/1.1.1/django/core/handlers/base.py#L204:
>
>     script_url = environ.get('SCRIPT_URL', u'')
>     if not script_url:
>         script_url = environ.get('REDIRECT_URL', u'')
>     if script_url:
>         return force_unicode(script_url[:-len(environ.get('PATH_INFO',
> ''))])
>     return force_unicode(environ.get('SCRIPT_NAME', u''))
>
> ...behaves incorrectly when, in the above example, /project is requested
> from Apache.
>
> The problem is when PATH_INFO is empty, as it is in this case. Python
> has no notion of positive or negative zero (;-)) so we end up returning
> script_url[:0]. This results in request.META['SCRIPT_NAME'] being set to
> '', which in turn results in urls being generated incorrectly.
>
> A workaround for this is to add the following rewrite rule before
> WSGIScriptAlias:
>
> RewriteRule ^/project$ /project/ [R]
>
> This bug is potentially related to #9435, but this is ticket describes a
> real world problem that I believe has bitten more people than just me...
>
> A solution for this could well just be to change the section to:
>
>     if script_url:
>         path_info = environ.get('PATH_INFO', '')
>         if path_info:
>             script_url = script_url[:-len(path_info)]
>         return force_unicode(script_url)
>     return force_unicode(environ.get('SCRIPT_NAME', u''))
>
> ...but I have no idea where to go about writing a test.

New description:

 Take the following Apache config snippet:

 {{{
 RewriteEngine On
 WSGIScriptAlias /project /django/project/bin/django.wsgi
 }}}

 This results in mod_rewrite setting the SCRIPT_URL environment variable.

 The code at
 
http://code.djangoproject.com/browser/django/tags/releases/1.1.1/django/core/handlers/base.py#L204:

 {{{
     script_url = environ.get('SCRIPT_URL', u'')
     if not script_url:
         script_url = environ.get('REDIRECT_URL', u'')
     if script_url:
         return force_unicode(script_url[:-len(environ.get('PATH_INFO',
 ''))])
     return force_unicode(environ.get('SCRIPT_NAME', u''))
 }}}

 ...behaves incorrectly when, in the above example, /project is requested
 from Apache.

 The problem is when PATH_INFO is empty, as it is in this case. Python
 has no notion of positive or negative zero (;-)) so we end up returning
 `script_url[:0]`. This results in `request.META['SCRIPT_NAME']`being set
 to `''`, which in turn results in urls being generated incorrectly.

 A workaround for this is to add the following rewrite rule before
 !WSGIScriptAlias:

 {{{
 RewriteRule ^/project$ /project/ [R]
 }}}

 This bug is potentially related to #9435, but this is ticket describes a
 real world problem that I believe has bitten more people than just me...

 A solution for this could well just be to change the section to:

 {{{
     if script_url:
         path_info = environ.get('PATH_INFO', '')
         if path_info:
             script_url = script_url[:-len(path_info)]
         return force_unicode(script_url)
     return force_unicode(environ.get('SCRIPT_NAME', u''))
 }}}

 ...but I have no idea where to go about writing a test.

Comment:

 (reformatted description, please use the Preview button before submitting
 a ticket)

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12464#comment:1>
Django <http://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-upda...@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