def read_request(reader, outputfile, request_status, parse_config): """ read and output one http request. """ if 'expect' in request_status and not textutils.ishttprequest( reader.fetchline()): headers = request_status['expect'] del request_status['expect'] else: headers = read_http_headers(reader, outputfile, parse_config.level) if headers.expect: # assume it is expect:continue-100 request_status['expect'] = headers if headers is None or not isinstance(headers, HttpRequestHeader): outputfile.write("{Error, cannot parse http request headers.}") outputfile.write('\n') print reader.readall() return mime, charset = textutils.parse_content_type(headers.content_type) # usually charset is not set in http post output_body = parse_config.level >= OutputLevel.ALL_BODY and not textutils.isbinarybody(mime) \ or parse_config.level >= OutputLevel.TEXT_BODY and textutils.istextbody(mime) content = '' # deal with body if not headers.chunked: if output_body: content = reader.read(headers.content_len) else: reader.skip(headers.content_len) else: content = read_chunked_body(reader) if not headers.gzip: # if is gzip by content magic header # someone missed the content-encoding header headers.gzip = textutils.isgzip(content) # if it is form url encode if 'expect' in request_status and not content: content = '{Expect-continue-100, see next content for http post body}' if output_body: #unescape www-form-encoded data.x-www-form-urlencoded if parse_config.encoding and not charset: charset = parse_config.encoding print_body(content, headers.gzip, charset, outputfile, 'www-form-encoded' in mime, parse_config.pretty)
def read_request(reader, outputfile, request_status, parse_config): """ read and output one http request. """ if 'expect' in request_status and not textutils.ishttprequest(reader.fetchline()): headers = request_status['expect'] del request_status['expect'] else: headers = read_http_headers(reader, outputfile, parse_config.level) if headers is None or not isinstance(headers, HttpRequestHeader): outputfile.write("{Error, cannot parse http request headers.}") outputfile.write('\n') reader.skipall() return if headers.expect: # assume it is expect:continue-100 request_status['expect'] = headers mime, charset = textutils.parse_content_type(headers.content_type) # usually charset is not set in http post output_body = parse_config.level >= OutputLevel.ALL_BODY and not textutils.isbinarybody(mime) \ or parse_config.level >= OutputLevel.TEXT_BODY and textutils.istextbody(mime) content = '' # deal with body if not headers.chunked: if output_body: content = reader.read(headers.content_len) else: reader.skip(headers.content_len) else: content = read_chunked_body(reader) if not headers.gzip: # if is gzip by content magic header # someone missed the content-encoding header headers.gzip = textutils.isgzip(content) # if it is form url encode if 'expect' in request_status and not content: content = '{Expect-continue-100, see next content for http post body}' if output_body: #unescape www-form-encoded data.x-www-form-urlencoded if parse_config.encoding and not charset: charset = parse_config.encoding print_body(content, headers.gzip, charset, outputfile, mime and 'form-urlencoded' in mime, parse_config.pretty)
def read_response(reader, outputfile, request_status, parse_config): """ read and output one http response """ headers = read_http_headers(reader, outputfile, parse_config.level) if headers is None or not isinstance(headers, HttpReponseHeader): outputfile.write("{Error, cannot parse http response headers.}") outputfile.write('\n') reader.skipall() return # read body mime, charset = textutils.parse_content_type(headers.content_type) if parse_config.encoding and not charset: charset = parse_config.encoding output_body = parse_config.level >= OutputLevel.ALL_BODY and not textutils.isbinarybody(mime) \ or parse_config.level >= OutputLevel.TEXT_BODY and textutils.istextbody(mime) content = '' # deal with body if not headers.chunked: if headers.content_len == 0: if headers.connectionclose: # we can't get content length, so asume it till the end of data. #TODO: add readall method headers.content_len = 10000000L else: #TODO: we can't get content length, and is not a chunked body. pass if output_body: content = reader.read(headers.content_len) else: reader.skip(headers.content_len) else: #TODO: could skip chunked data. content = read_chunked_body(reader) if output_body: print_body(content, headers.gzip, charset, outputfile, False, parse_config.pretty)