Code

Opened 5 years ago

Closed 5 years ago

#11638 closed (duplicate)

HttpResponseRedirect to a file causes UnicodeEncodeError

Reported by: BlindHunter Owned by: nobody
Component: HTTP handling Version: 1.1
Severity: Keywords: HttpResponseRedirect file UnicodeEncodeError us-ascii utf8
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by kmtracey)

I'm trying to download via GET a file with name in russian and getting

Environment:

Request Method: GET
Request URL: http://172.16.23.33/file/4719e0bdedaa4f741f032991894d52ecb08c3476a598504fd1fee92d
Django Version: 1.1
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'filez.filezupload',
 'debug_toolbar']
Installed Middleware:
('firepy.django.middleware.FirePHPMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  92.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/decorators.py" in __call__
  78.             return self.view_func(request, *args, **kwargs)
File "/var/www/filez/filezupload/views.py" in send_file
  259.         return HttpResponseRedirect(path)
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in __init__
  407.         self['Location'] = redirect_to
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in __setitem__
  320.         header, value = self._convert_to_ascii(header, value)
File "/usr/local/lib/python2.6/dist-packages/django/http/__init__.py" in _convert_to_ascii
  309.                     value = value.encode('us-ascii')

Exception Type: UnicodeEncodeError at /file/4719e0bdedaa4f741f032991894d52ecb08c3476a598504fd1fee92d
Exception Value: ('ascii', u'/site_media/upload/alecs/\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u043f\u043e\u0447\u0442\u044b.doc', 25, 34, 'ordinal not in range(128)')

The code which cause this error:

upfile = UpFile.objects.get(file_hash=file_hash)
path = os.path.join(settings.UPLOAD_ROOT, request.user.username,
upfile.file_name)
return HttpResponseRedirect(path)

If filename in English everything works ok.
The file was uploaded this way:

dir_path = os.path.join(settings.UPLOAD_ROOT, request.user.username)
upfile = UpFile.objects.create(user=user, file_name=file.name,
file_hash=file_hash,
file_content_type=file.content_type,file_path=file_path,
file_url=file_url, file_size=file.size)
path = '%s/%s' % (dir_path, file.name)
destination = open(path.encode('utf8'), 'wb+')
for chunk in file.chunks():
    destination.write(chunk)
destination.close() 
DEFAULT_CHARSET = 'utf-8'
DATABASE_ENGINE = 'sqlite3' 

The name of the file is correctly readable in my system(ls -lh):
locale

LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL= 

Attachments (0)

Change History (1)

comment:1 Changed 5 years ago by kmtracey

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Fixed formatting -- please use preview.

The description for #11522 seems to say the fix proposed there would also fix this case, so I'm going to close this as a dup of that one.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.