Repository: httpie/httpie
File: httpie/uploads.py (Python)

Code before:
        return False
    else:
        return file_no == sys.stdin.fileno()


READ_THRESHOLD = float(os.getenv("HTTPIE_STDIN_READ_THRESHOLD", 10.0))


def trigger_stdin_warning_thread(env: Environment, file: IO) -> None:
    # Windows unfortunately does not support select() operation
    # on regular files, like stdin in our use case.
    # https://docs.python.org/3/library/select.html#select.select
    if os.name == 'nt':
        return None

    import select
    import threading

    def worker():
        can_read, _, _ = select.select([file], [], [], READ_THRESHOLD)
        if not can_read:
            env.stderr.write(
                f"> no stdin data read in {READ_THRESHOLD}s "
                f"(perhaps you want to --ignore-stdin)\n"
            )
            env.stderr.write("> https://httpie.io/docs/cli/best-practices\n")

    thread = threading.Thread(
        target=worker
    )
    thread.start()


def _prepare_file_for_upload(
    env: Environment,
    file: Union[IO, 'MultipartEncoder'],
    callback: CallbackT,
    chunked: bool = False,
    content_length_header_value: Optional[int] = None,
) -> Union[bytes, IO, ChunkedStream]:
    if not super_len(file):
        if is_stdin(file):
            trigger_stdin_warning_thread(env, file)
        # Zero-length -> assume stdin.
        if content_length_header_value is None and not chunked:
            # Read the whole stdin to determine `Content-Length`.
            #
            # TODO: Instead of opt-in --chunked, consider making
            #   `Transfer-Encoding: chunked` for STDIN opt-out via
            #   something like --no-chunked.
            #   This would be backwards-incompatible so wait until v3.0.0.

Review comment (suggestion):
We should probably use the existing `env.log_error(msg, level='warning')`
https://github.com/httpie/httpie/blob/245cede2c274bef7b172fe65814bfabc6f87b0b4/httpie/context.py#L137-L139

Code after:
    except Exception:
        return False
    else:
        return file_no == sys.stdin.fileno()


READ_THRESHOLD = float(os.getenv("HTTPIE_STDIN_READ_WARN_THRESHOLD", 10.0))


def observe_stdin_for_data_thread(env: Environment, file: IO) -> None:
    # Windows unfortunately does not support select() operation
    # on regular files, like stdin in our use case.
    # https://docs.python.org/3/library/select.html#select.select
    if is_windows:
        return None

    # If the user configures READ_THRESHOLD to be 0, then
    # disable this warning.
    if READ_THRESHOLD == 0:
        return None

    import select
    import threading

    def worker():
        can_read, _, _ = select.select([file], [], [], READ_THRESHOLD)
        if not can_read:
            env.stderr.write(
                f'> warning: no stdin data read in {READ_THRESHOLD}s '
                f'(perhaps you want to --ignore-stdin)\n'
                f'> See: https://httpie.io/docs/cli/best-practices\n'
            )

    thread = threading.Thread(
        target=worker
    )
    thread.start()


def _prepare_file_for_upload(
    env: Environment,
    file: Union[IO, 'MultipartEncoder'],
    callback: CallbackT,
    chunked: bool = False,
    content_length_header_value: Optional[int] = None,
) -> Union[bytes, IO, ChunkedStream]:
    if not super_len(file):
        if is_stdin(file):
            observe_stdin_for_data_thread(env, file)
        # Zero-length -> assume stdin.
        if content_length_header_value is None and not chunked: