Class: Faraday::Adapter::NetHttp

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

Instance Method Summary collapse

Methods inherited from Middleware

#create_form_params, #full_path_for, #initialize, loaded?, #process_body_for_request, setup_parallel_manager

Constructor Details

This class inherits a constructor from Faraday::Middleware

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/faraday/adapter/net_http.rb', line 11

def call(env)
  process_body_for_request(env)

  is_ssl = env[:url].scheme == 'https'

  http = net_http_class(env).new(env[:url].host, env[:url].port || (is_ssl ? 443 : 80))
  if http.use_ssl = is_ssl
    ssl = env[:ssl]
    if ssl[:verify] == false
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    else
      http.verify_mode = ssl[:verify]
    end
    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]
  end
  req = env[:request]
  http.read_timeout = net.open_timeout = req[:timeout] if req[:timeout]
  http.open_timeout = req[:open_timeout]               if req[:open_timeout]
  
  full_path = full_path_for(env[:url].path, env[:url].query, env[:url].fragment)
  http_resp = http.send_request(env[:method].to_s.upcase, full_path, env[:body], env[:request_headers])

  resp_headers = {}
  http_resp.each_header do |key, value|
    resp_headers[key] = value
  end

  env.update \
    :status           => http_resp.code.to_i, 
    :response_headers => resp_headers, 
    :body             => http_resp.body

  @app.call env
rescue Errno::ECONNREFUSED
  raise Error::ConnectionFailed, "connection refused"
end

#net_http_class(env) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/faraday/adapter/net_http.rb', line 50

def net_http_class(env)
  if proxy = env[:request][:proxy]
    Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])
  else
    Net::HTTP
  end
end