Example #1
0
 def header(self):
     if 'brick.headers' not in self.environ:
         header = self.environ['brick.headers'] = HeaderDict()
         for key, value in self.environ.iteritems():
             if key.startswith('HTTP_'):
                 key = key[5:].replace('_', '-').title()
                 header[key] = value
     return self.environ['brick.headers']
Example #2
0
class HTTPResponse(BrickException):
    """ Used to break execution and immediately finish the response """
    def __init__(self, output='', status=200, header=None):
        super(BrickException, self).__init__("HTTP Response %d" % status)
        self.status = int(status)
        self.output = output
        self.headers = HeaderDict(header) if header else None

    def apply(self, response):
        if self.headers:
            for key, value in self.headers.iterallitems():
                response.headers[key] = value
        response.status = self.status
Example #3
0
class HTTPResponse(BrickException):
    """ Used to break execution and immediately finish the response """
    def __init__(self, output='', status=200, header=None):
        super(BrickException, self).__init__("HTTP Response %d" % status)
        self.status = int(status)
        self.output = output
        self.headers = HeaderDict(header) if header else None

    def apply(self, response):
        if self.headers:
            for key, value in self.headers.iterallitems():
                response.headers[key] = value
        response.status = self.status
Example #4
0
 def bind(self, config=None):
     self._COOKIES = None
     self.status = 200
     self.headers = HeaderDict()
     self.content_type = 'text/html; charset=UTF-8'
     self.config = config or {}
Example #5
0
class Response(object):
    def __init__(self, config=None):
        self.bind(config)

    def bind(self, config=None):
        self._COOKIES = None
        self.status = 200
        self.headers = HeaderDict()
        self.content_type = 'text/html; charset=UTF-8'
        self.config = config or {}

    @property
    def header(self):
        depr("Response.header renamed to Response.headers")
        return self.headers

    def copy(self):
        copy = Response(self.config)
        copy.status = self.status
        copy.headers = self.headers.copy()
        copy.content_type = self.content_type
        return copy

    def wsgiheader(self):
        for c in self.COOKIES.values():
            if c.OutputString() not in self.headers.getall('Set-Cookie'):
                self.headers.append('Set-Cookie', c.OutputString())
        if self.status in (204, 304) and 'content-type' in self.headers:
            del self.headers['content-type']
        if self.status == 304:
            for h in ('allow', 'content-encoding', 'content-language',
                      'content-length', 'content-md5', 'content-range',
                      'content-type', 'last-modified'): 
                if h in self.headers:
                    del self.headers[h]
        return list(self.headers.iterallitems())
    headerlist = property(wsgiheader)

    @property
    def charset(self):
        if 'charset=' in self.content_type:
            return self.content_type.split('charset=')[-1].split(';')[0].strip()
        return 'UTF-8'

    @property
    def COOKIES(self):
        if not self._COOKIES:
            self._COOKIES = SimpleCookie()
        return self._COOKIES

    def set_cookie(self, key, value, secret=None, **kargs):
        if not isinstance(value, basestring):
            if not secret:
                raise TypeError('Cookies must be strings when secret is not set')
            value = cookie_encode(value, secret).decode('ascii') 
        self.COOKIES[key] = value
        for k, v in kargs.iteritems():
            self.COOKIES[key][k.replace('_', '-')] = v

    def get_content_type(self):
        return self.headers['Content-Type']

    def set_content_type(self, value):
        self.headers['Content-Type'] = value

    content_type = property(get_content_type, set_content_type, None,
                            get_content_type.__doc__)
Example #6
0
 def __init__(self, output='', status=200, header=None):
     super(BrickException, self).__init__("HTTP Response %d" % status)
     self.status = int(status)
     self.output = output
     self.headers = HeaderDict(header) if header else None
Example #7
0
 def __init__(self, output='', status=200, header=None):
     super(BrickException, self).__init__("HTTP Response %d" % status)
     self.status = int(status)
     self.output = output
     self.headers = HeaderDict(header) if header else None
Example #8
0
 def bind(self, config=None):
     self._COOKIES = None
     self.status = 200
     self.headers = HeaderDict()
     self.content_type = 'text/html; charset=UTF-8'
     self.config = config or {}
Example #9
0
class Response(object):
    def __init__(self, config=None):
        self.bind(config)

    def bind(self, config=None):
        self._COOKIES = None
        self.status = 200
        self.headers = HeaderDict()
        self.content_type = 'text/html; charset=UTF-8'
        self.config = config or {}

    @property
    def header(self):
        depr("Response.header renamed to Response.headers")
        return self.headers

    def copy(self):
        copy = Response(self.config)
        copy.status = self.status
        copy.headers = self.headers.copy()
        copy.content_type = self.content_type
        return copy

    def wsgiheader(self):
        for c in self.COOKIES.values():
            if c.OutputString() not in self.headers.getall('Set-Cookie'):
                self.headers.append('Set-Cookie', c.OutputString())
        if self.status in (204, 304) and 'content-type' in self.headers:
            del self.headers['content-type']
        if self.status == 304:
            for h in ('allow', 'content-encoding', 'content-language',
                      'content-length', 'content-md5', 'content-range',
                      'content-type', 'last-modified'):
                if h in self.headers:
                    del self.headers[h]
        return list(self.headers.iterallitems())

    headerlist = property(wsgiheader)

    @property
    def charset(self):
        if 'charset=' in self.content_type:
            return self.content_type.split('charset=')[-1].split(
                ';')[0].strip()
        return 'UTF-8'

    @property
    def COOKIES(self):
        if not self._COOKIES:
            self._COOKIES = SimpleCookie()
        return self._COOKIES

    def set_cookie(self, key, value, secret=None, **kargs):
        if not isinstance(value, basestring):
            if not secret:
                raise TypeError(
                    'Cookies must be strings when secret is not set')
            value = cookie_encode(value, secret).decode('ascii')
        self.COOKIES[key] = value
        for k, v in kargs.iteritems():
            self.COOKIES[key][k.replace('_', '-')] = v

    def get_content_type(self):
        return self.headers['Content-Type']

    def set_content_type(self, value):
        self.headers['Content-Type'] = value

    content_type = property(get_content_type, set_content_type, None,
                            get_content_type.__doc__)