PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/new_relic/agent/http_clients/net_http_wrappers.rb

http://github.com/newrelic/rpm
Ruby | 79 lines | 60 code | 15 blank | 4 comment | 3 complexity | 55a62f96de6df8a10df0155de5296e07 MD5 | raw file
Possible License(s): Apache-2.0
  1. # encoding: utf-8
  2. # This file is distributed under New Relic's license terms.
  3. # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
  4. # frozen_string_literal: true
  5. require_relative 'abstract'
  6. require 'resolv'
  7. module NewRelic
  8. module Agent
  9. module HTTPClients
  10. class NetHTTPResponse < AbstractResponse
  11. def [](key)
  12. @wrapped_response[key]
  13. end
  14. def to_hash
  15. @wrapped_response.to_hash
  16. end
  17. end
  18. class NetHTTPRequest < AbstractRequest
  19. def initialize(connection, request)
  20. @connection = connection
  21. @request = request
  22. end
  23. NET_HTTP = 'Net::HTTP'
  24. def type
  25. NET_HTTP
  26. end
  27. HOST = 'host'
  28. COLON = ':'
  29. def host_from_header
  30. if hostname = self[HOST]
  31. hostname.split(COLON).first
  32. end
  33. end
  34. def host
  35. host_from_header || @connection.address
  36. end
  37. def method
  38. @request.method
  39. end
  40. def [](key)
  41. @request[key]
  42. end
  43. def []=(key, value)
  44. @request[key] = value
  45. end
  46. def uri
  47. case @request.path
  48. when /^https?:\/\//
  49. ::NewRelic::Agent::HTTPClients::URIUtil.parse_and_normalize_url(@request.path)
  50. else
  51. connection_address = @connection.address
  52. if (connection_address =~ Resolv::IPv6::Regex)
  53. connection_address = "[#{connection_address}]"
  54. end
  55. scheme = @connection.use_ssl? ? 'https' : 'http'
  56. ::NewRelic::Agent::HTTPClients::URIUtil.parse_and_normalize_url(
  57. "#{scheme}://#{connection_address}:#{@connection.port}#{@request.path}"
  58. )
  59. end
  60. end
  61. end
  62. end
  63. end
  64. end