def read_http_resp_header(self, reader): """read & parse http headers""" line = reader.readline() if line is None: return line line = line.strip() if not utils.is_response(line): return None resp_header = HttpResponseHeader() resp_header.status_line = line try: resp_header.status_code = int(line.split(' ')[1]) except: pass lines = [line] header_dict = self.read_headers(reader, lines) if b"content-length" in header_dict: resp_header.content_len = int(header_dict[b"content-length"]) if b'chunked' in header_dict[b"transfer-encoding"]: resp_header.chunked = True resp_header.content_type = header_dict[b'content-type'] resp_header.compress == utils.get_compress_type( header_dict[b"content-encoding"]) resp_header.connection_close = (header_dict[b'connection'] == b'close') resp_header.raw_data = b'\n'.join(lines) resp_header.filename = "" if b"content-disposition" in header_dict: cnt_dis = header_dict[b'content-disposition'] if cnt_dis.find("filename=") > 0: resp_header.filename = cnt_dis.split('=')[1].rstrip() return resp_header
def read_http_resp_header(self, reader): """read & parse http headers""" line = reader.readline() if line is None: return line line = line.strip() if not utils.is_response(line): return None resp_header = HttpResponseHeader() resp_header.status_line = line try: resp_header.status_code = int(line.split(' ')[1]) except: pass lines = [line] header_dict = self.read_headers(reader, lines) if b"content-length" in header_dict: resp_header.content_len = int(header_dict[b"content-length"]) if b'chunked' in header_dict[b"transfer-encoding"]: resp_header.chunked = True resp_header.content_type = header_dict[b'content-type'] resp_header.gzip = (b'gzip' in header_dict[b"content-encoding"]) resp_header.connection_close = (header_dict[b'connection'] == b'close') resp_header.raw_data = b'\n'.join(lines) return resp_header
def read_http_resp_header(self, reader): """read & parse response http headers Args: reader (DataReader): type of reader that is required Returns: `None` if not a response or any error happend , `dict` of parsed_header otherwise """ line = reader.read_line() if line is None: return line line = line.strip() if not utils.is_response(line): return None resp_header = HttpResponseHeader() resp_header.status_line = line try: resp_header.status_code = int(line.split(' ')[1]) except Exception: pass lines = [line] header_dict = self.read_headers(reader, lines) if b"content-length" in header_dict: resp_header.content_len = int(header_dict[b"content-length"]) if b"transfer-encoding" in header_dict and b'chunked' in header_dict[ b"transfer-encoding"]: resp_header.chunked = True resp_header.content_type = header_dict[b'content-type'] resp_header.compress == utils.get_compress_type(header_dict[ b"content-encoding"]) resp_header.connection_close = (header_dict[b'connection'] == b'close') resp_header.raw_data = b'\n'.join(lines) return resp_header
def read_http_resp_header(self, reader): """read & parse http headers""" line = reader.readline() if line is None: return line line = line.strip() if not utils.is_response(line): return None resp_header = HttpResponseHeader() resp_header.status_line = line try: resp_header.status_code = int(line.split(' ')[1]) except: pass lines = [line] header_dict = self.read_headers(reader, lines) if b"content-length" in header_dict: resp_header.content_len = int(header_dict[b"content-length"]) if b"location" in header_dict: resp_header.redirect_to = header_dict[b"location"] if b'chunked' in header_dict[b"transfer-encoding"]: resp_header.chunked = True resp_header.content_type = header_dict[b'content-type'] resp_header.compress == utils.get_compress_type(header_dict[b"content-encoding"]) resp_header.connection_close = (header_dict[b'connection'] == b'close') resp_header.raw_data = b'\n'.join(lines) resp_header.filename = "" if b"content-disposition" in header_dict: cnt_dis = header_dict[b'content-disposition'] if cnt_dis.find("filename=") > -1: resp_header.filename = cnt_dis.split('=')[1].rstrip() return resp_header
def test_is_response(self): self.assertTrue(utils.is_response(b'HTTP/1.1')) self.assertFalse(utils.is_response(b'HTTP1.1'))