diff options
author | Jakub Roztocil <jakub@roztocil.co> | 2021-04-07 15:21:59 +0200 |
---|---|---|
committer | Jakub Roztocil <jakub@roztocil.co> | 2021-04-07 15:21:59 +0200 |
commit | 46fa79eff88e8a51bfa1d16ed9230ba3ecb390be (patch) | |
tree | c01d729c4faeca80979dbaf35ba0d1d0fe976536 | |
parent | fc45bf0fe32711cbbdda2eb5f71ef7cd25b08312 (diff) |
Add PoC of `--send-term-headers` #805feature/term-headers
-rw-r--r-- | httpie/cli/definition.py | 10 | ||||
-rw-r--r-- | httpie/client.py | 9 | ||||
-rw-r--r-- | httpie/context.py | 19 | ||||
-rw-r--r-- | httpie/core.py | 10 |
4 files changed, 41 insertions, 7 deletions
diff --git a/httpie/cli/definition.py b/httpie/cli/definition.py index 4e04a4bc..6746b362 100644 --- a/httpie/cli/definition.py +++ b/httpie/cli/definition.py @@ -671,6 +671,16 @@ network.add_argument( """ ) +network.add_argument( + # <https://github.com/httpie/httpie/issues/805> + '--send-term-headers', + '-T', + default=False, + action='store_true', + help=""" + + """ +) ####################################################################### # SSL diff --git a/httpie/client.py b/httpie/client.py index d6629dfa..026310db 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -34,9 +34,10 @@ def collect_messages( args: argparse.Namespace, config_dir: Path, request_body_read_callback: Callable[[bytes], None] = None, + extra_headers: dict = None, ) -> Iterable[Union[requests.PreparedRequest, requests.Response]]: httpie_session = None - httpie_session_headers = None + base_headers = {} if args.session or args.session_read_only: httpie_session = get_httpie_session( config_dir=config_dir, @@ -44,11 +45,11 @@ def collect_messages( host=args.headers.get('Host'), url=args.url, ) - httpie_session_headers = httpie_session.headers - + base_headers = httpie_session.headers + base_headers.update(extra_headers) request_kwargs = make_request_kwargs( args=args, - base_headers=httpie_session_headers, + base_headers=base_headers, request_body_read_callback=request_body_read_callback ) send_kwargs = make_send_kwargs(args) diff --git a/httpie/context.py b/httpie/context.py index 86330fb2..278e05a6 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -1,6 +1,7 @@ import sys import os from pathlib import Path +from pprint import pprint from typing import IO, Optional @@ -37,6 +38,13 @@ class Environment: stderr_isatty: bool = stderr.isatty() colors = 256 program_name: str = 'http' + + # <https://github.com/httpie/httpie/issues/805> + env_lang = os.environ.get('LANG', '').split('_')[0] + env_term = os.environ.get('TERM', '') + env_columns = os.environ.get('COLUMNS', '') + env_lines = os.environ.get('LINES', '') + if not is_windows: if curses: try: @@ -126,3 +134,14 @@ class Environment: def log_error(self, msg, level='error'): assert level in ['error', 'warning'] self._orig_stderr.write(f'\n{self.program_name}: {level}: {msg}\n\n') + + +def get_term_info_headers(env:Environment): + # <https://github.com/httpie/httpie/issues/805> + return { + 'Accept-Language': env.env_lang, + 'X-Terminal': env.env_term, + 'X-Terminal-Attached': 'yes' if env.stdin.isatty() else 'no', + 'X-Terminal-Columns': env.env_columns, + 'X-Terminal-Lines': env.env_lines, + } diff --git a/httpie/core.py b/httpie/core.py index c71edd2c..8ce84fd9 100644 --- a/httpie/core.py +++ b/httpie/core.py @@ -11,7 +11,7 @@ from requests import __version__ as requests_version from httpie import __version__ as httpie_version from httpie.cli.constants import OUT_REQ_BODY, OUT_REQ_HEAD, OUT_RESP_BODY, OUT_RESP_HEAD from httpie.client import collect_messages -from httpie.context import Environment +from httpie.context import Environment, get_term_info_headers from httpie.downloads import Downloader from httpie.output.writer import write_message, write_stream, MESSAGE_SEPARATOR_BYTES from httpie.plugins.registry import plugin_manager @@ -160,8 +160,12 @@ def program(args: argparse.Namespace, env: Environment) -> ExitStatus: args.follow = True # --download implies --follow. downloader = Downloader(output_file=args.output_file, progress_file=env.stderr, resume=args.download_resume) downloader.pre_request(args.headers) - messages = collect_messages(args=args, config_dir=env.config.directory, - request_body_read_callback=request_body_read_callback) + messages = collect_messages( + args=args, + config_dir=env.config.directory, + extra_headers=get_term_info_headers(env) if args.send_term_headers else None, + request_body_read_callback=request_body_read_callback, + ) force_separator = False prev_with_body = False |