Beispiel #1
0
def read_request_head(rfile):
    """
    Parse an HTTP request head (request line + headers) from an input stream

    Args:
        rfile: The input stream

    Returns:
        The HTTP request object (without body)

    Raises:
        exceptions.HttpReadDisconnect: No bytes can be read from rfile.
        exceptions.HttpSyntaxException: The input is malformed HTTP.
        exceptions.HttpException: Any other error occured.
    """
    timestamp_start = time.time()
    if hasattr(rfile, "reset_timestamps"):
        rfile.reset_timestamps()

    form, method, scheme, host, port, path, http_version = _read_request_line(
        rfile)
    headers = _read_headers(rfile)

    if hasattr(rfile, "first_byte_timestamp"):
        # more accurate timestamp_start
        timestamp_start = rfile.first_byte_timestamp

    return request.Request(form, method, scheme, host, port, path,
                           http_version, headers, None, timestamp_start)
Beispiel #2
0
def read_request_head(lines: List[bytes]) -> request.Request:
    """
    Parse an HTTP request head (request line + headers) from an iterable of lines

    Args:
        lines: The input lines

    Returns:
        The HTTP request object (without body)

    Raises:
        ValueError: The input is malformed.
    """
    host, port, method, scheme, authority, path, http_version = _read_request_line(
        lines[0])
    headers = _read_headers(lines[1:])

    return request.Request(host=host,
                           port=port,
                           method=method,
                           scheme=scheme,
                           authority=authority,
                           path=path,
                           http_version=http_version,
                           headers=headers,
                           content=None,
                           trailers=None,
                           timestamp_start=time.time(),
                           timestamp_end=None)