mirror of
https://github.com/httpie/cli.git
synced 2025-06-20 17:47:48 +02:00
Show redirects WIP
This commit is contained in:
parent
dc1371d4d6
commit
c6d4f6cdf6
101
httpie/core.py
101
httpie/core.py
@ -99,7 +99,6 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
|
|||||||
return exit_status
|
return exit_status
|
||||||
|
|
||||||
download = None
|
download = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
args = parser.parse_args(args=args, env=env)
|
args = parser.parse_args(args=args, env=env)
|
||||||
|
|
||||||
@ -112,60 +111,64 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
|
|||||||
)
|
)
|
||||||
download.pre_request(args.headers)
|
download.pre_request(args.headers)
|
||||||
|
|
||||||
response = get_response(args, config_dir=env.config.directory)
|
last_response = get_response(args, config_dir=env.config.directory)
|
||||||
|
|
||||||
if args.check_status or download:
|
redirect_chain = last_response.history + [last_response]
|
||||||
|
for response in redirect_chain:
|
||||||
|
|
||||||
exit_status = get_exit_status(
|
if exit_status != ExitStatus.OK:
|
||||||
http_status=response.status_code,
|
break
|
||||||
follow=args.follow
|
|
||||||
)
|
|
||||||
|
|
||||||
if not env.stdout_isatty and exit_status != ExitStatus.OK:
|
if args.check_status or download:
|
||||||
error('HTTP %s %s',
|
|
||||||
response.raw.status,
|
|
||||||
response.raw.reason,
|
|
||||||
level='warning')
|
|
||||||
|
|
||||||
write_kwargs = {
|
exit_status = get_exit_status(
|
||||||
'stream': build_output_stream(
|
http_status=response.status_code,
|
||||||
args, env, response.request, response),
|
follow=args.follow
|
||||||
|
|
||||||
# This will in fact be `stderr` with `--download`
|
|
||||||
'outfile': env.stdout,
|
|
||||||
|
|
||||||
'flush': env.stdout_isatty or args.stream
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
|
||||||
|
|
||||||
if env.is_windows and is_py3 and 'colors' in args.prettify:
|
|
||||||
write_with_colors_win_py3(**write_kwargs)
|
|
||||||
else:
|
|
||||||
write(**write_kwargs)
|
|
||||||
|
|
||||||
if download and exit_status == ExitStatus.OK:
|
|
||||||
# Response body download.
|
|
||||||
download_stream, download_to = download.start(response)
|
|
||||||
write(
|
|
||||||
stream=download_stream,
|
|
||||||
outfile=download_to,
|
|
||||||
flush=False,
|
|
||||||
)
|
)
|
||||||
download.finish()
|
|
||||||
if download.interrupted:
|
|
||||||
exit_status = ExitStatus.ERROR
|
|
||||||
error('Incomplete download: size=%d; downloaded=%d' % (
|
|
||||||
download.status.total_size,
|
|
||||||
download.status.downloaded
|
|
||||||
))
|
|
||||||
|
|
||||||
except IOError as e:
|
if not env.stdout_isatty and exit_status != ExitStatus.OK:
|
||||||
if not traceback and e.errno == errno.EPIPE:
|
error('HTTP %s %s',
|
||||||
# Ignore broken pipes unless --traceback.
|
response.raw.status,
|
||||||
env.stderr.write('\n')
|
response.raw.reason,
|
||||||
else:
|
level='warning')
|
||||||
raise
|
|
||||||
|
write_kwargs = {
|
||||||
|
'stream': build_output_stream(args, env,
|
||||||
|
response.request,
|
||||||
|
response),
|
||||||
|
# This will in fact be `stderr` with `--download`
|
||||||
|
'outfile': env.stdout,
|
||||||
|
'flush': env.stdout_isatty or args.stream
|
||||||
|
}
|
||||||
|
|
||||||
|
try:
|
||||||
|
if env.is_windows and is_py3 and 'colors' in args.prettify:
|
||||||
|
write_with_colors_win_py3(**write_kwargs)
|
||||||
|
else:
|
||||||
|
write(**write_kwargs)
|
||||||
|
except IOError as e:
|
||||||
|
if not traceback and e.errno == errno.EPIPE:
|
||||||
|
# Ignore broken pipes unless --traceback.
|
||||||
|
env.stderr.write('\n')
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
if download and exit_status == ExitStatus.OK:
|
||||||
|
# Last response body download.
|
||||||
|
download_stream, download_to = download.start(last_response)
|
||||||
|
write(
|
||||||
|
stream=download_stream,
|
||||||
|
outfile=download_to,
|
||||||
|
flush=False,
|
||||||
|
)
|
||||||
|
download.finish()
|
||||||
|
if download.interrupted:
|
||||||
|
exit_status = ExitStatus.ERROR
|
||||||
|
error('Incomplete download: size=%d; downloaded=%d' % (
|
||||||
|
download.status.total_size,
|
||||||
|
download.status.downloaded
|
||||||
|
))
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
if traceback:
|
if traceback:
|
||||||
raise
|
raise
|
||||||
|
@ -22,7 +22,7 @@ SESSION_IGNORED_HEADER_PREFIXES = ['Content-', 'If-']
|
|||||||
|
|
||||||
def get_response(requests_session, session_name,
|
def get_response(requests_session, session_name,
|
||||||
config_dir, args, read_only=False):
|
config_dir, args, read_only=False):
|
||||||
"""Like `client.get_response`, but applies permanent
|
"""Like `client.get_responses`, but applies permanent
|
||||||
aspects of the session to the request.
|
aspects of the session to the request.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -30,8 +30,8 @@ def get_response(requests_session, session_name,
|
|||||||
if os.path.sep in session_name:
|
if os.path.sep in session_name:
|
||||||
path = os.path.expanduser(session_name)
|
path = os.path.expanduser(session_name)
|
||||||
else:
|
else:
|
||||||
hostname = (args.headers.get('Host', None)
|
hostname = (args.headers.get('Host', None) or
|
||||||
or urlsplit(args.url).netloc.split('@')[-1])
|
urlsplit(args.url).netloc.split('@')[-1])
|
||||||
if not hostname:
|
if not hostname:
|
||||||
# HACK/FIXME: httpie-unixsocket's URLs have no hostname.
|
# HACK/FIXME: httpie-unixsocket's URLs have no hostname.
|
||||||
hostname = 'localhost'
|
hostname = 'localhost'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user