paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@46 | 4 | Request helper classes. |
paulb@403 | 5 | |
paulb@403 | 6 | Copyright (C) 2004, 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@403 | 7 | |
paulb@403 | 8 | This library is free software; you can redistribute it and/or |
paulb@403 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@403 | 10 | License as published by the Free Software Foundation; either |
paulb@403 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@403 | 12 | |
paulb@403 | 13 | This library is distributed in the hope that it will be useful, |
paulb@403 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@403 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@403 | 16 | Lesser General Public License for more details. |
paulb@403 | 17 | |
paulb@403 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@403 | 19 | License along with this library; if not, write to the Free Software |
paulb@489 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 21 | """ |
paulb@46 | 22 | |
paulb@46 | 23 | class MessageBodyStream: |
paulb@46 | 24 | |
paulb@46 | 25 | """ |
paulb@46 | 26 | A naive stream class, providing a non-blocking stream for transactions when |
paulb@46 | 27 | reading the message body. According to the HTTP standard, the following |
paulb@46 | 28 | things decide how long the message is: |
paulb@46 | 29 | |
paulb@46 | 30 | * Use of the Content-Length header field (see 4.4 Message Length). |
paulb@46 | 31 | * Use of the Transfer-Coding header field (see 3.6 Transfer Codings), |
paulb@46 | 32 | particularly when the "chunked" coding is used. |
paulb@46 | 33 | |
paulb@46 | 34 | NOTE: For now, we don't support the Transfer-Coding business. |
paulb@46 | 35 | """ |
paulb@46 | 36 | |
paulb@46 | 37 | def __init__(self, stream, headers): |
paulb@46 | 38 | |
paulb@46 | 39 | """ |
paulb@46 | 40 | Initialise the object with the given underlying 'stream'. The supplied |
paulb@46 | 41 | 'headers' in a dictionary-style object are used to examine the nature of |
paulb@46 | 42 | the request. |
paulb@46 | 43 | """ |
paulb@46 | 44 | |
paulb@46 | 45 | self.stream = stream |
paulb@46 | 46 | self.headers = headers |
paulb@46 | 47 | self.length = int(headers.get("Content-Length") or 0) |
paulb@46 | 48 | |
paulb@46 | 49 | def read(self, limit=None): |
paulb@46 | 50 | |
paulb@46 | 51 | "Reads all remaining data from the message body." |
paulb@46 | 52 | |
paulb@46 | 53 | if limit is not None: |
paulb@46 | 54 | limit = min(limit, self.length) |
paulb@46 | 55 | else: |
paulb@46 | 56 | limit = self.length |
paulb@46 | 57 | data = self.stream.read(limit) |
paulb@46 | 58 | self.length = self.length - len(data) |
paulb@46 | 59 | return data |
paulb@46 | 60 | |
paulb@46 | 61 | def readline(self): |
paulb@46 | 62 | |
paulb@46 | 63 | "Reads a single line of data from the message body." |
paulb@46 | 64 | |
paulb@46 | 65 | data = [] |
paulb@46 | 66 | while self.length > 0: |
paulb@46 | 67 | data.append(self.read(1)) |
paulb@46 | 68 | if data[-1] == "\n": |
paulb@46 | 69 | break |
paulb@46 | 70 | return "".join(data) |
paulb@46 | 71 | |
paulb@46 | 72 | def readlines(self): |
paulb@46 | 73 | |
paulb@46 | 74 | """ |
paulb@46 | 75 | Reads all remaining data from the message body, splitting it into lines |
paulb@46 | 76 | and returning the data as a list of lines. |
paulb@46 | 77 | """ |
paulb@46 | 78 | |
paulb@46 | 79 | lines = self.read().split("\n") |
paulb@46 | 80 | for i in range(0, len(lines) - 1): |
paulb@46 | 81 | lines[i] = lines[i] + "\n" |
paulb@46 | 82 | return lines |
paulb@46 | 83 | |
paulb@46 | 84 | def close(self): |
paulb@46 | 85 | |
paulb@46 | 86 | "Closes the stream." |
paulb@46 | 87 | |
paulb@46 | 88 | self.stream.close() |
paulb@46 | 89 | |
paulb@464 | 90 | class HeaderValue: |
paulb@464 | 91 | |
paulb@464 | 92 | "A container for header information." |
paulb@464 | 93 | |
paulb@464 | 94 | def __init__(self, principal_value, **attributes): |
paulb@464 | 95 | |
paulb@464 | 96 | """ |
paulb@464 | 97 | Initialise the container with the given 'principal_value' and optional |
paulb@464 | 98 | keyword attributes representing the key=value pairs which accompany the |
paulb@464 | 99 | 'principal_value'. |
paulb@464 | 100 | """ |
paulb@464 | 101 | |
paulb@464 | 102 | self.principal_value = principal_value |
paulb@464 | 103 | self.attributes = attributes |
paulb@464 | 104 | |
paulb@464 | 105 | def __getattr__(self, name): |
paulb@464 | 106 | if self.attributes.has_key(name): |
paulb@464 | 107 | return self.attributes[name] |
paulb@464 | 108 | else: |
paulb@464 | 109 | raise AttributeError, name |
paulb@464 | 110 | |
paulb@464 | 111 | def __str__(self): |
paulb@464 | 112 | |
paulb@464 | 113 | """ |
paulb@464 | 114 | Format the header value object, producing a string suitable for the |
paulb@464 | 115 | response header field. |
paulb@464 | 116 | """ |
paulb@464 | 117 | |
paulb@464 | 118 | l = [] |
paulb@464 | 119 | if self.principal_value: |
paulb@464 | 120 | l.append(self.principal_value) |
paulb@464 | 121 | for name, value in self.attributes.items(): |
paulb@464 | 122 | l.append("; ") |
paulb@464 | 123 | l.append("%s=%s" % (name, value)) |
paulb@464 | 124 | |
paulb@464 | 125 | # Make sure that only ASCII is used. |
paulb@464 | 126 | |
paulb@464 | 127 | return "".join(l).encode("US-ASCII") |
paulb@464 | 128 | |
paulb@464 | 129 | class ContentType(HeaderValue): |
paulb@464 | 130 | |
paulb@464 | 131 | "A container for content type information." |
paulb@464 | 132 | |
paulb@464 | 133 | def __init__(self, media_type, charset=None, **attributes): |
paulb@464 | 134 | |
paulb@464 | 135 | """ |
paulb@464 | 136 | Initialise the container with the given 'media_type', an optional |
paulb@464 | 137 | 'charset', and optional keyword attributes representing the key=value |
paulb@464 | 138 | pairs which qualify content types. |
paulb@464 | 139 | """ |
paulb@464 | 140 | |
paulb@464 | 141 | if charset is not None: |
paulb@464 | 142 | attributes["charset"] = charset |
paulb@464 | 143 | HeaderValue.__init__(self, media_type, **attributes) |
paulb@464 | 144 | |
paulb@464 | 145 | def __getattr__(self, name): |
paulb@464 | 146 | if name == "media_type": |
paulb@464 | 147 | return self.principal_value |
paulb@464 | 148 | elif name == "charset": |
paulb@464 | 149 | return self.attributes.get("charset") |
paulb@464 | 150 | elif self.attributes.has_key(name): |
paulb@464 | 151 | return self.attributes[name] |
paulb@464 | 152 | else: |
paulb@464 | 153 | raise AttributeError, name |
paulb@464 | 154 | |
paulb@105 | 155 | class Cookie: |
paulb@105 | 156 | |
paulb@105 | 157 | """ |
paulb@105 | 158 | A simple cookie class for frameworks which do not return cookies in |
paulb@105 | 159 | structured form. |
paulb@105 | 160 | """ |
paulb@105 | 161 | |
paulb@105 | 162 | def __init__(self, name, value): |
paulb@105 | 163 | self.name = name |
paulb@105 | 164 | self.value = value |
paulb@105 | 165 | |
paulb@464 | 166 | class FileContent: |
paulb@464 | 167 | |
paulb@464 | 168 | """ |
paulb@464 | 169 | A simple class representing uploaded file content. This is useful in holding |
paulb@464 | 170 | metadata as well as being an indicator of such content in environments such |
paulb@464 | 171 | as Jython where it is not trivial to differentiate between plain strings and |
paulb@464 | 172 | Unicode in a fashion also applicable to CPython. |
paulb@551 | 173 | |
paulb@551 | 174 | Instances of this class contain the following attributes: |
paulb@551 | 175 | |
paulb@551 | 176 | * content - a plain string containing the contents of the uploaded file |
paulb@551 | 177 | * headers - a dictionary containing the headers associated with the |
paulb@551 | 178 | uploaded file |
paulb@464 | 179 | """ |
paulb@464 | 180 | |
paulb@464 | 181 | def __init__(self, content, headers=None): |
paulb@464 | 182 | |
paulb@464 | 183 | """ |
paulb@464 | 184 | Initialise the object with 'content' and optional 'headers' describing |
paulb@464 | 185 | the content. |
paulb@464 | 186 | """ |
paulb@464 | 187 | |
paulb@464 | 188 | self.content = content |
paulb@464 | 189 | self.headers = headers or {} |
paulb@464 | 190 | |
paulb@464 | 191 | def __str__(self): |
paulb@464 | 192 | return self.content |
paulb@464 | 193 | |
paulb@464 | 194 | def parse_header_value(header_class, header_value_str): |
paulb@464 | 195 | |
paulb@464 | 196 | """ |
paulb@464 | 197 | Create an object of the given 'header_class' by determining the details |
paulb@464 | 198 | of the given 'header_value_str' - a string containing the value of a |
paulb@464 | 199 | particular header. |
paulb@464 | 200 | """ |
paulb@464 | 201 | |
paulb@464 | 202 | if header_value_str is None: |
paulb@464 | 203 | return header_class(None) |
paulb@464 | 204 | |
paulb@464 | 205 | l = header_value_str.split(";") |
paulb@464 | 206 | attributes = {} |
paulb@464 | 207 | |
paulb@464 | 208 | # Find the attributes. |
paulb@464 | 209 | |
paulb@464 | 210 | principal_value, attributes_str = l[0].strip(), l[1:] |
paulb@464 | 211 | |
paulb@464 | 212 | for attribute_str in attributes_str: |
paulb@464 | 213 | t = attribute_str.split("=") |
paulb@464 | 214 | if len(t) > 1: |
paulb@464 | 215 | name, value = t[0].strip(), t[1].strip() |
paulb@464 | 216 | attributes[name] = value |
paulb@464 | 217 | |
paulb@464 | 218 | return header_class(principal_value, **attributes) |
paulb@464 | 219 | |
paulb@464 | 220 | def parse_headers(headers): |
paulb@464 | 221 | |
paulb@464 | 222 | """ |
paulb@464 | 223 | Parse the given 'headers' dictionary (containing names mapped to values), |
paulb@464 | 224 | returing a dictionary mapping names to HeaderValue objects. |
paulb@464 | 225 | """ |
paulb@464 | 226 | |
paulb@464 | 227 | new_headers = {} |
paulb@464 | 228 | for name, value in headers.items(): |
paulb@464 | 229 | new_headers[name] = parse_header_value(HeaderValue, value) |
paulb@464 | 230 | return new_headers |
paulb@464 | 231 | |
paulb@199 | 232 | def get_storage_items(storage_body): |
paulb@199 | 233 | |
paulb@199 | 234 | """ |
paulb@199 | 235 | Return the items (2-tuples of the form key, values) from the 'storage_body'. |
paulb@199 | 236 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 237 | """ |
paulb@199 | 238 | |
paulb@199 | 239 | items = [] |
paulb@199 | 240 | for key in storage_body.keys(): |
paulb@199 | 241 | items.append((key, storage_body[key])) |
paulb@199 | 242 | return items |
paulb@199 | 243 | |
paulb@199 | 244 | def get_body_fields(field_items, encoding): |
paulb@199 | 245 | |
paulb@199 | 246 | """ |
paulb@199 | 247 | Returns a dictionary mapping field names to lists of field values for all |
paulb@199 | 248 | entries in the given 'field_items' (2-tuples of the form key, values) using |
paulb@199 | 249 | the given 'encoding'. |
paulb@199 | 250 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 251 | """ |
paulb@199 | 252 | |
paulb@199 | 253 | fields = {} |
paulb@199 | 254 | |
paulb@199 | 255 | for field_name, field_values in field_items: |
paulb@440 | 256 | field_name = decode_value(field_name, encoding) |
paulb@440 | 257 | |
paulb@199 | 258 | if type(field_values) == type([]): |
paulb@199 | 259 | fields[field_name] = [] |
paulb@199 | 260 | for field_value in field_values: |
paulb@460 | 261 | fields[field_name].append(get_body_field_or_file(field_value, encoding)) |
paulb@199 | 262 | else: |
paulb@460 | 263 | fields[field_name] = [get_body_field_or_file(field_values, encoding)] |
paulb@199 | 264 | |
paulb@199 | 265 | return fields |
paulb@199 | 266 | |
paulb@460 | 267 | def get_body_field_or_file(field_value, encoding): |
paulb@460 | 268 | |
paulb@460 | 269 | """ |
paulb@460 | 270 | Returns the appropriate value for the given 'field_value' either for a |
paulb@460 | 271 | normal form field (thus employing the given 'encoding') or for a file |
paulb@460 | 272 | upload field (returning a plain string). |
paulb@460 | 273 | """ |
paulb@460 | 274 | |
paulb@460 | 275 | if hasattr(field_value, "headers") and field_value.headers.has_key("content-type"): |
paulb@460 | 276 | |
paulb@460 | 277 | # Detect stray FileUpload objects (eg. with Zope). |
paulb@460 | 278 | |
paulb@460 | 279 | if hasattr(field_value, "read"): |
paulb@464 | 280 | return FileContent(field_value.read(), parse_headers(field_value.headers)) |
paulb@460 | 281 | else: |
paulb@464 | 282 | return FileContent(field_value.value, parse_headers(field_value.headers)) |
paulb@460 | 283 | else: |
paulb@460 | 284 | return get_body_field(field_value, encoding) |
paulb@460 | 285 | |
paulb@199 | 286 | def get_body_field(field_str, encoding): |
paulb@199 | 287 | |
paulb@199 | 288 | """ |
paulb@199 | 289 | Returns the appropriate value for the given 'field_str' string using the |
paulb@199 | 290 | given 'encoding'. |
paulb@199 | 291 | """ |
paulb@199 | 292 | |
paulb@460 | 293 | # Detect stray FieldStorage objects (eg. with Webware). |
paulb@199 | 294 | |
paulb@199 | 295 | if hasattr(field_str, "value"): |
paulb@199 | 296 | return get_body_field(field_str.value, encoding) |
paulb@440 | 297 | else: |
paulb@440 | 298 | return decode_value(field_str, encoding) |
paulb@440 | 299 | |
paulb@440 | 300 | def decode_value(s, encoding): |
paulb@440 | 301 | if encoding is not None: |
paulb@250 | 302 | try: |
paulb@440 | 303 | return unicode(s, encoding) |
paulb@250 | 304 | except UnicodeError: |
paulb@440 | 305 | pass |
paulb@440 | 306 | # NOTE: Hacks to permit graceful failure. |
paulb@440 | 307 | return unicode(s, "iso-8859-1") |
paulb@199 | 308 | |
paulb@229 | 309 | def get_fields_from_query_string(query_string, decoder): |
paulb@229 | 310 | |
paulb@229 | 311 | """ |
paulb@229 | 312 | Returns a dictionary mapping field names to lists of values for the data |
paulb@229 | 313 | encoded in the given 'query_string'. Use the given 'decoder' function or |
paulb@229 | 314 | method to process the URL-encoded values. |
paulb@229 | 315 | """ |
paulb@229 | 316 | |
paulb@229 | 317 | fields = {} |
paulb@229 | 318 | |
paulb@229 | 319 | for pair in query_string.split("&"): |
paulb@229 | 320 | t = pair.split("=") |
paulb@229 | 321 | name = decoder(t[0]) |
paulb@229 | 322 | |
paulb@229 | 323 | if len(t) == 2: |
paulb@229 | 324 | value = decoder(t[1]) |
paulb@229 | 325 | else: |
paulb@229 | 326 | value = "" |
paulb@229 | 327 | |
paulb@289 | 328 | # NOTE: Remove empty names. |
paulb@289 | 329 | |
paulb@289 | 330 | if name: |
paulb@289 | 331 | if not fields.has_key(name): |
paulb@289 | 332 | fields[name] = [] |
paulb@289 | 333 | fields[name].append(value) |
paulb@229 | 334 | |
paulb@229 | 335 | return fields |
paulb@229 | 336 | |
paulb@250 | 337 | def filter_fields(all_fields, fields_from_path): |
paulb@250 | 338 | |
paulb@250 | 339 | """ |
paulb@250 | 340 | Taking items from the 'all_fields' dictionary, produce a new dictionary |
paulb@250 | 341 | which does not contain items from the 'fields_from_path' dictionary. |
paulb@250 | 342 | Return a new dictionary. |
paulb@250 | 343 | """ |
paulb@250 | 344 | |
paulb@250 | 345 | fields = {} |
paulb@250 | 346 | for field_name, field_values in all_fields.items(): |
paulb@250 | 347 | |
paulb@250 | 348 | # Find the path values for this field (for filtering below). |
paulb@250 | 349 | |
paulb@250 | 350 | if fields_from_path.has_key(field_name): |
paulb@250 | 351 | field_from_path_values = fields_from_path[field_name] |
paulb@250 | 352 | if type(field_from_path_values) != type([]): |
paulb@250 | 353 | field_from_path_values = [field_from_path_values] |
paulb@250 | 354 | else: |
paulb@250 | 355 | field_from_path_values = [] |
paulb@250 | 356 | |
paulb@250 | 357 | fields[field_name] = [] |
paulb@250 | 358 | for field_value in field_values: |
paulb@250 | 359 | |
paulb@250 | 360 | # Filter path values. |
paulb@250 | 361 | |
paulb@250 | 362 | if field_value not in field_from_path_values: |
paulb@250 | 363 | fields[field_name].append(field_value) |
paulb@250 | 364 | |
paulb@250 | 365 | # Remove filtered fields. |
paulb@250 | 366 | |
paulb@250 | 367 | if fields[field_name] == []: |
paulb@250 | 368 | del fields[field_name] |
paulb@250 | 369 | |
paulb@250 | 370 | return fields |
paulb@250 | 371 | |
paulb@46 | 372 | # vim: tabstop=4 expandtab shiftwidth=4 |