faraday 0.15.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +380 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +17 -345
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +118 -69
  9. data/lib/faraday/adapter/typhoeus.rb +4 -1
  10. data/lib/faraday/adapter.rb +72 -22
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +343 -185
  14. data/lib/faraday/dependency_loader.rb +37 -0
  15. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  16. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  17. data/lib/faraday/error.rb +118 -38
  18. data/lib/faraday/logging/formatter.rb +105 -0
  19. data/lib/faraday/methods.rb +6 -0
  20. data/lib/faraday/middleware.rb +19 -25
  21. data/lib/faraday/middleware_registry.rb +129 -0
  22. data/lib/faraday/options/connection_options.rb +22 -0
  23. data/lib/faraday/options/env.rb +181 -0
  24. data/lib/faraday/options/proxy_options.rb +32 -0
  25. data/lib/faraday/options/request_options.rb +22 -0
  26. data/lib/faraday/options/ssl_options.rb +59 -0
  27. data/lib/faraday/options.rb +39 -193
  28. data/lib/faraday/parameters.rb +4 -196
  29. data/lib/faraday/rack_builder.rb +77 -65
  30. data/lib/faraday/request/authorization.rb +51 -30
  31. data/lib/faraday/request/basic_authentication.rb +14 -7
  32. data/lib/faraday/request/instrumentation.rb +45 -27
  33. data/lib/faraday/request/json.rb +55 -0
  34. data/lib/faraday/request/token_authentication.rb +15 -10
  35. data/lib/faraday/request/url_encoded.rb +43 -23
  36. data/lib/faraday/request.rb +93 -32
  37. data/lib/faraday/response/json.rb +54 -0
  38. data/lib/faraday/response/logger.rb +20 -69
  39. data/lib/faraday/response/raise_error.rb +49 -14
  40. data/lib/faraday/response.rb +29 -23
  41. data/lib/faraday/utils/headers.rb +139 -0
  42. data/lib/faraday/utils/params_hash.rb +61 -0
  43. data/lib/faraday/utils.rb +38 -247
  44. data/lib/faraday/version.rb +5 -0
  45. data/lib/faraday.rb +134 -189
  46. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  47. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  48. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  49. data/spec/faraday/adapter/excon_spec.rb +49 -0
  50. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  51. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  52. data/spec/faraday/adapter/patron_spec.rb +18 -0
  53. data/spec/faraday/adapter/rack_spec.rb +8 -0
  54. data/spec/faraday/adapter/test_spec.rb +377 -0
  55. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  56. data/spec/faraday/adapter_registry_spec.rb +28 -0
  57. data/spec/faraday/adapter_spec.rb +55 -0
  58. data/spec/faraday/composite_read_io_spec.rb +80 -0
  59. data/spec/faraday/connection_spec.rb +736 -0
  60. data/spec/faraday/error_spec.rb +60 -0
  61. data/spec/faraday/middleware_spec.rb +52 -0
  62. data/spec/faraday/options/env_spec.rb +70 -0
  63. data/spec/faraday/options/options_spec.rb +297 -0
  64. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  65. data/spec/faraday/options/request_options_spec.rb +19 -0
  66. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  67. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  68. data/spec/faraday/rack_builder_spec.rb +345 -0
  69. data/spec/faraday/request/authorization_spec.rb +96 -0
  70. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  71. data/spec/faraday/request/json_spec.rb +111 -0
  72. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  73. data/spec/faraday/request_spec.rb +120 -0
  74. data/spec/faraday/response/json_spec.rb +119 -0
  75. data/spec/faraday/response/logger_spec.rb +220 -0
  76. data/spec/faraday/response/middleware_spec.rb +68 -0
  77. data/spec/faraday/response/raise_error_spec.rb +169 -0
  78. data/spec/faraday/response_spec.rb +75 -0
  79. data/spec/faraday/utils/headers_spec.rb +82 -0
  80. data/spec/faraday/utils_spec.rb +56 -0
  81. data/spec/faraday_spec.rb +37 -0
  82. data/spec/spec_helper.rb +132 -0
  83. data/spec/support/disabling_stub.rb +14 -0
  84. data/spec/support/fake_safe_buffer.rb +15 -0
  85. data/spec/support/helper_methods.rb +133 -0
  86. data/spec/support/shared_examples/adapter.rb +105 -0
  87. data/spec/support/shared_examples/params_encoder.rb +18 -0
  88. data/spec/support/shared_examples/request_method.rb +262 -0
  89. data/spec/support/streaming_response_checker.rb +35 -0
  90. data/spec/support/webmock_rack_app.rb +68 -0
  91. metadata +222 -29
  92. data/lib/faraday/adapter/em_http.rb +0 -243
  93. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  94. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  95. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  96. data/lib/faraday/adapter/excon.rb +0 -79
  97. data/lib/faraday/adapter/httpclient.rb +0 -128
  98. data/lib/faraday/adapter/net_http.rb +0 -137
  99. data/lib/faraday/adapter/net_http_persistent.rb +0 -63
  100. data/lib/faraday/adapter/patron.rb +0 -100
  101. data/lib/faraday/adapter/rack.rb +0 -58
  102. data/lib/faraday/request/multipart.rb +0 -68
  103. data/lib/faraday/request/retry.rb +0 -211
  104. data/lib/faraday/upload_io.rb +0 -67
