wsgiwrappers.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. # (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
  2. # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  3. """WSGI Wrappers for a Request and Response
  4. The WSGIRequest and WSGIResponse objects are light wrappers to make it easier
  5. to deal with an incoming request and sending a response.
  6. """
  7. import re
  8. import warnings
  9. from pprint import pformat
  10. from Cookie import SimpleCookie
  11. from paste.request import EnvironHeaders, get_cookie_dict, \
  12. parse_dict_querystring, parse_formvars
  13. from paste.util.multidict import MultiDict, UnicodeMultiDict
  14. from paste.registry import StackedObjectProxy
  15. from paste.response import HeaderDict
  16. from paste.wsgilib import encode_unicode_app_iter
  17. from paste.httpheaders import ACCEPT_LANGUAGE
  18. from paste.util.mimeparse import desired_matches
  19. __all__ = ['WSGIRequest', 'WSGIResponse']
  20. _CHARSET_RE = re.compile(r';\s*charset=([^;]*)', re.I)
  21. class DeprecatedSettings(StackedObjectProxy):
  22. def _push_object(self, obj):
  23. warnings.warn('paste.wsgiwrappers.settings is deprecated: Please use '
  24. 'paste.wsgiwrappers.WSGIRequest.defaults instead',
  25. DeprecationWarning, 3)
  26. WSGIResponse.defaults._push_object(obj)
  27. StackedObjectProxy._push_object(self, obj)
  28. # settings is deprecated: use WSGIResponse.defaults instead
  29. settings = DeprecatedSettings(default=dict())
  30. class environ_getter(object):
  31. """For delegating an attribute to a key in self.environ."""
  32. # @@: Also __set__? Should setting be allowed?
  33. def __init__(self, key, default='', default_factory=None):
  34. self.key = key
  35. self.default = default
  36. self.default_factory = default_factory
  37. def __get__(self, obj, type=None):
  38. if type is None:
  39. return self
  40. if self.key not in obj.environ:
  41. if self.default_factory:
  42. val = obj.environ[self.key] = self.default_factory()
  43. return val
  44. else:
  45. return self.default
  46. return obj.environ[self.key]
  47. def __repr__(self):
  48. return '<Proxy for WSGI environ %r key>' % self.key
  49. class WSGIRequest(object):
  50. """WSGI Request API Object
  51. This object represents a WSGI request with a more friendly interface.
  52. This does not expose every detail of the WSGI environment, and attempts
  53. to express nothing beyond what is available in the environment
  54. dictionary.
  55. The only state maintained in this object is the desired ``charset``,
  56. its associated ``errors`` handler, and the ``decode_param_names``
  57. option.
  58. The incoming parameter values will be automatically coerced to unicode
  59. objects of the ``charset`` encoding when ``charset`` is set. The
  60. incoming parameter names are not decoded to unicode unless the
  61. ``decode_param_names`` option is enabled.
  62. When unicode is expected, ``charset`` will overridden by the the
  63. value of the ``Content-Type`` header's charset parameter if one was
  64. specified by the client.
  65. The class variable ``defaults`` specifies default values for
  66. ``charset``, ``errors``, and ``langauge``. These can be overridden for the
  67. current request via the registry.
  68. The ``language`` default value is considered the fallback during i18n
  69. translations to ensure in odd cases that mixed languages don't occur should
  70. the ``language`` file contain the string but not another language in the
  71. accepted languages list. The ``language`` value only applies when getting
  72. a list of accepted languages from the HTTP Accept header.
  73. This behavior is duplicated from Aquarium, and may seem strange but is
  74. very useful. Normally, everything in the code is in "en-us". However,
  75. the "en-us" translation catalog is usually empty. If the user requests
  76. ``["en-us", "zh-cn"]`` and a translation isn't found for a string in
  77. "en-us", you don't want gettext to fallback to "zh-cn". You want it to
  78. just use the string itself. Hence, if a string isn't found in the
  79. ``language`` catalog, the string in the source code will be used.
  80. *All* other state is kept in the environment dictionary; this is
  81. essential for interoperability.
  82. You are free to subclass this object.
  83. """
  84. defaults = StackedObjectProxy(default=dict(charset=None, errors='replace',
  85. decode_param_names=False,
  86. language='en-us'))
  87. def __init__(self, environ):
  88. self.environ = environ
  89. # This isn't "state" really, since the object is derivative:
  90. self.headers = EnvironHeaders(environ)
  91. defaults = self.defaults._current_obj()
  92. self.charset = defaults.get('charset')
  93. if self.charset:
  94. # There's a charset: params will be coerced to unicode. In that
  95. # case, attempt to use the charset specified by the browser
  96. browser_charset = self.determine_browser_charset()
  97. if browser_charset:
  98. self.charset = browser_charset
  99. self.errors = defaults.get('errors', 'strict')
  100. self.decode_param_names = defaults.get('decode_param_names', False)
  101. self._languages = None
  102. body = environ_getter('wsgi.input')
  103. scheme = environ_getter('wsgi.url_scheme')
  104. method = environ_getter('REQUEST_METHOD')
  105. script_name = environ_getter('SCRIPT_NAME')
  106. path_info = environ_getter('PATH_INFO')
  107. def urlvars(self):
  108. """
  109. Return any variables matched in the URL (e.g.,
  110. ``wsgiorg.routing_args``).
  111. """
  112. if 'paste.urlvars' in self.environ:
  113. return self.environ['paste.urlvars']
  114. elif 'wsgiorg.routing_args' in self.environ:
  115. return self.environ['wsgiorg.routing_args'][1]
  116. else:
  117. return {}
  118. urlvars = property(urlvars, doc=urlvars.__doc__)
  119. def is_xhr(self):
  120. """Returns a boolean if X-Requested-With is present and a XMLHttpRequest"""
  121. return self.environ.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest'
  122. is_xhr = property(is_xhr, doc=is_xhr.__doc__)
  123. def host(self):
  124. """Host name provided in HTTP_HOST, with fall-back to SERVER_NAME"""
  125. return self.environ.get('HTTP_HOST', self.environ.get('SERVER_NAME'))
  126. host = property(host, doc=host.__doc__)
  127. def languages(self):
  128. """Return a list of preferred languages, most preferred first.
  129. The list may be empty.
  130. """
  131. if self._languages is not None:
  132. return self._languages
  133. acceptLanguage = self.environ.get('HTTP_ACCEPT_LANGUAGE')
  134. langs = ACCEPT_LANGUAGE.parse(self.environ)
  135. fallback = self.defaults.get('language', 'en-us')
  136. if not fallback:
  137. return langs
  138. if fallback not in langs:
  139. langs.append(fallback)
  140. index = langs.index(fallback)
  141. langs[index+1:] = []
  142. self._languages = langs
  143. return self._languages
  144. languages = property(languages, doc=languages.__doc__)
  145. def _GET(self):
  146. return parse_dict_querystring(self.environ)
  147. def GET(self):
  148. """
  149. Dictionary-like object representing the QUERY_STRING
  150. parameters. Always present, if possibly empty.
  151. If the same key is present in the query string multiple times, a
  152. list of its values can be retrieved from the ``MultiDict`` via
  153. the ``getall`` method.
  154. Returns a ``MultiDict`` container or a ``UnicodeMultiDict`` when
  155. ``charset`` is set.
  156. """
  157. params = self._GET()
  158. if self.charset:
  159. params = UnicodeMultiDict(params, encoding=self.charset,
  160. errors=self.errors,
  161. decode_keys=self.decode_param_names)
  162. return params
  163. GET = property(GET, doc=GET.__doc__)
  164. def _POST(self):
  165. return parse_formvars(self.environ, include_get_vars=False)
  166. def POST(self):
  167. """Dictionary-like object representing the POST body.
  168. Most values are encoded strings, or unicode strings when
  169. ``charset`` is set. There may also be FieldStorage objects
  170. representing file uploads. If this is not a POST request, or the
  171. body is not encoded fields (e.g., an XMLRPC request) then this
  172. will be empty.
  173. This will consume wsgi.input when first accessed if applicable,
  174. but the raw version will be put in
  175. environ['paste.parsed_formvars'].
  176. Returns a ``MultiDict`` container or a ``UnicodeMultiDict`` when
  177. ``charset`` is set.
  178. """
  179. params = self._POST()
  180. if self.charset:
  181. params = UnicodeMultiDict(params, encoding=self.charset,
  182. errors=self.errors,
  183. decode_keys=self.decode_param_names)
  184. return params
  185. POST = property(POST, doc=POST.__doc__)
  186. def params(self):
  187. """Dictionary-like object of keys from POST, GET, URL dicts
  188. Return a key value from the parameters, they are checked in the
  189. following order: POST, GET, URL
  190. Additional methods supported:
  191. ``getlist(key)``
  192. Returns a list of all the values by that key, collected from
  193. POST, GET, URL dicts
  194. Returns a ``MultiDict`` container or a ``UnicodeMultiDict`` when
  195. ``charset`` is set.
  196. """
  197. params = MultiDict()
  198. params.update(self._POST())
  199. params.update(self._GET())
  200. if self.charset:
  201. params = UnicodeMultiDict(params, encoding=self.charset,
  202. errors=self.errors,
  203. decode_keys=self.decode_param_names)
  204. return params
  205. params = property(params, doc=params.__doc__)
  206. def cookies(self):
  207. """Dictionary of cookies keyed by cookie name.
  208. Just a plain dictionary, may be empty but not None.
  209. """
  210. return get_cookie_dict(self.environ)
  211. cookies = property(cookies, doc=cookies.__doc__)
  212. def determine_browser_charset(self):
  213. """
  214. Determine the encoding as specified by the browser via the
  215. Content-Type's charset parameter, if one is set
  216. """
  217. charset_match = _CHARSET_RE.search(self.headers.get('Content-Type', ''))
  218. if charset_match:
  219. return charset_match.group(1)
  220. def match_accept(self, mimetypes):
  221. """Return a list of specified mime-types that the browser's HTTP Accept
  222. header allows in the order provided."""
  223. return desired_matches(mimetypes,
  224. self.environ.get('HTTP_ACCEPT', '*/*'))
  225. def __repr__(self):
  226. """Show important attributes of the WSGIRequest"""
  227. pf = pformat
  228. msg = '<%s.%s object at 0x%x method=%s,' % \
  229. (self.__class__.__module__, self.__class__.__name__,
  230. id(self), pf(self.method))
  231. msg += '\nscheme=%s, host=%s, script_name=%s, path_info=%s,' % \
  232. (pf(self.scheme), pf(self.host), pf(self.script_name),
  233. pf(self.path_info))
  234. msg += '\nlanguages=%s,' % pf(self.languages)
  235. if self.charset:
  236. msg += ' charset=%s, errors=%s,' % (pf(self.charset),
  237. pf(self.errors))
  238. msg += '\nGET=%s,' % pf(self.GET)
  239. msg += '\nPOST=%s,' % pf(self.POST)
  240. msg += '\ncookies=%s>' % pf(self.cookies)
  241. return msg
  242. class WSGIResponse(object):
  243. """A basic HTTP response with content, headers, and out-bound cookies
  244. The class variable ``defaults`` specifies default values for
  245. ``content_type``, ``charset`` and ``errors``. These can be overridden
  246. for the current request via the registry.
  247. """
  248. defaults = StackedObjectProxy(
  249. default=dict(content_type='text/html', charset='utf-8',
  250. errors='strict', headers={'Cache-Control':'no-cache'})
  251. )
  252. def __init__(self, content='', mimetype=None, code=200):
  253. self._iter = None
  254. self._is_str_iter = True
  255. self.content = content
  256. self.headers = HeaderDict()
  257. self.cookies = SimpleCookie()
  258. self.status_code = code
  259. defaults = self.defaults._current_obj()
  260. if not mimetype:
  261. mimetype = defaults.get('content_type', 'text/html')
  262. charset = defaults.get('charset')
  263. if charset:
  264. mimetype = '%s; charset=%s' % (mimetype, charset)
  265. self.headers.update(defaults.get('headers', {}))
  266. self.headers['Content-Type'] = mimetype
  267. self.errors = defaults.get('errors', 'strict')
  268. def __str__(self):
  269. """Returns a rendition of the full HTTP message, including headers.
  270. When the content is an iterator, the actual content is replaced with the
  271. output of str(iterator) (to avoid exhausting the iterator).
  272. """
  273. if self._is_str_iter:
  274. content = ''.join(self.get_content())
  275. else:
  276. content = str(self.content)
  277. return '\n'.join(['%s: %s' % (key, value)
  278. for key, value in self.headers.headeritems()]) \
  279. + '\n\n' + content
  280. def __call__(self, environ, start_response):
  281. """Convenience call to return output and set status information
  282. Conforms to the WSGI interface for calling purposes only.
  283. Example usage:
  284. .. code-block:: python
  285. def wsgi_app(environ, start_response):
  286. response = WSGIResponse()
  287. response.write("Hello world")
  288. response.headers['Content-Type'] = 'latin1'
  289. return response(environ, start_response)
  290. """
  291. status_text = STATUS_CODE_TEXT[self.status_code]
  292. status = '%s %s' % (self.status_code, status_text)
  293. response_headers = self.headers.headeritems()
  294. for c in self.cookies.values():
  295. response_headers.append(('Set-Cookie', c.output(header='')))
  296. start_response(status, response_headers)
  297. is_file = isinstance(self.content, file)
  298. if 'wsgi.file_wrapper' in environ and is_file:
  299. return environ['wsgi.file_wrapper'](self.content)
  300. elif is_file:
  301. return iter(lambda: self.content.read(), '')
  302. return self.get_content()
  303. def determine_charset(self):
  304. """
  305. Determine the encoding as specified by the Content-Type's charset
  306. parameter, if one is set
  307. """
  308. charset_match = _CHARSET_RE.search(self.headers.get('Content-Type', ''))
  309. if charset_match:
  310. return charset_match.group(1)
  311. def has_header(self, header):
  312. """
  313. Case-insensitive check for a header
  314. """
  315. warnings.warn('WSGIResponse.has_header is deprecated, use '
  316. 'WSGIResponse.headers.has_key instead', DeprecationWarning,
  317. 2)
  318. return self.headers.has_key(header)
  319. def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
  320. domain=None, secure=None, httponly=None):
  321. """
  322. Define a cookie to be sent via the outgoing HTTP headers
  323. """
  324. self.cookies[key] = value
  325. for var_name, var_value in [
  326. ('max_age', max_age), ('path', path), ('domain', domain),
  327. ('secure', secure), ('expires', expires), ('httponly', httponly)]:
  328. if var_value is not None and var_value is not False:
  329. self.cookies[key][var_name.replace('_', '-')] = var_value
  330. def delete_cookie(self, key, path='/', domain=None):
  331. """
  332. Notify the browser the specified cookie has expired and should be
  333. deleted (via the outgoing HTTP headers)
  334. """
  335. self.cookies[key] = ''
  336. if path is not None:
  337. self.cookies[key]['path'] = path
  338. if domain is not None:
  339. self.cookies[key]['domain'] = domain
  340. self.cookies[key]['expires'] = 0
  341. self.cookies[key]['max-age'] = 0
  342. def _set_content(self, content):
  343. if hasattr(content, '__iter__'):
  344. self._iter = content
  345. if isinstance(content, list):
  346. self._is_str_iter = True
  347. else:
  348. self._is_str_iter = False
  349. else:
  350. self._iter = [content]
  351. self._is_str_iter = True
  352. content = property(lambda self: self._iter, _set_content,
  353. doc='Get/set the specified content, where content can '
  354. 'be: a string, a list of strings, a generator function '
  355. 'that yields strings, or an iterable object that '
  356. 'produces strings.')
  357. def get_content(self):
  358. """
  359. Returns the content as an iterable of strings, encoding each element of
  360. the iterator from a Unicode object if necessary.
  361. """
  362. charset = self.determine_charset()
  363. if charset:
  364. return encode_unicode_app_iter(self.content, charset, self.errors)
  365. else:
  366. return self.content
  367. def wsgi_response(self):
  368. """
  369. Return this WSGIResponse as a tuple of WSGI formatted data, including:
  370. (status, headers, iterable)
  371. """
  372. status_text = STATUS_CODE_TEXT[self.status_code]
  373. status = '%s %s' % (self.status_code, status_text)
  374. response_headers = self.headers.headeritems()
  375. for c in self.cookies.values():
  376. response_headers.append(('Set-Cookie', c.output(header='')))
  377. return status, response_headers, self.get_content()
  378. # The remaining methods partially implement the file-like object interface.
  379. # See http://docs.python.org/lib/bltin-file-objects.html
  380. def write(self, content):
  381. if not self._is_str_iter:
  382. raise IOError, "This %s instance's content is not writable: (content " \
  383. 'is an iterator)' % self.__class__.__name__
  384. self.content.append(content)
  385. def flush(self):
  386. pass
  387. def tell(self):
  388. if not self._is_str_iter:
  389. raise IOError, 'This %s instance cannot tell its position: (content ' \
  390. 'is an iterator)' % self.__class__.__name__
  391. return sum([len(chunk) for chunk in self._iter])
  392. ########################################
  393. ## Content-type and charset
  394. def charset__get(self):
  395. """
  396. Get/set the charset (in the Content-Type)
  397. """
  398. header = self.headers.get('content-type')
  399. if not header:
  400. return None
  401. match = _CHARSET_RE.search(header)
  402. if match:
  403. return match.group(1)
  404. return None
  405. def charset__set(self, charset):
  406. if charset is None:
  407. del self.charset
  408. return
  409. try:
  410. header = self.headers.pop('content-type')
  411. except KeyError:
  412. raise AttributeError(
  413. "You cannot set the charset when no content-type is defined")
  414. match = _CHARSET_RE.search(header)
  415. if match:
  416. header = header[:match.start()] + header[match.end():]
  417. header += '; charset=%s' % charset
  418. self.headers['content-type'] = header
  419. def charset__del(self):
  420. try:
  421. header = self.headers.pop('content-type')
  422. except KeyError:
  423. # Don't need to remove anything
  424. return
  425. match = _CHARSET_RE.search(header)
  426. if match:
  427. header = header[:match.start()] + header[match.end():]
  428. self.headers['content-type'] = header
  429. charset = property(charset__get, charset__set, charset__del, doc=charset__get.__doc__)
  430. def content_type__get(self):
  431. """
  432. Get/set the Content-Type header (or None), *without* the
  433. charset or any parameters.
  434. If you include parameters (or ``;`` at all) when setting the
  435. content_type, any existing parameters will be deleted;
  436. otherwise they will be preserved.
  437. """
  438. header = self.headers.get('content-type')
  439. if not header:
  440. return None
  441. return header.split(';', 1)[0]
  442. def content_type__set(self, value):
  443. if ';' not in value:
  444. header = self.headers.get('content-type', '')
  445. if ';' in header:
  446. params = header.split(';', 1)[1]
  447. value += ';' + params
  448. self.headers['content-type'] = value
  449. def content_type__del(self):
  450. try:
  451. del self.headers['content-type']
  452. except KeyError:
  453. pass
  454. content_type = property(content_type__get, content_type__set,
  455. content_type__del, doc=content_type__get.__doc__)
  456. ## @@ I'd love to remove this, but paste.httpexceptions.get_exception
  457. ## doesn't seem to work...
  458. # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  459. STATUS_CODE_TEXT = {
  460. 100: 'CONTINUE',
  461. 101: 'SWITCHING PROTOCOLS',
  462. 200: 'OK',
  463. 201: 'CREATED',
  464. 202: 'ACCEPTED',
  465. 203: 'NON-AUTHORITATIVE INFORMATION',
  466. 204: 'NO CONTENT',
  467. 205: 'RESET CONTENT',
  468. 206: 'PARTIAL CONTENT',
  469. 226: 'IM USED',
  470. 300: 'MULTIPLE CHOICES',
  471. 301: 'MOVED PERMANENTLY',
  472. 302: 'FOUND',
  473. 303: 'SEE OTHER',
  474. 304: 'NOT MODIFIED',
  475. 305: 'USE PROXY',
  476. 306: 'RESERVED',
  477. 307: 'TEMPORARY REDIRECT',
  478. 400: 'BAD REQUEST',
  479. 401: 'UNAUTHORIZED',
  480. 402: 'PAYMENT REQUIRED',
  481. 403: 'FORBIDDEN',
  482. 404: 'NOT FOUND',
  483. 405: 'METHOD NOT ALLOWED',
  484. 406: 'NOT ACCEPTABLE',
  485. 407: 'PROXY AUTHENTICATION REQUIRED',
  486. 408: 'REQUEST TIMEOUT',
  487. 409: 'CONFLICT',
  488. 410: 'GONE',
  489. 411: 'LENGTH REQUIRED',
  490. 412: 'PRECONDITION FAILED',
  491. 413: 'REQUEST ENTITY TOO LARGE',
  492. 414: 'REQUEST-URI TOO LONG',
  493. 415: 'UNSUPPORTED MEDIA TYPE',
  494. 416: 'REQUESTED RANGE NOT SATISFIABLE',
  495. 417: 'EXPECTATION FAILED',
  496. 500: 'INTERNAL SERVER ERROR',
  497. 501: 'NOT IMPLEMENTED',
  498. 502: 'BAD GATEWAY',
  499. 503: 'SERVICE UNAVAILABLE',
  500. 504: 'GATEWAY TIMEOUT',
  501. 505: 'HTTP VERSION NOT SUPPORTED',
  502. }