def __call__(self, request): if not 'Content-Type' in self.headers: self.headers['Content-Type'] = 'text/html; charset=%s' % \ str(request.charset) headers = self.headers.get() if not self._cookies is None: for morsel in self._cookies.values(): headers.append(('Set-Cookie', morsel.output(header=''))) status = '%d %s' % (self.status, HTTP_STATUS_CODES.get(self.status, 'UNKNOWN')) request.start_response(status, headers) if self.response is None: yield '' elif isinstance(self.response, unicode): yield self.response.encode(request.charset) elif isinstance(self.response, str): yield self.response else: try: iterator = iter(self.response) except TypeError: raise TypeError('%r is not an valid response' % self.response) for line in iterator: if isinstance(line, unicode): yield line.encode(request.charset) elif isinstance(line, str): yield line else: raise TypeError('%r is not string or unicode' % line)
def __call__(self, request): if not 'Content-Type' in self.headers: self.headers['Content-Type'] = 'text/html; charset=%s' % \ str(request.charset) headers = self.headers.get() if not self._cookies is None: for morsel in self._cookies.values(): headers.append(('Set-Cookie', morsel.output(header=''))) status = '%d %s' % (self.status, HTTP_STATUS_CODES.get(self.status, 'UNKNOWN')) #enforce headers to be ascii strings headers = [ tuple(str(s) for s in x) for x in headers ] request.start_response(status, headers) return self.iter_response(request.charset)