1.1 --- a/WebStack/Generic.py Tue Aug 31 18:28:41 2004 +0000
1.2 +++ b/WebStack/Generic.py Wed Sep 01 22:51:40 2004 +0000
1.3 @@ -19,17 +19,38 @@
1.4
1.5 "A container for content type information."
1.6
1.7 - def __init__(self, content_type, charset=None, attributes=None):
1.8 + def __init__(self, media_type, charset=None, **attributes):
1.9 +
1.10 + """
1.11 + Initialise the container with the given 'media_type', an optional
1.12 + 'charset', and optional keyword attributes representing the key=value
1.13 + pairs which qualify content types.
1.14 + """
1.15 +
1.16 + self.media_type = media_type
1.17 + self.charset = charset
1.18 + self.attributes = attributes
1.19 +
1.20 + def __str__(self):
1.21
1.22 """
1.23 - Initialise the container with the given principal 'content_type', an
1.24 - optional 'charset', and optional 'attributes' (a list of 2-tuples each
1.25 - representing the key=value pairs which qualify content types).
1.26 + Format the content type object, producing a string suitable for the
1.27 + response header field.
1.28 """
1.29
1.30 - self.content_type = content_type
1.31 - self.charset = charset
1.32 - self.attributes = attributes
1.33 + l = []
1.34 + if self.media_type:
1.35 + l.append(self.media_type)
1.36 + if self.charset:
1.37 + l.append("; ")
1.38 + l.append("charset=%s" % self.charset)
1.39 + for name, value in self.attributes.items():
1.40 + l.append("; ")
1.41 + l.append("%s=%s" % (name, value))
1.42 +
1.43 + # Make sure that only ASCII is used.
1.44 +
1.45 + return "".join(l).encode("US-ASCII")
1.46
1.47 class Transaction:
1.48
1.49 @@ -38,6 +59,10 @@
1.50 overridden.
1.51 """
1.52
1.53 + # The default charset ties output together with body field interpretation.
1.54 +
1.55 + default_charset = "iso-8859-1"
1.56 +
1.57 def commit(self):
1.58
1.59 """
1.60 @@ -57,40 +82,26 @@
1.61 """
1.62
1.63 if content_type_field is None:
1.64 - return ContentType(None, "iso-8859-1")
1.65 + return ContentType(None)
1.66
1.67 l = content_type_field.split(";")
1.68 - content_type_attributes = []
1.69 - content_type_charset = "iso-8859-1"
1.70 + attributes = {}
1.71 + charset = None
1.72
1.73 # Find the charset and remember all other attributes.
1.74
1.75 - content_type_str, attributes = l[0].strip(), l[1:]
1.76 -
1.77 - for attribute in attributes:
1.78 - t = attribute.strip().split("=")
1.79 - if t[0] == "charset" and len(t) > 1:
1.80 - content_type_charset = t[1]
1.81 - else:
1.82 - content_type_attributes.append(t)
1.83 -
1.84 - return ContentType(content_type_str, content_type_charset, content_type_attributes)
1.85 -
1.86 - def format_content_type(self, content_type):
1.87 + media_type, attributes_str = l[0].strip(), l[1:]
1.88
1.89 - """
1.90 - Format the given 'content_type' object, producing a string suitable for
1.91 - the response header field.
1.92 - """
1.93 + for attribute_str in attributes_str:
1.94 + t = attribute_str.split("=")
1.95 + if len(t) > 1:
1.96 + name, value = t[0].strip(), t[1].strip()
1.97 + if name == "charset":
1.98 + charset = value
1.99 + else:
1.100 + attributes[name] = value
1.101
1.102 - if content_type.charset:
1.103 - field = "%s; charset=%s" % (content_type.content_type, content_type.charset)
1.104 - else:
1.105 - field = content_type.content_type
1.106 -
1.107 - # Make sure that only ASCII is used in the header.
1.108 -
1.109 - return field.encode("US-ASCII")
1.110 + return ContentType(media_type, charset, **attributes)
1.111
1.112 def format_header_value(self, value):
1.113