class Response: """ I have no words to describe this class """ def __init__(self): self.body = None self.headers = HeaderDict() self.status = 200 def __repr__(self): return '<Response {}>'.format(self.status) def get_status_detail(self): return status_code.get(str(self.status)) def get_status(self): return str(self.status) + ' ' + status_code.get(str(self.status)) def get_header(self): return [(key, val) for key, val in self.headers.items()] def get_body(self): if self.body is None: self.headers['Content-Length'] = 0 return '' if isinstance(self.body, str): self.headers['Content-Type'] = 'text/plain' return self.body if isinstance(self.body, dict): self.headers['Content-Type'] = 'text/json' return json.dumps(self.body) if isinstance(self.body, bytes): self.headers['Content-Type'] = 'application/octet-stream' return self.body
def headers(self): result = HeaderDict() for key in self.environ: if key[:5] == 'HTTP_': result[key[5:].title().replace('_', '-')] = self.environ[key] elif key in ('CONTENT_TYPE', 'CONTENT_LENGTH'): result[key.title().replace('_', '-')] = self.environ[key] return result
def __init__(self): self.body = None self.headers = HeaderDict() self.status = 200