Commit 445919df authored by Pierre-Yves David's avatar Pierre-Yves David
Browse files

[downloadable] fix filename in HTTP header (closes #2522325, #2522324)

Before this changeset we use the `filename` header with utf8 encoded filename
all the time.

However RFC6266 says:

  The parameters "filename" and "filename*" differ only in that "filename*"
  uses the encoding defined in [RFC5987], allowing the use of characters not
  present in the ISO-8859-1 character set ([ISO-8859-1]).

Therefore, we alter the code to:

1. Use `filename` and `ascii` encoding whenever possible,
2. use `filename*` with `utf8` encoding otherwise (with a filename fallback for
   old browser)

We also switch the `content-disposition` value to attachement if filename is
specified, this will result as a mandatory download according to RFC6266. This
mandatory download is the expected behavior.

We changes the filename encoding to RFC5987 which is simpler, supported by all
and modern browser (including IE from version 6) and does not suffer from the
continuation issue. (see ticket #2522324 for details)

branch : stable
parent 4fabc81cd924
......@@ -22,6 +22,7 @@ __docformat__ = "restructuredtext en"
import time
import random
import base64
import urllib
from hashlib import sha1 # pylint: disable=E0611
from Cookie import SimpleCookie
from calendar import timegm
......@@ -38,7 +39,6 @@ from logilab.common.deprecation import deprecated
from logilab.mtconverter import xml_escape
from cubicweb.dbapi import DBAPIRequest
from cubicweb.mail import header
from cubicweb.uilib import remove_html_tags, js
from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
......@@ -618,10 +618,17 @@ class CubicWebRequestBase(DBAPIRequest):
content_type += ';charset=' + (encoding or self.encoding)
self.set_header('content-type', content_type)
if filename:
if isinstance(filename, unicode):
filename = header(filename).encode()
self.set_header('content-disposition', 'inline; filename=%s'
% filename)
header = ['attachment']
filename = filename.encode('ascii')
header.append('filename=' + filename)
except UnicodeEncodeError:
# fallback filename for very old browser
header.append('filename=' + filename.encode('ascii', 'ignore'))
# encoded filename according RFC5987
filename = urllib.quote(filename.encode('utf-8'), '')
header.append("filename*=utf-8''" + filename)
self.set_header('content-disposition', ';'.join(header))
# high level methods for HTML headers management ##########################
# -*- coding: utf-8 -*-
# copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact --
# This file is part of CubicWeb.
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <>.
from __future__ import with_statement
from functools import partial
from logilab.common.testlib import unittest_main
from cubicweb.devtools.testlib import CubicWebTC
from cubicweb import view
from cubicweb.predicates import is_instance
class IDownloadableTC(CubicWebTC):
def setUp(self):
super(IDownloadableTC, self).setUp()
class IDownloadableUser(view.EntityAdapter):
__regid__ = 'IDownloadable'
__select__ = is_instance('CWUser')
def download_content_type(self):
"""return MIME type of the downloadable content"""
return 'text/plain'
def download_encoding(self):
"""return encoding of the downloadable content"""
return 'ascii'
def download_file_name(self):
"""return file name of the downloadable content"""
return + '.txt'
def download_data(self):
return 'Babar is not dead!'
self.addCleanup(partial(self.vreg.unregister, IDownloadableUser))
def test_header_simple_case(self):
req = self.request()
req.form['vid'] = 'download'
req.form['eid'] = str(req.user.eid)
data = self.ctrl_publish(req,'view')
get = req.headers_out.getRawHeaders
self.assertEqual('Babar is not dead!', data)
def test_header_unicode_filename(self):
req = self.request()
self.create_user(req, login=u'cécilia', password='babar')
with self.login(u'cécilia', password='babar'):
req = self.request()
req.form['vid'] = 'download'
req.form['eid'] = str(req.user.eid)
get = req.headers_out.getRawHeaders
def test_header_unicode_long_filename(self):
req = self.request()
name = u'Bèrte_hô_grand_nôm_ça_va_totallement_déborder_de_la_limite_là'
self.create_user(req, login=name, password='babar')
with self.login(name, password='babar'):
req = self.request()
req.form['vid'] = 'download'
req.form['eid'] = str(req.user.eid)
get = req.headers_out.getRawHeaders
if __name__ == '__main__':
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment