Sha256: 166d5c01c29c0e709de79afdb3d149037e4c8526b5eef12e298b684b7e8535db

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

if defined?(::HTTP::Client) && defined?(::HTTP::Connection)
  module ::HTTP
    class Client

      #
      request_method = respond_to?('make_request') ? 'make_request' : 'perform'
      orig_request_method = "orig_#{request_method}"
      alias_method(orig_request_method, request_method) unless method_defined?(orig_request_method)

      define_method request_method do |req, options|

        log_enabled = HttpLog.url_approved?(req.uri)

        if log_enabled
          HttpLog.log_request(req.verb, req.uri)
          HttpLog.log_headers(req.headers.to_h)

          if defined?(::HTTP::Request::Body)
            body = req.body.respond_to?(:source) ? req.body.source : req.body.instance_variable_get(:@body)
          else
            body = req.body
          end

          HttpLog.log_data(body.to_s)
          body.rewind if body.respond_to?(:rewind)
        end

        bm = Benchmark.realtime do
          @response = send(orig_request_method, req, options)
        end

        if log_enabled
          headers = @response.headers
          HttpLog.log_compact(req.verb, req.uri, @response.code, bm)
          HttpLog.log_status(@response.code)
          HttpLog.log_benchmark(bm)
          HttpLog.log_headers(@response.headers.to_h)
          HttpLog.log_body(@response.body, headers['Content-Encoding'], headers['Content-Type'])
        end

        @response
      end
    end

    class Connection
      alias_method(:orig_initialize, :initialize) unless method_defined?(:orig_initialize)

      def initialize(req, options)
        HttpLog.log_connection(req.uri.host, req.uri.port) if HttpLog.url_approved?(req.uri)
        orig_initialize(req, options)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
httplog-1.0.2 lib/httplog/adapters/http.rb
httplog-1.0.1 lib/httplog/adapters/http.rb