def test_dont_expand_full_ipv6_as_shorthand(self): args = parser.parse_args( args=['0000:0000:0000:0000:0000:0000:0000:0001'], env=TestEnvironment()) self.assertEqual(args.url, 'http://0000:0000:0000:0000:0000:0000:0000:0001')
def console_response(response): """Use httpie to coloize output to console""" args = [response.request.url] env = Environment() debug = False traceback = False args = parser.parse_args(args=args, env=env) write_kwargs = { 'stream': build_output_stream(args, env, response.request, response), 'outfile': env.stdout, 'flush': env.stdout_isatty } try: if env.is_windows and is_py3: 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. log.error('\n') else: raise
def console_response(response): """Use httpie to coloize output to console""" args=[response.request.url] env=Environment() debug = False traceback = False args = parser.parse_args(args=args, env=env) write_kwargs = { 'stream': build_output_stream(args, env, response.request, response), 'outfile': env.stdout, 'flush': env.stdout_isatty } try: if env.is_windows and is_py3: 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. log.error('\n') else: raise
def test_dont_expand_full_ipv6_as_shorthand(self): args = parser.parse_args( args=['0000:0000:0000:0000:0000:0000:0000:0001'], env=TestEnvironment() ) self.assertEqual( args.url, 'http://0000:0000:0000:0000:0000:0000:0000:0001' )
def make_app(): """Make a WSGI app that has all the HTTPie pieces baked in.""" env = Environment() args = parser.parse_args(args=['/'], env=env) args.output_options = 'HB' # Output only requests. server = 'HTTPony/{0}'.format(__version__) def application(environ, start_response): # The WSGI server puts content length and type in the environment # even when not provided with the request. Drop them if they are empty. if environ.get('CONTENT_LENGTH') == '': del environ['CONTENT_LENGTH'] if environ.get('CONTENT_TYPE') == '': del environ['CONTENT_TYPE'] wrequest = WerkzeugRequest(environ) data = wrequest.get_data() request = Request( method=wrequest.method, url=wrequest.url, headers=wrequest.headers, data=data, ) prepared = request.prepare() stream = streams.build_output_stream( args, env, prepared, response=None, output_options=args.output_options) streams.write_stream(stream, env.stdout, env.stdout_isatty) # When there is data in the request, give the next one breathing room. if data: print("\n", file=env.stdout) # Make dreams come true. response = Response(headers={'Server': server}) return response(environ, start_response) return application
def make_app(): """Make a WSGI app that has all the HTTPie pieces baked in.""" env = Environment() # STDIN is ignored because HTTPony runs a server that doesn't care. # Additionally, it is needed or else pytest blows up. args = parser.parse_args(args=['/', '--ignore-stdin'], env=env) args.output_options = 'HB' # Output only requests. server = 'HTTPony/{0}'.format(__version__) def application(environ, start_response): # The WSGI server puts content length and type in the environment # even when not provided with the request. Drop them if they are empty. if environ.get('CONTENT_LENGTH') == '': del environ['CONTENT_LENGTH'] if environ.get('CONTENT_TYPE') == '': del environ['CONTENT_TYPE'] wrequest = WerkzeugRequest(environ) data = wrequest.get_data() request = Request( method=wrequest.method, url=wrequest.url, headers=wrequest.headers, data=data, ) prepared = request.prepare() stream = streams.build_output_stream( args, env, prepared, response=None, output_options=args.output_options) streams.write_stream(stream, env.stdout, env.stdout_isatty) # When there is data in the request, give the next one breathing room. if data: print("\n", file=env.stdout) # Make dreams come true. response = Response(headers={'Server': server}) return response(environ, start_response) return application
def make_app(): """Make a WSGI app that has all the HTTPie pieces baked in.""" env = Environment() # STDIN is ignored because HTTPony runs a server that doesn't care. # Additionally, it is needed or else pytest blows up. args = parser.parse_args(args=["/", "--ignore-stdin"], env=env) args.output_options = "HB" # Output only requests. server = "HTTPony/{0}".format(__version__) def application(environ, start_response): # The WSGI server puts content length and type in the environment # even when not provided with the request. Drop them if they are empty. if environ.get("CONTENT_LENGTH") == "": del environ["CONTENT_LENGTH"] if environ.get("CONTENT_TYPE") == "": del environ["CONTENT_TYPE"] wrequest = WerkzeugRequest(environ) data = wrequest.get_data() request = Request( method=wrequest.method, url=wrequest.url, headers=wrequest.headers, data=data, ) prepared = request.prepare() stream = streams.build_output_stream( args, env, prepared, response=None, output_options=args.output_options ) streams.write_stream(stream, env.stdout, env.stdout_isatty) # When there is data in the request, give the next one breathing room. if data: print("\n", file=env.stdout) # Make dreams come true. response = Response(headers={"Server": server}) return response(environ, start_response) return application
def main(args=sys.argv[1:], env=Environment()): """Run the main program and write the output to ``env.stdout``. Return exit status code. """ args = decode_args(args, env.stdin_encoding) plugin_manager.load_installed_plugins() from httpie.cli import parser if env.config.default_options: args = env.config.default_options + args def error(msg, *args, **kwargs): msg = msg % args level = kwargs.get('level', 'error') env.stderr.write('\nhttp: %s: %s\n' % (level, msg)) debug = '--debug' in args traceback = debug or '--traceback' in args exit_status = ExitStatus.OK if debug: print_debug_info(env) if args == ['--debug']: return exit_status download = None try: args = parser.parse_args(args=args, env=env) if args.download: args.follow = True # --download implies --follow. download = Download(output_file=args.output_file, progress_file=env.stderr, resume=args.download_resume) download.pre_request(args.headers) response = get_response(args, config_dir=env.config.directory) if args.check_status or download: exit_status = get_exit_status(http_status=response.status_code, follow=args.follow) if not env.stdout_isatty and exit_status != ExitStatus.OK: error('HTTP %s %s', response.raw.status, response.raw.reason, level='warning') 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) 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 traceback and e.errno == errno.EPIPE: # Ignore broken pipes unless --traceback. env.stderr.write('\n') else: raise except KeyboardInterrupt: if traceback: raise env.stderr.write('\n') exit_status = ExitStatus.ERROR except SystemExit as e: if e.code != ExitStatus.OK: if traceback: raise env.stderr.write('\n') exit_status = ExitStatus.ERROR except requests.Timeout: exit_status = ExitStatus.ERROR_TIMEOUT error('Request timed out (%ss).', args.timeout) except Exception as e: # TODO: Better distinction between expected and unexpected errors. # Network errors vs. bugs, etc. if traceback: raise error('%s: %s', type(e).__name__, str(e)) exit_status = ExitStatus.ERROR finally: if download and not download.finished: download.failed() return exit_status
def test_dont_expand_full_ipv6_as_shorthand(self): args = parser.parse_args( args=['0000:0000:0000:0000:0000:0000:0000:0001'], env=MockEnvironment() ) assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
def test_dont_expand_longer_ipv6_as_shorthand(self): args = parser.parse_args( args=['::ffff:c000:0280'], env=MockEnvironment() ) assert args.url == 'http://::ffff:c000:0280'
def test_expand_localhost_shorthand_with_port_and_path(self): args = parser.parse_args(args=[':3000/path'], env=MockEnvironment()) assert args.url == 'http://localhost:3000/path'
def test_expand_localhost_shorthand_with_port_and_path(self): args = parser.parse_args(args=[':3000/path'], env=TestEnvironment()) self.assertEqual(args.url, 'http://localhost:3000/path')
def main(args=sys.argv[1:], env=Environment(), error=None): """Run the main program and write the output to ``env.stdout``. Return exit status code. """ args = decode_args(args, env.stdin_encoding) plugin_manager.load_installed_plugins() from httpie.cli import parser if env.config.default_options: args = env.config.default_options + args def _error(msg, *args, **kwargs): msg = msg % args level = kwargs.get('level', 'error') env.stderr.write('\nhttp: %s: %s\n' % (level, msg)) if error is None: error = _error debug = '--debug' in args traceback = debug or '--traceback' in args exit_status = ExitStatus.OK if debug: print_debug_info(env) if args == ['--debug']: return exit_status downloader = None try: args = parser.parse_args(args=args, env=env) if args.download: 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) last_response = get_response(args, config_dir=env.config.directory) if args.show_redirects: responses = last_response.history + [last_response] else: responses = [last_response] for response in responses: if args.check_status or downloader: exit_status = get_exit_status( http_status=response.status_code, follow=args.follow ) if not env.stdout_isatty and exit_status != ExitStatus.OK: error('HTTP %s %s', response.raw.status, response.raw.reason, level='warning') write_stream_kwargs = { 'stream': build_output_stream( args=args, env=env, request=response.request, response=response, ), # NOTE: `env.stdout` 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_stream_with_colors_win_py3(**write_stream_kwargs) else: write_stream(**write_stream_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 downloader and exit_status == ExitStatus.OK: # Last response body download. download_stream, download_to = downloader.start(last_response) write_stream( stream=download_stream, outfile=download_to, flush=False, ) downloader.finish() if downloader.interrupted: exit_status = ExitStatus.ERROR error('Incomplete download: size=%d; downloaded=%d' % ( downloader.status.total_size, downloader.status.downloaded )) except KeyboardInterrupt: if traceback: raise env.stderr.write('\n') exit_status = ExitStatus.ERROR except SystemExit as e: if e.code != ExitStatus.OK: if traceback: raise env.stderr.write('\n') exit_status = ExitStatus.ERROR except requests.Timeout: exit_status = ExitStatus.ERROR_TIMEOUT error('Request timed out (%ss).', args.timeout) except requests.TooManyRedirects: exit_status = ExitStatus.ERROR_TOO_MANY_REDIRECTS error('Too many redirects (--max-redirects=%s).', args.max_redirects) except Exception as e: # TODO: Better distinction between expected and unexpected errors. if traceback: raise msg = str(e) if hasattr(e, 'request'): request = e.request if hasattr(request, 'url'): msg += ' while doing %s request to URL: %s' % ( request.method, request.url) error('%s: %s', type(e).__name__, msg) exit_status = ExitStatus.ERROR finally: if downloader and not downloader.finished: downloader.failed() return exit_status
def test_expand_localhost_shorthand(self): args = parser.parse_args(args=[':'], env=_TestEnvironment()) assert args.url == 'http://localhost'
def test_expand_localhost_shorthand_with_path(self): args = parser.parse_args(args=[':/path'], env=TestEnvironment()) self.assertEqual(args.url, 'http://localhost/path')
def test_dont_expand_full_ipv6_as_shorthand(self): args = parser.parse_args(args=["0000:0000:0000:0000:0000:0000:0000:0001"], env=TestEnvironment()) assert args.url == "http://0000:0000:0000:0000:0000:0000:0000:0001"
def test_expand_localhost_shorthand_with_port_and_slash(self): args = parser.parse_args(args=[":3000/"], env=TestEnvironment()) assert args.url == "http://localhost:3000/"
def test_expand_localhost_shorthand(self): args = parser.parse_args(args=[":"], env=TestEnvironment()) assert args.url == "http://localhost"
def test_expand_localhost_shorthand_with_port_and_slash(self): args = parser.parse_args(args=[':3000/'], env=TestEnvironment()) self.assertEqual(args.url, 'http://localhost:3000/')
def test_dont_expand_longer_ipv6_as_shorthand(self): args = parser.parse_args( args=['::ffff:c000:0280'], env=TestEnvironment() ) self.assertEqual(args.url, 'http://::ffff:c000:0280')
def main(args=sys.argv[1:], env=Environment()): """Run the main program and write the output to ``env.stdout``. Return exit status code. """ args = decode_args(args, env.stdin_encoding) plugin_manager.load_installed_plugins() from httpie.cli import parser if env.config.default_options: args = env.config.default_options + args def error(msg, *args, **kwargs): msg = msg % args level = kwargs.get('level', 'error') env.stderr.write('\nhttp: %s: %s\n' % (level, msg)) debug = '--debug' in args traceback = debug or '--traceback' in args exit_status = ExitStatus.OK if debug: print_debug_info(env) if args == ['--debug']: return exit_status download = None try: args = parser.parse_args(args=args, env=env) if args.download: args.follow = True # --download implies --follow. download = Download( output_file=args.output_file, progress_file=env.stderr, resume=args.download_resume ) download.pre_request(args.headers) response = get_response(args, config_dir=env.config.directory) if args.check_status or download: exit_status = get_exit_status( http_status=response.status_code, follow=args.follow ) if not env.stdout_isatty and exit_status != ExitStatus.OK: error('HTTP %s %s', response.raw.status, response.raw.reason, level='warning') 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) 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 traceback and e.errno == errno.EPIPE: # Ignore broken pipes unless --traceback. env.stderr.write('\n') else: raise except (KeyboardInterrupt, SystemExit): if traceback: raise env.stderr.write('\n') exit_status = ExitStatus.ERROR except requests.Timeout: exit_status = ExitStatus.ERROR_TIMEOUT error('Request timed out (%ss).', args.timeout) except Exception as e: # TODO: Better distinction between expected and unexpected errors. # Network errors vs. bugs, etc. if traceback: raise error('%s: %s', type(e).__name__, str(e)) exit_status = ExitStatus.ERROR finally: if download and not download.finished: download.failed() return exit_status
def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): """ The main function. Pre-process args, handle some special types of invocations, and run the main program with error handling. Return exit status code. """ args = decode_args(args, env.stdin_encoding) plugin_manager.load_installed_plugins() def log_error(msg, *args, **kwargs): msg = msg % args level = kwargs.get('level', 'error') assert level in ['error', 'warning'] env.stderr.write('\nhttp: %s: %s\n' % (level, msg)) from httpie.cli import parser if env.config.default_options: args = env.config.default_options + args if custom_log_error: log_error = custom_log_error include_debug_info = '--debug' in args include_traceback = include_debug_info or '--traceback' in args if include_debug_info: print_debug_info(env) if args == ['--debug']: return ExitStatus.OK exit_status = ExitStatus.OK try: parsed_args = parser.parse_args(args=args, env=env) except KeyboardInterrupt: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR except SystemExit as e: if e.code != ExitStatus.OK: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR else: try: exit_status = program( args=parsed_args, env=env, log_error=log_error, ) except KeyboardInterrupt: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR except SystemExit as e: if e.code != ExitStatus.OK: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR except requests.Timeout: exit_status = ExitStatus.ERROR_TIMEOUT log_error('Request timed out (%ss).', parsed_args.timeout) except requests.TooManyRedirects: exit_status = ExitStatus.ERROR_TOO_MANY_REDIRECTS log_error('Too many redirects (--max-redirects=%s).', parsed_args.max_redirects) except Exception as e: # TODO: Further distinction between expected and unexpected errors. msg = str(e) if hasattr(e, 'request'): request = e.request if hasattr(request, 'url'): msg += ' while doing %s request to URL: %s' % ( request.method, request.url) log_error('%s: %s', type(e).__name__, msg) if include_traceback: raise exit_status = ExitStatus.ERROR return exit_status
def test_expand_localhost_shorthand_with_slash(self): args = parser.parse_args(args=[':/'], env=TestEnvironment()) assert args.url == 'http://localhost/'
def test_dont_expand_shorthand_ipv6_as_shorthand(self): args = parser.parse_args(args=['::1'], env=TestEnvironment()) assert args.url == 'http://::1'
def test_expand_localhost_shorthand_with_slash(self): args = parser.parse_args(args=[':/'], env=TestEnvironment()) self.assertEqual(args.url, 'http://localhost/')
def test_expand_localhost_shorthand_with_path(self): args = parser.parse_args(args=[':/path'], env=MockEnvironment()) assert args.url == 'http://localhost/path'
def main(args=sys.argv[1:], env=Environment(), custom_log_error=None): """ The main function. Pre-process args, handle some special types of invocations, and run the main program with error handling. Return exit status code. """ args = decode_args(args, env.stdin_encoding) plugin_manager.load_installed_plugins() def log_error(msg, *args, **kwargs): msg = msg % args level = kwargs.get('level', 'error') assert level in ['error', 'warning'] env.stderr.write('\nhttp: %s: %s\n' % (level, msg)) from httpie.cli import parser if env.config.default_options: args = env.config.default_options + args if custom_log_error: log_error = custom_log_error include_debug_info = '--debug' in args include_traceback = include_debug_info or '--traceback' in args if include_debug_info: print_debug_info(env) if args == ['--debug']: return ExitStatus.OK exit_status = ExitStatus.OK try: parsed_args = parser.parse_args(args=args, env=env) except KeyboardInterrupt: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR_CTRL_C except SystemExit as e: if e.code != ExitStatus.OK: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR else: try: exit_status = program( args=parsed_args, env=env, log_error=log_error, ) except KeyboardInterrupt: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR_CTRL_C except SystemExit as e: if e.code != ExitStatus.OK: env.stderr.write('\n') if include_traceback: raise exit_status = ExitStatus.ERROR except requests.Timeout: exit_status = ExitStatus.ERROR_TIMEOUT log_error('Request timed out (%ss).', parsed_args.timeout) except requests.TooManyRedirects: exit_status = ExitStatus.ERROR_TOO_MANY_REDIRECTS log_error('Too many redirects (--max-redirects=%s).', parsed_args.max_redirects) except Exception as e: # TODO: Further distinction between expected and unexpected errors. msg = str(e) if hasattr(e, 'request'): request = e.request if hasattr(request, 'url'): msg += ' while doing %s request to URL: %s' % ( request.method, request.url) log_error('%s: %s', type(e).__name__, msg) if include_traceback: raise exit_status = ExitStatus.ERROR return exit_status
def test_dont_expand_full_ipv6_as_shorthand(self): args = parser.parse_args( args=['0000:0000:0000:0000:0000:0000:0000:0001'], env=_TestEnvironment() ) assert args.url == 'http://0000:0000:0000:0000:0000:0000:0000:0001'
def test_expand_localhost_shorthand_with_path(self): args = parser.parse_args(args=[':/path'], env=TestEnvironment()) assert args.url == 'http://localhost/path'
def test_expand_localhost_shorthand_with_port_and_path(self): args = parser.parse_args(args=[':3000/path'], env=TestEnvironment()) assert args.url == 'http://localhost:3000/path'
def test_dont_expand_longer_ipv6_as_shorthand(self): args = parser.parse_args(args=['::ffff:c000:0280'], env=TestEnvironment()) assert args.url == 'http://::ffff:c000:0280'
def test_expand_localhost_shorthand_with_slash(self): args = parser.parse_args(args=[':/'], env=MockEnvironment()) assert args.url == 'http://localhost/'