Class: Faraday::Adapter::NetHttp

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/net_http.rb

Direct Known Subclasses

NetHttpPersistent

Constant Summary collapse

NET_HTTP_EXCEPTIONS =
[
  IOError,
  Errno::ECONNABORTED,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::EHOSTUNREACH,
  Errno::EINVAL,
  Errno::ENETUNREACH,
  Errno::EPIPE,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  SocketError,
  Zlib::GzipFile::Error,
]

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Instance Attribute Summary

Attributes included from Parallelism

#supports_parallel

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#initialize, #save_response

Methods included from Parallelism

#inherited, #supports_parallel?

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods inherited from Middleware

dependency, inherited, #initialize, loaded?, new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

This class inherits a constructor from Faraday::Adapter

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/faraday/adapter/net_http.rb', line 31

def call(env)
  super
  with_net_http_connection(env) do |http|
    configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
    configure_request(http, env[:request])

    begin
      http_response = perform_request(http, env)
    rescue *NET_HTTP_EXCEPTIONS => err
      if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
        raise Faraday::SSLError, err
      else
        raise Error::ConnectionFailed, err
      end
    end

    save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
      http_response.each_header do |key, value|
        response_headers[key] = value
      end
    end
  end

  @app.call env
rescue Timeout::Error, Errno::ETIMEDOUT => err
  raise Faraday::Error::TimeoutError, err
end

#configure_request(http, req) ⇒ Object



109
110
111
112
113
114
# File 'lib/faraday/adapter/net_http.rb', line 109

def configure_request(http, req)
  http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
  http.open_timeout = req[:open_timeout]                if req[:open_timeout]

  @config_block.call(http) if @config_block
end

#configure_ssl(http, ssl) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/faraday/adapter/net_http.rb', line 96

def configure_ssl(http, ssl)
  http.use_ssl      = true
  http.verify_mode  = ssl_verify_mode(ssl)
  http.cert_store   = ssl_cert_store(ssl)

  http.cert         = ssl[:client_cert]  if ssl[:client_cert]
  http.key          = ssl[:client_key]   if ssl[:client_key]
  http.ca_file      = ssl[:ca_file]      if ssl[:ca_file]
  http.ca_path      = ssl[:ca_path]      if ssl[:ca_path]
  http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
  http.ssl_version  = ssl[:version]      if ssl[:version]
end

#create_request(env) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/faraday/adapter/net_http.rb', line 59

def create_request(env)
  request = Net::HTTPGenericRequest.new \
    env[:method].to_s.upcase,    # request method
    !!env[:body],                # is there request body
    :head != env[:method],       # 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

#net_http_connection(env) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/faraday/adapter/net_http.rb', line 88

def net_http_connection(env)
  if proxy = env[:request][:proxy]
    Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
  else
    Net::HTTP
  end.new(env[:url].host, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
end

#perform_request(http, env) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/faraday/adapter/net_http.rb', line 75

def perform_request(http, env)
  if :get == env[:method] and !env[:body]
    # prefer `get` to `request` because the former handles gzip (ruby 1.9)
    http.get env[:url].request_uri, env[:request_headers]
  else
    http.request create_request(env)
  end
end

#ssl_cert_store(ssl) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/faraday/adapter/net_http.rb', line 116

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
  cert_store = OpenSSL::X509::Store.new
  cert_store.set_default_paths
  cert_store
end

#ssl_verify_mode(ssl) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/faraday/adapter/net_http.rb', line 124

def ssl_verify_mode(ssl)
  ssl[:verify_mode] || begin
    if ssl.fetch(:verify, true)
      OpenSSL::SSL::VERIFY_PEER
    else
      OpenSSL::SSL::VERIFY_NONE
    end
  end
end

#with_net_http_connection(env) {|net_http_connection(env)| ... } ⇒ Object

Yields:



84
85
86
# File 'lib/faraday/adapter/net_http.rb', line 84

def with_net_http_connection(env)
  yield net_http_connection(env)
end