@@ -1,106 +0,0 @@
1
- require 'uri'
2
-
3
- module Faraday
4
- class Adapter
5
- class EMSynchrony < Faraday::Adapter
6
- include EMHttp::Options
7
-
8
- dependency do
9
- require 'em-synchrony/em-http'
10
- require 'em-synchrony/em-multi'
11
- require 'fiber'
12
- end
13
-
14
- self.supports_parallel = true
15
-
16
- def self.setup_parallel_manager(options = {})
17
- ParallelManager.new
18
- end
19
-
20
- def call(env)
21
- super
22
- request = create_request(env)
23
-
24
- http_method = env[:method].to_s.downcase.to_sym
25
-
26
- # Queue requests for parallel execution.
27
- if env[:parallel_manager]
28
- env[:parallel_manager].add(request, http_method, request_config(env)) do |resp|
29
- save_response(env, resp.response_header.status, resp.response) do |resp_headers|
30
- resp.response_header.each do |name, value|
31
- resp_headers[name.to_sym] = value
32
- end
33
- end
34
-
35
- # Finalize the response object with values from `env`.
36
- env[:response].finish(env)
37
- end
38
-
39
- # Execute single request.
40
- else
41
- client = nil
42
- block = lambda { request.send(http_method, request_config(env)) }
43
-
44
- if !EM.reactor_running?
45
- EM.run do
46
- Fiber.new {
47
- client = block.call
48
- EM.stop
49
- }.resume
50
- end
51
- else
52
- client = block.call
53
- end
54
-
55
- raise client.error if client.error
56
-
57
- status = client.response_header.status
58
- reason = client.response_header.http_reason
59
- save_response(env, status, client.response, nil, reason) do |resp_headers|
60
- client.response_header.each do |name, value|
61
- resp_headers[name.to_sym] = value
62
- end
63
- end
64
- end
65
-
66
- @app.call env
67
- rescue Errno::ECONNREFUSED
68
- raise Error::ConnectionFailed, $!
69
- rescue EventMachine::Connectify::CONNECTError => err
70
- if err.message.include?("Proxy Authentication Required")
71
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
72
- else
73
- raise Error::ConnectionFailed, err
74
- end
75
- rescue Errno::ETIMEDOUT => err
76
- raise Error::TimeoutError, err
77
- rescue RuntimeError => err
78
- if err.message == "connection closed by server"
79
- raise Error::ConnectionFailed, err
80
- else
81
- raise
82
- end
83
- rescue => err
84
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
85
- raise Faraday::SSLError, err
86
- else
87
- raise
88
- end
89
- end
90
-
91
- def create_request(env)
92
- EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
93
- end
94
- end
95
- end
96
- end
97
-
98
- require 'faraday/adapter/em_synchrony/parallel_manager'
99
-
100
- begin
101
- require 'openssl'
102
- rescue LoadError
103
- warn "Warning: no such file to load -- openssl. Make sure it is installed if you want HTTPS support"
104
- else
105
- require 'faraday/adapter/em_http_ssl_patch'
106
- end if Faraday::Adapter::EMSynchrony.loaded?
@@ -1,79 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class Excon < Faraday::Adapter
4
- dependency 'excon'
5
-
6
- def call(env)
7
- super
8
-
9
- opts = {}
10
- if env[:url].scheme == 'https' && ssl = env[:ssl]
11
- opts[:ssl_verify_peer] = !!ssl.fetch(:verify, true)
12
- opts[:ssl_ca_path] = ssl[:ca_path] if ssl[:ca_path]
13
- opts[:ssl_ca_file] = ssl[:ca_file] if ssl[:ca_file]
14
- opts[:client_cert] = ssl[:client_cert] if ssl[:client_cert]
15
- opts[:client_key] = ssl[:client_key] if ssl[:client_key]
16
- opts[:certificate] = ssl[:certificate] if ssl[:certificate]
17
- opts[:private_key] = ssl[:private_key] if ssl[:private_key]
18
-
19
- # https://github.com/geemus/excon/issues/106
20
- # https://github.com/jruby/jruby-ossl/issues/19
21
- opts[:nonblock] = false
22
- end
23
-
24
- if ( req = env[:request] )
25
- if req[:timeout]
26
- opts[:read_timeout] = req[:timeout]
27
- opts[:connect_timeout] = req[:timeout]
28
- opts[:write_timeout] = req[:timeout]
29
- end
30
-
31
- if req[:open_timeout]
32
- opts[:connect_timeout] = req[:open_timeout]
33
- end
34
-
35
- if req[:proxy]
36
- opts[:proxy] = {
37
- :host => req[:proxy][:uri].host,
38
- :hostname => req[:proxy][:uri].hostname,
39
- :port => req[:proxy][:uri].port,
40
- :scheme => req[:proxy][:uri].scheme,
41
- :user => req[:proxy][:user],
42
- :password => req[:proxy][:password]
43
- }
44
- end
45
- end
46
-
47
- conn = create_connection(env, opts)
48
-
49
- resp = conn.request \
50
- :method => env[:method].to_s.upcase,
51
- :headers => env[:request_headers],
52
- :body => read_body(env)
53
-
54
- save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
55
-
56
- @app.call env
57
- rescue ::Excon::Errors::SocketError => err
58
- if err.message =~ /\btimeout\b/
59
- raise Error::TimeoutError, err
60
- elsif err.message =~ /\bcertificate\b/
61
- raise Faraday::SSLError, err
62
- else
63
- raise Error::ConnectionFailed, err
64
- end
65
- rescue ::Excon::Errors::Timeout => err
66
- raise Error::TimeoutError, err
67
- end
68
-
69
- def create_connection(env, opts)
70
- ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
71
- end
72
-
73
- # TODO: support streaming requests
74
- def read_body(env)
75
- env[:body].respond_to?(:read) ? env[:body].read : env[:body]
76
- end
77
- end
78
- end
79
- end
@@ -1,128 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class HTTPClient < Faraday::Adapter
4
- dependency 'httpclient'
5
-
6
- def client
7
- @client ||= ::HTTPClient.new
8
- end
9
-
10
- def call(env)
11
- super
12
-
13
- # enable compression
14
- client.transparent_gzip_decompression = true
15
-
16
- if req = env[:request]
17
- if proxy = req[:proxy]
18
- configure_proxy proxy
19
- end
20
-
21
- if bind = req[:bind]
22
- configure_socket bind
23
- end
24
-
25
- configure_timeouts req
26
- end
27
-
28
- if env[:url].scheme == 'https' && ssl = env[:ssl]
29
- configure_ssl ssl
30
- end
31
-
32
- configure_client
33
-
34
- # TODO Don't stream yet.
35
- # https://github.com/nahi/httpclient/pull/90
36
- env[:body] = env[:body].read if env[:body].respond_to? :read
37
-
38
- resp = client.request env[:method], env[:url],
39
- :body => env[:body],
40
- :header => env[:request_headers]
41
-
42
- save_response env, resp.status, resp.body, resp.headers, resp.reason
43
-
44
- @app.call env
45
- rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
46
- raise Faraday::Error::TimeoutError, $!
47
- rescue ::HTTPClient::BadResponseError => err
48
- if err.message.include?('status 407')
49
- raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
50
- else
51
- raise Faraday::Error::ClientError, $!
52
- end
53
- rescue Errno::ECONNREFUSED, IOError, SocketError
54
- raise Faraday::Error::ConnectionFailed, $!
55
- rescue => err
56
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
57
- raise Faraday::SSLError, err
58
- else
59
- raise
60
- end
61
- end
62
-
63
- def configure_socket(bind)
64
- client.socket_local.host = bind[:host]
65
- client.socket_local.port = bind[:port]
66
- end
67
-
68
- def configure_proxy(proxy)
69
- client.proxy = proxy[:uri]
70
- if proxy[:user] && proxy[:password]
71
- client.set_proxy_auth proxy[:user], proxy[:password]
72
- end
73
- end
74
-
75
- def configure_ssl(ssl)
76
- ssl_config = client.ssl_config
77
- ssl_config.verify_mode = ssl_verify_mode(ssl)
78
- ssl_config.cert_store = ssl_cert_store(ssl)
79
-
80
- ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
81
- ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
82
- ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
83
- ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
84
- ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
85
- end
86
-
87
- def configure_timeouts(req)
88
- if req[:timeout]
89
- client.connect_timeout = req[:timeout]
90
- client.receive_timeout = req[:timeout]
91
- client.send_timeout = req[:timeout]
92
- end
93
-
94
- if req[:open_timeout]
95
- client.connect_timeout = req[:open_timeout]
96
- client.send_timeout = req[:open_timeout]
97
- end
98
- end
99
-
100
- def configure_client
101
- @config_block.call(client) if @config_block
102
- end
103
-
104
- def ssl_cert_store(ssl)
105
- return ssl[:cert_store] if ssl[:cert_store]
106
- # Memoize the cert store so that the same one is passed to
107
- # HTTPClient each time, to avoid resyncing SSL sesions when
108
- # it's changed
109
- @cert_store ||= begin
110
- # Use the default cert store by default, i.e. system ca certs
111
- cert_store = OpenSSL::X509::Store.new
112
- cert_store.set_default_paths
113
- cert_store
114
- end
115
- end
116
-
117
- def ssl_verify_mode(ssl)
118
- ssl[:verify_mode] || begin
119
- if ssl.fetch(:verify, true)
120
- OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
121
- else
122
- OpenSSL::SSL::VERIFY_NONE
123
- end
124
- end
125
- end
126
- end
127
- end
128
- end
@@ -1,137 +0,0 @@
1
- begin
2
- require 'net/https'
3
- rescue LoadError
4
- warn "Warning: no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
5
- require 'net/http'
6
- end
7
- require 'zlib'
8
-
9
- module Faraday
10
- class Adapter
11
- class NetHttp < Faraday::Adapter
12
- NET_HTTP_EXCEPTIONS = [
13
- IOError,
14
- Errno::ECONNABORTED,
15
- Errno::ECONNREFUSED,
16
- Errno::ECONNRESET,
17
- Errno::EHOSTUNREACH,
18
- Errno::EINVAL,
19
- Errno::ENETUNREACH,
20
- Errno::EPIPE,
21
- Net::HTTPBadResponse,
22
- Net::HTTPHeaderSyntaxError,
23
- Net::ProtocolError,
24
- SocketError,
25
- Zlib::GzipFile::Error,
26
- ]
27
-
28
- NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
29
- NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
30
-
31
- def call(env)
32
- super
33
- with_net_http_connection(env) do |http|
34
- configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
35
- configure_request(http, env[:request])
36
-
37
- begin
38
- http_response = perform_request(http, env)
39
- rescue *NET_HTTP_EXCEPTIONS => err
40
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
41
- raise Faraday::SSLError, err
42
- else
43
- raise Error::ConnectionFailed, err
44
- end
45
- end
46
-
47
- save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
48
- http_response.each_header do |key, value|
49
- response_headers[key] = value
50
- end
51
- end
52
- end
53
-
54
- @app.call env
55
- rescue Timeout::Error, Errno::ETIMEDOUT => err
56
- raise Faraday::Error::TimeoutError, err
57
- end
58
-
59
- private
60
-
61
- def create_request(env)
62
- request = Net::HTTPGenericRequest.new \
63
- env[:method].to_s.upcase, # request method
64
- !!env[:body], # is there request body
65
- :head != env[:method], # is there response body
66
- env[:url].request_uri, # request uri path
67
- env[:request_headers] # request headers
68
-
69
- if env[:body].respond_to?(:read)
70
- request.body_stream = env[:body]
71
- else
72
- request.body = env[:body]
73
- end
74
- request
75
- end
76
-
77
- def perform_request(http, env)
78
- if :get == env[:method] and !env[:body]
79
- # prefer `get` to `request` because the former handles gzip (ruby 1.9)
80
- http.get env[:url].request_uri, env[:request_headers]
81
- else
82
- http.request create_request(env)
83
- end
84
- end
85
-
86
- def with_net_http_connection(env)
87
- yield net_http_connection(env)
88
- end
89
-
90
- def net_http_connection(env)
91
- if proxy = env[:request][:proxy]
92
- Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
93
- else
94
- Net::HTTP
95
- end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
96
- end
97
-
98
- def configure_ssl(http, ssl)
99
- http.use_ssl = true
100
- http.verify_mode = ssl_verify_mode(ssl)
101
- http.cert_store = ssl_cert_store(ssl)
102
-
103
- http.cert = ssl[:client_cert] if ssl[:client_cert]
104
- http.key = ssl[:client_key] if ssl[:client_key]
105
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
106
- http.ca_path = ssl[:ca_path] if ssl[:ca_path]
107
- http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
108
- http.ssl_version = ssl[:version] if ssl[:version]
109
- end
110
-
111
- def configure_request(http, req)
112
- http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
113
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
114
-
115
- @config_block.call(http) if @config_block
116
- end
117
-
118
- def ssl_cert_store(ssl)
119
- return ssl[:cert_store] if ssl[:cert_store]
120
- # Use the default cert store by default, i.e. system ca certs
121
- cert_store = OpenSSL::X509::Store.new
122
- cert_store.set_default_paths
123
- cert_store
124
- end
125
-
126
- def ssl_verify_mode(ssl)
127
- ssl[:verify_mode] || begin
128
- if ssl.fetch(:verify, true)
129
- OpenSSL::SSL::VERIFY_PEER
130
- else
131
- OpenSSL::SSL::VERIFY_NONE
132
- end
133
- end
134
- end
135
- end
136
- end
137
- end
@@ -1,63 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class NetHttpPersistent < NetHttp
4
- dependency 'net/http/persistent'
5
-
6
- private
7
-
8
- def net_http_connection(env)
9
- proxy_uri = proxy_uri(env)
10
-
11
- cached_connection env[:url], proxy_uri do
12
- if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
13
- Net::HTTP::Persistent.new(name: 'Faraday', proxy: proxy_uri)
14
- else
15
- Net::HTTP::Persistent.new('Faraday', proxy_uri)
16
- end
17
- end
18
- end
19
-
20
- def proxy_uri(env)
21
- proxy_uri = nil
22
- if (proxy = env[:request][:proxy])
23
- proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
24
- proxy_uri.user = proxy_uri.password = nil
25
- # awful patch for net-http-persistent 2.8 not unescaping user/password
26
- (class << proxy_uri; self; end).class_eval do
27
- define_method(:user) { proxy[:user] }
28
- define_method(:password) { proxy[:password] }
29
- end if proxy[:user]
30
- end
31
- proxy_uri
32
- end
33
-
34
- def perform_request(http, env)
35
- http.request env[:url], create_request(env)
36
- rescue Errno::ETIMEDOUT => error
37
- raise Faraday::Error::TimeoutError, error
38
- rescue Net::HTTP::Persistent::Error => error
39
- if error.message.include? 'Timeout'
40
- raise Faraday::Error::TimeoutError, error
41
- elsif error.message.include? 'connection refused'
42
- raise Faraday::Error::ConnectionFailed, error
43
- else
44
- raise
45
- end
46
- end
47
-
48
- def configure_ssl(http, ssl)
49
- http.verify_mode = ssl_verify_mode(ssl)
50
- http.cert_store = ssl_cert_store(ssl)
51
-
52
- http.certificate = ssl[:client_cert] if ssl[:client_cert]
53
- http.private_key = ssl[:client_key] if ssl[:client_key]
54
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
55
- http.ssl_version = ssl[:version] if ssl[:version]
56
- end
57
-
58
- def cached_connection(url, proxy_uri)
59
- (@cached_connection ||= {})[[url.scheme, url.host, url.port, proxy_uri]] ||= yield
60
- end
61
- end
62
- end
63
- end
@@ -1,100 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class Patron < Faraday::Adapter
4
- dependency 'patron'
5
-
6
- def call(env)
7
- super
8
- # TODO: support streaming requests
9
- env[:body] = env[:body].read if env[:body].respond_to? :read
10
-
11
- session = @session ||= create_session
12
- configure_ssl(session, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
13
-
14
- if req = env[:request]
15
- session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
16
- session.connect_timeout = req[:open_timeout] if req[:open_timeout]
17
-
18
- if proxy = req[:proxy]
19
- proxy_uri = proxy[:uri].dup
20
- proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
21
- proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
22
- session.proxy = proxy_uri.to_s
23
- end
24
- end
25
-
26
- response = begin
27
- data = env[:body] ? env[:body].to_s : nil
28
- session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
29
- rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
30
- raise Error::ConnectionFailed, $!
31
- end
32
-
33
- # Remove the "HTTP/1.1 200", leaving just the reason phrase
34
- reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
35
-
36
- save_response(env, response.status, response.body, response.headers, reason_phrase)
37
-
38
- @app.call env
39
- rescue ::Patron::TimeoutError => err
40
- if connection_timed_out_message?(err.message)
41
- raise Faraday::Error::ConnectionFailed, err
42
- else
43
- raise Faraday::Error::TimeoutError, err
44
- end
45
- rescue ::Patron::Error => err
46
- if err.message.include?("code 407")
47
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
48
- else
49
- raise Error::ConnectionFailed, err
50
- end
51
- end
52
-
53
- if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
54
- # HAX: helps but doesn't work completely
55
- # https://github.com/toland/patron/issues/34
56
- ::Patron::Request::VALID_ACTIONS.tap do |actions|
57
- if actions[0].is_a?(Symbol)
58
- actions << :patch unless actions.include? :patch
59
- actions << :options unless actions.include? :options
60
- else
61
- # Patron 0.4.20 and up
62
- actions << "PATCH" unless actions.include? "PATCH"
63
- actions << "OPTIONS" unless actions.include? "OPTIONS"
64
- end
65
- end
66
- end
67
-
68
- def create_session
69
- session = ::Patron::Session.new
70
- @config_block.call(session) if @config_block
71
- session
72
- end
73
-
74
- def configure_ssl(session, ssl)
75
- if ssl.fetch(:verify, true)
76
- session.cacert = ssl[:ca_file]
77
- else
78
- session.insecure = true
79
- end
80
- end
81
-
82
- private
83
-
84
- CURL_TIMEOUT_MESSAGES = [ "Connection time-out",
85
- "Connection timed out",
86
- "Timed out before name resolve",
87
- "server connect has timed out",
88
- "Resolving timed out",
89
- "name lookup timed out",
90
- "timed out before SSL",
91
- "connect() timed out"
92
- ].freeze
93
-
94
- def connection_timed_out_message?(message)
95
- CURL_TIMEOUT_MESSAGES.any? { |curl_message| message.include?(curl_message) }
96
- end
97
-
98
- end
99
- end
100
- end
@@ -1,58 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- # Sends requests to a Rack app.
4
- #
5
- # Examples
6
- #
7
- # class MyRackApp
8
- # def call(env)
9
- # [200, {'Content-Type' => 'text/html'}, ["hello world"]]
10
- # end
11
- # end
12
- #
13
- # Faraday.new do |conn|
14
- # conn.adapter :rack, MyRackApp.new
15
- # end
16
- class Rack < Faraday::Adapter
17
- dependency 'rack/test'
18
-
19
- # not prefixed with "HTTP_"
20
- SPECIAL_HEADERS = %w[ CONTENT_LENGTH CONTENT_TYPE ]
21
-
22
- def initialize(faraday_app, rack_app)
23
- super(faraday_app)
24
- mock_session = ::Rack::MockSession.new(rack_app)
25
- @session = ::Rack::Test::Session.new(mock_session)
26
- end
27
-
28
- def call(env)
29
- super
30
- rack_env = {
31
- :method => env[:method],
32
- :input => env[:body].respond_to?(:read) ? env[:body].read : env[:body],
33
- 'rack.url_scheme' => env[:url].scheme
34
- }
35
-
36
- env[:request_headers].each do |name, value|
37
- name = name.upcase.tr('-', '_')
38
- name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
39
- rack_env[name] = value
40
- end if env[:request_headers]
41
-
42
- timeout = env[:request][:timeout] || env[:request][:open_timeout]
43
- response = if timeout
44
- Timer.timeout(timeout, Faraday::Error::TimeoutError) { execute_request(env, rack_env) }
45
- else
46
- execute_request(env, rack_env)
47
- end
48
-
49
- save_response(env, response.status, response.body, response.headers)
50
- @app.call env
51
- end
52
-
53
- def execute_request(env, rack_env)
54
- @session.request(env[:url].to_s, rack_env)
55
- end
56
- end
57
- end
58
- end