paulb@187 | 1 | #!/usr/bin/env python |
paulb@187 | 2 | |
paulb@187 | 3 | """ |
paulb@187 | 4 | Zope classes. |
paulb@403 | 5 | |
paulb@623 | 6 | Copyright (C) 2004, 2005, 2006, 2007 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@403 | 21 | |
paulb@403 | 22 | -------- |
paulb@403 | 23 | |
paulb@187 | 24 | In places this resembles CGI a lot because Zope seems to recycle a lot of that |
paulb@187 | 25 | baggage. |
paulb@187 | 26 | """ |
paulb@187 | 27 | |
paulb@463 | 28 | import WebStack.Generic |
paulb@463 | 29 | from WebStack.Helpers import Environment |
paulb@463 | 30 | from WebStack.Helpers.Request import Cookie, get_body_field_or_file, decode_value, filter_fields |
paulb@463 | 31 | from WebStack.Helpers.Response import ConvertingStream |
paulb@463 | 32 | from WebStack.Helpers.Auth import UserInfo |
paulb@228 | 33 | import cgi |
paulb@187 | 34 | |
paulb@463 | 35 | class Transaction(WebStack.Generic.Transaction): |
paulb@187 | 36 | |
paulb@187 | 37 | """ |
paulb@187 | 38 | Zope transaction interface. |
paulb@187 | 39 | """ |
paulb@187 | 40 | |
paulb@299 | 41 | def __init__(self, request, adapter): |
paulb@187 | 42 | |
paulb@187 | 43 | """ |
paulb@299 | 44 | Initialise the transaction with the Zope 'request' object and the |
paulb@299 | 45 | 'adapter' which created this transaction. |
paulb@187 | 46 | """ |
paulb@187 | 47 | |
paulb@187 | 48 | self.request = request |
paulb@187 | 49 | self.response = request.RESPONSE |
paulb@299 | 50 | self.adapter = adapter |
paulb@188 | 51 | |
paulb@276 | 52 | # Cached information. |
paulb@276 | 53 | |
paulb@276 | 54 | self._fields = None |
paulb@276 | 55 | |
paulb@188 | 56 | # Attributes which may be changed later. |
paulb@188 | 57 | |
paulb@188 | 58 | self.content_type = None |
paulb@187 | 59 | |
paulb@338 | 60 | # Server-related methods. |
paulb@338 | 61 | |
paulb@338 | 62 | def get_server_name(self): |
paulb@338 | 63 | |
paulb@338 | 64 | "Returns the server name." |
paulb@338 | 65 | |
paulb@338 | 66 | return self.request.environ.get("SERVER_NAME") |
paulb@338 | 67 | |
paulb@338 | 68 | def get_server_port(self): |
paulb@338 | 69 | |
paulb@338 | 70 | "Returns the server port as a string." |
paulb@338 | 71 | |
paulb@338 | 72 | return self.request.environ.get("SERVER_PORT") |
paulb@338 | 73 | |
paulb@187 | 74 | # Request-related methods. |
paulb@187 | 75 | |
paulb@187 | 76 | def get_request_stream(self): |
paulb@187 | 77 | |
paulb@187 | 78 | """ |
paulb@187 | 79 | Returns the request stream for the transaction. |
paulb@378 | 80 | |
paulb@378 | 81 | NOTE: This method actually rewinds to the start of the stream, since |
paulb@378 | 82 | NOTE: Zope likes to read everything automatically. |
paulb@187 | 83 | """ |
paulb@187 | 84 | |
paulb@187 | 85 | # NOTE: Possibly not safe. |
paulb@187 | 86 | |
paulb@378 | 87 | stdin = self.request.stdin |
paulb@378 | 88 | stdin.seek(0) |
paulb@378 | 89 | return stdin |
paulb@187 | 90 | |
paulb@187 | 91 | def get_request_method(self): |
paulb@187 | 92 | |
paulb@187 | 93 | """ |
paulb@187 | 94 | Returns the request method. |
paulb@187 | 95 | """ |
paulb@187 | 96 | |
paulb@187 | 97 | return self.request.environ.get("REQUEST_METHOD") |
paulb@187 | 98 | |
paulb@187 | 99 | def get_headers(self): |
paulb@187 | 100 | |
paulb@187 | 101 | """ |
paulb@187 | 102 | Returns all request headers as a dictionary-like object mapping header |
paulb@187 | 103 | names to values. |
paulb@187 | 104 | """ |
paulb@187 | 105 | |
paulb@187 | 106 | return Environment.get_headers(self.request.environ) |
paulb@187 | 107 | |
paulb@187 | 108 | def get_header_values(self, key): |
paulb@187 | 109 | |
paulb@187 | 110 | """ |
paulb@187 | 111 | Returns a list of all request header values associated with the given |
paulb@187 | 112 | 'key'. Note that according to RFC 2616, 'key' is treated as a |
paulb@187 | 113 | case-insensitive string. |
paulb@187 | 114 | """ |
paulb@187 | 115 | |
paulb@187 | 116 | return self.convert_to_list(self.get_headers().get(key)) |
paulb@187 | 117 | |
paulb@187 | 118 | def get_content_type(self): |
paulb@187 | 119 | |
paulb@187 | 120 | """ |
paulb@187 | 121 | Returns the content type specified on the request, along with the |
paulb@187 | 122 | charset employed. |
paulb@187 | 123 | """ |
paulb@187 | 124 | |
paulb@187 | 125 | return self.parse_content_type(self.request.environ.get("CONTENT_TYPE")) |
paulb@187 | 126 | |
paulb@187 | 127 | def get_content_charsets(self): |
paulb@187 | 128 | |
paulb@187 | 129 | """ |
paulb@187 | 130 | Returns the character set preferences. |
paulb@187 | 131 | |
paulb@187 | 132 | NOTE: Not decently supported. |
paulb@187 | 133 | """ |
paulb@187 | 134 | |
paulb@187 | 135 | return self.parse_content_preferences(None) |
paulb@187 | 136 | |
paulb@187 | 137 | def get_content_languages(self): |
paulb@187 | 138 | |
paulb@187 | 139 | """ |
paulb@187 | 140 | Returns extracted language information from the transaction. |
paulb@187 | 141 | |
paulb@187 | 142 | NOTE: Not decently supported. |
paulb@187 | 143 | """ |
paulb@187 | 144 | |
paulb@187 | 145 | return self.parse_content_preferences(None) |
paulb@187 | 146 | |
paulb@434 | 147 | def get_path(self, encoding=None): |
paulb@187 | 148 | |
paulb@187 | 149 | """ |
paulb@434 | 150 | Returns the entire path from the request as a Unicode object. Any "URL |
paulb@434 | 151 | encoded" character values in the part of the path before the query |
paulb@434 | 152 | string will be decoded and presented as genuine characters; the query |
paulb@434 | 153 | string will remain "URL encoded", however. |
paulb@434 | 154 | |
paulb@434 | 155 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 156 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 157 | character values. |
paulb@187 | 158 | """ |
paulb@187 | 159 | |
paulb@187 | 160 | # NOTE: Based on WebStack.CGI.get_path. |
paulb@187 | 161 | |
paulb@434 | 162 | path = self.get_path_without_query(encoding) |
paulb@187 | 163 | qs = self.get_query_string() |
paulb@187 | 164 | if qs: |
paulb@434 | 165 | return path + "?" + qs |
paulb@434 | 166 | else: |
paulb@434 | 167 | return path |
paulb@187 | 168 | |
paulb@434 | 169 | def get_path_without_query(self, encoding=None): |
paulb@187 | 170 | |
paulb@187 | 171 | """ |
paulb@434 | 172 | Returns the entire path from the request minus the query string as a |
paulb@434 | 173 | Unicode object containing genuine characters (as opposed to "URL |
paulb@434 | 174 | encoded" character values). |
paulb@434 | 175 | |
paulb@434 | 176 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 177 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 178 | character values. |
paulb@187 | 179 | """ |
paulb@187 | 180 | |
paulb@187 | 181 | # NOTE: Based on WebStack.CGI.get_path. |
paulb@187 | 182 | |
paulb@623 | 183 | encoding = encoding or self.default_charset |
paulb@623 | 184 | |
paulb@682 | 185 | path = decode_value(self.request.get("SCRIPT_NAME") or "", encoding) |
paulb@682 | 186 | path += decode_value(self.request.environ.get("PATH_INFO") or "", encoding) |
paulb@187 | 187 | return path |
paulb@187 | 188 | |
paulb@434 | 189 | def get_path_info(self, encoding=None): |
paulb@187 | 190 | |
paulb@187 | 191 | """ |
paulb@187 | 192 | Returns the "path info" (the part of the URL after the resource name |
paulb@434 | 193 | handling the current request) from the request as a Unicode object |
paulb@434 | 194 | containing genuine characters (as opposed to "URL encoded" character |
paulb@434 | 195 | values). |
paulb@434 | 196 | |
paulb@434 | 197 | If the optional 'encoding' is set, use that in preference to the default |
paulb@434 | 198 | encoding to convert the path into a form not containing "URL encoded" |
paulb@434 | 199 | character values. |
paulb@187 | 200 | """ |
paulb@187 | 201 | |
paulb@623 | 202 | encoding = encoding or self.default_charset |
paulb@623 | 203 | |
paulb@316 | 204 | product_path = "/".join(self.adapter.getPhysicalPath()) |
paulb@316 | 205 | path_info = self.request.environ.get("PATH_INFO") or "" |
paulb@434 | 206 | real_path_info = path_info[len(product_path):] |
paulb@439 | 207 | return decode_value(real_path_info, encoding) |
paulb@187 | 208 | |
paulb@187 | 209 | def get_query_string(self): |
paulb@187 | 210 | |
paulb@187 | 211 | """ |
paulb@187 | 212 | Returns the query string from the path in the request. |
paulb@187 | 213 | """ |
paulb@187 | 214 | |
paulb@187 | 215 | return self.request.environ.get("QUERY_STRING") or "" |
paulb@187 | 216 | |
paulb@187 | 217 | # Higher level request-related methods. |
paulb@187 | 218 | |
paulb@430 | 219 | def get_fields_from_path(self, encoding=None): |
paulb@187 | 220 | |
paulb@187 | 221 | """ |
paulb@249 | 222 | Extracts fields (or request parameters) from the path specified in the |
paulb@249 | 223 | transaction. The underlying framework may refuse to supply fields from |
paulb@430 | 224 | the path if handling a POST transaction. The optional 'encoding' |
paulb@430 | 225 | parameter specifies the character encoding of the query string for cases |
paulb@430 | 226 | where the default encoding is to be overridden. |
paulb@187 | 227 | |
paulb@187 | 228 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@187 | 229 | single value is associated with any given field name). |
paulb@187 | 230 | """ |
paulb@187 | 231 | |
paulb@623 | 232 | encoding = encoding or self.default_charset |
paulb@623 | 233 | |
paulb@249 | 234 | fields = {} |
paulb@249 | 235 | for name, values in cgi.parse_qs(self.get_query_string()).items(): |
paulb@439 | 236 | name = decode_value(name, encoding) |
paulb@249 | 237 | fields[name] = [] |
paulb@249 | 238 | for value in values: |
paulb@439 | 239 | value = decode_value(value, encoding) |
paulb@430 | 240 | fields[name].append(value) |
paulb@249 | 241 | return fields |
paulb@187 | 242 | |
paulb@187 | 243 | def get_fields_from_body(self, encoding=None): |
paulb@187 | 244 | |
paulb@187 | 245 | """ |
paulb@249 | 246 | Extracts fields (or request parameters) from the message body in the |
paulb@249 | 247 | transaction. The optional 'encoding' parameter specifies the character |
paulb@249 | 248 | encoding of the message body for cases where no such information is |
paulb@249 | 249 | available, but where the default encoding is to be overridden. |
paulb@187 | 250 | |
paulb@187 | 251 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@193 | 252 | single value is associated with any given field name). Each value is |
paulb@193 | 253 | either a Unicode object (representing a simple form field, for example) |
paulb@543 | 254 | or a WebStack.Helpers.Request.FileContent object (representing a file |
paulb@543 | 255 | upload form field). |
paulb@187 | 256 | """ |
paulb@187 | 257 | |
paulb@249 | 258 | all_fields = self._get_fields(encoding) |
paulb@249 | 259 | fields_from_path = self.get_fields_from_path() |
paulb@249 | 260 | return filter_fields(all_fields, fields_from_path) |
paulb@228 | 261 | |
paulb@249 | 262 | def _get_fields(self, encoding=None): |
paulb@276 | 263 | if self._fields is not None: |
paulb@276 | 264 | return self._fields |
paulb@276 | 265 | |
paulb@224 | 266 | encoding = encoding or self.get_content_type().charset or self.default_charset |
paulb@276 | 267 | self._fields = {} |
paulb@439 | 268 | |
paulb@228 | 269 | for field_name, field_values in self.request.form.items(): |
paulb@439 | 270 | field_name = decode_value(field_name, encoding) |
paulb@228 | 271 | |
paulb@228 | 272 | # Find the body values. |
paulb@228 | 273 | |
paulb@200 | 274 | if type(field_values) == type([]): |
paulb@276 | 275 | self._fields[field_name] = [] |
paulb@200 | 276 | for field_str in field_values: |
paulb@460 | 277 | self._fields[field_name].append(get_body_field_or_file(field_str, encoding)) |
paulb@249 | 278 | else: |
paulb@460 | 279 | self._fields[field_name] = [get_body_field_or_file(field_values, encoding)] |
paulb@249 | 280 | |
paulb@276 | 281 | return self._fields |
paulb@228 | 282 | |
paulb@249 | 283 | def get_fields(self, encoding=None): |
paulb@249 | 284 | |
paulb@249 | 285 | """ |
paulb@249 | 286 | Extracts fields (or request parameters) from both the path specified in |
paulb@249 | 287 | the transaction as well as the message body. The optional 'encoding' |
paulb@249 | 288 | parameter specifies the character encoding of the message body for cases |
paulb@249 | 289 | where no such information is available, but where the default encoding |
paulb@249 | 290 | is to be overridden. |
paulb@228 | 291 | |
paulb@249 | 292 | Returns a dictionary mapping field names to lists of values (even if a |
paulb@249 | 293 | single value is associated with any given field name). Each value is |
paulb@249 | 294 | either a Unicode object (representing a simple form field, for example) |
paulb@543 | 295 | or a WebStack.Helpers.Request.FileContent object (representing a file |
paulb@543 | 296 | upload form field). |
paulb@249 | 297 | |
paulb@249 | 298 | Where a given field name is used in both the path and message body to |
paulb@249 | 299 | specify values, the values from both sources will be combined into a |
paulb@249 | 300 | single list associated with that field name. |
paulb@249 | 301 | """ |
paulb@228 | 302 | |
paulb@249 | 303 | # NOTE: Zope seems to provide only body fields upon POST requests. |
paulb@249 | 304 | |
paulb@249 | 305 | if self.get_request_method() == "GET": |
paulb@249 | 306 | return self._get_fields(encoding) |
paulb@249 | 307 | else: |
paulb@249 | 308 | fields = {} |
paulb@543 | 309 | fields.update(self.get_fields_from_path(encoding)) |
paulb@249 | 310 | for name, values in self._get_fields(encoding).items(): |
paulb@249 | 311 | if not fields.has_key(name): |
paulb@249 | 312 | fields[name] = values |
paulb@249 | 313 | else: |
paulb@249 | 314 | fields[name] += values |
paulb@187 | 315 | return fields |
paulb@187 | 316 | |
paulb@187 | 317 | def get_user(self): |
paulb@187 | 318 | |
paulb@187 | 319 | """ |
paulb@187 | 320 | Extracts user information from the transaction. |
paulb@187 | 321 | |
paulb@187 | 322 | Returns a username as a string or None if no user is defined. |
paulb@187 | 323 | """ |
paulb@187 | 324 | |
paulb@187 | 325 | if self.user is not None: |
paulb@187 | 326 | return self.user |
paulb@270 | 327 | |
paulb@270 | 328 | auth_header = self.request._auth |
paulb@270 | 329 | if auth_header: |
paulb@270 | 330 | return UserInfo(auth_header).username |
paulb@187 | 331 | else: |
paulb@270 | 332 | return None |
paulb@187 | 333 | |
paulb@187 | 334 | def get_cookies(self): |
paulb@187 | 335 | |
paulb@187 | 336 | """ |
paulb@187 | 337 | Obtains cookie information from the request. |
paulb@187 | 338 | |
paulb@187 | 339 | Returns a dictionary mapping cookie names to cookie objects. |
paulb@187 | 340 | """ |
paulb@187 | 341 | |
paulb@258 | 342 | return self.process_cookies(self.request.cookies, using_strings=1) |
paulb@187 | 343 | |
paulb@187 | 344 | def get_cookie(self, cookie_name): |
paulb@187 | 345 | |
paulb@187 | 346 | """ |
paulb@187 | 347 | Obtains cookie information from the request. |
paulb@187 | 348 | |
paulb@187 | 349 | Returns a cookie object for the given 'cookie_name' or None if no such |
paulb@187 | 350 | cookie exists. |
paulb@187 | 351 | """ |
paulb@187 | 352 | |
paulb@258 | 353 | value = self.request.cookies.get(self.encode_cookie_value(cookie_name)) |
paulb@258 | 354 | if value is not None: |
paulb@276 | 355 | return Cookie(cookie_name, self.decode_cookie_value(value)) |
paulb@258 | 356 | else: |
paulb@258 | 357 | return None |
paulb@187 | 358 | |
paulb@187 | 359 | # Response-related methods. |
paulb@187 | 360 | |
paulb@187 | 361 | def get_response_stream(self): |
paulb@187 | 362 | |
paulb@187 | 363 | """ |
paulb@187 | 364 | Returns the response stream for the transaction. |
paulb@187 | 365 | """ |
paulb@187 | 366 | |
paulb@224 | 367 | # Unicode can upset this operation. Using either the specified charset |
paulb@224 | 368 | # or a default encoding. |
paulb@187 | 369 | |
paulb@252 | 370 | encoding = self.get_response_stream_encoding() |
paulb@252 | 371 | return ConvertingStream(self.response, encoding) |
paulb@252 | 372 | |
paulb@252 | 373 | def get_response_stream_encoding(self): |
paulb@252 | 374 | |
paulb@252 | 375 | """ |
paulb@252 | 376 | Returns the response stream encoding. |
paulb@252 | 377 | """ |
paulb@252 | 378 | |
paulb@188 | 379 | if self.content_type: |
paulb@224 | 380 | encoding = self.content_type.charset |
paulb@252 | 381 | else: |
paulb@252 | 382 | encoding = None |
paulb@252 | 383 | return encoding or self.default_charset |
paulb@187 | 384 | |
paulb@187 | 385 | def get_response_code(self): |
paulb@187 | 386 | |
paulb@187 | 387 | """ |
paulb@187 | 388 | Get the response code associated with the transaction. If no response |
paulb@187 | 389 | code is defined, None is returned. |
paulb@187 | 390 | """ |
paulb@187 | 391 | |
paulb@187 | 392 | return self.response.status |
paulb@187 | 393 | |
paulb@187 | 394 | def set_response_code(self, response_code): |
paulb@187 | 395 | |
paulb@187 | 396 | """ |
paulb@187 | 397 | Set the 'response_code' using a numeric constant defined in the HTTP |
paulb@187 | 398 | specification. |
paulb@187 | 399 | """ |
paulb@187 | 400 | |
paulb@187 | 401 | self.response.setStatus(response_code) |
paulb@187 | 402 | |
paulb@187 | 403 | def set_header_value(self, header, value): |
paulb@187 | 404 | |
paulb@187 | 405 | """ |
paulb@187 | 406 | Set the HTTP 'header' with the given 'value'. |
paulb@187 | 407 | """ |
paulb@187 | 408 | |
paulb@187 | 409 | self.response.setHeader(header, value) |
paulb@187 | 410 | |
paulb@187 | 411 | def set_content_type(self, content_type): |
paulb@187 | 412 | |
paulb@187 | 413 | """ |
paulb@187 | 414 | Sets the 'content_type' for the response. |
paulb@187 | 415 | """ |
paulb@187 | 416 | |
paulb@188 | 417 | self.content_type = content_type |
paulb@224 | 418 | self.response.setHeader("Content-Type", str(content_type)) |
paulb@187 | 419 | |
paulb@187 | 420 | # Higher level response-related methods. |
paulb@187 | 421 | |
paulb@187 | 422 | def set_cookie(self, cookie): |
paulb@187 | 423 | |
paulb@187 | 424 | """ |
paulb@187 | 425 | Stores the given 'cookie' object in the response. |
paulb@187 | 426 | """ |
paulb@187 | 427 | |
paulb@187 | 428 | self.set_cookie_value(cookie.name, cookie.value) |
paulb@187 | 429 | |
paulb@187 | 430 | def set_cookie_value(self, name, value, path=None, expires=None): |
paulb@187 | 431 | |
paulb@187 | 432 | """ |
paulb@187 | 433 | Stores a cookie with the given 'name' and 'value' in the response. |
paulb@187 | 434 | |
paulb@187 | 435 | The optional 'path' is a string which specifies the scope of the cookie, |
paulb@187 | 436 | and the optional 'expires' parameter is a value compatible with the |
paulb@187 | 437 | time.time function, and indicates the expiry date/time of the cookie. |
paulb@187 | 438 | """ |
paulb@187 | 439 | |
paulb@258 | 440 | self.response.setCookie(self.encode_cookie_value(name), self.encode_cookie_value(value)) |
paulb@187 | 441 | |
paulb@187 | 442 | def delete_cookie(self, cookie_name): |
paulb@187 | 443 | |
paulb@187 | 444 | """ |
paulb@187 | 445 | Adds to the response a request that the cookie with the given |
paulb@187 | 446 | 'cookie_name' be deleted/discarded by the client. |
paulb@187 | 447 | """ |
paulb@187 | 448 | |
paulb@258 | 449 | self.response.expireCookie(self.encode_cookie_value(cookie_name)) |
paulb@187 | 450 | |
paulb@187 | 451 | # Session-related methods. |
paulb@187 | 452 | |
paulb@187 | 453 | def get_session(self, create=1): |
paulb@187 | 454 | |
paulb@187 | 455 | """ |
paulb@187 | 456 | Gets a session corresponding to an identifier supplied in the |
paulb@187 | 457 | transaction. |
paulb@187 | 458 | |
paulb@187 | 459 | If no session has yet been established according to information |
paulb@187 | 460 | provided in the transaction then the optional 'create' parameter |
paulb@187 | 461 | determines whether a new session will be established. |
paulb@187 | 462 | |
paulb@187 | 463 | Where no session has been established and where 'create' is set to 0 |
paulb@187 | 464 | then None is returned. In all other cases, a session object is created |
paulb@187 | 465 | (where appropriate) and returned. |
paulb@187 | 466 | """ |
paulb@187 | 467 | |
paulb@205 | 468 | return self.request.SESSION |
paulb@187 | 469 | |
paulb@187 | 470 | def expire_session(self): |
paulb@187 | 471 | |
paulb@187 | 472 | """ |
paulb@187 | 473 | Expires any session established according to information provided in the |
paulb@187 | 474 | transaction. |
paulb@187 | 475 | """ |
paulb@187 | 476 | |
paulb@205 | 477 | self.request.SESSION.invalidate() |
paulb@187 | 478 | |
paulb@187 | 479 | # vim: tabstop=4 expandtab shiftwidth=4 |