1.1 --- a/WebStack/Generic.py Fri Aug 26 18:08:56 2005 +0000
1.2 +++ b/WebStack/Generic.py Fri Aug 26 18:10:52 2005 +0000
1.3 @@ -35,7 +35,7 @@
1.4 """
1.5
1.6 import urllib
1.7 -from WebStack.Helpers.Request import Cookie
1.8 +from WebStack.Helpers.Request import Cookie, parse_header_value, ContentType, HeaderValue
1.9
1.10 class EndOfResponse(Exception):
1.11
1.12 @@ -43,71 +43,6 @@
1.13
1.14 pass
1.15
1.16 -class HeaderValue:
1.17 -
1.18 - "A container for header information."
1.19 -
1.20 - def __init__(self, principal_value, **attributes):
1.21 -
1.22 - """
1.23 - Initialise the container with the given 'principal_value' and optional
1.24 - keyword attributes representing the key=value pairs which accompany the
1.25 - 'principal_value'.
1.26 - """
1.27 -
1.28 - self.principal_value = principal_value
1.29 - self.attributes = attributes
1.30 -
1.31 - def __getattr__(self, name):
1.32 - if self.attributes.has_key(name):
1.33 - return self.attributes[name]
1.34 - else:
1.35 - raise AttributeError, name
1.36 -
1.37 - def __str__(self):
1.38 -
1.39 - """
1.40 - Format the header value object, producing a string suitable for the
1.41 - response header field.
1.42 - """
1.43 -
1.44 - l = []
1.45 - if self.principal_value:
1.46 - l.append(self.principal_value)
1.47 - for name, value in self.attributes.items():
1.48 - l.append("; ")
1.49 - l.append("%s=%s" % (name, value))
1.50 -
1.51 - # Make sure that only ASCII is used.
1.52 -
1.53 - return "".join(l).encode("US-ASCII")
1.54 -
1.55 -class ContentType(HeaderValue):
1.56 -
1.57 - "A container for content type information."
1.58 -
1.59 - def __init__(self, media_type, charset=None, **attributes):
1.60 -
1.61 - """
1.62 - Initialise the container with the given 'media_type', an optional
1.63 - 'charset', and optional keyword attributes representing the key=value
1.64 - pairs which qualify content types.
1.65 - """
1.66 -
1.67 - if charset is not None:
1.68 - attributes["charset"] = charset
1.69 - HeaderValue.__init__(self, media_type, **attributes)
1.70 -
1.71 - def __getattr__(self, name):
1.72 - if name == "media_type":
1.73 - return self.principal_value
1.74 - elif name == "charset":
1.75 - return self.attributes.get("charset")
1.76 - elif self.attributes.has_key(name):
1.77 - return self.attributes[name]
1.78 - else:
1.79 - raise AttributeError, name
1.80 -
1.81 class Transaction:
1.82
1.83 """
1.84 @@ -138,23 +73,9 @@
1.85 particular header.
1.86 """
1.87
1.88 - if header_value_str is None:
1.89 - return header_class(None)
1.90 -
1.91 - l = header_value_str.split(";")
1.92 - attributes = {}
1.93 -
1.94 - # Find the attributes.
1.95 + # Now uses the WebStack.Helpers.Request function of the same name.
1.96
1.97 - principal_value, attributes_str = l[0].strip(), l[1:]
1.98 -
1.99 - for attribute_str in attributes_str:
1.100 - t = attribute_str.split("=")
1.101 - if len(t) > 1:
1.102 - name, value = t[0].strip(), t[1].strip()
1.103 - attributes[name] = value
1.104 -
1.105 - return header_class(principal_value, **attributes)
1.106 + return parse_header_value(header_class, header_value_str)
1.107
1.108 def parse_content_type(self, content_type_field):
1.109