<commit_before>require 'billy/handlers/handler'
require 'addressable/uri'
require 'eventmachine'
require 'em-synchrony/em-http'

module Billy
  class ProxyHandler
    include Handler

    def handles_request?(_method, url, _headers, _body)
      !disabled_request?(url)
    end

    def handle_request(method, url, headers, body)
      if handles_request?(method, url, headers, body)
        opts = { inactivity_timeout: Billy.config.proxied_request_inactivity_timeout,
                 connect_timeout:    Billy.config.proxied_request_connect_timeout }

        if Billy.config.proxied_request_host && !bypass_internal_proxy?(url)
          opts.merge!({ proxy: { host: Billy.config.proxied_request_host,
                                 port: Billy.config.proxied_request_port }} )
        end

        req = EventMachine::HttpRequest.new(url, opts)
        req = req.send(method.downcase, build_request_options(url, headers, body))

        if req.error
          return { error: "Request to #{url} failed with error: #{req.error}" }
        end

        if req.response
          response = process_response(req)

          unless allowed_response_code?(response[:status])
            if Billy.config.non_successful_error_level == :error
              return { error: "Request failed due to response status #{response[:status]} for '#{url}' which was not allowed." }
            else<commit_msg>output the request method when proxy returns an error<commit_after>require 'billy/handlers/handler'
require 'addressable/uri'
require 'eventmachine'
require 'em-synchrony/em-http'

module Billy
  class ProxyHandler
    include Handler

    def handles_request?(_method, url, _headers, _body)
      !disabled_request?(url)
    end

    def handle_request(method, url, headers, body)
      if handles_request?(method, url, headers, body)
        opts = { inactivity_timeout: Billy.config.proxied_request_inactivity_timeout,
                 connect_timeout:    Billy.config.proxied_request_connect_timeout }

        if Billy.config.proxied_request_host && !bypass_internal_proxy?(url)
          opts.merge!({ proxy: { host: Billy.config.proxied_request_host,
                                 port: Billy.config.proxied_request_port }} )
        end

        req = EventMachine::HttpRequest.new(url, opts)
        req = req.send(method.downcase, build_request_options(url, headers, body))

        if req.error
          return { error: "Request to #{url} failed with error: #{req.error}" }
        end

        if req.response
          response = process_response(req)

          unless allowed_response_code?(response[:status])
            if Billy.config.non_successful_error_level == :error
              return { error: "#{method} Request failed due to response status #{response[:status]} for '#{url}' which was not allowed." }
            else
{'fraction_non_alphanumeric': 0.09585674157303371, 'fraction_numerical': 0.0, 'mean_word_length': 2.8604336043360434, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7539351', 'n_tokens_mistral': 798, 'n_tokens_neox': 793, 'n_words': 190}