Add PoC of --send-term-headers #805

This commit is contained in:
Jakub Roztocil 2021-04-07 15:21:59 +02:00
parent fc45bf0fe3
commit 46fa79eff8
4 changed files with 41 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,
}

View File

@ -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