web/request.py
changeset 8645 310040c668c0
parent 8609 112a04c0473d
parent 8612 3bb96935a6b9
child 8695 358d8bed9626
equal deleted inserted replaced
8644:97202ea671e4 8645:310040c668c0
   598             warn('[3.13] remove_cookie now take only a name as argument',
   598             warn('[3.13] remove_cookie now take only a name as argument',
   599                  DeprecationWarning, stacklevel=2)
   599                  DeprecationWarning, stacklevel=2)
   600             name = bwcompat
   600             name = bwcompat
   601         self.set_cookie(name, '', maxage=0, expires=date(2000, 1, 1))
   601         self.set_cookie(name, '', maxage=0, expires=date(2000, 1, 1))
   602 
   602 
   603     def set_content_type(self, content_type, filename=None, encoding=None):
   603     def set_content_type(self, content_type, filename=None, encoding=None,
       
   604                          disposition='inline'):
   604         """set output content type for this request. An optional filename
   605         """set output content type for this request. An optional filename
   605         may be given
   606         may be given.
       
   607 
       
   608         The disposition argument may be `attachement` or `inline` as specified
       
   609         for the Content-disposition HTTP header. The disposition parameter have
       
   610         no effect if no filename are specified.
   606         """
   611         """
   607         if content_type.startswith('text/') and ';charset=' not in content_type:
   612         if content_type.startswith('text/') and ';charset=' not in content_type:
   608             content_type += ';charset=' + (encoding or self.encoding)
   613             content_type += ';charset=' + (encoding or self.encoding)
   609         self.set_header('content-type', content_type)
   614         self.set_header('content-type', content_type)
   610         if filename:
   615         if filename:
   611             header = ['attachment']
   616             header = [disposition]
       
   617             unicode_filename = None
   612             try:
   618             try:
   613                 filename = filename.encode('ascii')
   619                 ascii_filename = filename.encode('ascii')
   614                 header.append('filename=' + filename)
       
   615             except UnicodeEncodeError:
   620             except UnicodeEncodeError:
   616                 # fallback filename for very old browser
   621                 # fallback filename for very old browser
   617                 header.append('filename=' + filename.encode('ascii', 'ignore'))
   622                 unicode_filename = filename
       
   623                 ascii_filename = filename.encode('ascii', 'ignore')
       
   624             # escape " and \
       
   625             # see http://greenbytes.de/tech/tc2231/#attwithfilenameandextparamescaped
       
   626             ascii_filename = ascii_filename.replace('\x5c', r'\\').replace('"', r'\"')
       
   627             header.append('filename="%s"' % ascii_filename)
       
   628             if unicode_filename is not None:
   618                 # encoded filename according RFC5987
   629                 # encoded filename according RFC5987
   619                 filename = urllib.quote(filename.encode('utf-8'), '')
   630                 urlquoted_filename = urllib.quote(unicode_filename.encode('utf-8'), '')
   620                 header.append("filename*=utf-8''" + filename)
   631                 header.append("filename*=utf-8''" + urlquoted_filename)
   621             self.set_header('content-disposition', ';'.join(header))
   632             self.set_header('content-disposition', ';'.join(header))
   622 
   633 
   623     # high level methods for HTML headers management ##########################
   634     # high level methods for HTML headers management ##########################
   624 
   635 
   625     def add_onload(self, jscode):
   636     def add_onload(self, jscode):