Sha256: 70ff43f6ac4fecfe55b7c6fac369bf281a3c9a3263494e1c0ca526a2d6d726e4

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Net
  class HTTP
    alias orig_request request unless method_defined?(:orig_request)
    alias orig_connect connect unless method_defined?(:orig_connect)

    def request(req, body = nil, &block)
      url = "http://#{@address}:#{@port}#{req.path}"

      log_enabled = HttpLog.url_approved?(url)

      if log_enabled && started?
        HttpLog.log_request(req.method, url)
        HttpLog.log_headers(req.each_header.collect)
        # A bit convoluted becase post_form uses form_data= to assign the data, so
        # in that case req.body will be empty.
        HttpLog.log_data(req.body.nil? || req.body.empty? ? body : req.body) # if req.method == 'POST'
      end

      bm = Benchmark.realtime do
        @response = orig_request(req, body, &block)
      end

      if log_enabled && started?
        HttpLog.log_compact(req.method, url, @response.code, bm)
        HttpLog.log_status(@response.code)
        HttpLog.log_benchmark(bm)
        HttpLog.log_headers(@response.each_header.collect)
        HttpLog.log_body(@response.body, @response['Content-Encoding'], @response['Content-Type'])
      end

      @response
    end

    def connect
      HttpLog.log_connection(@address, @port) if !started? && HttpLog.url_approved?("#{@address}:#{@port}")

      orig_connect
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
httplog-1.1.1 lib/httplog/adapters/net_http.rb
httplog-1.1.0 lib/httplog/adapters/net_http.rb
httplog-1.0.3 lib/httplog/adapters/net_http.rb