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__)
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__)