def parse(self): try: request_line, headers = self.raw.split('\r\n', 1) self.method, self.path, self.version = request_line.split(' ', 2) self.path = [unquote(x) for x in self.path.split('/') if x] headers, body = headers.split('\r\n\r\n', 1) try: self.body = striptags(json.loads(body)) except: pass for header in headers.split('\r\n'): key, value = header.split(':', 1) self.headers[key.strip()] = value.strip() if 'Cookie' in self.headers: key, value = self.headers['Cookie'].split(';')[0].split('=') if key == 'session_id' and value: self.session_id = value self.is_valid = True except: self.is_valid = False
def __init__(self, version='HTTP/1.0', status=None, headers=None, body=None): self.version = version if status and status in self.statuses: self.status = self.statuses[status] else: self.status = self.statuses['default'] if headers: self.headers = headers else: self.headers = {} if not 'Content-type' in self.headers: self.headers['Content-type'] = 'application/json' if body: try: self.body = json.dumps(striptags(body)) except: self.body = None self.status = self.statuses[500] return self.headers['Content-length'] = len(self.body) else: self.body = None
def __init__(self, version = 'HTTP/1.0', status = None, headers = None, body = None): self.version = version if status and status in self.statuses: self.status = self.statuses[status] else: self.status = self.statuses['default'] if headers: self.headers = headers else: self.headers = {} if not 'Content-type' in self.headers: self.headers['Content-type'] = 'application/json' if body: try: self.body = json.dumps(striptags(body)) except: self.body = None self.status = self.statuses[500] return self.headers['Content-length'] = len(self.body) else: self.body = None