httpgateway.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. """
  2. HTTP gateway: connects the web browser's world of javascript+http and Pyro.
  3. Creates a stateless HTTP server that essentially is a proxy for the Pyro objects behind it.
  4. It exposes the Pyro objects through a HTTP interface and uses the JSON serializer,
  5. so that you can immediately process the response data in the browser.
  6. You can start this module as a script from the command line, to easily get a
  7. http gateway server running:
  8. :command:`python -m Pyro4.utils.httpgateway`
  9. or simply: :command:`pyro4-httpgateway`
  10. It is also possible to import the 'pyro_app' function and stick that into a WSGI
  11. server of your choice, to have more control.
  12. The javascript code in the web page of the gateway server works with the same-origin
  13. browser policy because it is served by the gateway itself. If you want to access it
  14. from scripts in different sites, you have to work around this or embed the gateway app
  15. in your site. Non-browser clients that access the http api have no problems.
  16. See the `http` example for two of such clients (node.js and python).
  17. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  18. """
  19. from __future__ import print_function
  20. import sys
  21. import re
  22. import cgi
  23. import os
  24. import uuid
  25. import warnings
  26. from wsgiref.simple_server import make_server
  27. import traceback
  28. from Pyro4.util import json # don't import stdlib json directly, we want to use the JSON_MODULE config item
  29. from Pyro4.configuration import config
  30. from Pyro4 import constants, errors, core, message, util, naming
  31. __all__ = ["pyro_app", "main"]
  32. _nameserver = None
  33. def get_nameserver(hmac=None):
  34. global _nameserver
  35. if not _nameserver:
  36. _nameserver = naming.locateNS(hmac_key=hmac)
  37. try:
  38. _nameserver.ping()
  39. return _nameserver
  40. except errors.ConnectionClosedError:
  41. _nameserver = None
  42. print("Connection with nameserver lost, reconnecting...")
  43. return get_nameserver(hmac)
  44. def invalid_request(start_response):
  45. """Called if invalid http method."""
  46. start_response('405 Method Not Allowed', [('Content-Type', 'text/plain')])
  47. return [b'Error 405: Method Not Allowed']
  48. def not_found(start_response):
  49. """Called if Url not found."""
  50. start_response('404 Not Found', [('Content-Type', 'text/plain')])
  51. return [b'Error 404: Not Found']
  52. def redirect(start_response, target):
  53. """Called to do a redirect"""
  54. start_response('302 Found', [('Location', target)])
  55. return []
  56. index_page_template = """<!DOCTYPE html>
  57. <html>
  58. <head>
  59. <title>Pyro HTTP gateway</title>
  60. <style type="text/css">
  61. body {{ margin: 1em; }}
  62. table, th, td {{border: 1px solid #bbf; padding: 4px;}}
  63. table {{border-collapse: collapse;}}
  64. pre {{border: 1px solid #bbf; padding: 1ex; margin: 1ex; white-space: pre-wrap;}}
  65. #title-logo {{ float: left; margin: 0 1em 0 0; }}
  66. </style>
  67. </head>
  68. <body>
  69. <script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
  70. <script>
  71. "use strict";
  72. function pyro_call(name, method, params) {{
  73. $.ajax({{
  74. url: name+"/"+method,
  75. type: "GET",
  76. data: params,
  77. dataType: "json",
  78. // headers: {{ "X-Pyro-Correlation-Id": "11112222-1111-2222-3333-222244449999" }},
  79. // headers: {{ "X-Pyro-Gateway-Key": "secret-key" }},
  80. // headers: {{ "X-Pyro-Options": "oneway" }},
  81. beforeSend: function(xhr, settings) {{
  82. $("#pyro_call").text(settings.type+" "+settings.url);
  83. }},
  84. error: function(xhr, status, error) {{
  85. var errormessage = "ERROR: "+xhr.status+" "+error+" \\n"+xhr.responseText;
  86. $("#pyro_response").text(errormessage);
  87. }},
  88. success: function(data) {{
  89. $("#pyro_response").text(JSON.stringify(data, null, 4));
  90. }}
  91. }});
  92. }}
  93. </script>
  94. <div id="title-logo"><img src="http://pyro4.readthedocs.io/en/stable/_static/pyro.png"></div>
  95. <div id="title-text">
  96. <h1>Pyro HTTP gateway</h1>
  97. <p>
  98. Use http+json to talk to Pyro objects.
  99. <a href="http://pyro4.readthedocs.io/en/stable/tipstricks.html#pyro-via-http-and-json">Docs.</a>
  100. </p>
  101. </div>
  102. <p><em>Note: performance isn't a key concern here; it is a stateless server.
  103. It does a name lookup and uses a new Pyro proxy for each request.</em></p>
  104. <h2>Currently exposed contents of name server on {hostname}:</h2>
  105. <p>(Limited to 10 entries, exposed name pattern = '{ns_regex}')</p>
  106. {name_server_contents_list}
  107. <p>Name server examples: (these examples are working if you expose the Pyro.NameServer object)</p>
  108. <ul>
  109. <li><a href="Pyro.NameServer/$meta" onclick="pyro_call('Pyro.NameServer','$meta'); return false;">Pyro.NameServer/$meta</a>
  110. -- gives meta info of the name server (methods)</li>
  111. <li><a href="Pyro.NameServer/list" onclick="pyro_call('Pyro.NameServer','list'); return false;">Pyro.NameServer/list</a>
  112. -- lists the contents of the name server</li>
  113. <li><a href="Pyro.NameServer/list?prefix=test."
  114. onclick="pyro_call('Pyro.NameServer','list', {{'prefix':'test.'}}); return false;">
  115. Pyro.NameServer/list?prefix=test.</a> -- lists the contents of the name server starting with 'test.'</li>
  116. <li><a href="Pyro.NameServer/lookup?name=Pyro.NameServer"
  117. onclick="pyro_call('Pyro.NameServer','lookup', {{'name':'Pyro.NameServer'}}); return false;">
  118. Pyro.NameServer/lookup?name=Pyro.NameServer</a> -- perform lookup method of the name server</li>
  119. <li><a href="Pyro.NameServer/lookup?name=test.echoserver"
  120. onclick="pyro_call('Pyro.NameServer','lookup', {{'name':'test.echoserver'}}); return false;">
  121. Pyro.NameServer/lookup?name=test.echoserver</a> -- perform lookup method of the echo server</li>
  122. </ul>
  123. <p>Echoserver examples: (these examples are working if you expose the test.echoserver object)</p>
  124. <ul>
  125. <li><a href="test.echoserver/error" onclick="pyro_call('test.echoserver','error'); return false;">test.echoserver/error</a>
  126. -- perform error call on echoserver</li>
  127. <li><a href="test.echoserver/echo?message=Hi there, browser script!"
  128. onclick="pyro_call('test.echoserver','echo', {{'message':'Hi there, browser script!'}}); return false;">
  129. test.echoserver/echo?message=Hi there, browser script!</a> -- perform echo call on echoserver</li>
  130. </ul>
  131. <h2>Pyro response data (via Ajax):</h2>
  132. Call: <pre id="pyro_call"> &nbsp; </pre>
  133. Response: <pre id="pyro_response"> &nbsp; </pre>
  134. <p>Pyro version: {pyro_version} &mdash; &copy; Irmen de Jong</p>
  135. </body>
  136. </html>
  137. """
  138. def return_homepage(environ, start_response):
  139. try:
  140. nameserver = get_nameserver(hmac=pyro_app.hmac_key)
  141. except errors.NamingError as x:
  142. print("Name server error:", x)
  143. start_response('500 Internal Server Error', [('Content-Type', 'text/plain')])
  144. return [b"Cannot connect to the Pyro name server. Is it running? Refresh page to retry."]
  145. start_response('200 OK', [('Content-Type', 'text/html')])
  146. nslist = ["<table><tr><th>Name</th><th>methods</th><th>attributes (zero-param methods)</th></tr>"]
  147. names = sorted(list(nameserver.list(regex=pyro_app.ns_regex).keys())[:10])
  148. with core.batch(nameserver) as nsbatch:
  149. for name in names:
  150. nsbatch.lookup(name)
  151. for name, uri in zip(names, nsbatch()):
  152. attributes = "-"
  153. try:
  154. with core.Proxy(uri) as proxy:
  155. proxy._pyroHmacKey = pyro_app.hmac_key
  156. proxy._pyroBind()
  157. methods = " &nbsp; ".join(proxy._pyroMethods) or "-"
  158. attributes = [
  159. "<a href=\"{name}/{attribute}\" onclick=\"pyro_call('{name}','{attribute}'); return false;\">{attribute}</a>"
  160. .format(name=name, attribute=attribute)
  161. for attribute in proxy._pyroAttrs
  162. ]
  163. attributes = " &nbsp; ".join(attributes) or "-"
  164. except errors.PyroError as x:
  165. stderr = environ["wsgi.errors"]
  166. print("ERROR getting metadata for {0}:".format(uri), file=stderr)
  167. traceback.print_exc(file=stderr)
  168. methods = "??error:%s??" % str(x)
  169. nslist.append(
  170. "<tr><td><a href=\"{name}/$meta\" onclick=\"pyro_call('{name}','$meta'); "
  171. "return false;\">{name}</a></td><td>{methods}</td><td>{attributes}</td></tr>"
  172. .format(name=name, methods=methods, attributes=attributes))
  173. nslist.append("</table>")
  174. index_page = index_page_template.format(ns_regex=pyro_app.ns_regex,
  175. name_server_contents_list="".join(nslist),
  176. pyro_version=constants.VERSION,
  177. hostname=nameserver._pyroUri.location)
  178. return [index_page.encode("utf-8")]
  179. def process_pyro_request(environ, path, parameters, start_response):
  180. pyro_options = environ.get("HTTP_X_PYRO_OPTIONS", "").split(",")
  181. if not path:
  182. return return_homepage(environ, start_response)
  183. matches = re.match(r"(.+)/(.+)", path)
  184. if not matches:
  185. return not_found(start_response)
  186. object_name, method = matches.groups()
  187. if pyro_app.gateway_key:
  188. gateway_key = environ.get("HTTP_X_PYRO_GATEWAY_KEY", "") or parameters.get("$key", "")
  189. gateway_key = gateway_key.encode("utf-8")
  190. if gateway_key != pyro_app.gateway_key:
  191. start_response('403 Forbidden', [('Content-Type', 'text/plain')])
  192. return [b"403 Forbidden - incorrect gateway api key"]
  193. if "$key" in parameters:
  194. del parameters["$key"]
  195. if pyro_app.ns_regex and not re.match(pyro_app.ns_regex, object_name):
  196. start_response('403 Forbidden', [('Content-Type', 'text/plain')])
  197. return [b"403 Forbidden - access to the requested object has been denied"]
  198. try:
  199. nameserver = get_nameserver(hmac=pyro_app.hmac_key)
  200. uri = nameserver.lookup(object_name)
  201. with core.Proxy(uri) as proxy:
  202. header_corr_id = environ.get("HTTP_X_PYRO_CORRELATION_ID", "")
  203. if header_corr_id:
  204. core.current_context.correlation_id = uuid.UUID(header_corr_id) # use the correlation id from the request header
  205. else:
  206. core.current_context.correlation_id = uuid.uuid4() # set new correlation id
  207. proxy._pyroHmacKey = pyro_app.hmac_key
  208. proxy._pyroGetMetadata()
  209. if "oneway" in pyro_options:
  210. proxy._pyroOneway.add(method)
  211. if method == "$meta":
  212. result = {"methods": tuple(proxy._pyroMethods), "attributes": tuple(proxy._pyroAttrs)}
  213. reply = json.dumps(result).encode("utf-8")
  214. start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8'),
  215. ('X-Pyro-Correlation-Id', str(core.current_context.correlation_id))])
  216. return [reply]
  217. else:
  218. proxy._pyroRawWireResponse = True # we want to access the raw response json
  219. if method in proxy._pyroAttrs:
  220. # retrieve the attribute
  221. assert not parameters, "attribute lookup can't have query parameters"
  222. msg = getattr(proxy, method)
  223. else:
  224. # call the remote method
  225. msg = getattr(proxy, method)(**parameters)
  226. if msg is None or "oneway" in pyro_options:
  227. # was a oneway call, no response available
  228. start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8'),
  229. ('X-Pyro-Correlation-Id', str(core.current_context.correlation_id))])
  230. return []
  231. elif msg.flags & message.FLAGS_EXCEPTION:
  232. # got an exception response so send a 500 status
  233. start_response('500 Internal Server Error', [('Content-Type', 'application/json; charset=utf-8')])
  234. return [msg.data]
  235. else:
  236. # normal response
  237. start_response('200 OK', [('Content-Type', 'application/json; charset=utf-8'),
  238. ('X-Pyro-Correlation-Id', str(core.current_context.correlation_id))])
  239. return [msg.data]
  240. except Exception as x:
  241. stderr = environ["wsgi.errors"]
  242. print("ERROR handling {0} with params {1}:".format(path, parameters), file=stderr)
  243. traceback.print_exc(file=stderr)
  244. start_response('500 Internal Server Error', [('Content-Type', 'application/json; charset=utf-8')])
  245. reply = json.dumps(util.SerializerBase.class_to_dict(x)).encode("utf-8")
  246. return [reply]
  247. def pyro_app(environ, start_response):
  248. """
  249. The WSGI app function that is used to process the requests.
  250. You can stick this into a wsgi server of your choice, or use the main() method
  251. to use the default wsgiref server.
  252. """
  253. config.SERIALIZER = "json" # we only talk json through the http proxy
  254. config.COMMTIMEOUT = pyro_app.comm_timeout
  255. method = environ.get("REQUEST_METHOD")
  256. path = environ.get('PATH_INFO', '').lstrip('/')
  257. if not path:
  258. return redirect(start_response, "/pyro/")
  259. if path.startswith("pyro/"):
  260. if method in ("GET", "POST"):
  261. parameters = singlyfy_parameters(cgi.parse(environ['wsgi.input'], environ))
  262. return process_pyro_request(environ, path[5:], parameters, start_response)
  263. else:
  264. return invalid_request(start_response)
  265. return not_found(start_response)
  266. def singlyfy_parameters(parameters):
  267. """
  268. Makes a cgi-parsed parameter dictionary into a dict where the values that
  269. are just a list of a single value, are converted to just that single value.
  270. """
  271. for key, value in parameters.items():
  272. if isinstance(value, (list, tuple)) and len(value) == 1:
  273. parameters[key] = value[0]
  274. return parameters
  275. pyro_app.ns_regex = r"http\."
  276. pyro_app.hmac_key = None
  277. pyro_app.gateway_key = None
  278. pyro_app.comm_timeout = config.COMMTIMEOUT
  279. def main(args=None):
  280. from optparse import OptionParser
  281. parser = OptionParser()
  282. parser.add_option("-H", "--host", default="localhost", help="hostname to bind server on (default=%default)")
  283. parser.add_option("-p", "--port", type="int", default=8080, help="port to bind server on (default=%default)")
  284. parser.add_option("-e", "--expose", default=pyro_app.ns_regex, help="a regex of object names to expose (default=%default)")
  285. parser.add_option("-k", "--pyrokey", help="the HMAC key to use to connect with Pyro (deprecated)")
  286. parser.add_option("-g", "--gatewaykey", help="the api key to use to connect to the gateway itself")
  287. parser.add_option("-t", "--timeout", type="float", default=pyro_app.comm_timeout,
  288. help="Pyro timeout value to use (COMMTIMEOUT setting, default=%default)")
  289. options, args = parser.parse_args(args)
  290. if options.pyrokey or options.gatewaykey:
  291. warnings.warn("using -k and/or -g to supply keys on the command line is a security problem "
  292. "and is deprecated since Pyro 4.72. See the documentation for an alternative.")
  293. if "PYRO_HMAC_KEY" in os.environ:
  294. if options.pyrokey:
  295. raise SystemExit("error: don't use -k and PYRO_HMAC_KEY at the same time")
  296. options.pyrokey = os.environ["PYRO_HMAC_KEY"]
  297. if "PYRO_HTTPGATEWAY_KEY" in os.environ:
  298. if options.gatewaykey:
  299. raise SystemExit("error: don't use -g and PYRO_HTTPGATEWAY_KEY at the same time")
  300. options.gatewaykey = os.environ["PYRO_HTTPGATEWAY_KEY"]
  301. pyro_app.hmac_key = (options.pyrokey or "").encode("utf-8")
  302. pyro_app.gateway_key = (options.gatewaykey or "").encode("utf-8")
  303. pyro_app.ns_regex = options.expose
  304. pyro_app.comm_timeout = config.COMMTIMEOUT = options.timeout
  305. if pyro_app.ns_regex:
  306. print("Exposing objects with names matching: ", pyro_app.ns_regex)
  307. else:
  308. print("Warning: exposing all objects (no expose regex set)")
  309. try:
  310. ns = get_nameserver(hmac=pyro_app.hmac_key)
  311. except errors.PyroError:
  312. print("Not yet connected to a name server.")
  313. else:
  314. print("Connected to name server at: ", ns._pyroUri)
  315. server = make_server(options.host, options.port, pyro_app)
  316. print("Pyro HTTP gateway running on http://{0}:{1}/pyro/".format(*server.socket.getsockname()))
  317. server.serve_forever()
  318. server.server_close()
  319. return 0
  320. if __name__ == "__main__":
  321. sys.exit(main())