Ejemplo n.º 1
0
    def generate_response(self, environ, start_response):
        if self.content_length is not None:
            del self.content_length
        headerlist = list(self.headerlist)
        accept_value = environ.get('HTTP_ACCEPT', '')
        accept = MIMEAccept(accept_value)
        match = accept.best_match(['text/html', 'application/json'])

        if match == 'text/html':
            content_type = 'text/html'
            body = self.html_body(environ)
        elif match == 'application/json':
            content_type = 'application/json'
            body = self.json_body(environ)
        else:
            content_type = 'text/plain'
            body = self.plain_body(environ)
        extra_kw = {}
        if isinstance(body, text_type):
            extra_kw.update(charset='utf-8')
        resp = Response(body,
                        status=self.status,
                        headerlist=headerlist,
                        content_type=content_type,
                        **extra_kw
                        )
        resp.content_type = content_type
        return resp(environ, start_response)
Ejemplo n.º 2
0
    def generate_response(self, environ, start_response):
        if self.content_length is not None:
            del self.content_length
        headerlist = list(self.headerlist)
        accept_value = environ.get('HTTP_ACCEPT', '')
        accept = MIMEAccept(accept_value)
        match = accept.best_match(['text/html', 'application/json'])

        if match == 'text/html':
            content_type = 'text/html'
            body = self.html_body(environ)
        elif match == 'application/json':
            content_type = 'application/json'
            body = self.json_body(environ)
        else:
            content_type = 'text/plain'
            body = self.plain_body(environ)
        resp = Response(
            body,
            status=self.status,
            headerlist=headerlist,
            content_type=content_type,
        )
        resp.content_type = content_type
        return resp(environ, start_response)
Ejemplo n.º 3
0
def test_accept_json():
    mimeaccept = MIMEAccept('text/html, *; q=.2, */*; q=.2')
    assert mimeaccept.best_match(['application/json']) == 'application/json'
Ejemplo n.º 4
0
    def prepare(self, environ):
        if not self.has_body and not self.empty_body:
            html_comment = ''
            comment = self.comment or ''
            accept_value = environ.get('HTTP_ACCEPT', '')
            accept = MIMEAccept(accept_value)
            # Attempt to match text/html or application/json, if those don't
            # match, we will fall through to defaulting to text/plain
            match = accept.best_match(['text/html', 'application/json'])

            if match == 'text/html':
                self.content_type = 'text/html'
                escape = _html_escape
                page_template = self.html_template_obj
                br = '<br/>'
                if comment:
                    html_comment = '<!-- %s -->' % escape(comment)
            elif match == 'application/json':
                self.content_type = 'application/json'
                self.charset = None
                escape = _no_escape
                br = '\n'
                if comment:
                    html_comment = escape(comment)

                class JsonPageTemplate(object):
                    def __init__(self, excobj):
                        self.excobj = excobj

                    def substitute(self, status, body):
                        jsonbody = self.excobj._json_formatter(
                            status=status,
                            body=body,
                            title=self.excobj.title,
                            environ=environ)
                        return json.dumps(jsonbody)

                page_template = JsonPageTemplate(self)
            else:
                self.content_type = 'text/plain'
                escape = _no_escape
                page_template = self.plain_template_obj
                br = '\n'
                if comment:
                    html_comment = escape(comment)
            args = {
                'br': br,
                'explanation': escape(self.explanation),
                'detail': escape(self.detail or ''),
                'comment': escape(comment),
                'html_comment': html_comment,
            }
            body_tmpl = self.body_template_obj
            if HTTPException.body_template_obj is not body_tmpl:
                # Custom template; add headers to args
                for k, v in environ.items():
                    if (not k.startswith('wsgi.')) and ('.' in k):
                        # omit custom environ variables, stringifying them may
                        # trigger code that should not be executed here; see
                        # https://github.com/Pylons/pyramid/issues/239
                        continue
                    args[k] = escape(v)
                for k, v in self.headers.items():
                    args[k.lower()] = escape(v)
            body = body_tmpl.substitute(args)
            page = page_template.substitute(status=self.status, body=body)
            if isinstance(page, text_type):
                page = page.encode(self.charset if self.charset else 'UTF-8')
            self.app_iter = [page]
            self.body = page
Ejemplo n.º 5
0
def test_match_mixedcase():
    mimeaccept = MIMEAccept('image/jpg; q=.2, Image/pNg; Q=.4, image/*; q=.05')
    assert mimeaccept.best_match(['Image/JpG']) == 'Image/JpG'
    assert mimeaccept.best_match(['image/Tiff']) == 'image/Tiff'
    assert mimeaccept.best_match(['image/Tiff', 'image/PnG', 'image/jpg']) == 'image/PnG'