[web] Fix handling of some http headers

The generator takes as argument a list of raw string values, and is supposed to return the parsed version. Calling str on that list makes no sense.

authorJulien Cristau <julien.cristau@logilab.fr>
changesetac0bc417b55f
branchdefault
phasepublic
hiddenno
parent revision#e8b9a3d23ad9 [web/request] deprecate user_callback
child revision#14452b344d19 [devtools] add a 'method' argument to RepoAccess.web_request
files modified by this revision
web/http_headers.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1394793494 -3600
# Fri Mar 14 11:38:14 2014 +0100
# Node ID ac0bc417b55fd590b38f8a511875f7531d105a6b
# Parent e8b9a3d23ad93e47391fe566012812245f2a751b
[web] Fix handling of some http headers

The generator takes as argument a list of raw string values, and is
supposed to return the parsed version. Calling str on that list makes
no sense.

diff --git a/web/http_headers.py b/web/http_headers.py
@@ -378,10 +378,18 @@
1  def last(seq):
2      """Return seq[-1]"""
3 
4      return seq[-1]
5 
6 +def unique(seq):
7 +    '''if seq is not a string, check it's a sequence of one element and return it'''
8 +    if isinstance(seq, basestring):
9 +        return seq
10 +    if len(seq) != 1:
11 +        raise ValueError('single value required, not %s' % seq)
12 +    return seq[0]
13 +
14  ##### Generation utilities
15  def quoteString(s):
16      return '"%s"' % s.replace('\\', '\\\\').replace('"', '\\"')
17 
18  def listGenerator(fun):
@@ -1470,23 +1478,23 @@
19      'Accept-Encoding': (iteritems, removeDefaultEncoding, listGenerator(generateAcceptQvalue),singleHeader),
20      'Accept-Language': (iteritems, listGenerator(generateAcceptQvalue),singleHeader),
21      'Authorization': (generateAuthorization,), # what is "credentials"
22      'Cookie':(generateCookie,singleHeader),
23      'Expect':(iteritems, listGenerator(generateExpect), singleHeader),
24 -    'From':(str,singleHeader),
25 -    'Host':(str,singleHeader),
26 +    'From':(unique, str,singleHeader),
27 +    'Host':(unique, str,singleHeader),
28      'If-Match':(listGenerator(generateStarOrETag), singleHeader),
29      'If-Modified-Since':(generateDateTime,singleHeader),
30      'If-None-Match':(listGenerator(generateStarOrETag), singleHeader),
31      'If-Range':(generateIfRange, singleHeader),
32      'If-Unmodified-Since':(generateDateTime,singleHeader),
33 -    'Max-Forwards':(str, singleHeader),
34 +    'Max-Forwards':(unique, str, singleHeader),
35  #    'Proxy-Authorization':str, # what is "credentials"
36      'Range':(generateRange,singleHeader),
37 -    'Referer':(str,singleHeader),
38 +    'Referer':(unique, str,singleHeader),
39      'TE': (iteritems, listGenerator(generateAcceptQvalue),singleHeader),
40 -    'User-Agent':(str,singleHeader),
41 +    'User-Agent':(unique, str,singleHeader),
42  }
43 
44  parser_response_headers = {
45      'Accept-Ranges':(tokenize, filterTokens),
46      'Age':(last,int),
@@ -1502,16 +1510,16 @@
47                           parseWWWAuthenticate,)
48  }
49 
50  generator_response_headers = {
51      'Accept-Ranges':(generateList, singleHeader),
52 -    'Age':(str, singleHeader),
53 +    'Age':(unique, str, singleHeader),
54      'ETag':(ETag.generate, singleHeader),
55 -    'Location':(str, singleHeader),
56 +    'Location':(unique, str, singleHeader),
57  #    'Proxy-Authenticate'
58      'Retry-After':(generateRetryAfter, singleHeader),
59 -    'Server':(str, singleHeader),
60 +    'Server':(unique, str, singleHeader),
61      'Set-Cookie':(generateSetCookie,),
62      'Set-Cookie2':(generateSetCookie2,),
63      'Vary':(generateList, singleHeader),
64      'WWW-Authenticate':(generateWWWAuthenticate,)
65  }
@@ -1531,12 +1539,12 @@
66 
67  generator_entity_headers = {
68      'Allow':(generateList, singleHeader),
69      'Content-Encoding':(generateList, singleHeader),
70      'Content-Language':(generateList, singleHeader),
71 -    'Content-Length':(str, singleHeader),
72 -    'Content-Location':(str, singleHeader),
73 +    'Content-Length':(unique, str, singleHeader),
74 +    'Content-Location':(unique, str, singleHeader),
75      'Content-MD5':(base64.encodestring, lambda x: x.strip("\n"), singleHeader),
76      'Content-Range':(generateContentRange, singleHeader),
77      'Content-Type':(generateContentType, singleHeader),
78      'Expires':(generateDateTime, singleHeader),
79      'Last-Modified':(generateDateTime, singleHeader),