Exemplo n.º 1
0
    def handle(self, env, start_response):
        url = env['PATH_INFO'].strip('/')
        method = env['REQUEST_METHOD']
        handler = self.handlers.get(url)
        if handler:
            if method in handler:
                req = Request(env)
                res = Response()
                content = handler[method](req, res)
                template = handler['TEMPLATE']
                template_dir = handler['TEMPLATE_DIR']

                if isinstance(content, dict):  #possibly wrapped by @template
                    if 'template' in content:
                        template = content['template']
                        content = content['content']

                if isinstance(content, HTTPError):
                    content = getattr(content, method.lower())(req, res)
                elif template and isinstance(content, dict):
                    template = os.path.join(template_dir, template)
                    content = str(
                        Template(self.get_template(template)).render(content))
                    res.content_type = 'text/html'
                elif isinstance(content, dict):
                    content = json.dumps(content)
                    res.content_type = 'application/json'
                elif not content:
                    content = ''

                res.content_length = str(len(content))
                start_response(res.status, res.headers)
                return [content]
Exemplo n.º 2
0
    def handle(self, env, start_response):
        url = env['PATH_INFO'].strip('/')
        method = env['REQUEST_METHOD']
        handler =  self.handlers.get(url)
        if handler:
            if method in handler:
                req = Request(env)
                res = Response()
                content = handler[method](req, res)
                template = handler['TEMPLATE']
                template_dir = handler['TEMPLATE_DIR']

                if isinstance(content, dict): #possibly wrapped by @template
                    if 'template' in content:
                        template = content['template']
                        content = content['content']

                if isinstance(content, HTTPError):
                    content = getattr(content, method.lower())(req, res)
                elif template and isinstance(content, dict):
                    template = os.path.join(template_dir, template)
                    content = str(Template(self.get_template(template)).render(content))
                    res.content_type = 'text/html'
                elif isinstance(content, dict):
                    content = json.dumps(content)
                    res.content_type = 'application/json'
                elif not content:
                    content = ''

                res.content_length = str(len(content))
                start_response(res.status, res.headers)
                return [content]
Exemplo n.º 3
0
    def make_response(self):
        response = Response(ResponseCode.NOT_FOUND)
        real_path = os.path.normpath(self.root + self.request.path)       

        
        if (os.path.commonpath([real_path, self.root])) != self.root:     
            return response

        if os.path.isfile(os.path.join(real_path, DEFAULT_PAGE)):
            real_path = os.path.join(real_path, DEFAULT_PAGE)
        elif os.path.exists(real_path):                                         
            response.code = ResponseCode.FORBIDDEN
        else:                                                                   
            return response

        try:
            file = open(real_path, 'rb')
            content = file.read()
            if self.request.method == 'GET':
                response.content = content
                response.content_type = self.get_content_type(real_path)
            response.content_length = len(content)
            response.code = ResponseCode.OK
            file.close()
        except IOError as e:
            print("Error in path: " + real_path)                                       

        return response
Exemplo n.º 4
0
    def post(self, p_id, p_key):
        if self.throttler and self.throttler.reject(self.request):
            raise tornado.web.HTTPError(403)
        responder = Response(self.request)
        self.set_header("Content-Type", responder.content_type())

        content = self.get_post_content()
        if not content:  # Empty paest, treat as delete
            if not self.paestdb.delete_paest(p_id, p_key):
                raise tornado.web.HTTPError(401, responder.bad_id_or_key())
            else:
                raise tornado.web.HTTPError(200, responder.paest_deleted())
        else:  # We have content, it's a create/update

            paest = None
            updated = False
            if p_key:
                # paest updated.
                updated = self.paestdb.update_paest(p_id, p_key, content)

            if not updated:  # Wasn't an update, or at least failed to update
                # Implement the update_or_create logic
                paest = self.paestdb.create_paest(p_id, p_key, content)
                if paest:
                    p_id = paest.pid
                    p_key = paest.key

            if paest or updated:  # Create or update succeded
                raise tornado.web.HTTPError(200,
                                            responder.paest_links(p_id, p_key))
            else:
                raise tornado.web.HTTPError(400, responder.paest_failed())
Exemplo n.º 5
0
    def make_response(self):
        response = Response(ResponseCode.NOT_FOUND)
        real_path = os.path.normpath(self.root +
                                     self.request.path)  # нормализую путь

        # проверка на выход из root
        if (os.path.commonpath([
                real_path, self.root
        ])) != self.root:  # commonpath - вернет общий путь
            return response

        if os.path.isfile(os.path.join(real_path, DEFAULT_PAGE)):
            real_path = os.path.join(real_path, DEFAULT_PAGE)
        elif os.path.exists(real_path):  # директория||файл существует
            response.code = ResponseCode.FORBIDDEN
        else:  # директория||файл не существует
            return response

        try:
            file = open(real_path, 'rb')
            content = file.read()
            if self.request.method == 'GET':
                response.content = content
                response.content_type = self.get_content_type(real_path)
            response.content_length = len(content)
            response.code = ResponseCode.OK
            file.close()
        except IOError as e:
            print("Error in path: " + real_path)  # 403 (см. http_const)

        return response
Exemplo n.º 6
0
    def get(self, p_id, p_key):
        # p_key is not used during get requests
        # pylint: disable=W0613

        if self.throttler and self.throttler.reject(self.request):
            raise tornado.web.HTTPError(403)

        responder = Response(self.request)
        self.set_header("Content-Type", responder.content_type())

        paest = self.paestdb.get_paest(p_id)
        if paest is None:
            raise tornado.web.HTTPError(404, responder.not_found())
        else:
            raise tornado.web.HTTPError(200, responder.raw(paest.content))