2016-05-31 20:49:30 +00:00
|
|
|
import tornado
|
|
|
|
import tornado.web
|
|
|
|
import tornado.gen
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from objects import glob
|
2016-06-15 17:01:00 +00:00
|
|
|
from raven.contrib.tornado import SentryMixin
|
|
|
|
from raven.contrib.tornado import AsyncSentryClient
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
class asyncRequestHandler(tornado.web.RequestHandler):
|
|
|
|
"""
|
|
|
|
Tornado asynchronous request handler
|
|
|
|
create a class that extends this one (requestHelper.asyncRequestHandler)
|
|
|
|
use asyncGet() and asyncPost() instad of get() and post().
|
|
|
|
Done. I'm not kidding.
|
|
|
|
"""
|
|
|
|
@tornado.web.asynchronous
|
|
|
|
@tornado.gen.engine
|
|
|
|
def get(self, *args, **kwargs):
|
2016-06-15 17:01:00 +00:00
|
|
|
try:
|
|
|
|
yield tornado.gen.Task(runBackground, (self.asyncGet, tuple(args), dict(kwargs)))
|
|
|
|
except Exception as e:
|
|
|
|
yield tornado.gen.Task(self.captureException, exc_info=True)
|
2016-06-16 11:38:17 +00:00
|
|
|
finally:
|
2016-06-17 11:49:39 +00:00
|
|
|
if not self._finished:
|
|
|
|
self.finish()
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
@tornado.web.asynchronous
|
|
|
|
@tornado.gen.engine
|
|
|
|
def post(self, *args, **kwargs):
|
2016-06-15 17:01:00 +00:00
|
|
|
try:
|
|
|
|
yield tornado.gen.Task(runBackground, (self.asyncPost, tuple(args), dict(kwargs)))
|
|
|
|
except Exception as e:
|
|
|
|
yield tornado.gen.Task(self.captureException, exc_info=True)
|
2016-06-16 11:38:17 +00:00
|
|
|
finally:
|
2016-06-17 11:49:39 +00:00
|
|
|
if not self._finished:
|
|
|
|
self.finish()
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
def asyncGet(self, *args, **kwargs):
|
|
|
|
self.send_error(405)
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
def asyncPost(self, *args, **kwargs):
|
|
|
|
self.send_error(405)
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
def getRequestIP(self):
|
2016-06-23 15:51:19 +00:00
|
|
|
realIP = self.request.headers.get("X-Forwarded-For") if glob.cloudflare == True else self.request.headers.get("X-Real-IP")
|
2016-05-31 20:49:30 +00:00
|
|
|
if realIP != None:
|
|
|
|
return realIP
|
|
|
|
return self.request.remote_ip
|
|
|
|
|
|
|
|
|
|
|
|
def runBackground(data, callback):
|
|
|
|
"""
|
|
|
|
Run a function in the background.
|
|
|
|
Used to handle multiple requests at the same time
|
|
|
|
"""
|
|
|
|
func, args, kwargs = data
|
|
|
|
def _callback(result):
|
|
|
|
IOLoop.instance().add_callback(lambda: callback(result))
|
|
|
|
glob.pool.apply_async(func, args, kwargs, _callback)
|
|
|
|
|
|
|
|
|
|
|
|
def checkArguments(arguments, requiredArguments):
|
|
|
|
"""
|
|
|
|
Check that every requiredArguments elements are in arguments
|
|
|
|
|
|
|
|
arguments -- full argument list, from tornado
|
|
|
|
requiredArguments -- required arguments list es: ["u", "ha"]
|
|
|
|
handler -- handler string name to print in exception. Optional
|
|
|
|
return -- True if all arguments are passed, none if not
|
|
|
|
"""
|
|
|
|
for i in requiredArguments:
|
|
|
|
if i not in arguments:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def printArguments(t):
|
|
|
|
"""
|
|
|
|
Print passed arguments, for debug purposes
|
|
|
|
|
|
|
|
t -- tornado object (self)
|
|
|
|
"""
|
|
|
|
print("ARGS::")
|
|
|
|
for i in t.request.arguments:
|
|
|
|
print ("{}={}".format(i, t.get_argument(i)))
|