faraday 0.9.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +360 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +28 -195
  5. data/Rakefile +4 -68
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +158 -58
  9. data/lib/faraday/adapter/typhoeus.rb +7 -115
  10. data/lib/faraday/adapter.rb +79 -20
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +378 -168
  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 +128 -29
  18. data/lib/faraday/file_part.rb +128 -0
  19. data/lib/faraday/logging/formatter.rb +105 -0
  20. data/lib/faraday/methods.rb +6 -0
  21. data/lib/faraday/middleware.rb +19 -25
  22. data/lib/faraday/middleware_registry.rb +129 -0
  23. data/lib/faraday/options/connection_options.rb +22 -0
  24. data/lib/faraday/options/env.rb +181 -0
  25. data/lib/faraday/options/proxy_options.rb +32 -0
  26. data/lib/faraday/options/request_options.rb +22 -0
  27. data/lib/faraday/options/ssl_options.rb +59 -0
  28. data/lib/faraday/options.rb +61 -193
  29. data/lib/faraday/param_part.rb +53 -0
  30. data/lib/faraday/parameters.rb +4 -180
  31. data/lib/faraday/rack_builder.rb +84 -47
  32. data/lib/faraday/request/authorization.rb +51 -31
  33. data/lib/faraday/request/basic_authentication.rb +14 -7
  34. data/lib/faraday/request/instrumentation.rb +45 -27
  35. data/lib/faraday/request/multipart.rb +88 -45
  36. data/lib/faraday/request/retry.rb +212 -121
  37. data/lib/faraday/request/token_authentication.rb +15 -10
  38. data/lib/faraday/request/url_encoded.rb +43 -23
  39. data/lib/faraday/request.rb +96 -32
  40. data/lib/faraday/response/logger.rb +22 -48
  41. data/lib/faraday/response/raise_error.rb +49 -14
  42. data/lib/faraday/response.rb +33 -25
  43. data/lib/faraday/utils/headers.rb +139 -0
  44. data/lib/faraday/utils/params_hash.rb +61 -0
  45. data/lib/faraday/utils.rb +38 -218
  46. data/lib/faraday/version.rb +5 -0
  47. data/lib/faraday.rb +130 -213
  48. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  49. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  50. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  51. data/spec/faraday/adapter/excon_spec.rb +49 -0
  52. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  53. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  54. data/spec/faraday/adapter/patron_spec.rb +18 -0
  55. data/spec/faraday/adapter/rack_spec.rb +8 -0
  56. data/spec/faraday/adapter/test_spec.rb +377 -0
  57. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  58. data/spec/faraday/adapter_registry_spec.rb +28 -0
  59. data/spec/faraday/adapter_spec.rb +55 -0
  60. data/spec/faraday/composite_read_io_spec.rb +80 -0
  61. data/spec/faraday/connection_spec.rb +736 -0
  62. data/spec/faraday/error_spec.rb +60 -0
  63. data/spec/faraday/middleware_spec.rb +52 -0
  64. data/spec/faraday/options/env_spec.rb +70 -0
  65. data/spec/faraday/options/options_spec.rb +297 -0
  66. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  67. data/spec/faraday/options/request_options_spec.rb +19 -0
  68. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  69. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  70. data/spec/faraday/rack_builder_spec.rb +345 -0
  71. data/spec/faraday/request/authorization_spec.rb +96 -0
  72. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  73. data/spec/faraday/request/multipart_spec.rb +302 -0
  74. data/spec/faraday/request/retry_spec.rb +242 -0
  75. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  76. data/spec/faraday/request_spec.rb +120 -0
  77. data/spec/faraday/response/logger_spec.rb +220 -0
  78. data/spec/faraday/response/middleware_spec.rb +68 -0
  79. data/spec/faraday/response/raise_error_spec.rb +169 -0
  80. data/spec/faraday/response_spec.rb +75 -0
  81. data/spec/faraday/utils/headers_spec.rb +82 -0
  82. data/spec/faraday/utils_spec.rb +56 -0
  83. data/spec/faraday_spec.rb +37 -0
  84. data/spec/spec_helper.rb +132 -0
  85. data/spec/support/disabling_stub.rb +14 -0
  86. data/spec/support/fake_safe_buffer.rb +15 -0
  87. data/spec/support/helper_methods.rb +133 -0
  88. data/spec/support/shared_examples/adapter.rb +105 -0
  89. data/spec/support/shared_examples/params_encoder.rb +18 -0
  90. data/spec/support/shared_examples/request_method.rb +262 -0
  91. data/spec/support/streaming_response_checker.rb +35 -0
  92. data/spec/support/webmock_rack_app.rb +68 -0
  93. metadata +199 -98
  94. data/.document +0 -6
  95. data/CONTRIBUTING.md +0 -36
  96. data/Gemfile +0 -25
  97. data/faraday.gemspec +0 -34
  98. data/lib/faraday/adapter/em_http.rb +0 -237
  99. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  100. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  101. data/lib/faraday/adapter/em_synchrony.rb +0 -92
  102. data/lib/faraday/adapter/excon.rb +0 -80
  103. data/lib/faraday/adapter/httpclient.rb +0 -106
  104. data/lib/faraday/adapter/net_http.rb +0 -130
  105. data/lib/faraday/adapter/net_http_persistent.rb +0 -48
  106. data/lib/faraday/adapter/patron.rb +0 -72
  107. data/lib/faraday/adapter/rack.rb +0 -58
  108. data/lib/faraday/upload_io.rb +0 -67
  109. data/script/cached-bundle +0 -46
  110. data/script/console +0 -7
  111. data/script/generate_certs +0 -42
  112. data/script/package +0 -7
  113. data/script/proxy-server +0 -42
  114. data/script/release +0 -17
  115. data/script/s3-put +0 -71
  116. data/script/server +0 -36
  117. data/script/test +0 -172
  118. data/test/adapters/default_test.rb +0 -14
  119. data/test/adapters/em_http_test.rb +0 -20
  120. data/test/adapters/em_synchrony_test.rb +0 -20
  121. data/test/adapters/excon_test.rb +0 -20
  122. data/test/adapters/httpclient_test.rb +0 -21
  123. data/test/adapters/integration.rb +0 -254
  124. data/test/adapters/logger_test.rb +0 -82
  125. data/test/adapters/net_http_persistent_test.rb +0 -20
  126. data/test/adapters/net_http_test.rb +0 -14
  127. data/test/adapters/patron_test.rb +0 -20
  128. data/test/adapters/rack_test.rb +0 -31
  129. data/test/adapters/test_middleware_test.rb +0 -114
  130. data/test/adapters/typhoeus_test.rb +0 -28
  131. data/test/authentication_middleware_test.rb +0 -65
  132. data/test/composite_read_io_test.rb +0 -111
  133. data/test/connection_test.rb +0 -522
  134. data/test/env_test.rb +0 -218
  135. data/test/helper.rb +0 -81
  136. data/test/live_server.rb +0 -67
  137. data/test/middleware/instrumentation_test.rb +0 -88
  138. data/test/middleware/retry_test.rb +0 -177
  139. data/test/middleware_stack_test.rb +0 -173
  140. data/test/multibyte.txt +0 -1
  141. data/test/options_test.rb +0 -252
  142. data/test/parameters_test.rb +0 -64
  143. data/test/request_middleware_test.rb +0 -142
  144. data/test/response_middleware_test.rb +0 -72
  145. data/test/strawberry.rb +0 -2
  146. data/test/utils_test.rb +0 -58
