Exemplo n.º 1
0
    def do_POST(self):
        ''' handling thrift messages '''
        client_host, client_port = self.client_address
        LOG.debug('Processing request from: ' +
                  client_host + ':' +
                  str(client_port))

        # create new thrift handler
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        suppress_handler = self.server.suppress_handler

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        otrans = TTransport.TFileObjectTransport(self.wfile)
        itrans = TTransport.TBufferedTransport(itrans,
                                               int(self.headers['Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        try:
            session = self.sc_session()
            acc_handler = ThriftRequestHandler(session,
                                               checker_md_docs,
                                               checker_md_docs_map,
                                               suppress_handler,
                                               self.db_version_info)

            processor = codeCheckerDBAccess.Processor(acc_handler)
            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.sc_session.remove()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            LOG.error(str(exn))
            self.send_error(404, "Request failed.")
            # self.send_header("content-type", "application/x-thrift")
            # self.end_headers()
            # self.wfile.write('')
            return
    def do_POST(self):
        """ Handling thrift messages. """
        client_host, client_port = self.client_address
        LOG.debug('Processing request from: ' + client_host + ':' +
                  str(client_port))

        # Create new thrift handler.
        checker_md_docs = self.server.checker_md_docs
        checker_md_docs_map = self.server.checker_md_docs_map
        suppress_handler = self.server.suppress_handler

        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
        input_protocol_factory = protocol_factory
        output_protocol_factory = protocol_factory

        itrans = TTransport.TFileObjectTransport(self.rfile)
        otrans = TTransport.TFileObjectTransport(self.wfile)
        itrans = TTransport.TBufferedTransport(
            itrans, int(self.headers['Content-Length']))
        otrans = TTransport.TMemoryBuffer()

        iprot = input_protocol_factory.getProtocol(itrans)
        oprot = output_protocol_factory.getProtocol(otrans)

        sess_token = self.check_auth_in_request()
        if self.path != '/Authentication' and not sess_token:
            # Bail out if the user is not authenticated...
            # This response has the possibility of melting down Thrift clients,
            # but the user is expected to properly authenticate first.

            LOG.debug(client_host + ":" + str(client_port) +
                      " Invalid access, credentials not found " +
                      "- session refused.")
            self.send_response(401)
            self.send_header("Content-type", "text/plain")
            self.send_header("Content-length", str(0))
            self.end_headers()

            return

        # Authentication is handled, we may now respond to the user.
        try:
            session = self.sc_session()
            acc_handler = ThriftRequestHandler(session, checker_md_docs,
                                               checker_md_docs_map,
                                               suppress_handler,
                                               self.db_version_info)

            if self.path == '/Authentication':
                # Authentication requests must be routed to a different
                # handler.
                auth_handler = ThriftAuthHandler(self.manager, client_host,
                                                 sess_token)
                processor = codeCheckerAuthentication.Processor(auth_handler)
            else:
                acc_handler = ThriftRequestHandler(session, checker_md_docs,
                                                   checker_md_docs_map,
                                                   suppress_handler,
                                                   self.db_version_info)

                processor = codeCheckerDBAccess.Processor(acc_handler)

            processor.process(iprot, oprot)
            result = otrans.getvalue()

            self.sc_session.remove()

            self.send_response(200)
            self.send_header("content-type", "application/x-thrift")
            self.send_header("Content-Length", len(result))
            self.end_headers()
            self.wfile.write(result)
            return

        except Exception as exn:
            LOG.error(str(exn))
            self.send_error(404, "Request failed.")
            return