rubygems-update 3.4.22 → 3.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +99 -2
- data/Manifest.txt +34 -221
- data/README.md +1 -3
- data/bundler/CHANGELOG.md +53 -0
- data/bundler/bundler.gemspec +4 -2
- data/bundler/exe/bundle +1 -10
- data/bundler/lib/bundler/build_metadata.rb +3 -3
- data/bundler/lib/bundler/capistrano.rb +1 -1
- data/bundler/lib/bundler/checksum.rb +245 -0
- data/bundler/lib/bundler/ci_detector.rb +75 -0
- data/bundler/lib/bundler/cli/add.rb +3 -3
- data/bundler/lib/bundler/cli/binstubs.rb +4 -4
- data/bundler/lib/bundler/cli/cache.rb +1 -1
- data/bundler/lib/bundler/cli/check.rb +1 -1
- data/bundler/lib/bundler/cli/common.rb +9 -1
- data/bundler/lib/bundler/cli/config.rb +8 -7
- data/bundler/lib/bundler/cli/console.rb +3 -2
- data/bundler/lib/bundler/cli/doctor.rb +2 -2
- data/bundler/lib/bundler/cli/exec.rb +1 -1
- data/bundler/lib/bundler/cli/gem.rb +28 -23
- data/bundler/lib/bundler/cli/info.rb +2 -13
- data/bundler/lib/bundler/cli/install.rb +5 -4
- data/bundler/lib/bundler/cli/issue.rb +1 -1
- data/bundler/lib/bundler/cli/lock.rb +4 -4
- data/bundler/lib/bundler/cli/open.rb +1 -1
- data/bundler/lib/bundler/cli/outdated.rb +6 -6
- data/bundler/lib/bundler/cli/plugin.rb +7 -14
- data/bundler/lib/bundler/cli/pristine.rb +38 -30
- data/bundler/lib/bundler/cli/show.rb +2 -2
- data/bundler/lib/bundler/cli/update.rb +5 -5
- data/bundler/lib/bundler/cli.rb +215 -263
- data/bundler/lib/bundler/compact_index_client/cache.rb +29 -9
- data/bundler/lib/bundler/compact_index_client/cache_file.rb +153 -0
- data/bundler/lib/bundler/compact_index_client/gem_parser.rb +7 -3
- data/bundler/lib/bundler/compact_index_client/updater.rb +79 -81
- data/bundler/lib/bundler/compact_index_client.rb +14 -7
- data/bundler/lib/bundler/constants.rb +1 -1
- data/bundler/lib/bundler/current_ruby.rb +5 -21
- data/bundler/lib/bundler/definition.rb +42 -15
- data/bundler/lib/bundler/dependency.rb +16 -12
- data/bundler/lib/bundler/digest.rb +2 -2
- data/bundler/lib/bundler/dsl.rb +43 -25
- data/bundler/lib/bundler/endpoint_specification.rb +5 -1
- data/bundler/lib/bundler/env.rb +1 -3
- data/bundler/lib/bundler/errors.rb +43 -0
- data/bundler/lib/bundler/fetcher/base.rb +3 -1
- data/bundler/lib/bundler/fetcher/compact_index.rb +4 -4
- data/bundler/lib/bundler/fetcher/downloader.rb +13 -11
- data/bundler/lib/bundler/fetcher/gem_remote_fetcher.rb +16 -0
- data/bundler/lib/bundler/fetcher/index.rb +1 -1
- data/bundler/lib/bundler/fetcher.rb +28 -25
- data/bundler/lib/bundler/friendly_errors.rb +5 -5
- data/bundler/lib/bundler/gem_helper.rb +1 -1
- data/bundler/lib/bundler/gem_helpers.rb +5 -2
- data/bundler/lib/bundler/graph.rb +9 -9
- data/bundler/lib/bundler/index.rb +1 -2
- data/bundler/lib/bundler/injector.rb +1 -1
- data/bundler/lib/bundler/inline.rb +3 -3
- data/bundler/lib/bundler/installer/gem_installer.rb +5 -5
- data/bundler/lib/bundler/installer/parallel_installer.rb +16 -8
- data/bundler/lib/bundler/installer/standalone.rb +2 -3
- data/bundler/lib/bundler/installer.rb +9 -9
- data/bundler/lib/bundler/lazy_specification.rb +24 -17
- data/bundler/lib/bundler/lockfile_generator.rb +9 -0
- data/bundler/lib/bundler/lockfile_parser.rb +81 -10
- data/bundler/lib/bundler/man/bundle-add.1 +3 -26
- data/bundler/lib/bundler/man/bundle-binstubs.1 +4 -16
- data/bundler/lib/bundler/man/bundle-cache.1 +3 -24
- data/bundler/lib/bundler/man/bundle-check.1 +3 -12
- data/bundler/lib/bundler/man/bundle-clean.1 +3 -10
- data/bundler/lib/bundler/man/bundle-config.1 +20 -211
- data/bundler/lib/bundler/man/bundle-config.1.ronn +6 -0
- data/bundler/lib/bundler/man/bundle-console.1 +4 -22
- data/bundler/lib/bundler/man/bundle-doctor.1 +4 -18
- data/bundler/lib/bundler/man/bundle-exec.1 +12 -73
- data/bundler/lib/bundler/man/bundle-gem.1 +13 -49
- data/bundler/lib/bundler/man/bundle-help.1 +3 -7
- data/bundler/lib/bundler/man/bundle-info.1 +3 -9
- data/bundler/lib/bundler/man/bundle-init.1 +3 -12
- data/bundler/lib/bundler/man/bundle-inject.1 +6 -19
- data/bundler/lib/bundler/man/bundle-install.1 +27 -125
- data/bundler/lib/bundler/man/bundle-install.1.ronn +1 -0
- data/bundler/lib/bundler/man/bundle-list.1 +4 -19
- data/bundler/lib/bundler/man/bundle-lock.1 +5 -29
- data/bundler/lib/bundler/man/bundle-open.1 +7 -27
- data/bundler/lib/bundler/man/bundle-outdated.1 +3 -55
- data/bundler/lib/bundler/man/bundle-outdated.1.ronn +1 -0
- data/bundler/lib/bundler/man/bundle-platform.1 +5 -27
- data/bundler/lib/bundler/man/bundle-plugin.1 +3 -29
- data/bundler/lib/bundler/man/bundle-pristine.1 +5 -16
- data/bundler/lib/bundler/man/bundle-remove.1 +4 -14
- data/bundler/lib/bundler/man/bundle-show.1 +3 -10
- data/bundler/lib/bundler/man/bundle-update.1 +18 -137
- data/bundler/lib/bundler/man/bundle-version.1 +3 -16
- data/bundler/lib/bundler/man/bundle-viz.1 +4 -16
- data/bundler/lib/bundler/man/bundle.1 +5 -44
- data/bundler/lib/bundler/man/gemfile.5 +24 -301
- data/bundler/lib/bundler/man/gemfile.5.ronn +4 -0
- data/bundler/lib/bundler/match_metadata.rb +4 -0
- data/bundler/lib/bundler/match_platform.rb +1 -1
- data/bundler/lib/bundler/plugin/api/source.rb +3 -2
- data/bundler/lib/bundler/plugin/installer.rb +1 -1
- data/bundler/lib/bundler/plugin.rb +3 -3
- data/bundler/lib/bundler/resolver/base.rb +1 -1
- data/bundler/lib/bundler/resolver/incompatibility.rb +1 -1
- data/bundler/lib/bundler/resolver/spec_group.rb +1 -4
- data/bundler/lib/bundler/resolver.rb +16 -16
- data/bundler/lib/bundler/ruby_dsl.rb +20 -12
- data/bundler/lib/bundler/ruby_version.rb +1 -1
- data/bundler/lib/bundler/rubygems_ext.rb +24 -50
- data/bundler/lib/bundler/rubygems_gem_installer.rb +6 -56
- data/bundler/lib/bundler/rubygems_integration.rb +25 -94
- data/bundler/lib/bundler/runtime.rb +2 -2
- data/bundler/lib/bundler/self_manager.rb +23 -7
- data/bundler/lib/bundler/settings.rb +27 -7
- data/bundler/lib/bundler/setup.rb +4 -1
- data/bundler/lib/bundler/shared_helpers.rb +35 -13
- data/bundler/lib/bundler/source/git/git_proxy.rb +15 -15
- data/bundler/lib/bundler/source/git.rb +4 -3
- data/bundler/lib/bundler/source/metadata.rb +15 -15
- data/bundler/lib/bundler/source/path.rb +7 -6
- data/bundler/lib/bundler/source/rubygems.rb +21 -14
- data/bundler/lib/bundler/source.rb +2 -0
- data/bundler/lib/bundler/spec_set.rb +38 -10
- data/bundler/lib/bundler/stub_specification.rb +1 -0
- data/bundler/lib/bundler/templates/Executable.bundler +1 -1
- data/bundler/lib/bundler/templates/newgem/README.md.tt +3 -3
- data/bundler/lib/bundler/templates/newgem/Rakefile.tt +2 -6
- data/bundler/lib/bundler/templates/newgem/ext/newgem/Cargo.toml.tt +1 -1
- data/bundler/lib/bundler/templates/newgem/standard.yml.tt +1 -1
- data/bundler/lib/bundler/ui/shell.rb +1 -1
- data/bundler/lib/bundler/vendor/connection_pool/lib/connection_pool/version.rb +1 -1
- data/bundler/lib/bundler/vendor/connection_pool/lib/connection_pool.rb +53 -6
- data/bundler/lib/bundler/vendor/fileutils/lib/fileutils.rb +8 -20
- data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/connection.rb +3 -3
- data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/pool.rb +2 -2
- data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent/timed_stack_multi.rb +1 -1
- data/bundler/lib/bundler/vendor/net-http-persistent/lib/net/http/persistent.rb +35 -35
- data/bundler/lib/bundler/vendor/tsort/lib/tsort.rb +3 -0
- data/bundler/lib/bundler/vendor/uri/lib/uri/common.rb +256 -132
- data/bundler/lib/bundler/vendor/uri/lib/uri/generic.rb +1 -0
- data/bundler/lib/bundler/vendor/uri/lib/uri/rfc3986_parser.rb +95 -31
- data/bundler/lib/bundler/vendor/uri/lib/uri/version.rb +1 -1
- data/bundler/lib/bundler/vendored_net_http.rb +8 -0
- data/bundler/lib/bundler/vendored_persistent.rb +0 -4
- data/bundler/lib/bundler/vendored_timeout.rb +8 -0
- data/bundler/lib/bundler/version.rb +1 -1
- data/bundler/lib/bundler/vlad.rb +1 -1
- data/bundler/lib/bundler/yaml_serializer.rb +3 -3
- data/bundler/lib/bundler.rb +38 -27
- data/lib/rubygems/available_set.rb +4 -4
- data/lib/rubygems/basic_specification.rb +35 -37
- data/lib/rubygems/bundler_version_finder.rb +4 -4
- data/lib/rubygems/ci_detector.rb +75 -0
- data/lib/rubygems/command.rb +13 -15
- data/lib/rubygems/command_manager.rb +5 -4
- data/lib/rubygems/commands/build_command.rb +2 -2
- data/lib/rubygems/commands/cert_command.rb +1 -2
- data/lib/rubygems/commands/check_command.rb +4 -4
- data/lib/rubygems/commands/cleanup_command.rb +12 -14
- data/lib/rubygems/commands/contents_command.rb +4 -4
- data/lib/rubygems/commands/dependency_command.rb +4 -5
- data/lib/rubygems/commands/environment_command.rb +1 -3
- data/lib/rubygems/commands/exec_command.rb +1 -1
- data/lib/rubygems/commands/fetch_command.rb +2 -2
- data/lib/rubygems/commands/generate_index_command.rb +39 -74
- data/lib/rubygems/commands/help_command.rb +3 -3
- data/lib/rubygems/commands/info_command.rb +2 -2
- data/lib/rubygems/commands/install_command.rb +8 -16
- data/lib/rubygems/commands/list_command.rb +2 -2
- data/lib/rubygems/commands/lock_command.rb +1 -1
- data/lib/rubygems/commands/open_command.rb +1 -1
- data/lib/rubygems/commands/owner_command.rb +1 -1
- data/lib/rubygems/commands/pristine_command.rb +13 -15
- data/lib/rubygems/commands/push_command.rb +2 -2
- data/lib/rubygems/commands/query_command.rb +4 -5
- data/lib/rubygems/commands/rdoc_command.rb +2 -2
- data/lib/rubygems/commands/search_command.rb +2 -2
- data/lib/rubygems/commands/setup_command.rb +31 -34
- data/lib/rubygems/commands/sources_command.rb +12 -12
- data/lib/rubygems/commands/specification_command.rb +10 -10
- data/lib/rubygems/commands/stale_command.rb +1 -1
- data/lib/rubygems/commands/uninstall_command.rb +9 -10
- data/lib/rubygems/commands/unpack_command.rb +4 -4
- data/lib/rubygems/commands/update_command.rb +10 -12
- data/lib/rubygems/commands/which_command.rb +1 -1
- data/lib/rubygems/commands/yank_command.rb +1 -1
- data/lib/rubygems/compatibility.rb +5 -6
- data/lib/rubygems/config_file.rb +4 -4
- data/lib/rubygems/core_ext/kernel_gem.rb +0 -2
- data/lib/rubygems/core_ext/kernel_require.rb +19 -48
- data/lib/rubygems/core_ext/kernel_warn.rb +1 -1
- data/lib/rubygems/core_ext/tcpsocket_init.rb +1 -1
- data/lib/rubygems/defaults.rb +15 -3
- data/lib/rubygems/dependency.rb +12 -14
- data/lib/rubygems/dependency_installer.rb +29 -30
- data/lib/rubygems/dependency_list.rb +1 -1
- data/lib/rubygems/deprecate.rb +16 -15
- data/lib/rubygems/doctor.rb +5 -5
- data/lib/rubygems/errors.rb +2 -6
- data/lib/rubygems/exceptions.rb +2 -1
- data/lib/rubygems/ext/builder.rb +15 -10
- data/lib/rubygems/ext/cargo_builder.rb +5 -5
- data/lib/rubygems/ext/ext_conf_builder.rb +1 -3
- data/lib/rubygems/gem_runner.rb +4 -4
- data/lib/rubygems/gemcutter_utilities/webauthn_listener/response.rb +3 -3
- data/lib/rubygems/gemcutter_utilities/webauthn_poller.rb +3 -3
- data/lib/rubygems/gemcutter_utilities.rb +18 -19
- data/lib/rubygems/install_update_options.rb +18 -19
- data/lib/rubygems/installer.rb +59 -33
- data/lib/rubygems/installer_uninstaller_utils.rb +0 -2
- data/lib/rubygems/local_remote_options.rb +7 -10
- data/lib/rubygems/name_tuple.rb +7 -9
- data/lib/rubygems/net/http.rb +3 -0
- data/lib/rubygems/net-http/LICENSE.txt +22 -0
- data/lib/rubygems/net-http/lib/net/http/backward.rb +40 -0
- data/lib/rubygems/net-http/lib/net/http/exceptions.rb +34 -0
- data/lib/rubygems/net-http/lib/net/http/generic_request.rb +414 -0
- data/lib/rubygems/net-http/lib/net/http/header.rb +981 -0
- data/lib/rubygems/net-http/lib/net/http/proxy_delta.rb +17 -0
- data/lib/rubygems/net-http/lib/net/http/request.rb +88 -0
- data/lib/rubygems/net-http/lib/net/http/requests.rb +425 -0
- data/lib/rubygems/net-http/lib/net/http/response.rb +738 -0
- data/lib/rubygems/net-http/lib/net/http/responses.rb +1174 -0
- data/lib/rubygems/net-http/lib/net/http/status.rb +84 -0
- data/lib/rubygems/net-http/lib/net/http.rb +2496 -0
- data/lib/rubygems/net-http/lib/net/https.rb +23 -0
- data/lib/rubygems/net-protocol/LICENSE.txt +22 -0
- data/lib/rubygems/net-protocol/lib/net/protocol.rb +544 -0
- data/lib/rubygems/optparse/lib/optparse.rb +39 -17
- data/lib/rubygems/package/old.rb +2 -2
- data/lib/rubygems/package/tar_header.rb +45 -39
- data/lib/rubygems/package/tar_reader/entry.rb +5 -4
- data/lib/rubygems/package/tar_reader.rb +5 -3
- data/lib/rubygems/package/tar_writer.rb +19 -17
- data/lib/rubygems/package.rb +27 -26
- data/lib/rubygems/package_task.rb +2 -2
- data/lib/rubygems/path_support.rb +9 -10
- data/lib/rubygems/platform.rb +60 -45
- data/lib/rubygems/query_utils.rb +7 -9
- data/lib/rubygems/remote_fetcher.rb +15 -15
- data/lib/rubygems/request/connection_pools.rb +3 -3
- data/lib/rubygems/request.rb +20 -17
- data/lib/rubygems/request_set/gem_dependency_api.rb +119 -122
- data/lib/rubygems/request_set/lockfile/parser.rb +9 -9
- data/lib/rubygems/request_set/lockfile/tokenizer.rb +20 -12
- data/lib/rubygems/request_set/lockfile.rb +6 -11
- data/lib/rubygems/request_set.rb +5 -5
- data/lib/rubygems/requirement.rb +7 -7
- data/lib/rubygems/resolv/LICENSE.txt +22 -0
- data/lib/rubygems/resolv/lib/resolv.rb +3387 -0
- data/lib/rubygems/resolver/activation_request.rb +1 -3
- data/lib/rubygems/resolver/api_set/gem_parser.rb +7 -3
- data/lib/rubygems/resolver/best_set.rb +1 -1
- data/lib/rubygems/resolver/composed_set.rb +1 -1
- data/lib/rubygems/resolver/conflict.rb +4 -12
- data/lib/rubygems/resolver/index_set.rb +4 -4
- data/lib/rubygems/resolver/index_specification.rb +2 -2
- data/lib/rubygems/resolver/installer_set.rb +5 -6
- data/lib/rubygems/resolver/lock_set.rb +1 -1
- data/lib/rubygems/resolver.rb +3 -10
- data/lib/rubygems/s3_uri_signer.rb +6 -6
- data/lib/rubygems/safe_marshal/elements.rb +138 -0
- data/lib/rubygems/safe_marshal/reader.rb +306 -0
- data/lib/rubygems/safe_marshal/visitors/stream_printer.rb +31 -0
- data/lib/rubygems/safe_marshal/visitors/to_ruby.rb +385 -0
- data/lib/rubygems/safe_marshal/visitors/visitor.rb +74 -0
- data/lib/rubygems/safe_marshal.rb +74 -0
- data/lib/rubygems/safe_yaml.rb +5 -28
- data/lib/rubygems/security/policies.rb +36 -38
- data/lib/rubygems/security/policy.rb +7 -11
- data/lib/rubygems/security/signer.rb +1 -1
- data/lib/rubygems/security/trust_dir.rb +3 -3
- data/lib/rubygems/security.rb +8 -22
- data/lib/rubygems/source/git.rb +1 -3
- data/lib/rubygems/source/installed.rb +0 -2
- data/lib/rubygems/source/local.rb +5 -8
- data/lib/rubygems/source/lock.rb +1 -3
- data/lib/rubygems/source/specific_file.rb +0 -1
- data/lib/rubygems/source/vendor.rb +0 -2
- data/lib/rubygems/source.rb +12 -12
- data/lib/rubygems/source_list.rb +4 -4
- data/lib/rubygems/spec_fetcher.rb +29 -29
- data/lib/rubygems/specification.rb +125 -138
- data/lib/rubygems/specification_policy.rb +55 -25
- data/lib/rubygems/stub_specification.rb +4 -5
- data/lib/rubygems/text.rb +1 -2
- data/lib/rubygems/timeout/LICENSE.txt +22 -0
- data/lib/rubygems/timeout/lib/timeout.rb +199 -0
- data/lib/rubygems/timeout.rb +3 -0
- data/lib/rubygems/tsort/lib/tsort.rb +3 -0
- data/lib/rubygems/uninstaller.rb +7 -9
- data/lib/rubygems/update_suggestion.rb +5 -18
- data/lib/rubygems/uri_formatter.rb +1 -1
- data/lib/rubygems/user_interaction.rb +15 -21
- data/lib/rubygems/util/licenses.rb +65 -35
- data/lib/rubygems/util/list.rb +3 -1
- data/lib/rubygems/util.rb +2 -4
- data/lib/rubygems/validator.rb +5 -3
- data/lib/rubygems/version.rb +34 -28
- data/lib/rubygems/version_option.rb +2 -5
- data/lib/rubygems/yaml_serializer.rb +3 -3
- data/lib/rubygems.rb +37 -37
- data/rubygems-update.gemspec +4 -4
- data/setup.rb +2 -2
- metadata +38 -225
- data/lib/rubygems/indexer.rb +0 -428
- data/lib/rubygems/mock_gem_ui.rb +0 -86
- data/test/rubygems/alternate_cert.pem +0 -19
- data/test/rubygems/alternate_cert_32.pem +0 -19
- data/test/rubygems/alternate_key.pem +0 -27
- data/test/rubygems/bad_rake.rb +0 -3
- data/test/rubygems/bundler_test_gem.rb +0 -424
- data/test/rubygems/ca_cert.pem +0 -77
- data/test/rubygems/child_cert.pem +0 -19
- data/test/rubygems/child_cert_32.pem +0 -19
- data/test/rubygems/child_key.pem +0 -27
- data/test/rubygems/client.pem +0 -107
- data/test/rubygems/data/excon-0.7.7.gemspec.rz +0 -0
- data/test/rubygems/data/gem-private_key.pem +0 -27
- data/test/rubygems/data/gem-public_cert.pem +0 -20
- data/test/rubygems/data/null-required-ruby-version.gemspec.rz +0 -0
- data/test/rubygems/data/null-required-rubygems-version.gemspec.rz +0 -0
- data/test/rubygems/data/pry-0.4.7.gemspec.rz +0 -0
- data/test/rubygems/encrypted_private_key.pem +0 -30
- data/test/rubygems/expired_cert.pem +0 -19
- data/test/rubygems/fake_certlib/openssl.rb +0 -9
- data/test/rubygems/foo/discover.rb +0 -1
- data/test/rubygems/future_cert.pem +0 -19
- data/test/rubygems/future_cert_32.pem +0 -19
- data/test/rubygems/good_rake.rb +0 -3
- data/test/rubygems/grandchild_cert.pem +0 -19
- data/test/rubygems/grandchild_cert_32.pem +0 -19
- data/test/rubygems/grandchild_key.pem +0 -27
- data/test/rubygems/helper.rb +0 -1649
- data/test/rubygems/installer_test_case.rb +0 -248
- data/test/rubygems/invalid_client.pem +0 -49
- data/test/rubygems/invalid_issuer_cert.pem +0 -20
- data/test/rubygems/invalid_issuer_cert_32.pem +0 -20
- data/test/rubygems/invalid_key.pem +0 -27
- data/test/rubygems/invalid_signer_cert.pem +0 -19
- data/test/rubygems/invalid_signer_cert_32.pem +0 -19
- data/test/rubygems/invalidchild_cert.pem +0 -19
- data/test/rubygems/invalidchild_cert_32.pem +0 -19
- data/test/rubygems/invalidchild_key.pem +0 -27
- data/test/rubygems/multifactor_auth_utilities.rb +0 -111
- data/test/rubygems/package/tar_test_case.rb +0 -175
- data/test/rubygems/packages/Bluebie-legs-0.6.2.gem +0 -0
- data/test/rubygems/packages/ascii_binder-0.1.10.1.gem +0 -0
- data/test/rubygems/packages/ill-formatted-platform-1.0.0.10.gem +0 -0
- data/test/rubygems/plugin/exception/rubygems_plugin.rb +0 -4
- data/test/rubygems/plugin/load/rubygems_plugin.rb +0 -5
- data/test/rubygems/plugin/standarderror/rubygems_plugin.rb +0 -4
- data/test/rubygems/private3072_key.pem +0 -40
- data/test/rubygems/private_ec_key.pem +0 -9
- data/test/rubygems/private_key.pem +0 -27
- data/test/rubygems/public3072_cert.pem +0 -25
- data/test/rubygems/public_cert.pem +0 -20
- data/test/rubygems/public_cert_32.pem +0 -19
- data/test/rubygems/public_key.pem +0 -9
- data/test/rubygems/rubygems/commands/crash_command.rb +0 -5
- data/test/rubygems/rubygems_plugin.rb +0 -24
- data/test/rubygems/sff/discover.rb +0 -1
- data/test/rubygems/simple_gem.rb +0 -68
- data/test/rubygems/specifications/bar-0.0.2.gemspec +0 -9
- data/test/rubygems/specifications/foo-0.0.1-x86-mswin32.gemspec +0 -0
- data/test/rubygems/specifications/rubyforge-0.0.1.gemspec +0 -14
- data/test/rubygems/ssl_cert.pem +0 -80
- data/test/rubygems/ssl_key.pem +0 -27
- data/test/rubygems/test_bundled_ca.rb +0 -61
- data/test/rubygems/test_config.rb +0 -28
- data/test/rubygems/test_deprecate.rb +0 -158
- data/test/rubygems/test_exit.rb +0 -17
- data/test/rubygems/test_gem.rb +0 -1799
- data/test/rubygems/test_gem_available_set.rb +0 -130
- data/test/rubygems/test_gem_bundler_version_finder.rb +0 -127
- data/test/rubygems/test_gem_command.rb +0 -403
- data/test/rubygems/test_gem_command_manager.rb +0 -400
- data/test/rubygems/test_gem_commands_build_command.rb +0 -739
- data/test/rubygems/test_gem_commands_cert_command.rb +0 -866
- data/test/rubygems/test_gem_commands_check_command.rb +0 -68
- data/test/rubygems/test_gem_commands_cleanup_command.rb +0 -292
- data/test/rubygems/test_gem_commands_contents_command.rb +0 -271
- data/test/rubygems/test_gem_commands_dependency_command.rb +0 -228
- data/test/rubygems/test_gem_commands_environment_command.rb +0 -169
- data/test/rubygems/test_gem_commands_exec_command.rb +0 -857
- data/test/rubygems/test_gem_commands_fetch_command.rb +0 -258
- data/test/rubygems/test_gem_commands_generate_index_command.rb +0 -81
- data/test/rubygems/test_gem_commands_help_command.rb +0 -94
- data/test/rubygems/test_gem_commands_info_command.rb +0 -70
- data/test/rubygems/test_gem_commands_install_command.rb +0 -1573
- data/test/rubygems/test_gem_commands_list_command.rb +0 -33
- data/test/rubygems/test_gem_commands_lock_command.rb +0 -67
- data/test/rubygems/test_gem_commands_mirror.rb +0 -20
- data/test/rubygems/test_gem_commands_open_command.rb +0 -101
- data/test/rubygems/test_gem_commands_outdated_command.rb +0 -50
- data/test/rubygems/test_gem_commands_owner_command.rb +0 -503
- data/test/rubygems/test_gem_commands_pristine_command.rb +0 -708
- data/test/rubygems/test_gem_commands_push_command.rb +0 -603
- data/test/rubygems/test_gem_commands_query_command.rb +0 -858
- data/test/rubygems/test_gem_commands_search_command.rb +0 -16
- data/test/rubygems/test_gem_commands_server_command.rb +0 -20
- data/test/rubygems/test_gem_commands_setup_command.rb +0 -474
- data/test/rubygems/test_gem_commands_signin_command.rb +0 -259
- data/test/rubygems/test_gem_commands_signout_command.rb +0 -30
- data/test/rubygems/test_gem_commands_sources_command.rb +0 -534
- data/test/rubygems/test_gem_commands_specification_command.rb +0 -277
- data/test/rubygems/test_gem_commands_stale_command.rb +0 -43
- data/test/rubygems/test_gem_commands_uninstall_command.rb +0 -542
- data/test/rubygems/test_gem_commands_unpack_command.rb +0 -224
- data/test/rubygems/test_gem_commands_update_command.rb +0 -836
- data/test/rubygems/test_gem_commands_which_command.rb +0 -85
- data/test/rubygems/test_gem_commands_yank_command.rb +0 -299
- data/test/rubygems/test_gem_config_file.rb +0 -551
- data/test/rubygems/test_gem_dependency.rb +0 -398
- data/test/rubygems/test_gem_dependency_installer.rb +0 -1190
- data/test/rubygems/test_gem_dependency_list.rb +0 -265
- data/test/rubygems/test_gem_dependency_resolution_error.rb +0 -27
- data/test/rubygems/test_gem_doctor.rb +0 -195
- data/test/rubygems/test_gem_ext_builder.rb +0 -337
- data/test/rubygems/test_gem_ext_cargo_builder/custom_name/.gitignore +0 -1
- data/test/rubygems/test_gem_ext_cargo_builder/custom_name/custom_name.gemspec +0 -10
- data/test/rubygems/test_gem_ext_cargo_builder/custom_name/ext/custom_name_lib/Cargo.lock +0 -249
- data/test/rubygems/test_gem_ext_cargo_builder/custom_name/ext/custom_name_lib/Cargo.toml +0 -10
- data/test/rubygems/test_gem_ext_cargo_builder/custom_name/ext/custom_name_lib/src/lib.rs +0 -27
- data/test/rubygems/test_gem_ext_cargo_builder/custom_name/lib/custom_name.rb +0 -3
- data/test/rubygems/test_gem_ext_cargo_builder/rust_ruby_example/.gitignore +0 -1
- data/test/rubygems/test_gem_ext_cargo_builder/rust_ruby_example/Cargo.lock +0 -249
- data/test/rubygems/test_gem_ext_cargo_builder/rust_ruby_example/Cargo.toml +0 -10
- data/test/rubygems/test_gem_ext_cargo_builder/rust_ruby_example/rust_ruby_example.gemspec +0 -10
- data/test/rubygems/test_gem_ext_cargo_builder/rust_ruby_example/src/lib.rs +0 -51
- data/test/rubygems/test_gem_ext_cargo_builder.rb +0 -167
- data/test/rubygems/test_gem_ext_cargo_builder_link_flag_converter.rb +0 -34
- data/test/rubygems/test_gem_ext_cargo_builder_unit.rb +0 -60
- data/test/rubygems/test_gem_ext_cmake_builder.rb +0 -84
- data/test/rubygems/test_gem_ext_configure_builder.rb +0 -80
- data/test/rubygems/test_gem_ext_ext_conf_builder.rb +0 -229
- data/test/rubygems/test_gem_ext_rake_builder.rb +0 -113
- data/test/rubygems/test_gem_gem_runner.rb +0 -119
- data/test/rubygems/test_gem_gemcutter_utilities.rb +0 -361
- data/test/rubygems/test_gem_impossible_dependencies_error.rb +0 -60
- data/test/rubygems/test_gem_indexer.rb +0 -381
- data/test/rubygems/test_gem_install_update_options.rb +0 -208
- data/test/rubygems/test_gem_installer.rb +0 -2512
- data/test/rubygems/test_gem_local_remote_options.rb +0 -133
- data/test/rubygems/test_gem_name_tuple.rb +0 -43
- data/test/rubygems/test_gem_package.rb +0 -1306
- data/test/rubygems/test_gem_package_old.rb +0 -91
- data/test/rubygems/test_gem_package_tar_header.rb +0 -226
- data/test/rubygems/test_gem_package_tar_reader.rb +0 -150
- data/test/rubygems/test_gem_package_tar_reader_entry.rb +0 -350
- data/test/rubygems/test_gem_package_tar_writer.rb +0 -331
- data/test/rubygems/test_gem_package_task.rb +0 -118
- data/test/rubygems/test_gem_path_support.rb +0 -139
- data/test/rubygems/test_gem_platform.rb +0 -497
- data/test/rubygems/test_gem_rdoc.rb +0 -137
- data/test/rubygems/test_gem_remote_fetcher.rb +0 -1227
- data/test/rubygems/test_gem_request.rb +0 -547
- data/test/rubygems/test_gem_request_connection_pools.rb +0 -152
- data/test/rubygems/test_gem_request_set.rb +0 -672
- data/test/rubygems/test_gem_request_set_gem_dependency_api.rb +0 -853
- data/test/rubygems/test_gem_request_set_lockfile.rb +0 -469
- data/test/rubygems/test_gem_request_set_lockfile_parser.rb +0 -544
- data/test/rubygems/test_gem_request_set_lockfile_tokenizer.rb +0 -307
- data/test/rubygems/test_gem_requirement.rb +0 -505
- data/test/rubygems/test_gem_resolver.rb +0 -859
- data/test/rubygems/test_gem_resolver_activation_request.rb +0 -43
- data/test/rubygems/test_gem_resolver_api_set.rb +0 -210
- data/test/rubygems/test_gem_resolver_api_specification.rb +0 -167
- data/test/rubygems/test_gem_resolver_best_set.rb +0 -159
- data/test/rubygems/test_gem_resolver_composed_set.rb +0 -44
- data/test/rubygems/test_gem_resolver_conflict.rb +0 -82
- data/test/rubygems/test_gem_resolver_dependency_request.rb +0 -83
- data/test/rubygems/test_gem_resolver_git_set.rb +0 -188
- data/test/rubygems/test_gem_resolver_git_specification.rb +0 -114
- data/test/rubygems/test_gem_resolver_index_set.rb +0 -88
- data/test/rubygems/test_gem_resolver_index_specification.rb +0 -93
- data/test/rubygems/test_gem_resolver_installed_specification.rb +0 -47
- data/test/rubygems/test_gem_resolver_installer_set.rb +0 -320
- data/test/rubygems/test_gem_resolver_local_specification.rb +0 -44
- data/test/rubygems/test_gem_resolver_lock_set.rb +0 -62
- data/test/rubygems/test_gem_resolver_lock_specification.rb +0 -98
- data/test/rubygems/test_gem_resolver_requirement_list.rb +0 -19
- data/test/rubygems/test_gem_resolver_specification.rb +0 -63
- data/test/rubygems/test_gem_resolver_vendor_set.rb +0 -82
- data/test/rubygems/test_gem_resolver_vendor_specification.rb +0 -82
- data/test/rubygems/test_gem_security.rb +0 -341
- data/test/rubygems/test_gem_security_policy.rb +0 -535
- data/test/rubygems/test_gem_security_signer.rb +0 -218
- data/test/rubygems/test_gem_security_trust_dir.rb +0 -99
- data/test/rubygems/test_gem_silent_ui.rb +0 -123
- data/test/rubygems/test_gem_source.rb +0 -254
- data/test/rubygems/test_gem_source_fetch_problem.rb +0 -37
- data/test/rubygems/test_gem_source_git.rb +0 -310
- data/test/rubygems/test_gem_source_installed.rb +0 -35
- data/test/rubygems/test_gem_source_list.rb +0 -119
- data/test/rubygems/test_gem_source_local.rb +0 -107
- data/test/rubygems/test_gem_source_lock.rb +0 -113
- data/test/rubygems/test_gem_source_specific_file.rb +0 -76
- data/test/rubygems/test_gem_source_subpath_problem.rb +0 -50
- data/test/rubygems/test_gem_source_vendor.rb +0 -30
- data/test/rubygems/test_gem_spec_fetcher.rb +0 -338
- data/test/rubygems/test_gem_specification.rb +0 -3856
- data/test/rubygems/test_gem_stream_ui.rb +0 -255
- data/test/rubygems/test_gem_stub_specification.rb +0 -278
- data/test/rubygems/test_gem_text.rb +0 -103
- data/test/rubygems/test_gem_uninstaller.rb +0 -675
- data/test/rubygems/test_gem_unsatisfiable_dependency_error.rb +0 -31
- data/test/rubygems/test_gem_update_suggestion.rb +0 -209
- data/test/rubygems/test_gem_uri.rb +0 -41
- data/test/rubygems/test_gem_uri_formatter.rb +0 -27
- data/test/rubygems/test_gem_util.rb +0 -91
- data/test/rubygems/test_gem_validator.rb +0 -42
- data/test/rubygems/test_gem_version.rb +0 -305
- data/test/rubygems/test_gem_version_option.rb +0 -165
- data/test/rubygems/test_kernel.rb +0 -124
- data/test/rubygems/test_project_sanity.rb +0 -49
- data/test/rubygems/test_remote_fetch_error.rb +0 -20
- data/test/rubygems/test_require.rb +0 -732
- data/test/rubygems/test_rubygems.rb +0 -76
- data/test/rubygems/test_webauthn_listener.rb +0 -143
- data/test/rubygems/test_webauthn_listener_response.rb +0 -93
- data/test/rubygems/test_webauthn_poller.rb +0 -124
- data/test/rubygems/utilities.rb +0 -436
- data/test/rubygems/wrong_key_cert.pem +0 -19
- data/test/rubygems/wrong_key_cert_32.pem +0 -19
- data/test/test_changelog_generator.rb +0 -17
@@ -0,0 +1,981 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# The \HTTPHeader module provides access to \HTTP headers.
|
4
|
+
#
|
5
|
+
# The module is included in:
|
6
|
+
#
|
7
|
+
# - Gem::Net::HTTPGenericRequest (and therefore Gem::Net::HTTPRequest).
|
8
|
+
# - Gem::Net::HTTPResponse.
|
9
|
+
#
|
10
|
+
# The headers are a hash-like collection of key/value pairs called _fields_.
|
11
|
+
#
|
12
|
+
# == Request and Response Fields
|
13
|
+
#
|
14
|
+
# Headers may be included in:
|
15
|
+
#
|
16
|
+
# - A Gem::Net::HTTPRequest object:
|
17
|
+
# the object's headers will be sent with the request.
|
18
|
+
# Any fields may be defined in the request;
|
19
|
+
# see {Setters}[rdoc-ref:Gem::Net::HTTPHeader@Setters].
|
20
|
+
# - A Gem::Net::HTTPResponse object:
|
21
|
+
# the objects headers are usually those returned from the host.
|
22
|
+
# Fields may be retrieved from the object;
|
23
|
+
# see {Getters}[rdoc-ref:Gem::Net::HTTPHeader@Getters]
|
24
|
+
# and {Iterators}[rdoc-ref:Gem::Net::HTTPHeader@Iterators].
|
25
|
+
#
|
26
|
+
# Exactly which fields should be sent or expected depends on the host;
|
27
|
+
# see:
|
28
|
+
#
|
29
|
+
# - {Request fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields].
|
30
|
+
# - {Response fields}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Response_fields].
|
31
|
+
#
|
32
|
+
# == About the Examples
|
33
|
+
#
|
34
|
+
# :include: doc/net-http/examples.rdoc
|
35
|
+
#
|
36
|
+
# == Fields
|
37
|
+
#
|
38
|
+
# A header field is a key/value pair.
|
39
|
+
#
|
40
|
+
# === Field Keys
|
41
|
+
#
|
42
|
+
# A field key may be:
|
43
|
+
#
|
44
|
+
# - A string: Key <tt>'Accept'</tt> is treated as if it were
|
45
|
+
# <tt>'Accept'.downcase</tt>; i.e., <tt>'accept'</tt>.
|
46
|
+
# - A symbol: Key <tt>:Accept</tt> is treated as if it were
|
47
|
+
# <tt>:Accept.to_s.downcase</tt>; i.e., <tt>'accept'</tt>.
|
48
|
+
#
|
49
|
+
# Examples:
|
50
|
+
#
|
51
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
52
|
+
# req[:accept] # => "*/*"
|
53
|
+
# req['Accept'] # => "*/*"
|
54
|
+
# req['ACCEPT'] # => "*/*"
|
55
|
+
#
|
56
|
+
# req['accept'] = 'text/html'
|
57
|
+
# req[:accept] = 'text/html'
|
58
|
+
# req['ACCEPT'] = 'text/html'
|
59
|
+
#
|
60
|
+
# === Field Values
|
61
|
+
#
|
62
|
+
# A field value may be returned as an array of strings or as a string:
|
63
|
+
#
|
64
|
+
# - These methods return field values as arrays:
|
65
|
+
#
|
66
|
+
# - #get_fields: Returns the array value for the given key,
|
67
|
+
# or +nil+ if it does not exist.
|
68
|
+
# - #to_hash: Returns a hash of all header fields:
|
69
|
+
# each key is a field name; its value is the array value for the field.
|
70
|
+
#
|
71
|
+
# - These methods return field values as string;
|
72
|
+
# the string value for a field is equivalent to
|
73
|
+
# <tt>self[key.downcase.to_s].join(', '))</tt>:
|
74
|
+
#
|
75
|
+
# - #[]: Returns the string value for the given key,
|
76
|
+
# or +nil+ if it does not exist.
|
77
|
+
# - #fetch: Like #[], but accepts a default value
|
78
|
+
# to be returned if the key does not exist.
|
79
|
+
#
|
80
|
+
# The field value may be set:
|
81
|
+
#
|
82
|
+
# - #[]=: Sets the value for the given key;
|
83
|
+
# the given value may be a string, a symbol, an array, or a hash.
|
84
|
+
# - #add_field: Adds a given value to a value for the given key
|
85
|
+
# (not overwriting the existing value).
|
86
|
+
# - #delete: Deletes the field for the given key.
|
87
|
+
#
|
88
|
+
# Example field values:
|
89
|
+
#
|
90
|
+
# - \String:
|
91
|
+
#
|
92
|
+
# req['Accept'] = 'text/html' # => "text/html"
|
93
|
+
# req['Accept'] # => "text/html"
|
94
|
+
# req.get_fields('Accept') # => ["text/html"]
|
95
|
+
#
|
96
|
+
# - \Symbol:
|
97
|
+
#
|
98
|
+
# req['Accept'] = :text # => :text
|
99
|
+
# req['Accept'] # => "text"
|
100
|
+
# req.get_fields('Accept') # => ["text"]
|
101
|
+
#
|
102
|
+
# - Simple array:
|
103
|
+
#
|
104
|
+
# req[:foo] = %w[bar baz bat]
|
105
|
+
# req[:foo] # => "bar, baz, bat"
|
106
|
+
# req.get_fields(:foo) # => ["bar", "baz", "bat"]
|
107
|
+
#
|
108
|
+
# - Simple hash:
|
109
|
+
#
|
110
|
+
# req[:foo] = {bar: 0, baz: 1, bat: 2}
|
111
|
+
# req[:foo] # => "bar, 0, baz, 1, bat, 2"
|
112
|
+
# req.get_fields(:foo) # => ["bar", "0", "baz", "1", "bat", "2"]
|
113
|
+
#
|
114
|
+
# - Nested:
|
115
|
+
#
|
116
|
+
# req[:foo] = [%w[bar baz], {bat: 0, bam: 1}]
|
117
|
+
# req[:foo] # => "bar, baz, bat, 0, bam, 1"
|
118
|
+
# req.get_fields(:foo) # => ["bar", "baz", "bat", "0", "bam", "1"]
|
119
|
+
#
|
120
|
+
# req[:foo] = {bar: %w[baz bat], bam: {bah: 0, bad: 1}}
|
121
|
+
# req[:foo] # => "bar, baz, bat, bam, bah, 0, bad, 1"
|
122
|
+
# req.get_fields(:foo) # => ["bar", "baz", "bat", "bam", "bah", "0", "bad", "1"]
|
123
|
+
#
|
124
|
+
# == Convenience Methods
|
125
|
+
#
|
126
|
+
# Various convenience methods retrieve values, set values, query values,
|
127
|
+
# set form values, or iterate over fields.
|
128
|
+
#
|
129
|
+
# === Setters
|
130
|
+
#
|
131
|
+
# \Method #[]= can set any field, but does little to validate the new value;
|
132
|
+
# some of the other setter methods provide some validation:
|
133
|
+
#
|
134
|
+
# - #[]=: Sets the string or array value for the given key.
|
135
|
+
# - #add_field: Creates or adds to the array value for the given key.
|
136
|
+
# - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
|
137
|
+
# - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>.
|
138
|
+
# - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>.
|
139
|
+
# - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
|
140
|
+
# - #set_range: Sets the value for field <tt>'Range'</tt>.
|
141
|
+
#
|
142
|
+
# === Form Setters
|
143
|
+
#
|
144
|
+
# - #set_form: Sets an HTML form data set.
|
145
|
+
# - #set_form_data: Sets header fields and a body from HTML form data.
|
146
|
+
#
|
147
|
+
# === Getters
|
148
|
+
#
|
149
|
+
# \Method #[] can retrieve the value of any field that exists,
|
150
|
+
# but always as a string;
|
151
|
+
# some of the other getter methods return something different
|
152
|
+
# from the simple string value:
|
153
|
+
#
|
154
|
+
# - #[]: Returns the string field value for the given key.
|
155
|
+
# - #content_length: Returns the integer value of field <tt>'Content-Length'</tt>.
|
156
|
+
# - #content_range: Returns the Range value of field <tt>'Content-Range'</tt>.
|
157
|
+
# - #content_type: Returns the string value of field <tt>'Content-Type'</tt>.
|
158
|
+
# - #fetch: Returns the string field value for the given key.
|
159
|
+
# - #get_fields: Returns the array field value for the given +key+.
|
160
|
+
# - #main_type: Returns first part of the string value of field <tt>'Content-Type'</tt>.
|
161
|
+
# - #sub_type: Returns second part of the string value of field <tt>'Content-Type'</tt>.
|
162
|
+
# - #range: Returns an array of Range objects of field <tt>'Range'</tt>, or +nil+.
|
163
|
+
# - #range_length: Returns the integer length of the range given in field <tt>'Content-Range'</tt>.
|
164
|
+
# - #type_params: Returns the string parameters for <tt>'Content-Type'</tt>.
|
165
|
+
#
|
166
|
+
# === Queries
|
167
|
+
#
|
168
|
+
# - #chunked?: Returns whether field <tt>'Transfer-Encoding'</tt> is set to <tt>'chunked'</tt>.
|
169
|
+
# - #connection_close?: Returns whether field <tt>'Connection'</tt> is set to <tt>'close'</tt>.
|
170
|
+
# - #connection_keep_alive?: Returns whether field <tt>'Connection'</tt> is set to <tt>'keep-alive'</tt>.
|
171
|
+
# - #key?: Returns whether a given key exists.
|
172
|
+
#
|
173
|
+
# === Iterators
|
174
|
+
#
|
175
|
+
# - #each_capitalized: Passes each field capitalized-name/value pair to the block.
|
176
|
+
# - #each_capitalized_name: Passes each capitalized field name to the block.
|
177
|
+
# - #each_header: Passes each field name/value pair to the block.
|
178
|
+
# - #each_name: Passes each field name to the block.
|
179
|
+
# - #each_value: Passes each string field value to the block.
|
180
|
+
#
|
181
|
+
module Gem::Net::HTTPHeader
|
182
|
+
MAX_KEY_LENGTH = 1024
|
183
|
+
MAX_FIELD_LENGTH = 65536
|
184
|
+
|
185
|
+
def initialize_http_header(initheader) #:nodoc:
|
186
|
+
@header = {}
|
187
|
+
return unless initheader
|
188
|
+
initheader.each do |key, value|
|
189
|
+
warn "net/http: duplicated HTTP header: #{key}", uplevel: 3 if key?(key) and $VERBOSE
|
190
|
+
if value.nil?
|
191
|
+
warn "net/http: nil HTTP header: #{key}", uplevel: 3 if $VERBOSE
|
192
|
+
else
|
193
|
+
value = value.strip # raise error for invalid byte sequences
|
194
|
+
if key.to_s.bytesize > MAX_KEY_LENGTH
|
195
|
+
raise ArgumentError, "too long (#{key.bytesize} bytes) header: #{key[0, 30].inspect}..."
|
196
|
+
end
|
197
|
+
if value.to_s.bytesize > MAX_FIELD_LENGTH
|
198
|
+
raise ArgumentError, "header #{key} has too long field value: #{value.bytesize}"
|
199
|
+
end
|
200
|
+
if value.count("\r\n") > 0
|
201
|
+
raise ArgumentError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
|
202
|
+
end
|
203
|
+
@header[key.downcase.to_s] = [value]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def size #:nodoc: obsolete
|
209
|
+
@header.size
|
210
|
+
end
|
211
|
+
|
212
|
+
alias length size #:nodoc: obsolete
|
213
|
+
|
214
|
+
# Returns the string field value for the case-insensitive field +key+,
|
215
|
+
# or +nil+ if there is no such key;
|
216
|
+
# see {Fields}[rdoc-ref:Gem::Net::HTTPHeader@Fields]:
|
217
|
+
#
|
218
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
219
|
+
# res['Connection'] # => "keep-alive"
|
220
|
+
# res['Nosuch'] # => nil
|
221
|
+
#
|
222
|
+
# Note that some field values may be retrieved via convenience methods;
|
223
|
+
# see {Getters}[rdoc-ref:Gem::Net::HTTPHeader@Getters].
|
224
|
+
def [](key)
|
225
|
+
a = @header[key.downcase.to_s] or return nil
|
226
|
+
a.join(', ')
|
227
|
+
end
|
228
|
+
|
229
|
+
# Sets the value for the case-insensitive +key+ to +val+,
|
230
|
+
# overwriting the previous value if the field exists;
|
231
|
+
# see {Fields}[rdoc-ref:Gem::Net::HTTPHeader@Fields]:
|
232
|
+
#
|
233
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
234
|
+
# req['Accept'] # => "*/*"
|
235
|
+
# req['Accept'] = 'text/html'
|
236
|
+
# req['Accept'] # => "text/html"
|
237
|
+
#
|
238
|
+
# Note that some field values may be set via convenience methods;
|
239
|
+
# see {Setters}[rdoc-ref:Gem::Net::HTTPHeader@Setters].
|
240
|
+
def []=(key, val)
|
241
|
+
unless val
|
242
|
+
@header.delete key.downcase.to_s
|
243
|
+
return val
|
244
|
+
end
|
245
|
+
set_field(key, val)
|
246
|
+
end
|
247
|
+
|
248
|
+
# Adds value +val+ to the value array for field +key+ if the field exists;
|
249
|
+
# creates the field with the given +key+ and +val+ if it does not exist.
|
250
|
+
# see {Fields}[rdoc-ref:Gem::Net::HTTPHeader@Fields]:
|
251
|
+
#
|
252
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
253
|
+
# req.add_field('Foo', 'bar')
|
254
|
+
# req['Foo'] # => "bar"
|
255
|
+
# req.add_field('Foo', 'baz')
|
256
|
+
# req['Foo'] # => "bar, baz"
|
257
|
+
# req.add_field('Foo', %w[baz bam])
|
258
|
+
# req['Foo'] # => "bar, baz, baz, bam"
|
259
|
+
# req.get_fields('Foo') # => ["bar", "baz", "baz", "bam"]
|
260
|
+
#
|
261
|
+
def add_field(key, val)
|
262
|
+
stringified_downcased_key = key.downcase.to_s
|
263
|
+
if @header.key?(stringified_downcased_key)
|
264
|
+
append_field_value(@header[stringified_downcased_key], val)
|
265
|
+
else
|
266
|
+
set_field(key, val)
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
private def set_field(key, val)
|
271
|
+
case val
|
272
|
+
when Enumerable
|
273
|
+
ary = []
|
274
|
+
append_field_value(ary, val)
|
275
|
+
@header[key.downcase.to_s] = ary
|
276
|
+
else
|
277
|
+
val = val.to_s # for compatibility use to_s instead of to_str
|
278
|
+
if val.b.count("\r\n") > 0
|
279
|
+
raise ArgumentError, 'header field value cannot include CR/LF'
|
280
|
+
end
|
281
|
+
@header[key.downcase.to_s] = [val]
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
private def append_field_value(ary, val)
|
286
|
+
case val
|
287
|
+
when Enumerable
|
288
|
+
val.each{|x| append_field_value(ary, x)}
|
289
|
+
else
|
290
|
+
val = val.to_s
|
291
|
+
if /[\r\n]/n.match?(val.b)
|
292
|
+
raise ArgumentError, 'header field value cannot include CR/LF'
|
293
|
+
end
|
294
|
+
ary.push val
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Returns the array field value for the given +key+,
|
299
|
+
# or +nil+ if there is no such field;
|
300
|
+
# see {Fields}[rdoc-ref:Gem::Net::HTTPHeader@Fields]:
|
301
|
+
#
|
302
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
303
|
+
# res.get_fields('Connection') # => ["keep-alive"]
|
304
|
+
# res.get_fields('Nosuch') # => nil
|
305
|
+
#
|
306
|
+
def get_fields(key)
|
307
|
+
stringified_downcased_key = key.downcase.to_s
|
308
|
+
return nil unless @header[stringified_downcased_key]
|
309
|
+
@header[stringified_downcased_key].dup
|
310
|
+
end
|
311
|
+
|
312
|
+
# call-seq:
|
313
|
+
# fetch(key, default_val = nil) {|key| ... } -> object
|
314
|
+
# fetch(key, default_val = nil) -> value or default_val
|
315
|
+
#
|
316
|
+
# With a block, returns the string value for +key+ if it exists;
|
317
|
+
# otherwise returns the value of the block;
|
318
|
+
# ignores the +default_val+;
|
319
|
+
# see {Fields}[rdoc-ref:Gem::Net::HTTPHeader@Fields]:
|
320
|
+
#
|
321
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
322
|
+
#
|
323
|
+
# # Field exists; block not called.
|
324
|
+
# res.fetch('Connection') do |value|
|
325
|
+
# fail 'Cannot happen'
|
326
|
+
# end # => "keep-alive"
|
327
|
+
#
|
328
|
+
# # Field does not exist; block called.
|
329
|
+
# res.fetch('Nosuch') do |value|
|
330
|
+
# value.downcase
|
331
|
+
# end # => "nosuch"
|
332
|
+
#
|
333
|
+
# With no block, returns the string value for +key+ if it exists;
|
334
|
+
# otherwise, returns +default_val+ if it was given;
|
335
|
+
# otherwise raises an exception:
|
336
|
+
#
|
337
|
+
# res.fetch('Connection', 'Foo') # => "keep-alive"
|
338
|
+
# res.fetch('Nosuch', 'Foo') # => "Foo"
|
339
|
+
# res.fetch('Nosuch') # Raises KeyError.
|
340
|
+
#
|
341
|
+
def fetch(key, *args, &block) #:yield: +key+
|
342
|
+
a = @header.fetch(key.downcase.to_s, *args, &block)
|
343
|
+
a.kind_of?(Array) ? a.join(', ') : a
|
344
|
+
end
|
345
|
+
|
346
|
+
# Calls the block with each key/value pair:
|
347
|
+
#
|
348
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
349
|
+
# res.each_header do |key, value|
|
350
|
+
# p [key, value] if key.start_with?('c')
|
351
|
+
# end
|
352
|
+
#
|
353
|
+
# Output:
|
354
|
+
#
|
355
|
+
# ["content-type", "application/json; charset=utf-8"]
|
356
|
+
# ["connection", "keep-alive"]
|
357
|
+
# ["cache-control", "max-age=43200"]
|
358
|
+
# ["cf-cache-status", "HIT"]
|
359
|
+
# ["cf-ray", "771d17e9bc542cf5-ORD"]
|
360
|
+
#
|
361
|
+
# Returns an enumerator if no block is given.
|
362
|
+
#
|
363
|
+
# Gem::Net::HTTPHeader#each is an alias for Gem::Net::HTTPHeader#each_header.
|
364
|
+
def each_header #:yield: +key+, +value+
|
365
|
+
block_given? or return enum_for(__method__) { @header.size }
|
366
|
+
@header.each do |k,va|
|
367
|
+
yield k, va.join(', ')
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
alias each each_header
|
372
|
+
|
373
|
+
# Calls the block with each field key:
|
374
|
+
#
|
375
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
376
|
+
# res.each_key do |key|
|
377
|
+
# p key if key.start_with?('c')
|
378
|
+
# end
|
379
|
+
#
|
380
|
+
# Output:
|
381
|
+
#
|
382
|
+
# "content-type"
|
383
|
+
# "connection"
|
384
|
+
# "cache-control"
|
385
|
+
# "cf-cache-status"
|
386
|
+
# "cf-ray"
|
387
|
+
#
|
388
|
+
# Returns an enumerator if no block is given.
|
389
|
+
#
|
390
|
+
# Gem::Net::HTTPHeader#each_name is an alias for Gem::Net::HTTPHeader#each_key.
|
391
|
+
def each_name(&block) #:yield: +key+
|
392
|
+
block_given? or return enum_for(__method__) { @header.size }
|
393
|
+
@header.each_key(&block)
|
394
|
+
end
|
395
|
+
|
396
|
+
alias each_key each_name
|
397
|
+
|
398
|
+
# Calls the block with each capitalized field name:
|
399
|
+
#
|
400
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
401
|
+
# res.each_capitalized_name do |key|
|
402
|
+
# p key if key.start_with?('C')
|
403
|
+
# end
|
404
|
+
#
|
405
|
+
# Output:
|
406
|
+
#
|
407
|
+
# "Content-Type"
|
408
|
+
# "Connection"
|
409
|
+
# "Cache-Control"
|
410
|
+
# "Cf-Cache-Status"
|
411
|
+
# "Cf-Ray"
|
412
|
+
#
|
413
|
+
# The capitalization is system-dependent;
|
414
|
+
# see {Case Mapping}[https://docs.ruby-lang.org/en/master/case_mapping_rdoc.html].
|
415
|
+
#
|
416
|
+
# Returns an enumerator if no block is given.
|
417
|
+
def each_capitalized_name #:yield: +key+
|
418
|
+
block_given? or return enum_for(__method__) { @header.size }
|
419
|
+
@header.each_key do |k|
|
420
|
+
yield capitalize(k)
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
# Calls the block with each string field value:
|
425
|
+
#
|
426
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
427
|
+
# res.each_value do |value|
|
428
|
+
# p value if value.start_with?('c')
|
429
|
+
# end
|
430
|
+
#
|
431
|
+
# Output:
|
432
|
+
#
|
433
|
+
# "chunked"
|
434
|
+
# "cf-q-config;dur=6.0000002122251e-06"
|
435
|
+
# "cloudflare"
|
436
|
+
#
|
437
|
+
# Returns an enumerator if no block is given.
|
438
|
+
def each_value #:yield: +value+
|
439
|
+
block_given? or return enum_for(__method__) { @header.size }
|
440
|
+
@header.each_value do |va|
|
441
|
+
yield va.join(', ')
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
# Removes the header for the given case-insensitive +key+
|
446
|
+
# (see {Fields}[rdoc-ref:Gem::Net::HTTPHeader@Fields]);
|
447
|
+
# returns the deleted value, or +nil+ if no such field exists:
|
448
|
+
#
|
449
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
450
|
+
# req.delete('Accept') # => ["*/*"]
|
451
|
+
# req.delete('Nosuch') # => nil
|
452
|
+
#
|
453
|
+
def delete(key)
|
454
|
+
@header.delete(key.downcase.to_s)
|
455
|
+
end
|
456
|
+
|
457
|
+
# Returns +true+ if the field for the case-insensitive +key+ exists, +false+ otherwise:
|
458
|
+
#
|
459
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
460
|
+
# req.key?('Accept') # => true
|
461
|
+
# req.key?('Nosuch') # => false
|
462
|
+
#
|
463
|
+
def key?(key)
|
464
|
+
@header.key?(key.downcase.to_s)
|
465
|
+
end
|
466
|
+
|
467
|
+
# Returns a hash of the key/value pairs:
|
468
|
+
#
|
469
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
470
|
+
# req.to_hash
|
471
|
+
# # =>
|
472
|
+
# {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
|
473
|
+
# "accept"=>["*/*"],
|
474
|
+
# "user-agent"=>["Ruby"],
|
475
|
+
# "host"=>["jsonplaceholder.typicode.com"]}
|
476
|
+
#
|
477
|
+
def to_hash
|
478
|
+
@header.dup
|
479
|
+
end
|
480
|
+
|
481
|
+
# Like #each_header, but the keys are returned in capitalized form.
|
482
|
+
#
|
483
|
+
# Gem::Net::HTTPHeader#canonical_each is an alias for Gem::Net::HTTPHeader#each_capitalized.
|
484
|
+
def each_capitalized
|
485
|
+
block_given? or return enum_for(__method__) { @header.size }
|
486
|
+
@header.each do |k,v|
|
487
|
+
yield capitalize(k), v.join(', ')
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
alias canonical_each each_capitalized
|
492
|
+
|
493
|
+
def capitalize(name)
|
494
|
+
name.to_s.split(/-/).map {|s| s.capitalize }.join('-')
|
495
|
+
end
|
496
|
+
private :capitalize
|
497
|
+
|
498
|
+
# Returns an array of Range objects that represent
|
499
|
+
# the value of field <tt>'Range'</tt>,
|
500
|
+
# or +nil+ if there is no such field;
|
501
|
+
# see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
|
502
|
+
#
|
503
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
504
|
+
# req['Range'] = 'bytes=0-99,200-299,400-499'
|
505
|
+
# req.range # => [0..99, 200..299, 400..499]
|
506
|
+
# req.delete('Range')
|
507
|
+
# req.range # # => nil
|
508
|
+
#
|
509
|
+
def range
|
510
|
+
return nil unless @header['range']
|
511
|
+
|
512
|
+
value = self['Range']
|
513
|
+
# byte-range-set = *( "," OWS ) ( byte-range-spec / suffix-byte-range-spec )
|
514
|
+
# *( OWS "," [ OWS ( byte-range-spec / suffix-byte-range-spec ) ] )
|
515
|
+
# corrected collected ABNF
|
516
|
+
# http://tools.ietf.org/html/draft-ietf-httpbis-p5-range-19#section-5.4.1
|
517
|
+
# http://tools.ietf.org/html/draft-ietf-httpbis-p5-range-19#appendix-C
|
518
|
+
# http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-19#section-3.2.5
|
519
|
+
unless /\Abytes=((?:,[ \t]*)*(?:\d+-\d*|-\d+)(?:[ \t]*,(?:[ \t]*\d+-\d*|-\d+)?)*)\z/ =~ value
|
520
|
+
raise Gem::Net::HTTPHeaderSyntaxError, "invalid syntax for byte-ranges-specifier: '#{value}'"
|
521
|
+
end
|
522
|
+
|
523
|
+
byte_range_set = $1
|
524
|
+
result = byte_range_set.split(/,/).map {|spec|
|
525
|
+
m = /(\d+)?\s*-\s*(\d+)?/i.match(spec) or
|
526
|
+
raise Gem::Net::HTTPHeaderSyntaxError, "invalid byte-range-spec: '#{spec}'"
|
527
|
+
d1 = m[1].to_i
|
528
|
+
d2 = m[2].to_i
|
529
|
+
if m[1] and m[2]
|
530
|
+
if d1 > d2
|
531
|
+
raise Gem::Net::HTTPHeaderSyntaxError, "last-byte-pos MUST greater than or equal to first-byte-pos but '#{spec}'"
|
532
|
+
end
|
533
|
+
d1..d2
|
534
|
+
elsif m[1]
|
535
|
+
d1..-1
|
536
|
+
elsif m[2]
|
537
|
+
-d2..-1
|
538
|
+
else
|
539
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'range is not specified'
|
540
|
+
end
|
541
|
+
}
|
542
|
+
# if result.empty?
|
543
|
+
# byte-range-set must include at least one byte-range-spec or suffix-byte-range-spec
|
544
|
+
# but above regexp already denies it.
|
545
|
+
if result.size == 1 && result[0].begin == 0 && result[0].end == -1
|
546
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'only one suffix-byte-range-spec with zero suffix-length'
|
547
|
+
end
|
548
|
+
result
|
549
|
+
end
|
550
|
+
|
551
|
+
# call-seq:
|
552
|
+
# set_range(length) -> length
|
553
|
+
# set_range(offset, length) -> range
|
554
|
+
# set_range(begin..length) -> range
|
555
|
+
#
|
556
|
+
# Sets the value for field <tt>'Range'</tt>;
|
557
|
+
# see {Range request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#range-request-header]:
|
558
|
+
#
|
559
|
+
# With argument +length+:
|
560
|
+
#
|
561
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
562
|
+
# req.set_range(100) # => 100
|
563
|
+
# req['Range'] # => "bytes=0-99"
|
564
|
+
#
|
565
|
+
# With arguments +offset+ and +length+:
|
566
|
+
#
|
567
|
+
# req.set_range(100, 100) # => 100...200
|
568
|
+
# req['Range'] # => "bytes=100-199"
|
569
|
+
#
|
570
|
+
# With argument +range+:
|
571
|
+
#
|
572
|
+
# req.set_range(100..199) # => 100..199
|
573
|
+
# req['Range'] # => "bytes=100-199"
|
574
|
+
#
|
575
|
+
# Gem::Net::HTTPHeader#range= is an alias for Gem::Net::HTTPHeader#set_range.
|
576
|
+
def set_range(r, e = nil)
|
577
|
+
unless r
|
578
|
+
@header.delete 'range'
|
579
|
+
return r
|
580
|
+
end
|
581
|
+
r = (r...r+e) if e
|
582
|
+
case r
|
583
|
+
when Numeric
|
584
|
+
n = r.to_i
|
585
|
+
rangestr = (n > 0 ? "0-#{n-1}" : "-#{-n}")
|
586
|
+
when Range
|
587
|
+
first = r.first
|
588
|
+
last = r.end
|
589
|
+
last -= 1 if r.exclude_end?
|
590
|
+
if last == -1
|
591
|
+
rangestr = (first > 0 ? "#{first}-" : "-#{-first}")
|
592
|
+
else
|
593
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'range.first is negative' if first < 0
|
594
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'range.last is negative' if last < 0
|
595
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'must be .first < .last' if first > last
|
596
|
+
rangestr = "#{first}-#{last}"
|
597
|
+
end
|
598
|
+
else
|
599
|
+
raise TypeError, 'Range/Integer is required'
|
600
|
+
end
|
601
|
+
@header['range'] = ["bytes=#{rangestr}"]
|
602
|
+
r
|
603
|
+
end
|
604
|
+
|
605
|
+
alias range= set_range
|
606
|
+
|
607
|
+
# Returns the value of field <tt>'Content-Length'</tt> as an integer,
|
608
|
+
# or +nil+ if there is no such field;
|
609
|
+
# see {Content-Length request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-length-request-header]:
|
610
|
+
#
|
611
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/nosuch/1')
|
612
|
+
# res.content_length # => 2
|
613
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
614
|
+
# res.content_length # => nil
|
615
|
+
#
|
616
|
+
def content_length
|
617
|
+
return nil unless key?('Content-Length')
|
618
|
+
len = self['Content-Length'].slice(/\d+/) or
|
619
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'wrong Content-Length format'
|
620
|
+
len.to_i
|
621
|
+
end
|
622
|
+
|
623
|
+
# Sets the value of field <tt>'Content-Length'</tt> to the given numeric;
|
624
|
+
# see {Content-Length response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-length-response-header]:
|
625
|
+
#
|
626
|
+
# _uri = uri.dup
|
627
|
+
# hostname = _uri.hostname # => "jsonplaceholder.typicode.com"
|
628
|
+
# _uri.path = '/posts' # => "/posts"
|
629
|
+
# req = Gem::Net::HTTP::Post.new(_uri) # => #<Gem::Net::HTTP::Post POST>
|
630
|
+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
|
631
|
+
# req.content_length = req.body.size # => 42
|
632
|
+
# req.content_type = 'application/json'
|
633
|
+
# res = Gem::Net::HTTP.start(hostname) do |http|
|
634
|
+
# http.request(req)
|
635
|
+
# end # => #<Gem::Net::HTTPCreated 201 Created readbody=true>
|
636
|
+
#
|
637
|
+
def content_length=(len)
|
638
|
+
unless len
|
639
|
+
@header.delete 'content-length'
|
640
|
+
return nil
|
641
|
+
end
|
642
|
+
@header['content-length'] = [len.to_i.to_s]
|
643
|
+
end
|
644
|
+
|
645
|
+
# Returns +true+ if field <tt>'Transfer-Encoding'</tt>
|
646
|
+
# exists and has value <tt>'chunked'</tt>,
|
647
|
+
# +false+ otherwise;
|
648
|
+
# see {Transfer-Encoding response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#transfer-encoding-response-header]:
|
649
|
+
#
|
650
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
651
|
+
# res['Transfer-Encoding'] # => "chunked"
|
652
|
+
# res.chunked? # => true
|
653
|
+
#
|
654
|
+
def chunked?
|
655
|
+
return false unless @header['transfer-encoding']
|
656
|
+
field = self['Transfer-Encoding']
|
657
|
+
(/(?:\A|[^\-\w])chunked(?![\-\w])/i =~ field) ? true : false
|
658
|
+
end
|
659
|
+
|
660
|
+
# Returns a Range object representing the value of field
|
661
|
+
# <tt>'Content-Range'</tt>, or +nil+ if no such field exists;
|
662
|
+
# see {Content-Range response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-range-response-header]:
|
663
|
+
#
|
664
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
665
|
+
# res['Content-Range'] # => nil
|
666
|
+
# res['Content-Range'] = 'bytes 0-499/1000'
|
667
|
+
# res['Content-Range'] # => "bytes 0-499/1000"
|
668
|
+
# res.content_range # => 0..499
|
669
|
+
#
|
670
|
+
def content_range
|
671
|
+
return nil unless @header['content-range']
|
672
|
+
m = %r<\A\s*(\w+)\s+(\d+)-(\d+)/(\d+|\*)>.match(self['Content-Range']) or
|
673
|
+
raise Gem::Net::HTTPHeaderSyntaxError, 'wrong Content-Range format'
|
674
|
+
return unless m[1] == 'bytes'
|
675
|
+
m[2].to_i .. m[3].to_i
|
676
|
+
end
|
677
|
+
|
678
|
+
# Returns the integer representing length of the value of field
|
679
|
+
# <tt>'Content-Range'</tt>, or +nil+ if no such field exists;
|
680
|
+
# see {Content-Range response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-range-response-header]:
|
681
|
+
#
|
682
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
683
|
+
# res['Content-Range'] # => nil
|
684
|
+
# res['Content-Range'] = 'bytes 0-499/1000'
|
685
|
+
# res.range_length # => 500
|
686
|
+
#
|
687
|
+
def range_length
|
688
|
+
r = content_range() or return nil
|
689
|
+
r.end - r.begin + 1
|
690
|
+
end
|
691
|
+
|
692
|
+
# Returns the {media type}[https://en.wikipedia.org/wiki/Media_type]
|
693
|
+
# from the value of field <tt>'Content-Type'</tt>,
|
694
|
+
# or +nil+ if no such field exists;
|
695
|
+
# see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
|
696
|
+
#
|
697
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
698
|
+
# res['content-type'] # => "application/json; charset=utf-8"
|
699
|
+
# res.content_type # => "application/json"
|
700
|
+
#
|
701
|
+
def content_type
|
702
|
+
main = main_type()
|
703
|
+
return nil unless main
|
704
|
+
|
705
|
+
sub = sub_type()
|
706
|
+
if sub
|
707
|
+
"#{main}/#{sub}"
|
708
|
+
else
|
709
|
+
main
|
710
|
+
end
|
711
|
+
end
|
712
|
+
|
713
|
+
# Returns the leading ('type') part of the
|
714
|
+
# {media type}[https://en.wikipedia.org/wiki/Media_type]
|
715
|
+
# from the value of field <tt>'Content-Type'</tt>,
|
716
|
+
# or +nil+ if no such field exists;
|
717
|
+
# see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
|
718
|
+
#
|
719
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
720
|
+
# res['content-type'] # => "application/json; charset=utf-8"
|
721
|
+
# res.main_type # => "application"
|
722
|
+
#
|
723
|
+
def main_type
|
724
|
+
return nil unless @header['content-type']
|
725
|
+
self['Content-Type'].split(';').first.to_s.split('/')[0].to_s.strip
|
726
|
+
end
|
727
|
+
|
728
|
+
# Returns the trailing ('subtype') part of the
|
729
|
+
# {media type}[https://en.wikipedia.org/wiki/Media_type]
|
730
|
+
# from the value of field <tt>'Content-Type'</tt>,
|
731
|
+
# or +nil+ if no such field exists;
|
732
|
+
# see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
|
733
|
+
#
|
734
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
735
|
+
# res['content-type'] # => "application/json; charset=utf-8"
|
736
|
+
# res.sub_type # => "json"
|
737
|
+
#
|
738
|
+
def sub_type
|
739
|
+
return nil unless @header['content-type']
|
740
|
+
_, sub = *self['Content-Type'].split(';').first.to_s.split('/')
|
741
|
+
return nil unless sub
|
742
|
+
sub.strip
|
743
|
+
end
|
744
|
+
|
745
|
+
# Returns the trailing ('parameters') part of the value of field <tt>'Content-Type'</tt>,
|
746
|
+
# or +nil+ if no such field exists;
|
747
|
+
# see {Content-Type response header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-response-header]:
|
748
|
+
#
|
749
|
+
# res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
|
750
|
+
# res['content-type'] # => "application/json; charset=utf-8"
|
751
|
+
# res.type_params # => {"charset"=>"utf-8"}
|
752
|
+
#
|
753
|
+
def type_params
|
754
|
+
result = {}
|
755
|
+
list = self['Content-Type'].to_s.split(';')
|
756
|
+
list.shift
|
757
|
+
list.each do |param|
|
758
|
+
k, v = *param.split('=', 2)
|
759
|
+
result[k.strip] = v.strip
|
760
|
+
end
|
761
|
+
result
|
762
|
+
end
|
763
|
+
|
764
|
+
# Sets the value of field <tt>'Content-Type'</tt>;
|
765
|
+
# returns the new value;
|
766
|
+
# see {Content-Type request header}[https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#content-type-request-header]:
|
767
|
+
#
|
768
|
+
# req = Gem::Net::HTTP::Get.new(uri)
|
769
|
+
# req.set_content_type('application/json') # => ["application/json"]
|
770
|
+
#
|
771
|
+
# Gem::Net::HTTPHeader#content_type= is an alias for Gem::Net::HTTPHeader#set_content_type.
|
772
|
+
def set_content_type(type, params = {})
|
773
|
+
@header['content-type'] = [type + params.map{|k,v|"; #{k}=#{v}"}.join('')]
|
774
|
+
end
|
775
|
+
|
776
|
+
alias content_type= set_content_type
|
777
|
+
|
778
|
+
# Sets the request body to a URL-encoded string derived from argument +params+,
|
779
|
+
# and sets request header field <tt>'Content-Type'</tt>
|
780
|
+
# to <tt>'application/x-www-form-urlencoded'</tt>.
|
781
|
+
#
|
782
|
+
# The resulting request is suitable for HTTP request +POST+ or +PUT+.
|
783
|
+
#
|
784
|
+
# Argument +params+ must be suitable for use as argument +enum+ to
|
785
|
+
# {URI.encode_www_form}[https://docs.ruby-lang.org/en/master/URI.html#method-c-encode_www_form].
|
786
|
+
#
|
787
|
+
# With only argument +params+ given,
|
788
|
+
# sets the body to a URL-encoded string with the default separator <tt>'&'</tt>:
|
789
|
+
#
|
790
|
+
# req = Gem::Net::HTTP::Post.new('example.com')
|
791
|
+
#
|
792
|
+
# req.set_form_data(q: 'ruby', lang: 'en')
|
793
|
+
# req.body # => "q=ruby&lang=en"
|
794
|
+
# req['Content-Type'] # => "application/x-www-form-urlencoded"
|
795
|
+
#
|
796
|
+
# req.set_form_data([['q', 'ruby'], ['lang', 'en']])
|
797
|
+
# req.body # => "q=ruby&lang=en"
|
798
|
+
#
|
799
|
+
# req.set_form_data(q: ['ruby', 'perl'], lang: 'en')
|
800
|
+
# req.body # => "q=ruby&q=perl&lang=en"
|
801
|
+
#
|
802
|
+
# req.set_form_data([['q', 'ruby'], ['q', 'perl'], ['lang', 'en']])
|
803
|
+
# req.body # => "q=ruby&q=perl&lang=en"
|
804
|
+
#
|
805
|
+
# With string argument +sep+ also given,
|
806
|
+
# uses that string as the separator:
|
807
|
+
#
|
808
|
+
# req.set_form_data({q: 'ruby', lang: 'en'}, '|')
|
809
|
+
# req.body # => "q=ruby|lang=en"
|
810
|
+
#
|
811
|
+
# Gem::Net::HTTPHeader#form_data= is an alias for Gem::Net::HTTPHeader#set_form_data.
|
812
|
+
def set_form_data(params, sep = '&')
|
813
|
+
query = URI.encode_www_form(params)
|
814
|
+
query.gsub!(/&/, sep) if sep != '&'
|
815
|
+
self.body = query
|
816
|
+
self.content_type = 'application/x-www-form-urlencoded'
|
817
|
+
end
|
818
|
+
|
819
|
+
alias form_data= set_form_data
|
820
|
+
|
821
|
+
# Stores form data to be used in a +POST+ or +PUT+ request.
|
822
|
+
#
|
823
|
+
# The form data given in +params+ consists of zero or more fields;
|
824
|
+
# each field is:
|
825
|
+
#
|
826
|
+
# - A scalar value.
|
827
|
+
# - A name/value pair.
|
828
|
+
# - An IO stream opened for reading.
|
829
|
+
#
|
830
|
+
# Argument +params+ should be an
|
831
|
+
# {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Enumerable+in+Ruby+Classes]
|
832
|
+
# (method <tt>params.map</tt> will be called),
|
833
|
+
# and is often an array or hash.
|
834
|
+
#
|
835
|
+
# First, we set up a request:
|
836
|
+
#
|
837
|
+
# _uri = uri.dup
|
838
|
+
# _uri.path ='/posts'
|
839
|
+
# req = Gem::Net::HTTP::Post.new(_uri)
|
840
|
+
#
|
841
|
+
# <b>Argument +params+ As an Array</b>
|
842
|
+
#
|
843
|
+
# When +params+ is an array,
|
844
|
+
# each of its elements is a subarray that defines a field;
|
845
|
+
# the subarray may contain:
|
846
|
+
#
|
847
|
+
# - One string:
|
848
|
+
#
|
849
|
+
# req.set_form([['foo'], ['bar'], ['baz']])
|
850
|
+
#
|
851
|
+
# - Two strings:
|
852
|
+
#
|
853
|
+
# req.set_form([%w[foo 0], %w[bar 1], %w[baz 2]])
|
854
|
+
#
|
855
|
+
# - When argument +enctype+ (see below) is given as
|
856
|
+
# <tt>'multipart/form-data'</tt>:
|
857
|
+
#
|
858
|
+
# - A string name and an IO stream opened for reading:
|
859
|
+
#
|
860
|
+
# require 'stringio'
|
861
|
+
# req.set_form([['file', StringIO.new('Ruby is cool.')]])
|
862
|
+
#
|
863
|
+
# - A string name, an IO stream opened for reading,
|
864
|
+
# and an options hash, which may contain these entries:
|
865
|
+
#
|
866
|
+
# - +:filename+: The name of the file to use.
|
867
|
+
# - +:content_type+: The content type of the uploaded file.
|
868
|
+
#
|
869
|
+
# Example:
|
870
|
+
#
|
871
|
+
# req.set_form([['file', file, {filename: "other-filename.foo"}]]
|
872
|
+
#
|
873
|
+
# The various forms may be mixed:
|
874
|
+
#
|
875
|
+
# req.set_form(['foo', %w[bar 1], ['file', file]])
|
876
|
+
#
|
877
|
+
# <b>Argument +params+ As a Hash</b>
|
878
|
+
#
|
879
|
+
# When +params+ is a hash,
|
880
|
+
# each of its entries is a name/value pair that defines a field:
|
881
|
+
#
|
882
|
+
# - The name is a string.
|
883
|
+
# - The value may be:
|
884
|
+
#
|
885
|
+
# - +nil+.
|
886
|
+
# - Another string.
|
887
|
+
# - An IO stream opened for reading
|
888
|
+
# (only when argument +enctype+ -- see below -- is given as
|
889
|
+
# <tt>'multipart/form-data'</tt>).
|
890
|
+
#
|
891
|
+
# Examples:
|
892
|
+
#
|
893
|
+
# # Nil-valued fields.
|
894
|
+
# req.set_form({'foo' => nil, 'bar' => nil, 'baz' => nil})
|
895
|
+
#
|
896
|
+
# # String-valued fields.
|
897
|
+
# req.set_form({'foo' => 0, 'bar' => 1, 'baz' => 2})
|
898
|
+
#
|
899
|
+
# # IO-valued field.
|
900
|
+
# require 'stringio'
|
901
|
+
# req.set_form({'file' => StringIO.new('Ruby is cool.')})
|
902
|
+
#
|
903
|
+
# # Mixture of fields.
|
904
|
+
# req.set_form({'foo' => nil, 'bar' => 1, 'file' => file})
|
905
|
+
#
|
906
|
+
# Optional argument +enctype+ specifies the value to be given
|
907
|
+
# to field <tt>'Content-Type'</tt>, and must be one of:
|
908
|
+
#
|
909
|
+
# - <tt>'application/x-www-form-urlencoded'</tt> (the default).
|
910
|
+
# - <tt>'multipart/form-data'</tt>;
|
911
|
+
# see {RFC 7578}[https://www.rfc-editor.org/rfc/rfc7578].
|
912
|
+
#
|
913
|
+
# Optional argument +formopt+ is a hash of options
|
914
|
+
# (applicable only when argument +enctype+
|
915
|
+
# is <tt>'multipart/form-data'</tt>)
|
916
|
+
# that may include the following entries:
|
917
|
+
#
|
918
|
+
# - +:boundary+: The value is the boundary string for the multipart message.
|
919
|
+
# If not given, the boundary is a random string.
|
920
|
+
# See {Boundary}[https://www.rfc-editor.org/rfc/rfc7578#section-4.1].
|
921
|
+
# - +:charset+: Value is the character set for the form submission.
|
922
|
+
# Field names and values of non-file fields should be encoded with this charset.
|
923
|
+
#
|
924
|
+
def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
|
925
|
+
@body_data = params
|
926
|
+
@body = nil
|
927
|
+
@body_stream = nil
|
928
|
+
@form_option = formopt
|
929
|
+
case enctype
|
930
|
+
when /\Aapplication\/x-www-form-urlencoded\z/i,
|
931
|
+
/\Amultipart\/form-data\z/i
|
932
|
+
self.content_type = enctype
|
933
|
+
else
|
934
|
+
raise ArgumentError, "invalid enctype: #{enctype}"
|
935
|
+
end
|
936
|
+
end
|
937
|
+
|
938
|
+
# Sets header <tt>'Authorization'</tt> using the given
|
939
|
+
# +account+ and +password+ strings:
|
940
|
+
#
|
941
|
+
# req.basic_auth('my_account', 'my_password')
|
942
|
+
# req['Authorization']
|
943
|
+
# # => "Basic bXlfYWNjb3VudDpteV9wYXNzd29yZA=="
|
944
|
+
#
|
945
|
+
def basic_auth(account, password)
|
946
|
+
@header['authorization'] = [basic_encode(account, password)]
|
947
|
+
end
|
948
|
+
|
949
|
+
# Sets header <tt>'Proxy-Authorization'</tt> using the given
|
950
|
+
# +account+ and +password+ strings:
|
951
|
+
#
|
952
|
+
# req.proxy_basic_auth('my_account', 'my_password')
|
953
|
+
# req['Proxy-Authorization']
|
954
|
+
# # => "Basic bXlfYWNjb3VudDpteV9wYXNzd29yZA=="
|
955
|
+
#
|
956
|
+
def proxy_basic_auth(account, password)
|
957
|
+
@header['proxy-authorization'] = [basic_encode(account, password)]
|
958
|
+
end
|
959
|
+
|
960
|
+
def basic_encode(account, password)
|
961
|
+
'Basic ' + ["#{account}:#{password}"].pack('m0')
|
962
|
+
end
|
963
|
+
private :basic_encode
|
964
|
+
|
965
|
+
# Returns whether the HTTP session is to be closed.
|
966
|
+
def connection_close?
|
967
|
+
token = /(?:\A|,)\s*close\s*(?:\z|,)/i
|
968
|
+
@header['connection']&.grep(token) {return true}
|
969
|
+
@header['proxy-connection']&.grep(token) {return true}
|
970
|
+
false
|
971
|
+
end
|
972
|
+
|
973
|
+
# Returns whether the HTTP session is to be kept alive.
|
974
|
+
def connection_keep_alive?
|
975
|
+
token = /(?:\A|,)\s*keep-alive\s*(?:\z|,)/i
|
976
|
+
@header['connection']&.grep(token) {return true}
|
977
|
+
@header['proxy-connection']&.grep(token) {return true}
|
978
|
+
false
|
979
|
+
end
|
980
|
+
|
981
|
+
end
|