X
X
Xaip2019-01-01 14:20:37
Tornado
Xaip, 2019-01-01 14:20:37

Why is tornado blocking headers?

It is necessary to throw Authorization with a certain key in the headers. If you set the headers directly through the options and set_default_headers methods, then everything is ok. But there are more than 100 handlers in the code, and it’s not comme il faut to hang up your own options for each.
This is how it works

class LongPollingHandler(tornado.web.RequestHandler):

    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")

    def options(self, *args, **kwargs):
        self.set_header("Access-Control-Allow-Headers", "*")
        self.set_header('Access-Control-Allow-Methods', '*')


    async def get(self):
        pass

But it does not work if inherited through the Mixin class:
class BaseHandler(tornado.web.RequestHandler):

    def set_default_headers(self):
        self.set_header("Access-Control-Allow-Origin", "*")


    def options(self, *args, **kwargs):
        self.set_header("Access-Control-Allow-Headers", "*")
        self.set_header('Access-Control-Allow-Methods', '*')

    def prepare(self):
        if db.is_closed():
            db.connect()
        return super(BaseHandler, self).prepare()

    def on_finish(self):
        if not db.is_closed():
            db.close()
        return super(BaseHandler, self).on_finish()

class LongPollingHandler(BaseHandler):
    pass

Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-01-01
@Xaip

class BaseHandler(tornado.web.RequestHandler):
    def set_default_headers(self):
        self.set_header('Access-Control-Allow-Origin', '*')

    def options(self, *args, **kwargs):
        self.set_header('Access-Control-Allow-Headers', '*')
        self.set_header('Access-Control-Allow-Methods', '*')
        self.set_status(204)
        self.finish()


class LongPollingHandler(BaseHandler):
    def get(self, status):
        self.write('{"data": "Hello"}')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question