Show redirects WIP

This commit is contained in:
Jakub Roztocil 2016-02-29 12:56:40 +08:00
parent dc1371d4d6
commit c6d4f6cdf6
2 changed files with 55 additions and 52 deletions

View File

@ -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,7 +111,13 @@ 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)
redirect_chain = last_response.history + [last_response]
for response in redirect_chain:
if exit_status != ExitStatus.OK:
break
if args.check_status or download: if args.check_status or download:
@ -128,25 +133,29 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
level='warning') level='warning')
write_kwargs = { write_kwargs = {
'stream': build_output_stream( 'stream': build_output_stream(args, env,
args, env, response.request, response), response.request,
response),
# This will in fact be `stderr` with `--download` # This will in fact be `stderr` with `--download`
'outfile': env.stdout, 'outfile': env.stdout,
'flush': env.stdout_isatty or args.stream 'flush': env.stdout_isatty or args.stream
} }
try: try:
if env.is_windows and is_py3 and 'colors' in args.prettify: if env.is_windows and is_py3 and 'colors' in args.prettify:
write_with_colors_win_py3(**write_kwargs) write_with_colors_win_py3(**write_kwargs)
else: else:
write(**write_kwargs) 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: if download and exit_status == ExitStatus.OK:
# Response body download. # Last response body download.
download_stream, download_to = download.start(response) download_stream, download_to = download.start(last_response)
write( write(
stream=download_stream, stream=download_stream,
outfile=download_to, outfile=download_to,
@ -160,12 +169,6 @@ def main(args=sys.argv[1:], env=Environment(), error=None):
download.status.downloaded download.status.downloaded
)) ))
except IOError as e:
if not traceback and e.errno == errno.EPIPE:
# Ignore broken pipes unless --traceback.
env.stderr.write('\n')
else:
raise
except KeyboardInterrupt: except KeyboardInterrupt:
if traceback: if traceback:
raise raise

View File

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