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@403 | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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@105 | 90 | class Cookie: |
paulb@105 | 91 | |
paulb@105 | 92 | """ |
paulb@105 | 93 | A simple cookie class for frameworks which do not return cookies in |
paulb@105 | 94 | structured form. |
paulb@105 | 95 | """ |
paulb@105 | 96 | |
paulb@105 | 97 | def __init__(self, name, value): |
paulb@105 | 98 | self.name = name |
paulb@105 | 99 | self.value = value |
paulb@105 | 100 | |
paulb@199 | 101 | def get_storage_items(storage_body): |
paulb@199 | 102 | |
paulb@199 | 103 | """ |
paulb@199 | 104 | Return the items (2-tuples of the form key, values) from the 'storage_body'. |
paulb@199 | 105 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 106 | """ |
paulb@199 | 107 | |
paulb@199 | 108 | items = [] |
paulb@199 | 109 | for key in storage_body.keys(): |
paulb@199 | 110 | items.append((key, storage_body[key])) |
paulb@199 | 111 | return items |
paulb@199 | 112 | |
paulb@199 | 113 | def get_body_fields(field_items, encoding): |
paulb@199 | 114 | |
paulb@199 | 115 | """ |
paulb@199 | 116 | Returns a dictionary mapping field names to lists of field values for all |
paulb@199 | 117 | entries in the given 'field_items' (2-tuples of the form key, values) using |
paulb@199 | 118 | the given 'encoding'. |
paulb@199 | 119 | This is used in conjunction with FieldStorage objects. |
paulb@199 | 120 | """ |
paulb@199 | 121 | |
paulb@199 | 122 | fields = {} |
paulb@199 | 123 | |
paulb@199 | 124 | for field_name, field_values in field_items: |
paulb@440 | 125 | field_name = decode_value(field_name, encoding) |
paulb@440 | 126 | |
paulb@199 | 127 | if type(field_values) == type([]): |
paulb@199 | 128 | fields[field_name] = [] |
paulb@199 | 129 | for field_value in field_values: |
paulb@199 | 130 | fields[field_name].append(get_body_field(field_value.value, encoding)) |
paulb@199 | 131 | else: |
paulb@199 | 132 | fields[field_name] = [get_body_field(field_values.value, encoding)] |
paulb@199 | 133 | |
paulb@199 | 134 | return fields |
paulb@199 | 135 | |
paulb@199 | 136 | def get_body_field(field_str, encoding): |
paulb@199 | 137 | |
paulb@199 | 138 | """ |
paulb@199 | 139 | Returns the appropriate value for the given 'field_str' string using the |
paulb@199 | 140 | given 'encoding'. |
paulb@199 | 141 | """ |
paulb@199 | 142 | |
paulb@200 | 143 | # Detect stray FieldStorage objects (eg. with Webware) or stray FileUpload |
paulb@200 | 144 | # objects (eg. with Zope). |
paulb@199 | 145 | |
paulb@199 | 146 | if hasattr(field_str, "value"): |
paulb@199 | 147 | return get_body_field(field_str.value, encoding) |
paulb@200 | 148 | elif hasattr(field_str, "read"): |
paulb@200 | 149 | return field_str.read() |
paulb@440 | 150 | else: |
paulb@440 | 151 | return decode_value(field_str, encoding) |
paulb@440 | 152 | |
paulb@440 | 153 | def decode_value(s, encoding): |
paulb@440 | 154 | if encoding is not None: |
paulb@250 | 155 | try: |
paulb@440 | 156 | return unicode(s, encoding) |
paulb@250 | 157 | except UnicodeError: |
paulb@440 | 158 | pass |
paulb@440 | 159 | # NOTE: Hacks to permit graceful failure. |
paulb@440 | 160 | return unicode(s, "iso-8859-1") |
paulb@199 | 161 | |
paulb@229 | 162 | def get_fields_from_query_string(query_string, decoder): |
paulb@229 | 163 | |
paulb@229 | 164 | """ |
paulb@229 | 165 | Returns a dictionary mapping field names to lists of values for the data |
paulb@229 | 166 | encoded in the given 'query_string'. Use the given 'decoder' function or |
paulb@229 | 167 | method to process the URL-encoded values. |
paulb@229 | 168 | """ |
paulb@229 | 169 | |
paulb@229 | 170 | fields = {} |
paulb@229 | 171 | |
paulb@229 | 172 | for pair in query_string.split("&"): |
paulb@229 | 173 | t = pair.split("=") |
paulb@229 | 174 | name = decoder(t[0]) |
paulb@229 | 175 | |
paulb@229 | 176 | if len(t) == 2: |
paulb@229 | 177 | value = decoder(t[1]) |
paulb@229 | 178 | else: |
paulb@229 | 179 | value = "" |
paulb@229 | 180 | |
paulb@289 | 181 | # NOTE: Remove empty names. |
paulb@289 | 182 | |
paulb@289 | 183 | if name: |
paulb@289 | 184 | if not fields.has_key(name): |
paulb@289 | 185 | fields[name] = [] |
paulb@289 | 186 | fields[name].append(value) |
paulb@229 | 187 | |
paulb@229 | 188 | return fields |
paulb@229 | 189 | |
paulb@250 | 190 | def filter_fields(all_fields, fields_from_path): |
paulb@250 | 191 | |
paulb@250 | 192 | """ |
paulb@250 | 193 | Taking items from the 'all_fields' dictionary, produce a new dictionary |
paulb@250 | 194 | which does not contain items from the 'fields_from_path' dictionary. |
paulb@250 | 195 | Return a new dictionary. |
paulb@250 | 196 | """ |
paulb@250 | 197 | |
paulb@250 | 198 | fields = {} |
paulb@250 | 199 | for field_name, field_values in all_fields.items(): |
paulb@250 | 200 | |
paulb@250 | 201 | # Find the path values for this field (for filtering below). |
paulb@250 | 202 | |
paulb@250 | 203 | if fields_from_path.has_key(field_name): |
paulb@250 | 204 | field_from_path_values = fields_from_path[field_name] |
paulb@250 | 205 | if type(field_from_path_values) != type([]): |
paulb@250 | 206 | field_from_path_values = [field_from_path_values] |
paulb@250 | 207 | else: |
paulb@250 | 208 | field_from_path_values = [] |
paulb@250 | 209 | |
paulb@250 | 210 | fields[field_name] = [] |
paulb@250 | 211 | for field_value in field_values: |
paulb@250 | 212 | |
paulb@250 | 213 | # Filter path values. |
paulb@250 | 214 | |
paulb@250 | 215 | if field_value not in field_from_path_values: |
paulb@250 | 216 | fields[field_name].append(field_value) |
paulb@250 | 217 | |
paulb@250 | 218 | # Remove filtered fields. |
paulb@250 | 219 | |
paulb@250 | 220 | if fields[field_name] == []: |
paulb@250 | 221 | del fields[field_name] |
paulb@250 | 222 | |
paulb@250 | 223 | return fields |
paulb@250 | 224 | |
paulb@46 | 225 | # vim: tabstop=4 expandtab shiftwidth=4 |