<commit_before>require 'libraries'

module Hurl
  def self.redis
    @redis
  end

  def self.redis=(redis)
    @redis = redis
  end

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

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

    enable :sessions

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

    def redis
      Hurl.redis
    end


    #
    # routes
    #

    before do
      if load_session
        @user = User.find_by_email(@session['email'])
      end
      @flash = session.delete('flash')
    end

    helpers do
      include Hurl::Helpers
    end

    get '/' do
      @hurl = {}
      erb :index
    end

    get '/hurls/?' do
      redirect('/') and return unless logged_in?
      @hurls = @user.hurls
      erb :hurls
    end

    get '/hurls/:id/?' do
      @hurl = find_hurl_or_view(params[:id])
      @hurl ? erb(:index) : not_found
    end

    delete '/hurls/:id/?' do
      redirect('/') and return unless logged_in?

      if @hurl = find_hurl_or_view(params[:id])
        @user.remove_hurl(@hurl['id'])
      end
      request.xhr? ? "ok" : redirect('/')
    end

    get '/hurls/:id/:view_id/?' do
      @hurl = find_hurl_or_view(params[:id])
      @view = find_hurl_or_view(params[:view_id])
      @view_id = params[:view_id]
      @hurl && @view ? erb(:index) : not_found
    end

    get '/views/:id/?' do
      @view = find_hurl_or_view(params[:id])
      @view ? erb(:view, :layout => false) : not_found
    end

    get '/about/?' do
      erb :about
    end

    get '/stats/?' do
      erb :stats
    end

    get '/logout/?' do
      clear_session
      session['flash'] = 'see you later!'
      redirect '/'
    end

    post '/login/?' do
      email, password = params.values_at(:email, :password)

      if User.authenticate(email, password)
        create_session(:email => email)
        json :success => true
      else
        json :error => 'incorrect email or password'
      end
    end

    post '/signup/?' do
      email, password = params.values_at(:email, :password)
      user = User.create(:email => email, :password => password)

      if user.valid?
        create_session(:email => email)
        stat :users
        session['flash'] = 'welcome to hurl!'
        json :success => true
      else
        json :error => user.errors.to_s
      end
    end

    post '/' do
      return json(:error => "Calm down and try my margarita!") if rate_limited?

      url, method, auth = params.values_at(:url, :method, :auth)

      return json(:error => "That's... wait.. what?") if invalid_url?(url)

      curl = Curl::Easy.new(url)

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

      curl.follow_location = true if params[:follow_redirects]
<commit_msg>Prevent Hurl from posting to itself. closes #1<commit_after>require 'libraries'

module Hurl
  def self.redis
    @redis
  end

  def self.redis=(redis)
    @redis = redis
  end

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

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

    enable :sessions

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

    def redis
      Hurl.redis
    end


    #
    # routes
    #

    before do
      if load_session
        @user = User.find_by_email(@session['email'])
      end
      @flash = session.delete('flash')
    end

    helpers do
      include Hurl::Helpers
    end

    get '/' do
      @hurl = {}
      erb :index
    end

    get '/hurls/?' do
      redirect('/') and return unless logged_in?
      @hurls = @user.hurls
      erb :hurls
    end

    get '/hurls/:id/?' do
      @hurl = find_hurl_or_view(params[:id])
      @hurl ? erb(:index) : not_found
    end

    delete '/hurls/:id/?' do
      redirect('/') and return unless logged_in?

      if @hurl = find_hurl_or_view(params[:id])
        @user.remove_hurl(@hurl['id'])
      end
      request.xhr? ? "ok" : redirect('/')
    end

    get '/hurls/:id/:view_id/?' do
      @hurl = find_hurl_or_view(params[:id])
      @view = find_hurl_or_view(params[:view_id])
      @view_id = params[:view_id]
      @hurl && @view ? erb(:index) : not_found
    end

    get '/views/:id/?' do
      @view = find_hurl_or_view(params[:id])
      @view ? erb(:view, :layout => false) : not_found
    end

    get '/about/?' do
      erb :about
    end

    get '/stats/?' do
      erb :stats
    end

    get '/logout/?' do
      clear_session
      session['flash'] = 'see you later!'
      redirect '/'
    end

    post '/login/?' do
      email, password = params.values_at(:email, :password)

      if User.authenticate(email, password)
        create_session(:email => email)
        json :success => true
      else
        json :error => 'incorrect email or password'
      end
    end

    post '/signup/?' do
      email, password = params.values_at(:email, :password)
      user = User.create(:email => email, :password => password)

      if user.valid?
        create_session(:email => email)
        stat :users
        session['flash'] = 'welcome to hurl!'
        json :success => true
      else
        json :error => user.errors.to_s
      end
    end

    post '/' do
      return json(:error => "Calm down and try my margarita!") if rate_limited?

      url, method, auth = params.values_at(:url, :method, :auth)

      return json(:error => "That's... wait.. what?!") if invalid_url?(url)

      curl = Curl::Easy.new(url)

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

      curl.follow_location = true if params[:follow_redirects]

{'fraction_non_alphanumeric': 0.1333663039894494, 'fraction_numerical': 0.0034619188921859545, 'mean_word_length': 2.132163138874548, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5332422', 'n_tokens_mistral': 2083, 'n_tokens_neox': 2022, 'n_words': 513}