def read_message()

in packages/autorest.python/autorest/jsonrpc/stdstream.py [0:0]


def read_message(stream: BinaryIO = sys.stdin.buffer) -> str:
    # Content-Length
    order = stream.readline().rstrip()

    if not order.startswith(b"Content-Length"):
        raise ValueError("I was expecting to see Content-Length")
    _LOGGER.debug("Received: %s", order)
    try:
        bytes_size = int(order.split(b":")[1].strip())
    except Exception as err:
        raise ValueError(f"Was unable to read length from {order!r}") from err
    # Double new line, so read another emptyline and ignore it
    stream.readline()

    # Read the right number of bytes
    _LOGGER.debug("Trying to read the message")
    message = stream.read(bytes_size)
    assert isinstance(message, bytes)
    message_str = message.decode("utf-8")
    _LOGGER.debug("Received a %d bytes message", len(message_str))
    # _LOGGER.debug("Read %s", message)

    return message_str