wsgiwrappers.py 22 KB

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