def read_http_req_header(self, reader): """read & parse http headers""" line = reader.read_line() if line is None: return None line = line.strip() if not utils.is_request(line): return None req_header = HttpRequestHeader() items = line.split(b' ') if len(items) == 3: req_header.method = items[0] req_header.uri = items[1] req_header.protocol = items[2] lines = [line] header_dict = self.read_headers(reader, lines) if b"content-length" in header_dict: req_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"]: req_header.chunked = True req_header.content_type = header_dict[b'content-type'] req_header.compress = utils.get_compress_type( header_dict[b"content-encoding"]) req_header.host = header_dict[b"host"] if b'expect' in header_dict: req_header.expect = header_dict[b'expect'] req_header.raw_data = b'\n'.join(lines) return req_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.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.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: 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_req_header(self, reader): """read & parse http headers""" line = reader.read_line() if line is None: return None line = line.strip() if not utils.is_request(line): return None req_header = HttpRequestHeader() items = line.split(b' ') if len(items) == 3: req_header.method = items[0] req_header.uri = items[1] req_header.protocol = items[2] lines = [line] header_dict = self.read_headers(reader, lines) if b"content-length" in header_dict: req_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"]: req_header.chunked = True req_header.content_type = header_dict[b'content-type'] req_header.compress = utils.get_compress_type(header_dict[b"content-encoding"]) req_header.host = header_dict[b"host"] if b'expect' in header_dict: req_header.expect = header_dict[b'expect'] req_header.raw_data = b'\n'.join(lines) return req_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_req_header(self, reader): """read & parse http headers""" line = reader.readline() if line is None: return None line = line.strip() if not utils.is_request(line): return None req_header = HttpRequestHeader() items = line.split(b' ') if len(items) == 3: req_header.method = items[0] req_header.uri = items[1] req_header.protocol = items[2] lines = [line] header_dict = self.read_headers(reader, lines) # In case proxy was used if req_header.uri.find("http://") == 0: req_header.uri = req_header.uri[len("http://") + len(header_dict[b"host"]):] for key in header_dict.iterkeys(): CTCore.client.add_header(key, header_dict[key]) if b"content-length" in header_dict: req_header.content_len = int(header_dict[b"content-length"]) if b'chunked' in header_dict[b"transfer-encoding"]: req_header.chunked = True req_header.content_type = header_dict[b'content-type'] req_header.compress = utils.get_compress_type( header_dict[b"content-encoding"]) req_header.host = header_dict[b"host"] if b'expect' in header_dict: req_header.expect = header_dict[b'expect'] req_header.referer = "" if b"referer" in header_dict: req_header.referer = header_dict[b'referer'] req_header.raw_data = b'\n'.join(lines) return req_header
def read_http_req_header(self, reader): """read & parse http headers""" line = reader.readline() if line is None: return None line = line.strip() if not utils.is_request(line): return None req_header = HttpRequestHeader() items = line.split(b' ') if len(items) == 3: req_header.method = items[0] req_header.uri = items[1] req_header.protocol = items[2] lines = [line] header_dict = self.read_headers(reader, lines) # In case proxy was used if req_header.uri.find("http://") == 0: req_header.uri = req_header.uri[len("http://") + len(header_dict[b"host"]):] for key in header_dict.iterkeys(): CTCore.client.add_header(key, header_dict[key]) if b"content-length" in header_dict: req_header.content_len = int(header_dict[b"content-length"]) if b'chunked' in header_dict[b"transfer-encoding"]: req_header.chunked = True req_header.content_type = header_dict[b'content-type'] req_header.compress = utils.get_compress_type(header_dict[b"content-encoding"]) req_header.host = header_dict[b"host"] if b'expect' in header_dict: req_header.expect = header_dict[b'expect'] req_header.referer = "" if b"referer" in header_dict: req_header.referer = header_dict[b'referer'] req_header.raw_data = b'\n'.join(lines) return req_header