<commit_before>require 'libraries'

class Hurl < Sinatra::Base
  dir = File.dirname(File.expand_path(__FILE__))

  set :views,  "#{dir}/views"
  set :public, "#{dir}/public"
  set :static, true

  def initialize(*args)
    super
    @redis = Redis.new(:host => '127.0.0.1', :port => 6379)
  end


  #
  # routes
  #

  get '/' do
    erb :index
  end

  post '/' do
    url, method, auth = params.values_at(:url, :method, :auth)
    curl = Curl::Easy.new(url)

    requests = []
    curl.on_debug do |type, data|
      # track request headers
      requests << data if type == Curl::CURLINFO_HEADER_OUT
    end

    curl.follow_location = true if params[:follow_redirects]

    # ensure a method is set
    method = method.to_s.empty? ? 'GET' : method

    # basic auth
    if auth == 'basic'
      username, password = params.values_at(:basic_username, :basic_password)
      encoded = Base64.encode64("#{username}:#{password}")
      curl.headers['Authorization'] = "Basic #{encoded}"
    end

    # arbitrary headers
    add_headers_from_arrays(curl, params["header-keys"], params["header-vals"])

    if method == 'POST'
      fields = []
      params["param-keys"].each_with_index do |name, i|
        value = params["param-vals"][i]
        next if name.to_s.empty? || value.to_s.empty?
        fields << Curl::PostField.content(name, value)
      end
      curl.post_body = fields.join('&')
    end

    begin
      curl.send "http_#{method.downcase}"
      json :header  => pretty_print_headers(curl.header_str),
           :body    => pretty_print(curl.content_type, curl.body_str),
           :request => pretty_print_requests(requests, fields)
    rescue => e
      json :error => "error: #{e}"
    end
  end


  #
  # http helpers
  #

  # accepts two arrays: keys and values
  # the elements in keys must map to the
  # elements in values
  #
  # empty values means the key is ignored
  def add_headers_from_arrays(curl, keys, values)
    keys, values = Array(keys), Array(values)

    keys.each_with_index do |key, i|<commit_msg>old way is a ok<commit_after>require 'libraries'

class Hurl < Sinatra::Base
  dir = File.dirname(File.expand_path(__FILE__))

  set :views,  "#{dir}/views"
  set :public, "#{dir}/public"
  set :static, true

  def initialize(*args)
    super
    @redis = Redis.new(:host => '127.0.0.1', :port => 6379)
  end


  #
  # routes
  #

  get '/' do
    erb :index
  end

  post '/' do
    url, method, auth = params.values_at(:url, :method, :auth)
    curl = Curl::Easy.new(url)

    requests = []
    curl.on_debug do |type, data|
      # track request headers
      requests << data if type == Curl::CURLINFO_HEADER_OUT
    end

    curl.follow_location = true if params[:follow_redirects]

    # ensure a method is set
    method = method.to_s.empty? ? 'GET' : method

    # basic auth
    if auth == 'basic'
      username, password = params.values_at(:basic_username, :basic_password)
      encoded = Base64.encode64("#{username}:#{password}")
      curl.headers['Authorization'] = "Basic #{encoded}"
    end

    # arbitrary headers
    add_headers_from_arrays(curl, params["header-keys"], params["header-vals"])

    fields = []
    if method == 'POST'
      params["param-keys"].each_with_index do |name, i|
        value = params["param-vals"][i]
        next if name.to_s.empty? || value.to_s.empty?
        fields << Curl::PostField.content(name, value)
      end
    end

    begin
      curl.send("http_#{method.downcase}", *fields)
      json :header  => pretty_print_headers(curl.header_str),
           :body    => pretty_print(curl.content_type, curl.body_str),
           :request => pretty_print_requests(requests, fields)
    rescue => e
      json :error => "error: #{e}"
    end
  end


  #
  # http helpers
  #

  # accepts two arrays: keys and values
  # the elements in keys must map to the
  # elements in values
  #
  # empty values means the key is ignored
  def add_headers_from_arrays(curl, keys, values)
    keys, values = Array(keys), Array(values)

    keys.each_with_index do |key, i|
{'fraction_non_alphanumeric': 0.13313609467455623, 'fraction_numerical': 0.006903353057199211, 'mean_word_length': 2.863809523809524, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1123889', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1322, 'n_words': 335}