Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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]