<commit_before>require 'faraday'
require 'faraday_middleware'
require 'json'
require_relative 'response_rewriter'

module RestSpy
  module ProxyServer
    extend self

    def execute_remote_request(original_request, redirect_url, environment, rewrites)
      headers = extract_relevant_headers(environment)
      composed_url = URI::join(redirect_url, original_request.fullpath).to_s

      http_client = http_client(rewrites)

      if original_request.get?
        http_client.get(composed_url, headers)
      elsif original_request.post?
        body = original_request.body.read
        puts "Body: #{body}"
        puts "Request content type: #{original_request.content_type}"
        http_client.post(composed_url, headers, body)
      elsif original_request.put?
        http_client.put(composed_url, headers, original_request.body.read)
      elsif original_request.delete?
        http_client.delete(composed_url, headers)
      elsif original_request.head?
        http_client.head(composed_url, headers)
      else
        raise "#{original_request.request_method} requests are not supported."
      end
    end

    def extract_relevant_headers(environment)
      headers = Hash[environment
               .select { |k, _| k.start_with?("HTTP_") && k != "HTTP_HOST"}
               .map do |k, v|
                  header_field = k.sub(/^HTTP_/, '').gsub('_', '-')
                  [header_field, v]
               end]

      if environment['CONTENT_TYPE']
        headers['CONTENT-TYPE'] = environment['CONTENT_TYPE']
      end

      puts "Headers: #{headers}"
      headers
    end

    def http_client(rewrites)
      HttpClient.new(rewrites)
    end

    class HttpClient<commit_msg>Removed debug output.<commit_after>require 'faraday'
require 'faraday_middleware'
require 'json'
require_relative 'response_rewriter'

module RestSpy
  module ProxyServer
    extend self

    def execute_remote_request(original_request, redirect_url, environment, rewrites)
      headers = extract_relevant_headers(environment)
      composed_url = URI::join(redirect_url, original_request.fullpath).to_s

      http_client = http_client(rewrites)

      if original_request.get?
        http_client.get(composed_url, headers)
      elsif original_request.post?
        http_client.post(composed_url, headers, original_request.body.read)
      elsif original_request.put?
        http_client.put(composed_url, headers, original_request.body.read)
      elsif original_request.delete?
        http_client.delete(composed_url, headers)
      elsif original_request.head?
        http_client.head(composed_url, headers)
      else
        raise "#{original_request.request_method} requests are not supported."
      end
    end

    def extract_relevant_headers(environment)
      headers = Hash[environment
               .select { |k, _| k.start_with?("HTTP_") && k != "HTTP_HOST"}
               .map do |k, v|
                  header_field = k.sub(/^HTTP_/, '').gsub('_', '-')
                  [header_field, v]
               end]

      if environment['CONTENT_TYPE']
        headers['CONTENT-TYPE'] = environment['CONTENT_TYPE']
      end

      headers
    end

    def http_client(rewrites)
      HttpClient.new(rewrites)
    end

    class HttpClient
{'fraction_non_alphanumeric': 0.09288779889638259, 'fraction_numerical': 0.0, 'mean_word_length': 2.870699881376038, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2857686', 'n_tokens_mistral': 974, 'n_tokens_neox': 944, 'n_words': 192}