web/request.py
branchtls-sprint
changeset 1716 b12d9e22bac3
parent 1560 7dd2a81b8bc8
child 1717 d2c4d3bd0602
equal deleted inserted replaced
1715:cba9f175da2d 1716:b12d9e22bac3
   596             except Exception, ex:
   596             except Exception, ex:
   597                 self.debug('bad authorization %s (%s: %s)',
   597                 self.debug('bad authorization %s (%s: %s)',
   598                            auth, ex.__class__.__name__, ex)
   598                            auth, ex.__class__.__name__, ex)
   599         return None, None
   599         return None, None
   600 
   600 
       
   601     @obsolete("use parse_accept_header('Accept-Language')")
   601     def header_accept_language(self):
   602     def header_accept_language(self):
   602         """returns an ordered list of preferred languages"""
   603         """returns an ordered list of preferred languages"""
   603         acceptedlangs = self.get_header('Accept-Language', '')
   604         return [value.split('-')[0] for value in
   604         langs = []
   605                 self.parse_accept_header('Accept-Language')]
   605         for langinfo in acceptedlangs.split(','):
   606 
       
   607     def parse_accept_header(self, header):
       
   608         """returns an ordered list of preferred languages"""
       
   609         accepteds = self.get_header(header, '')
       
   610         values = []
       
   611         for info in accepteds.split(','):
   606             try:
   612             try:
   607                 lang, score = langinfo.split(';')
   613                 value, scores = info.split(';', 1)
   608                 score = float(score[2:]) # remove 'q='
       
   609             except ValueError:
   614             except ValueError:
   610                 lang = langinfo
   615                 value = info
   611                 score = 1.0
   616                 score = 1.0
   612             lang = lang.split('-')[0]
   617             else:
   613             langs.append( (score, lang) )
   618                 for score in scores.split(';'):
   614         langs.sort(reverse=True)
   619                     try:
   615         return (lang for (score, lang) in langs)
   620                         scorekey, scoreval = score.split('=')
       
   621                         if scorekey = 'q': # XXX 'level'
       
   622                             score = float(score[2:]) # remove 'q='
       
   623                     except ValueError:
       
   624                         continue
       
   625             values.append(value, score)
       
   626         values.sort(reverse=True)
       
   627         return (value for (score, value) in values)
   616 
   628 
   617     def header_if_modified_since(self):
   629     def header_if_modified_since(self):
   618         """If the HTTP header If-modified-since is set, return the equivalent
   630         """If the HTTP header If-modified-since is set, return the equivalent
   619         mx date time value (GMT), else return None
   631         mx date time value (GMT), else return None
   620         """
   632         """