Class: Rack::Client::Handler::NetHTTP

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
DualBand
Defined in:
lib/rack/client/handler/net_http.rb

Instance Method Summary collapse

Methods included from DualBand

#call

Instance Method Details

#async_call(env) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rack/client/handler/net_http.rb', line 23

def async_call(env)
  request = Rack::Request.new(env)

  connection_for(request).request(net_request_for(request), body_for(request)) do |net_response|
    yield parse(net_response).finish
  end
end

#body_for(request) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/rack/client/handler/net_http.rb', line 55

def body_for(request)
  case request.body
  when StringIO then request.body.string
  when IO       then request.body.read
  when Array    then request.body.to_s
  when String   then request.body
  end
end

#connection_for(request) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/client/handler/net_http.rb', line 31

def connection_for(request)
  connection = Net::HTTP.new(request.host, request.port)

  if request.scheme == 'https'
    connection.use_ssl = true
    connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  connection.start
  connection
end

#connectionsObject



79
80
81
# File 'lib/rack/client/handler/net_http.rb', line 79

def connections
  @connections ||= {}
end

#net_request_for(request) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/client/handler/net_http.rb', line 43

def net_request_for(request)
  klass = case request.request_method
          when 'DELETE' then Net::HTTP::Delete
          when 'GET'    then Net::HTTP::Get
          when 'HEAD'   then Net::HTTP::Head
          when 'POST'   then Net::HTTP::Post
          when 'PUT'    then Net::HTTP::Put
          end

  klass.new(request.path, Headers.from(request.env).to_http)
end

#parse(net_response) ⇒ Object



64
65
66
67
# File 'lib/rack/client/handler/net_http.rb', line 64

def parse(net_response)
  body = (net_response.body.nil? || net_response.body.empty?) ? [] : StringIO.new(net_response.body)
  Response.new(net_response.code.to_i, parse_headers(net_response), body)
end

#parse_headers(net_response) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/rack/client/handler/net_http.rb', line 69

def parse_headers(net_response)
  headers = Headers.new

  net_response.each do |(k,v)|
    headers.update(k => v)
  end

  headers.to_http
end

#sync_call(env) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rack/client/handler/net_http.rb', line 15

def sync_call(env)
  request = Rack::Request.new(env)

  connection_for(request).request(net_request_for(request), body_for(request)) do |net_response|
    return parse(net_response).finish
  end
end