@@ -1,130 +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
- EOFError,
14
- Errno::ECONNABORTED,
15
- Errno::ECONNREFUSED,
16
- Errno::ECONNRESET,
17
- Errno::EHOSTUNREACH,
18
- Errno::EINVAL,
19
- Errno::ENETUNREACH,
20
- Net::HTTPBadResponse,
21
- Net::HTTPHeaderSyntaxError,
22
- Net::ProtocolError,
23
- SocketError,
24
- Zlib::GzipFile::Error,
25
- ]
26
-
27
- NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
28
- NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
29
-
30
- def call(env)
31
- super
32
- with_net_http_connection(env) do |http|
33
- configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
34
-
35
- req = env[:request]
36
- http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
37
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
38
-
39
- begin
40
- http_response = perform_request(http, env)
41
- rescue *NET_HTTP_EXCEPTIONS => err
42
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
43
- raise Faraday::SSLError, err
44
- else
45
- raise Error::ConnectionFailed, err
46
- end
47
- end
48
-
49
- save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
50
- http_response.each_header do |key, value|
51
- response_headers[key] = value
52
- end
53
- end
54
- end
55
-
56
- @app.call env
57
- rescue Timeout::Error => err
58
- raise Faraday::Error::TimeoutError, err
59
- end
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].host, proxy[:uri].port, proxy[:user], proxy[:password])
93
- else
94
- Net::HTTP
95
- end.new(env[:url].host, env[:url].port)
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 ssl_cert_store(ssl)
112
- return ssl[:cert_store] if ssl[:cert_store]
113
- # Use the default cert store by default, i.e. system ca certs
114
- cert_store = OpenSSL::X509::Store.new
115
- cert_store.set_default_paths
116
- cert_store
117
- end
118
-
119
- def ssl_verify_mode(ssl)
120
- ssl[:verify_mode] || begin
121
- if ssl.fetch(:verify, true)
122
- OpenSSL::SSL::VERIFY_PEER
123
- else
124
- OpenSSL::SSL::VERIFY_NONE
125
- end
126
- end
127
- end
128
- end
129
- end
130
- end
@@ -1,48 +0,0 @@
1
- # Rely on autoloading instead of explicit require; helps avoid the "already
2
- # initialized constant" warning on Ruby 1.8.7 when NetHttp is refereced below.
3
- # require 'faraday/adapter/net_http'
4
-
5
- module Faraday
6
- class Adapter
7
- # Experimental adapter for net-http-persistent
8
- class NetHttpPersistent < NetHttp
9
- dependency 'net/http/persistent'
10
-
11
- def with_net_http_connection(env)
12
- if proxy = env[:request][:proxy]
13
- proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
14
- proxy_uri.user = proxy_uri.password = nil
15
- # awful patch for net-http-persistent 2.8 not unescaping user/password
16
- (class << proxy_uri; self; end).class_eval do
17
- define_method(:user) { proxy[:user] }
18
- define_method(:password) { proxy[:password] }
19
- end if proxy[:user]
20
- end
21
-
22
- yield Net::HTTP::Persistent.new 'Faraday', proxy_uri
23
- end
24
-
25
- def perform_request(http, env)
26
- http.request env[:url], create_request(env)
27
- rescue Net::HTTP::Persistent::Error => error
28
- if error.message.include? 'Timeout'
29
- raise Faraday::Error::TimeoutError, error
30
- elsif error.message.include? 'connection refused'
31
- raise Faraday::Error::ConnectionFailed, error
32
- else
33
- raise
34
- end
35
- end
36
-
37
- def configure_ssl(http, ssl)
38
- http.verify_mode = ssl_verify_mode(ssl)
39
- http.cert_store = ssl_cert_store(ssl)
40
-
41
- http.certificate = ssl[:client_cert] if ssl[:client_cert]
42
- http.private_key = ssl[:client_key] if ssl[:client_key]
43
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
44
- http.ssl_version = ssl[:version] if ssl[:version]
45
- end
46
- end
47
- end
48
- end
@@ -1,72 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class Patron < Faraday::Adapter
4
- dependency 'patron'
5
-
6
- def initialize(app, &block)
7
- super(app)
8
- @block = block
9
- end
10
-
11
- def call(env)
12
- super
13
-
14
- # TODO: support streaming requests
15
- env[:body] = env[:body].read if env[:body].respond_to? :read
16
-
17
- session = @session ||= create_session
18
-
19
- if req = env[:request]
20
- session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
21
- session.connect_timeout = req[:open_timeout] if req[:open_timeout]
22
-
23
- if proxy = req[:proxy]
24
- proxy_uri = proxy[:uri].dup
25
- proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
26
- proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
27
- session.proxy = proxy_uri.to_s
28
- end
29
- end
30
-
31
- response = begin
32
- data = env[:body] ? env[:body].to_s : nil
33
- session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
34
- rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
35
- raise Error::ConnectionFailed, $!
36
- end
37
-
38
- save_response(env, response.status, response.body, response.headers)
39
-
40
- @app.call env
41
- rescue ::Patron::TimeoutError => err
42
- if err.message == "Connection time-out"
43
- raise Faraday::Error::ConnectionFailed, err
44
- else
45
- raise Faraday::Error::TimeoutError, err
46
- end
47
- rescue ::Patron::Error => err
48
- if err.message.include?("code 407")
49
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
50
- else
51
- raise Error::ConnectionFailed, err
52
- end
53
- end
54
-
55
- if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
56
- # HAX: helps but doesn't work completely
57
- # https://github.com/toland/patron/issues/34
58
- ::Patron::Request::VALID_ACTIONS.tap do |actions|
59
- actions << :patch unless actions.include? :patch
60
- actions << :options unless actions.include? :options
61
- end
62
- end
63
-
64
- def create_session
65
- session = ::Patron::Session.new
66
- session.insecure = true
67
- @block.call(session) if @block
68
- session
69
- end
70
- end
71
- end
72
- 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
@@ -1,67 +0,0 @@
1
- begin
2
- require 'composite_io'
3
- require 'parts'
4
- require 'stringio'
5
- rescue LoadError
6
- $stderr.puts "Install the multipart-post gem."
7
- raise
8
- end
9
-
10
- module Faraday
11
- # Similar but not compatible with ::CompositeReadIO provided by multipart-post.
12
- class CompositeReadIO
13
- def initialize(*parts)
14
- @parts = parts.flatten
15
- @ios = @parts.map { |part| part.to_io }
16
- @index = 0
17
- end
18
-
19
- def length
20
- @parts.inject(0) { |sum, part| sum + part.length }
21
- end
22
-
23
- def rewind
24
- @ios.each { |io| io.rewind }
25
- @index = 0
26
- end
27
-
28
- # Read from IOs in order until `length` bytes have been received.
29
- def read(length = nil, outbuf = nil)
30
- got_result = false
31
- outbuf = outbuf ? outbuf.replace("") : ""
32
-
33
- while io = current_io
34
- if result = io.read(length)
35
- got_result ||= !result.nil?
36
- result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
37
- outbuf << result
38
- length -= result.length if length
39
- break if length == 0
40
- end
41
- advance_io
42
- end
43
- (!got_result && length) ? nil : outbuf
44
- end
45
-
46
- def close
47
- @ios.each { |io| io.close }
48
- end
49
-
50
- def ensure_open_and_readable
51
- # Rubinius compatibility
52
- end
53
-
54
- private
55
-
56
- def current_io
57
- @ios[@index]
58
- end
59
-
60
- def advance_io
61
- @index += 1
62
- end
63
- end
64
-
65
- UploadIO = ::UploadIO
66
- Parts = ::Parts
67
- end
data/script/cached-bundle DELETED
@@ -1,46 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Usage: cached-bundle install --deployment
3
- #
4
- # After running `bundle`, caches the `./bundle` directory to S3.
5
- # On the next run, restores the cached directory before running `bundle`.
6
- # When `Gemfile` changes, the cache gets rebuilt.
7
- #
8
- # Requirements:
9
- # - Gemfile
10
- # - TRAVIS_REPO_SLUG
11
- # - TRAVIS_RUBY_VERSION
12
- # - AMAZON_S3_BUCKET
13
- # - script/s3-put
14
- # - bundle
15
- # - curl
16
- #
17
- # Author: Mislav Marohnić
18
-
19
- set -e
20
-
21
- compute_md5() {
22
- local output="$(openssl md5)"
23
- echo "${output##* }"
24
- }
25
-
26
- download() {
27
- curl --tcp-nodelay -qsfL "$1" -o "$2"
28
- }
29
-
30
- bundle_path="bundle"
31
- gemfile_hash="$(compute_md5 <"${BUNDLE_GEMFILE:-Gemfile}")"
32
- cache_name="${TRAVIS_RUBY_VERSION}-${gemfile_hash}.tgz"
33
- fetch_url="http://${AMAZON_S3_BUCKET}.s3.amazonaws.com/${TRAVIS_REPO_SLUG}/${cache_name}"
34
-
35
- if download "$fetch_url" "$cache_name"; then
36
- echo "Reusing cached bundle ${cache_name}"
37
- tar xzf "$cache_name"
38
- fi
39
-
40
- bundle "$@"
41
-
42
- if [ ! -f "$cache_name" ] && [ -n "$AMAZON_SECRET_ACCESS_KEY" ]; then
43
- echo "Caching \`${bundle_path}' to S3"
44
- tar czf "$cache_name" "$bundle_path"
45
- script/s3-put "$cache_name" "${AMAZON_S3_BUCKET}:${TRAVIS_REPO_SLUG}/${cache_name}"
46
- fi
data/script/console DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Usage: script/console
3
- # Starts an IRB console with this library loaded.
4
-
5
- gemspec="$(ls *.gemspec | head -1)"
6
-
7
- exec bundle exec irb -r "${gemspec%.*}"
@@ -1,42 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Usage: generate_certs
3
- # Generate test certs for testing Faraday with SSL
4
-
5
- require 'openssl'
6
- require 'fileutils'
7
-
8
- $shell = ARGV.include? '-s'
9
-
10
- # Adapted from WEBrick::Utils. Skips cert extensions so it
11
- # can be used as a CA bundle
12
- def create_self_signed_cert(bits, cn, comment)
13
- rsa = OpenSSL::PKey::RSA.new(bits)
14
- cert = OpenSSL::X509::Certificate.new
15
- cert.version = 2
16
- cert.serial = 1
17
- name = OpenSSL::X509::Name.new(cn)
18
- cert.subject = name
19
- cert.issuer = name
20
- cert.not_before = Time.now
21
- cert.not_after = Time.now + (365*24*60*60)
22
- cert.public_key = rsa.public_key
23
- cert.sign(rsa, OpenSSL::Digest::SHA1.new)
24
- return [cert, rsa]
25
- end
26
-
27
- def write(file, contents, env_var)
28
- FileUtils.mkdir_p(File.dirname(file))
29
- File.open(file, 'w') {|f| f.puts(contents) }
30
- puts %(export #{env_var}="#{file}") if $shell
31
- end
32
-
33
-
34
- # One cert / CA for ease of testing when ignoring verification
35
- cert, key = create_self_signed_cert(1024, [['CN', 'localhost']], 'Faraday Test CA')
36
- write 'tmp/faraday-cert.key', key, 'SSL_KEY'
37
- write 'tmp/faraday-cert.crt', cert, 'SSL_FILE'
38
-
39
- # And a second CA to prove that verification can fail
40
- cert, key = create_self_signed_cert(1024, [['CN', 'real-ca.com']], 'A different CA')
41
- write 'tmp/faraday-different-ca-cert.key', key, 'SSL_KEY_ALT'
42
- write 'tmp/faraday-different-ca-cert.crt', cert, 'SSL_FILE_ALT'
data/script/package DELETED
@@ -1,7 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Usage: script/gem
3
- # Updates the gemspec and builds a new gem in the pkg directory.
4
-
5
- mkdir -p pkg
6
- gem build *.gemspec
7
- mv *.gem pkg
data/script/proxy-server DELETED
@@ -1,42 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # Usage: script/proxy-server [-p PORT] [-u USER:PASSWORD]
3
- require 'webrick'
4
- require 'webrick/httpproxy'
5
-
6
- port = 4001
7
-
8
- if found = ARGV.index('-p')
9
- port = ARGV[found + 1].to_i
10
- end
11
- if found = ARGV.index('-u')
12
- username, password = ARGV[found + 1].split(':', 2)
13
- end
14
-
15
- match_credentials = lambda { |credentials|
16
- got_username, got_password = credentials.to_s.unpack("m*")[0].split(":", 2)
17
- got_username == username && got_password == password
18
- }
19
-
20
- log_io = $stdout
21
- log_io.sync = true
22
-
23
- webrick_opts = {
24
- :Port => port, :Logger => WEBrick::Log::new(log_io),
25
- :AccessLog => [[log_io, "[%{X-Faraday-Adapter}i] %m %U -> %s %b"]],
26
- :ProxyAuthProc => lambda { |req, res|
27
- if username
28
- type, credentials = req.header['proxy-authorization'].first.to_s.split(/\s+/, 2)
29
- unless "Basic" == type && match_credentials.call(credentials)
30
- res['proxy-authenticate'] = %{Basic realm="testing"}
31
- raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
32
- end
33
- end
34
- }
35
- }
36
-
37
- proxy = WEBrick::HTTPProxyServer.new(webrick_opts)
38
-
39
- trap(:TERM) { proxy.shutdown }
40
- trap(:INT) { proxy.shutdown }
41
-
42
- proxy.start
data/script/release DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Usage: script/release
3
- # Build the package, tag a commit, push it to origin, and then release the
4
- # package publicly.
5
-
6
- set -e
7
-
8
- version="$(script/package | grep Version: | awk '{print $2}')"
9
- [ -n "$version" ] || exit 1
10
-
11
- git commit --allow-empty -a -m "Release $version"
12
- git tag "v$version"
13
- git push origin
14
- git push origin "v$version"
15
- git push legacy
16
- git push legacy "v$version"
17
- gem push pkg/*-${version}.gem
data/script/s3-put DELETED
@@ -1,71 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Usage: s3-put <FILE> <S3_BUCKET>[:<PATH>] [<CONTENT_TYPE>]
3
- #
4
- # Uploads a file to the Amazon S3 service.
5
- # Outputs the URL for the newly uploaded file.
6
- #
7
- # Requirements:
8
- # - AMAZON_ACCESS_KEY_ID
9
- # - AMAZON_SECRET_ACCESS_KEY
10
- # - openssl
11
- # - curl
12
- #
13
- # Author: Mislav Marohnić
14
-
15
- set -e
16
-
17
- authorization() {
18
- local signature="$(string_to_sign | hmac_sha1 | base64)"
19
- echo "AWS ${AMAZON_ACCESS_KEY_ID?}:${signature}"
20
- }
21
-
22
- hmac_sha1() {
23
- openssl dgst -binary -sha1 -hmac "${AMAZON_SECRET_ACCESS_KEY?}"
24
- }
25
-
26
- base64() {
27
- openssl enc -base64
28
- }
29
-
30
- bin_md5() {
31
- openssl dgst -binary -md5
32
- }
33
-
34
- string_to_sign() {
35
- echo "$http_method"
36
- echo "$content_md5"
37
- echo "$content_type"
38
- echo "$date"
39
- echo "x-amz-acl:$acl"
40
- printf "/$bucket/$remote_path"
41
- }
42
-
43
- date_string() {
44
- LC_TIME=C date "+%a, %d %h %Y %T %z"
45
- }
46
-
47
- file="$1"
48
- bucket="${2%%:*}"
49
- remote_path="${2#*:}"
50
- content_type="$3"
51
-
52
- if [ -z "$remote_path" ] || [ "$remote_path" = "$bucket" ]; then
53
- remote_path="${file##*/}"
54
- fi
55
-
56
- http_method=PUT
57
- acl="public-read"
58
- content_md5="$(bin_md5 < "$file" | base64)"
59
- date="$(date_string)"
60
-
61
- url="https://$bucket.s3.amazonaws.com/$remote_path"
62
-
63
- curl -qsSf -T "$file" \
64
- -H "Authorization: $(authorization)" \
65
- -H "x-amz-acl: $acl" \
66
- -H "Date: $date" \
67
- -H "Content-MD5: $content_md5" \
68
- -H "Content-Type: $content_type" \
69
- "$url"
70
-
71
- echo "$url"
data/script/server DELETED
@@ -1,36 +0,0 @@
1
- #!/usr/bin/env ruby
2
- old_verbose, $VERBOSE = $VERBOSE, nil
3
- begin
4
- require File.expand_path('../../test/live_server', __FILE__)
5
- ensure
6
- $VERBOSE = old_verbose
7
- end
8
- require 'webrick'
9
-
10
- port = 4000
11
- if found = ARGV.index('-p')
12
- port = ARGV[found + 1].to_i
13
- end
14
-
15
- log_io = $stdout
16
- log_io.sync = true
17
-
18
- webrick_opts = {
19
- :Port => port, :Logger => WEBrick::Log::new(log_io),
20
- :AccessLog => [[log_io, "[%{X-Faraday-Adapter}i] %m %U -> %s %b"]]
21
- }
22
-
23
- if ENV['SSL_KEY']
24
- require 'openssl'
25
- require 'webrick/https'
26
- webrick_opts.update \
27
- :SSLEnable => true,
28
- :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.read(ENV['SSL_KEY'])),
29
- :SSLCertificate => OpenSSL::X509::Certificate.new(File.read(ENV['SSL_FILE'])),
30
- :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE
31
- end
32
-
33
- Rack::Handler::WEBrick.run(Faraday::LiveServer, webrick_opts) do |server|
34
- trap(:INT) { server.stop }
35
- trap(:TERM) { server.stop }
36
- end