Class: HTTPAdapter::NetHTTPAdapter

Inherits:
Object
  • Object
show all
Includes:
HTTPAdapter
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
}
STATUS_MESSAGES =
{
  100 => "Continue",
  101 => "Switching Protocols",
  102 => "Processing",

  200 => "OK",
  201 => "Created",
  202 => "Accepted",
  203 => "Non-Authoritative Information",
  204 => "No Content",
  205 => "Reset Content",
  206 => "Partial Content",
  207 => "Multi-Status",
  226 => "IM Used",

  300 => "Multiple Choices",
  301 => "Moved Permanently",
  302 => "Found",
  303 => "See Other",
  304 => "Not Modified",
  305 => "Use Proxy",
  307 => "Temporary Redirect",

  400 => "Bad Request",
  401 => "Unauthorized",
  402 => "Payment Required",
  403 => "Forbidden",
  404 => "Not Found",
  405 => "Method Not Allowed",
  406 => "Not Acceptable",
  407 => "Proxy Authentication Required",
  408 => "Request Timeout",
  409 => "Conflict",
  410 => "Gone",
  411 => "Length Required",
  412 => "Precondition Failed",
  413 => "Request Entity Too Large",
  414 => "Request-URI Too Long",
  415 => "Unsupported Media Type",
  416 => "Requested Range Not Satisfiable",
  417 => "Expectation Failed",
  422 => "Unprocessable Entity",
  423 => "Locked",
  424 => "Failed Dependency",
  426 => "Upgrade Required",

  500 => "Internal Server Error",
  501 => "Not Implemented",
  502 => "Bad Gateway",
  503 => "Service Unavailable",
  504 => "Gateway Timeout",
  505 => "HTTP Version Not Supported",
  507 => "Insufficient Storage",
  510 => "Not Extended"
}
STATUS_MAPPING =
Net::HTTPResponse::CODE_TO_OBJ

Instance Method Summary collapse

Methods included from HTTPAdapter

#adapt_request, #adapt_response, #specialize_request, #specialize_response, #transmit, verified_request, verified_response

Constructor Details

#initialize(&block) ⇒ NetHTTPAdapter

Returns a new instance of NetHTTPAdapter.



101
102
103
# File 'lib/httpadapter/adapters/net_http.rb', line 101

def initialize(&block)
  @connection_config = block
end

Instance Method Details

#convert_request_from_a(request_ary) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/httpadapter/adapters/net_http.rb', line 133

def convert_request_from_a(request_ary)
  method, uri, headers, body = request_ary
  method = method.to_s.upcase
  host_from_header = nil
  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
    elsif header.downcase == 'Host'.downcase
      host_from_header = value
    end
  end
  if host_from_header == nil && uri.host
    request['Host'] = uri.host
  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

#convert_request_to_a(request_obj) ⇒ Object



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
# File 'lib/httpadapter/adapters/net_http.rb', line 105

def convert_request_to_a(request_obj)
  unless request_obj.kind_of?(Net::HTTPRequest)
    raise TypeError, "Expected Net::HTTPRequest, got #{request_obj.class}."
  end
  method = request_obj.method.to_s.upcase
  host_from_header = nil
  scheme_from_header = nil
  headers = []
  request_obj.canonical_each do |header, value|
    if header.downcase == 'X-Forwarded-Proto'.downcase
      scheme_from_header = value
    elsif header.downcase == 'Host'.downcase
      host_from_header = value
    end
    headers << [header, value]
  end
  uri = Addressable::URI.parse(request_obj.path || "")
  uri.host ||= host_from_header
  if uri.host
    uri.scheme ||= scheme_from_header || 'http'
    uri.scheme = uri.normalized_scheme
    uri.authority = uri.normalized_authority
  end
  uri = uri.to_str
  body = request_obj.body || ""
  return [method, uri, headers, [body]]
end

#convert_response_from_a(response_ary) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/httpadapter/adapters/net_http.rb', line 180

def convert_response_from_a(response_ary)
  status, headers, body = response_ary
  message = STATUS_MESSAGES[status.to_i]
  response_class = STATUS_MAPPING[status.to_s]
  unless message && response_class
    raise ArgumentError, "Unknown status code: #{status}"
  end
  status = status.to_i
  response = response_class.new('1.1', status.to_s, message)
  headers.each do |header, value|
    response.add_field(header, value)
  end
  merged_body = ""
  body.each do |chunk|
    merged_body += chunk
  end

  # Ugh
  response.instance_variable_set('@read', true)
  response.instance_variable_set('@body', merged_body)

  return response
end

#convert_response_to_a(response_obj) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/httpadapter/adapters/net_http.rb', line 166

def convert_response_to_a(response_obj)
  unless response_obj.kind_of?(Net::HTTPResponse)
    raise TypeError,
      "Expected Net::HTTPResponse, got #{response_obj.class}."
  end
  status = response_obj.code.to_i
  headers = []
  response_obj.canonical_each do |header, value|
    headers << [header, value]
  end
  body = response_obj.body || ""
  return [status, headers, [body]]
end

#fetch_resource(request_ary, connection = nil) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/httpadapter/adapters/net_http.rb', line 204

def fetch_resource(request_ary, connection=nil)
  method, uri, headers, body = request_ary
  uri = Addressable::URI.parse(uri)
  net_http_request = self.convert_request_from_a(
    [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
      ca_file = File.expand_path(ENV['CA_FILE'] || '~/.cacert.pem')
      if File.exists?(ca_file)
        http.ca_file = ca_file
      end
      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)
        # Fix for annoying warning
        context.tmp_dh_callback ||= lambda {}
      end
    end
    connection = HTTPAdapter::Connection.new(
      uri.host, uri.inferred_port, http,
      :open => [:start, [], nil],
      :close => [:finish, [], nil]
    )
  else
    http = nil
  end
  if @connection_config
    @connection_config.call(connection)
  end
  if connection.connection && !connection.connection.active?
    connection.connection.start
  end
  net_http_response = connection.connection.request(net_http_request)
  if http
    connection.close
  end
  return self.convert_response_to_a(net_http_response)
end