faraday-net-http-persistent-2.1.0/0000755000175100017510000000000014444537251016117 5ustar pravipravifaraday-net-http-persistent-2.1.0/faraday-net_http_persistent.gemspec0000644000175100017510000000350714444537251025203 0ustar pravipravi######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: faraday-net_http_persistent 2.1.0 ruby lib Gem::Specification.new do |s| s.name = "faraday-net_http_persistent".freeze s.version = "2.1.0" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "changelog_uri" => "https://github.com/lostisland/faraday-net_http_persistent/releases/tag/v2.1.0", "homepage_uri" => "https://github.com/lostisland/faraday-net_http_persistent", "source_code_uri" => "https://github.com/lostisland/faraday-net_http_persistent" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] s.authors = ["Mike Rogers".freeze] s.date = "2022-08-11" s.description = "Faraday adapter for NetHttpPersistent".freeze s.email = ["me@mikerogers.io".freeze] s.files = ["LICENSE.md".freeze, "README.md".freeze, "lib/faraday/adapter/net_http_persistent.rb".freeze, "lib/faraday/net_http_persistent.rb".freeze, "lib/faraday/net_http_persistent/version.rb".freeze] s.homepage = "https://github.com/lostisland/faraday-net_http_persistent".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 2.6.0".freeze) s.rubygems_version = "3.3.15".freeze s.summary = "Faraday adapter for NetHttpPersistent".freeze if s.respond_to? :specification_version then s.specification_version = 4 end if s.respond_to? :add_runtime_dependency then s.add_runtime_dependency(%q.freeze, ["~> 2.5"]) s.add_runtime_dependency(%q.freeze, ["~> 4.0"]) else s.add_dependency(%q.freeze, ["~> 2.5"]) s.add_dependency(%q.freeze, ["~> 4.0"]) end end faraday-net-http-persistent-2.1.0/LICENSE.md0000644000175100017510000000207214444537251017524 0ustar pravipraviThe MIT License (MIT) Copyright (c) 2020 Jan van der Pas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. faraday-net-http-persistent-2.1.0/lib/0000755000175100017510000000000014444537251016665 5ustar pravipravifaraday-net-http-persistent-2.1.0/lib/faraday/0000755000175100017510000000000014444537251020274 5ustar pravipravifaraday-net-http-persistent-2.1.0/lib/faraday/net_http_persistent.rb0000644000175100017510000000145014444537251024726 0ustar pravipravi# frozen_string_literal: true require "faraday" require "faraday/adapter/net_http_persistent" require "faraday/net_http_persistent/version" module Faraday module NetHttpPersistent # Faraday allows you to register your middleware for easier configuration. # This step is totally optional, but it basically allows users to use a custom symbol (in this case, `:net_http_persistent`), # to use your adapter in their connections. # After calling this line, the following are both valid ways to set the adapter in a connection: # * conn.adapter Faraday::Adapter::NetNttpPersistent # * conn.adapter :net_http_persistent # Without this line, only the former method is valid. Faraday::Adapter.register_middleware(net_http_persistent: Faraday::Adapter::NetHttpPersistent) end end faraday-net-http-persistent-2.1.0/lib/faraday/adapter/0000755000175100017510000000000014444537251021714 5ustar pravipravifaraday-net-http-persistent-2.1.0/lib/faraday/adapter/net_http_persistent.rb0000644000175100017510000001471514444537251026356 0ustar pravipravi# frozen_string_literal: true require "net/http/persistent" module Faraday class Adapter # Net::HTTP::Persistent adapter. class NetHttpPersistent < Faraday::Adapter exceptions = [ IOError, Errno::EADDRNOTAVAIL, Errno::EALREADY, Errno::ECONNABORTED, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::EINVAL, Errno::ENETUNREACH, Errno::EPIPE, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError, Zlib::GzipFile::Error ] exceptions << ::OpenSSL::SSL::SSLError if defined?(::OpenSSL::SSL::SSLError) # TODO (breaking): Enable this to make it consistent with net_http adapter. # See https://github.com/lostisland/faraday/issues/718#issuecomment-344549382 # exceptions << ::Net::OpenTimeout if defined?(::Net::OpenTimeout) NET_HTTP_EXCEPTIONS = exceptions.freeze def initialize(app = nil, opts = {}, &block) @ssl_cert_store = nil super(app, opts, &block) end def call(env) super connection(env) do |http| perform_request(http, env) rescue Net::HTTP::Persistent::Error => e raise Faraday::TimeoutError, e if e.message.include?("Timeout") raise Faraday::ConnectionFailed, e if e.message.include?("connection refused") raise rescue *NET_HTTP_EXCEPTIONS => e raise Faraday::SSLError, e if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError) raise Faraday::ConnectionFailed, e end @app.call env rescue Timeout::Error, Errno::ETIMEDOUT => e raise Faraday::TimeoutError, e end private def build_connection(env) net_http_connection(env).tap do |http| http.use_ssl = env[:url].scheme == "https" if http.respond_to?(:use_ssl=) configure_ssl(http, env[:ssl]) configure_request(http, env[:request]) end end def create_request(env) request = Net::HTTPGenericRequest.new \ env[:method].to_s.upcase, # request method !!env[:body], # is there request body env[:method] != :head, # is there response body env[:url].request_uri, # request uri path env[:request_headers] # request headers if env[:body].respond_to?(:read) request.body_stream = env[:body] else request.body = env[:body] end request end def save_http_response(env, http_response) save_response( env, http_response.code.to_i, nil, nil, http_response.message, finished: false ) do |response_headers| http_response.each_header do |key, value| response_headers[key] = value end end end def configure_request(http, req) if (sec = request_timeout(:read, req)) http.read_timeout = sec end if (sec = http.respond_to?(:write_timeout=) && request_timeout(:write, req)) http.write_timeout = sec end if (sec = request_timeout(:open, req)) http.open_timeout = sec end # Only set if Net::Http supports it, since Ruby 2.5. http.max_retries = 0 if http.respond_to?(:max_retries=) @config_block&.call(http) end def ssl_cert_store(ssl) return ssl[:cert_store] if ssl[:cert_store] # Use the default cert store by default, i.e. system ca certs @ssl_cert_store ||= OpenSSL::X509::Store.new.tap(&:set_default_paths) end def net_http_connection(env) @cached_connection ||= Net::HTTP::Persistent.new(**init_options) proxy_uri = proxy_uri(env) @cached_connection.proxy = proxy_uri if @cached_connection.proxy_uri != proxy_uri @cached_connection end def init_options options = {name: "Faraday"} options[:pool_size] = @connection_options[:pool_size] if @connection_options.key?(:pool_size) options end def proxy_uri(env) proxy_uri = nil if (proxy = env[:request][:proxy]) proxy_uri = if proxy[:uri].is_a?(::URI::HTTP) proxy[:uri].dup else ::URI.parse(proxy[:uri].to_s) end proxy_uri.user = proxy[:user] if proxy[:user] proxy_uri.password = proxy[:password] if proxy[:password] end proxy_uri end def perform_request(http, env) if env.stream_response? http_response = env.stream_response do |&on_data| request_with_wrapped_block(http, env, &on_data) end http_response.body = nil else http_response = request_with_wrapped_block(http, env) end env.response_body = encoded_body(http_response) env.response.finish(env) http_response end def request_with_wrapped_block(http, env) http.request(env[:url], create_request(env)) do |response| save_http_response(env, response) if block_given? response.read_body do |chunk| yield(chunk) end end end end SSL_CONFIGURATIONS = { certificate: :client_cert, private_key: :client_key, ca_file: :ca_file, ssl_version: :version, min_version: :min_version, max_version: :max_version }.freeze def configure_ssl(http, ssl) return unless ssl http_set(http, :verify_mode, ssl_verify_mode(ssl)) http_set(http, :cert_store, ssl_cert_store(ssl)) SSL_CONFIGURATIONS .select { |_, key| ssl[key] } .each { |target, key| http_set(http, target, ssl[key]) } end def http_set(http, attr, value) http.send("#{attr}=", value) if http.send(attr) != value end def ssl_verify_mode(ssl) ssl[:verify_mode] || if ssl.fetch(:verify, true) OpenSSL::SSL::VERIFY_PEER else OpenSSL::SSL::VERIFY_NONE end end def encoded_body(http_response) body = http_response.body || +"" /\bcharset=\s*(.+?)\s*(;|$)/.match(http_response["Content-Type"]) do |match| content_charset = ::Encoding.find(match.captures.first) body = body.dup if body.frozen? body.force_encoding(content_charset) rescue ArgumentError nil end body end end end end faraday-net-http-persistent-2.1.0/lib/faraday/net_http_persistent/0000755000175100017510000000000014444537251024401 5ustar pravipravifaraday-net-http-persistent-2.1.0/lib/faraday/net_http_persistent/version.rb0000644000175100017510000000015114444537251026410 0ustar pravipravi# frozen_string_literal: true module Faraday module NetHttpPersistent VERSION = "2.1.0" end end faraday-net-http-persistent-2.1.0/README.md0000644000175100017510000000463514444537251017406 0ustar pravipravi# Faraday::NetHttpPersistent [![Gem Version](https://badge.fury.io/rb/faraday-net_http_persistent.svg)](https://rubygems.org/gems/faraday-net_http_persistent) [![GitHub Actions CI](https://github.com/lostisland/faraday-net_http_persistent/workflows/CI/badge.svg)](https://github.com/lostisland/faraday-net_http_persistent/actions?query=workflow%3ACI) This gem is a [Faraday][faraday] adapter for the [Net::HTTP::Persistent gem][net-http-persistent]. ## Installation Add this to your application's Gemfile: ```ruby gem 'faraday-net_http_persistent', '~> 2.0' ``` And then execute: $ bundle ## Usage ```ruby require 'faraday/net_http_persistent' conn = Faraday.new(...) do |f| f.adapter :net_http_persistent, pool_size: 5 do |http| # yields Net::HTTP::Persistent http.idle_timeout = 100 end end ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org]. ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/lostisland/faraday-net_http_persistent. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant][covenant] code of conduct. ## License The gem is available as open source under the terms of the [MIT License][mit-license]. ## Code of Conduct This project is intended to be a safe, welcoming space for collaboration. Everyone interacting in the Faraday::Http project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct][code-of-conduct]. [code-of-conduct]: https://github.com/lostisland/faraday-http/blob/main/CODE_OF_CONDUCT.md [covenant]: http://contributor-covenant.org [faraday]: https://github.com/lostisland/faraday [faraday-website]: https://lostisland.github.io/faraday [net-http-persistent]: https://github.com/drbrain/net-http-persistent [mit-license]: https://opensource.org/licenses/MIT [rubygems.org]: https://rubygems.org