paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@46 | 4 | Request helper classes. |
paulb@46 | 5 | """ |
paulb@46 | 6 | |
paulb@46 | 7 | class MessageBodyStream: |
paulb@46 | 8 | |
paulb@46 | 9 | """ |
paulb@46 | 10 | A naive stream class, providing a non-blocking stream for transactions when |
paulb@46 | 11 | reading the message body. According to the HTTP standard, the following |
paulb@46 | 12 | things decide how long the message is: |
paulb@46 | 13 | |
paulb@46 | 14 | * Use of the Content-Length header field (see 4.4 Message Length). |
paulb@46 | 15 | * Use of the Transfer-Coding header field (see 3.6 Transfer Codings), |
paulb@46 | 16 | particularly when the "chunked" coding is used. |
paulb@46 | 17 | |
paulb@46 | 18 | NOTE: For now, we don't support the Transfer-Coding business. |
paulb@46 | 19 | """ |
paulb@46 | 20 | |
paulb@46 | 21 | def __init__(self, stream, headers): |
paulb@46 | 22 | |
paulb@46 | 23 | """ |
paulb@46 | 24 | Initialise the object with the given underlying 'stream'. The supplied |
paulb@46 | 25 | 'headers' in a dictionary-style object are used to examine the nature of |
paulb@46 | 26 | the request. |
paulb@46 | 27 | """ |
paulb@46 | 28 | |
paulb@46 | 29 | self.stream = stream |
paulb@46 | 30 | self.headers = headers |
paulb@46 | 31 | self.length = int(headers.get("Content-Length") or 0) |
paulb@46 | 32 | |
paulb@46 | 33 | def read(self, limit=None): |
paulb@46 | 34 | |
paulb@46 | 35 | "Reads all remaining data from the message body." |
paulb@46 | 36 | |
paulb@46 | 37 | if limit is not None: |
paulb@46 | 38 | limit = min(limit, self.length) |
paulb@46 | 39 | else: |
paulb@46 | 40 | limit = self.length |
paulb@46 | 41 | data = self.stream.read(limit) |
paulb@46 | 42 | self.length = self.length - len(data) |
paulb@46 | 43 | return data |
paulb@46 | 44 | |
paulb@46 | 45 | def readline(self): |
paulb@46 | 46 | |
paulb@46 | 47 | "Reads a single line of data from the message body." |
paulb@46 | 48 | |
paulb@46 | 49 | data = [] |
paulb@46 | 50 | while self.length > 0: |
paulb@46 | 51 | data.append(self.read(1)) |
paulb@46 | 52 | if data[-1] == "\n": |
paulb@46 | 53 | break |
paulb@46 | 54 | return "".join(data) |
paulb@46 | 55 | |
paulb@46 | 56 | def readlines(self): |
paulb@46 | 57 | |
paulb@46 | 58 | """ |
paulb@46 | 59 | Reads all remaining data from the message body, splitting it into lines |
paulb@46 | 60 | and returning the data as a list of lines. |
paulb@46 | 61 | """ |
paulb@46 | 62 | |
paulb@46 | 63 | lines = self.read().split("\n") |
paulb@46 | 64 | for i in range(0, len(lines) - 1): |
paulb@46 | 65 | lines[i] = lines[i] + "\n" |
paulb@46 | 66 | return lines |
paulb@46 | 67 | |
paulb@46 | 68 | def close(self): |
paulb@46 | 69 | |
paulb@46 | 70 | "Closes the stream." |
paulb@46 | 71 | |
paulb@46 | 72 | self.stream.close() |
paulb@46 | 73 | |
paulb@105 | 74 | class Cookie: |
paulb@105 | 75 | |
paulb@105 | 76 | """ |
paulb@105 | 77 | A simple cookie class for frameworks which do not return cookies in |
paulb@105 | 78 | structured form. |
paulb@105 | 79 | """ |
paulb@105 | 80 | |
paulb@105 | 81 | def __init__(self, name, value): |
paulb@105 | 82 | self.name = name |
paulb@105 | 83 | self.value = value |
paulb@105 | 84 | |
paulb@199 | 85 | def get_storage_items(storage_body): |
paulb@199 | 86 | |
paulb@199 | 87 | """ |
paulb@199 | 88 | Return the items (2-tuples of the form key, values) from the 'storage_body'. |
paulb@199 | 89 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 90 | """ |
paulb@199 | 91 | |
paulb@199 | 92 | items = [] |
paulb@199 | 93 | for key in storage_body.keys(): |
paulb@199 | 94 | items.append((key, storage_body[key])) |
paulb@199 | 95 | return items |
paulb@199 | 96 | |
paulb@199 | 97 | def get_body_fields(field_items, encoding): |
paulb@199 | 98 | |
paulb@199 | 99 | """ |
paulb@199 | 100 | Returns a dictionary mapping field names to lists of field values for all |
paulb@199 | 101 | entries in the given 'field_items' (2-tuples of the form key, values) using |
paulb@199 | 102 | the given 'encoding'. |
paulb@199 | 103 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 104 | """ |
paulb@199 | 105 | |
paulb@199 | 106 | fields = {} |
paulb@199 | 107 | |
paulb@199 | 108 | for field_name, field_values in field_items: |
paulb@199 | 109 | if type(field_values) == type([]): |
paulb@199 | 110 | fields[field_name] = [] |
paulb@199 | 111 | for field_value in field_values: |
paulb@199 | 112 | fields[field_name].append(get_body_field(field_value.value, encoding)) |
paulb@199 | 113 | else: |
paulb@199 | 114 | fields[field_name] = [get_body_field(field_values.value, encoding)] |
paulb@199 | 115 | |
paulb@199 | 116 | return fields |
paulb@199 | 117 | |
paulb@199 | 118 | def get_body_field(field_str, encoding): |
paulb@199 | 119 | |
paulb@199 | 120 | """ |
paulb@199 | 121 | Returns the appropriate value for the given 'field_str' string using the |
paulb@199 | 122 | given 'encoding'. |
paulb@199 | 123 | """ |
paulb@199 | 124 | |
paulb@200 | 125 | # Detect stray FieldStorage objects (eg. with Webware) or stray FileUpload |
paulb@200 | 126 | # objects (eg. with Zope). |
paulb@199 | 127 | |
paulb@199 | 128 | if hasattr(field_str, "value"): |
paulb@199 | 129 | return get_body_field(field_str.value, encoding) |
paulb@200 | 130 | elif hasattr(field_str, "read"): |
paulb@200 | 131 | return field_str.read() |
paulb@199 | 132 | elif encoding is not None: |
paulb@250 | 133 | try: |
paulb@250 | 134 | return unicode(field_str, encoding) |
paulb@250 | 135 | except UnicodeError: |
paulb@250 | 136 | # NOTE: Hacks to permit graceful failure. |
paulb@250 | 137 | try: |
paulb@250 | 138 | return unicode(field_str, "iso-8859-1") |
paulb@250 | 139 | except UnicodeError: |
paulb@250 | 140 | return u"" |
paulb@199 | 141 | else: |
paulb@199 | 142 | return field_str |
paulb@199 | 143 | |
paulb@229 | 144 | def get_fields_from_query_string(query_string, decoder): |
paulb@229 | 145 | |
paulb@229 | 146 | """ |
paulb@229 | 147 | Returns a dictionary mapping field names to lists of values for the data |
paulb@229 | 148 | encoded in the given 'query_string'. Use the given 'decoder' function or |
paulb@229 | 149 | method to process the URL-encoded values. |
paulb@229 | 150 | """ |
paulb@229 | 151 | |
paulb@229 | 152 | fields = {} |
paulb@229 | 153 | |
paulb@229 | 154 | for pair in query_string.split("&"): |
paulb@229 | 155 | t = pair.split("=") |
paulb@229 | 156 | name = decoder(t[0]) |
paulb@229 | 157 | |
paulb@229 | 158 | if len(t) == 2: |
paulb@229 | 159 | value = decoder(t[1]) |
paulb@229 | 160 | else: |
paulb@229 | 161 | value = "" |
paulb@229 | 162 | |
paulb@289 | 163 | # NOTE: Remove empty names. |
paulb@289 | 164 | |
paulb@289 | 165 | if name: |
paulb@289 | 166 | if not fields.has_key(name): |
paulb@289 | 167 | fields[name] = [] |
paulb@289 | 168 | fields[name].append(value) |
paulb@229 | 169 | |
paulb@229 | 170 | return fields |
paulb@229 | 171 | |
paulb@250 | 172 | def filter_fields(all_fields, fields_from_path): |
paulb@250 | 173 | |
paulb@250 | 174 | """ |
paulb@250 | 175 | Taking items from the 'all_fields' dictionary, produce a new dictionary |
paulb@250 | 176 | which does not contain items from the 'fields_from_path' dictionary. |
paulb@250 | 177 | Return a new dictionary. |
paulb@250 | 178 | """ |
paulb@250 | 179 | |
paulb@250 | 180 | fields = {} |
paulb@250 | 181 | for field_name, field_values in all_fields.items(): |
paulb@250 | 182 | |
paulb@250 | 183 | # Find the path values for this field (for filtering below). |
paulb@250 | 184 | |
paulb@250 | 185 | if fields_from_path.has_key(field_name): |
paulb@250 | 186 | field_from_path_values = fields_from_path[field_name] |
paulb@250 | 187 | if type(field_from_path_values) != type([]): |
paulb@250 | 188 | field_from_path_values = [field_from_path_values] |
paulb@250 | 189 | else: |
paulb@250 | 190 | field_from_path_values = [] |
paulb@250 | 191 | |
paulb@250 | 192 | fields[field_name] = [] |
paulb@250 | 193 | for field_value in field_values: |
paulb@250 | 194 | |
paulb@250 | 195 | # Filter path values. |
paulb@250 | 196 | |
paulb@250 | 197 | if field_value not in field_from_path_values: |
paulb@250 | 198 | fields[field_name].append(field_value) |
paulb@250 | 199 | |
paulb@250 | 200 | # Remove filtered fields. |
paulb@250 | 201 | |
paulb@250 | 202 | if fields[field_name] == []: |
paulb@250 | 203 | del fields[field_name] |
paulb@250 | 204 | |
paulb@250 | 205 | return fields |
paulb@250 | 206 | |
paulb@46 | 207 | # vim: tabstop=4 expandtab shiftwidth=4 |