# frozen_string_literal: truerequire"base64"require"http/headers"moduleHTTPmoduleChainable# Request a get sans response body# @param uri# @option options [Hash]defhead(uri,options={})# rubocop:disable Style/OptionHashrequest:head,uri,optionsend# Get a resource# @param uri# @option options [Hash]defget(uri,options={})# rubocop:disable Style/OptionHashrequest:get,uri,optionsend# Post to a resource# @param uri# @option options [Hash]defpost(uri,options={})# rubocop:disable Style/OptionHashrequest:post,uri,optionsend# Put to a resource# @param uri# @option options [Hash]defput(uri,options={})# rubocop:disable Style/OptionHashrequest:put,uri,optionsend# Delete a resource# @param uri# @option options [Hash]defdelete(uri,options={})# rubocop:disable Style/OptionHashrequest:delete,uri,optionsend# Echo the request back to the client# @param uri# @option options [Hash]deftrace(uri,options={})# rubocop:disable Style/OptionHashrequest:trace,uri,optionsend# Return the methods supported on the given URI# @param uri# @option options [Hash]defoptions(uri,options={})# rubocop:disable Style/OptionHashrequest:options,uri,optionsend# Convert to a transparent TCP/IP tunnel# @param uri# @option options [Hash]defconnect(uri,options={})# rubocop:disable Style/OptionHashrequest:connect,uri,optionsend# Apply partial modifications to a resource# @param uri# @option options [Hash]defpatch(uri,options={})# rubocop:disable Style/OptionHashrequest:patch,uri,optionsend# Make an HTTP request with the given verb# @param uri# @option options [Hash]defrequest(verb,uri,options={})# rubocop:disable Style/OptionHashbranch(options).requestverb,uriend# @overload timeout(options = {})# Syntax sugar for `timeout(:per_operation, options)`# @overload timeout(klass, options = {})# Adds a timeout to the request.# @param [#to_sym] klass# either :null, :global, or :per_operation# @param [Hash] options# @option options [Float] :read Read timeout# @option options [Float] :write Write timeout# @option options [Float] :connect Connect timeoutdeftimeout(klass,options={})# rubocop:disable Style/OptionHashifklass.is_a?Hashoptions=klassklass=:per_operationendklass=caseklass.to_symwhen:nullthenHTTP::Timeout::Nullwhen:globalthenHTTP::Timeout::Globalwhen:per_operationthenHTTP::Timeout::PerOperationelseraiseArgumentError,"Unsupported Timeout class: #{klass}"end%i[read write connect].eachdo|k|nextunlessoptions.key?koptions["#{k}_timeout".to_sym]=options.deletekendbranchdefault_options.merge(:timeout_class=>klass,:timeout_options=>options)end# @overload persistent(host, timeout: 5)# Flags as persistent# @param [String] host# @option [Integer] timeout Keep alive timeout# @raise [Request::Error] if Host is invalid# @return [HTTP::Client] Persistent client# @overload persistent(host, timeout: 5, &block)# Executes given block with persistent client and automatically closes# connection at the end of execution.## @example## def keys(users)# HTTP.persistent("https://github.com") do |http|# users.map { |u| http.get("/#{u}.keys").to_s }# end# end## # same as## def keys(users)# http = HTTP.persistent "https://github.com"# users.map { |u| http.get("/#{u}.keys").to_s }# ensure# http.close if http# end### @yieldparam [HTTP::Client] client Persistent client# @return [Object] result of last expression in the blockdefpersistent(host,timeout: 5)options={:keep_alive_timeout=>timeout}p_client=branchdefault_options.merge(options).with_persistenthostreturnp_clientunlessblock_given?yieldp_clientensurep_client.closeifp_clientend# Make a request through an HTTP proxy# @param [Array] proxy# @raise [Request::Error] if HTTP proxy is invaliddefvia(*proxy)proxy_hash={}proxy_hash[:proxy_address]=proxy[0]ifproxy[0].is_a?(String)proxy_hash[:proxy_port]=proxy[1]ifproxy[1].is_a?(Integer)proxy_hash[:proxy_username]=proxy[2]ifproxy[2].is_a?(String)proxy_hash[:proxy_password]=proxy[3]ifproxy[3].is_a?(String)proxy_hash[:proxy_headers]=proxy[2]ifproxy[2].is_a?(Hash)proxy_hash[:proxy_headers]=proxy[4]ifproxy[4].is_a?(Hash)raise(RequestError,"invalid HTTP proxy: #{proxy_hash}")unless(2..5).cover?(proxy_hash.keys.size)branchdefault_options.with_proxy(proxy_hash)endaliasthroughvia# Make client follow redirects.# @param opts# @return [HTTP::Client]# @see Redirector#initializedeffollow(options={})# rubocop:disable Style/OptionHashbranchdefault_options.with_followoptionsend# Make a request with the given headers# @param headersdefheaders(headers)branchdefault_options.with_headers(headers)end# Make a request with the given cookiesdefcookies(cookies)branchdefault_options.with_cookies(cookies)end# Force a specific encoding for response bodydefencoding(encoding)branchdefault_options.with_encoding(encoding)end# Accept the given MIME type(s)# @param typedefaccept(type)headersHeaders::ACCEPT=>MimeType.normalize(type)end# Make a request with the given Authorization header# @param [#to_s] value Authorization header valuedefauth(value)headersHeaders::AUTHORIZATION=>value.to_send# Make a request with the given Basic authorization header# @see http://tools.ietf.org/html/rfc2617# @param [#fetch] opts# @option opts [#to_s] :user# @option opts [#to_s] :passdefbasic_auth(opts)user=opts.fetch:userpass=opts.fetch:passauth("Basic "+Base64.strict_encode64("#{user}:#{pass}"))end# Get options for HTTP# @return [HTTP::Options]defdefault_options@default_options||=HTTP::Options.newend# Set options for HTTP# @param opts# @return [HTTP::Options]defdefault_options=(opts)@default_options=HTTP::Options.new(opts)end# Set TCP_NODELAY on the socketdefnodelaybranchdefault_options.with_nodelay(true)end# Turn on given features. Available features are:# * auto_inflate# * auto_deflate# @param featuresdefuse(*features)branchdefault_options.with_features(features)endprivate# :nodoc:defbranch(options)HTTP::Client.new(options)endendend