Class: HTTPAdapter::NetHTTPRequestAdapter
- Inherits:
-
Object
- Object
- HTTPAdapter::NetHTTPRequestAdapter
- Defined in:
- lib/httpadapter/adapters/net_http.rb
Constant Summary collapse
- METHOD_MAPPING =
{ # RFC 2616 'OPTIONS' => Net::HTTP::Options, 'GET' => Net::HTTP::Get, 'HEAD' => Net::HTTP::Head, 'POST' => Net::HTTP::Post, 'PUT' => Net::HTTP::Put, 'DELETE' => Net::HTTP::Delete, 'TRACE' => Net::HTTP::Trace, # Other standards supported by Net::HTTP 'COPY' => Net::HTTP::Copy, 'LOCK' => Net::HTTP::Lock, 'MKCOL' => Net::HTTP::Mkcol, 'MOVE' => Net::HTTP::Move, 'PROPFIND' => Net::HTTP::Propfind, 'PROPPATCH' => Net::HTTP::Proppatch, 'UNLOCK' => Net::HTTP::Unlock }
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(request, options = {}) ⇒ NetHTTPRequestAdapter
constructor
A new instance of NetHTTPRequestAdapter.
- #to_ary ⇒ Object
Constructor Details
#initialize(request, options = {}) ⇒ NetHTTPRequestAdapter
Returns a new instance of NetHTTPRequestAdapter.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/httpadapter/adapters/net_http.rb', line 41 def initialize(request, ={}) unless request.kind_of?(Net::HTTPRequest) raise TypeError, "Expected Net::HTTPRequest, got #{request.class}." end @request = request @uri = Addressable::URI.parse([:uri] || request.path || "") if !@uri.host && [:host] @uri.host = [:host] if !@uri.scheme && [:scheme] @uri.scheme = [:scheme] elsif !@uri.scheme @uri.scheme = 'http' end if !@uri.port && [:port] @uri.port = [:port] end @uri.scheme = @uri.normalized_scheme @uri. = @uri. end end |
Class Method Details
.from_ary(array) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/httpadapter/adapters/net_http.rb', line 73 def self.from_ary(array) method, uri, headers, body = array method = method.to_s.upcase uri = Addressable::URI.parse(uri) request_class = METHOD_MAPPING[method] unless request_class raise ArgumentError, "Unknown HTTP method: #{method}" end request = request_class.new(uri.request_uri) headers.each do |header, value| request[header] = value if header.downcase == 'Content-Type'.downcase request.content_type = value end end merged_body = "" body.each do |chunk| merged_body += chunk end if merged_body.length > 0 request.body = merged_body elsif ['POST', 'PUT'].include?(method) request.content_length = 0 end return request end |
.transmit(request, connection = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/httpadapter/adapters/net_http.rb', line 100 def self.transmit(request, connection=nil) method, uri, headers, body = request uri = Addressable::URI.parse(uri) net_http_request = self.from_ary([method, uri, headers, body]) net_http_response = nil unless connection http = Net::HTTP.new(uri.host, uri.inferred_port) if uri.normalized_scheme == 'https' require 'net/https' http.use_ssl = true if http.respond_to?(:enable_post_connection_check=) http.enable_post_connection_check = true end http.verify_mode = OpenSSL::SSL::VERIFY_PEER store = OpenSSL::X509::Store.new store.set_default_paths http.cert_store = store context = http.instance_variable_get('@ssl_context') if context && context.respond_to?(:tmp_dh_callback) && context.tmp_dh_callback == nil context.tmp_dh_callback = lambda do |*args| tmp_dh_key_file = File.( ENV['TMP_DH_KEY_FILE'] || "~/.dhparams.pem" ) if File.exists?(tmp_dh_key_file) OpenSSL::PKey::DH.new(File.read(tmp_dh_key_file)) else # Slow, fix with `openssl dhparam -out ~/.dhparams.pem 2048` OpenSSL::PKey::DH.new(512) end end end end http.start connection = HTTPAdapter::Connection.new( uri.host, uri.inferred_port, http, :open => [:start, [], nil], :close => [:finish, [], nil] ) else http = nil end net_http_response = connection.connection.request(net_http_request) if http connection.close end return NetHTTPResponseAdapter.new(net_http_response).to_ary end |
Instance Method Details
#to_ary ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/httpadapter/adapters/net_http.rb', line 62 def to_ary method = @request.method.to_s.upcase uri = @uri.to_str headers = [] @request.canonical_each do |header, value| headers << [header, value] end body = @request.body || "" return [method, uri, headers, [body]] end |