Class: Rack::Client::Handler::NetHTTP
- Inherits:
-
Object
- Object
- Rack::Client::Handler::NetHTTP
- Extended by:
- Forwardable
- Includes:
- DualBand
- Defined in:
- lib/rack/client/handler/net_http.rb
Instance Method Summary collapse
- #async_call(env) ⇒ Object
- #body_for(request) ⇒ Object
- #connections ⇒ Object
- #net_connection_for(request) ⇒ Object
- #net_request_for(request) ⇒ Object
- #parse(net_response) ⇒ Object
- #parse_headers(net_response) ⇒ Object
- #parse_stream(net_response) ⇒ Object
- #required_streaming_headers?(request) ⇒ Boolean
- #streaming_body?(request) ⇒ Boolean
- #sync_call(env) ⇒ Object
Methods included from DualBand
Instance Method Details
#async_call(env) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/rack/client/handler/net_http.rb', line 29 def async_call(env) request = Rack::Request.new(env) net_connection_for(request).request(net_request_for(request), body_for(request)) do |net_response| yield parse_stream(net_response).finish end end |
#body_for(request) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/rack/client/handler/net_http.rb', line 65 def body_for(request) case request.body when StringIO then request.body.string when IO then request.body.read when Array then request.body.join when String then request.body end end |
#connections ⇒ Object
105 106 107 |
# File 'lib/rack/client/handler/net_http.rb', line 105 def connections @connections ||= {} end |
#net_connection_for(request) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rack/client/handler/net_http.rb', line 37 def net_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 |
#net_request_for(request) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rack/client/handler/net_http.rb', line 49 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 net_request = klass.new(request.fullpath, Headers.from(request.env).to_http) net_request.body_stream = request.body if streaming_body?(request) net_request end |
#parse(net_response) ⇒ Object
86 87 88 89 |
# File 'lib/rack/client/handler/net_http.rb', line 86 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
95 96 97 98 99 100 101 102 103 |
# File 'lib/rack/client/handler/net_http.rb', line 95 def parse_headers(net_response) headers = Headers.new net_response.each do |k,v| headers.update(k => v) end headers.to_http end |
#parse_stream(net_response) ⇒ Object
91 92 93 |
# File 'lib/rack/client/handler/net_http.rb', line 91 def parse_stream(net_response) Response.new(net_response.code.to_i, parse_headers(net_response)) {|block| net_response.read_body(&block) } end |
#required_streaming_headers?(request) ⇒ Boolean
81 82 83 84 |
# File 'lib/rack/client/handler/net_http.rb', line 81 def required_streaming_headers?(request) request.env.keys.include?('HTTP_CONTENT_LENGTH') || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked' end |
#streaming_body?(request) ⇒ Boolean
74 75 76 77 78 79 |
# File 'lib/rack/client/handler/net_http.rb', line 74 def streaming_body?(request) request.request_method == 'POST' && required_streaming_headers?(request) && request.body.is_a?(IO) && !request.body.is_a?(StringIO) end |
#sync_call(env) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rack/client/handler/net_http.rb', line 15 def sync_call(env) request = Rack::Request.new(env) net_connection, net_request = net_connection_for(request), net_request_for(request) if streaming_body?(request) net_response = net_connection.request(net_request) else net_response = net_connection.request(net_request, body_for(request)) end parse(net_response).finish end |