module WebMock::NetHTTPUtility

def self.check_right_http_connection

def self.check_right_http_connection
  @was_right_http_connection_loaded = defined?(RightHttpConnection)
end

def self.get_uri(net_http, path = nil)

def self.get_uri(net_http, path = nil)
  protocol = net_http.use_ssl? ? "https" : "http"
  hostname = net_http.address
  hostname = "[#{hostname}]" if /\A\[.*\]\z/ !~ hostname && /:/ =~ hostname
  "#{protocol}://#{hostname}:#{net_http.port}#{path}"
end

def self.puts_warning_for_right_http_if_needed

def self.puts_warning_for_right_http_if_needed
  if !@was_right_http_connection_loaded && defined?(RightHttpConnection)
    $stderr.puts "\nWarning: RightHttpConnection has to be required before WebMock is required !!!\n"
  end
end

def self.request_signature_from_request(net_http, request, body = nil)

def self.request_signature_from_request(net_http, request, body = nil)
  path = request.path
  if path.respond_to?(:request_uri) #https://github.com/bblimke/webmock/issues/288
    path = path.request_uri
  end
  path = WebMock::Util::URI.heuristic_parse(path).request_uri if path =~ /^http/
  uri = get_uri(net_http, path)
  method = request.method.downcase.to_sym
  headers = Hash[*request.to_hash.map {|k,v| [k, v]}.inject([]) {|r,x| r + x}]
  if request.body_stream
    body = request.body_stream.read
    request.body_stream = nil
  end
  if body != nil && body.respond_to?(:read)
    request.set_body_internal body.read
  else
    request.set_body_internal body
  end
  WebMock::RequestSignature.new(method, uri, body: request.body, headers: headers)
end