<commit_before>require 'sinatra/base'
require 'uri'
require_relative 'urls'

class UrlShortener < Sinatra::Application

  INVALID_URL_ERROR = 'Please+enter+a+valid+URL'
  INVALID_VANITY_ERROR = 'That+vanity+name+is+already+taken'

  class << self
    attr_accessor :urls
  end

  helpers do
    include Rack::Utils
    alias_method :h, :escape_html
  end

  get '/' do
    error = params[:error]
    url_to_shorten = params[:url]
    vanity_name = params[:vanity_name]
    erb :index, :locals => {:error => error, :url_to_shorten => url_to_shorten, :vanity_name => vanity_name}
  end

  post '/shorten' do
    url_to_shorten = params[:url_to_shorten]
    vanity_name = params[:vanity_name]
    if is_valid_url?(url_to_shorten)
      vanity_url = Urls.create?(url_to_shorten, vanity_name)
      if vanity_url
        redirect to("/#{vanity_url}?stats=true")
      else
        redirect to("/?url=#{url_to_shorten}&error=#{INVALID_VANITY_ERROR}&vanity_name=#{vanity_name}")
      end
    else
      redirect to("/?url=#{url_to_shorten}&error=#{INVALID_URL_ERROR}&vanity_name=#{vanity_name}")
    end

  end

  get '/:id' do
    view_stats = params[:stats] == 'true'
    url_record = Urls.find?(params[:id])
    new_url = "#{request.base_url}/#{params[:id]}"
    if url_record && view_stats
      erb :stats, :locals => {:url_record => url_record, :new_url => new_url}
    elsif !url_record
      redirect '/'
    else
      url_record.increase_views
      redirect url_record.url
    end
  end

  private
  def is_valid_url?(url_to_shorten)
    url = false
    if url_to_shorten.empty? || url_to_shorten.split(' ').count > 1 || url_to_shorten.nil?
    elsif url_to_shorten
      if url_to_shorten =~ /^#{URI::regexp}$/
        url = true
      end
      url
    end
  end

end<commit_msg>Added URI parse to URL to fix vulnerability<commit_after>require 'sinatra/base'
require 'uri'
require_relative 'urls'

class UrlShortener < Sinatra::Application

  INVALID_URL_ERROR = 'Please+enter+a+valid+URL'
  INVALID_VANITY_ERROR = 'That+vanity+name+is+already+taken'

  class << self
    attr_accessor :urls
  end

  helpers do
    include Rack::Utils
    alias_method :h, :escape_html
  end

  get '/' do
    error = params[:error]
    url_to_shorten = params[:url]
    vanity_name = params[:vanity_name]
    erb :index, :locals => {:error => error, :url_to_shorten => url_to_shorten, :vanity_name => vanity_name}
  end

  post '/shorten' do
    url_to_shorten = params[:url_to_shorten]
    vanity_name = params[:vanity_name]
    if is_valid_url?(url_to_shorten)
      vanity_url = Urls.create?(url_to_shorten, vanity_name)
      if vanity_url
        redirect to("/#{vanity_url}?stats=true")
      else
        redirect to(URI.parse(URI.encode("/?url=#{url_to_shorten}&error=#{INVALID_VANITY_ERROR}&vanity_name=#{vanity_name}")))
      end
    else
      redirect to(URI.parse(URI.encode("/?url=#{url_to_shorten}&error=#{INVALID_URL_ERROR}&vanity_name=#{vanity_name}")))
    end

  end

  get '/:id' do
    view_stats = params[:stats] == 'true'
    url_record = Urls.find?(params[:id])
    new_url = "#{request.base_url}/#{params[:id]}"
    if url_record && view_stats
      erb :stats, :locals => {:url_record => url_record, :new_url => new_url}
    elsif !url_record
      redirect '/'
    else
      url_record.increase_views
      redirect url_record.url
    end
  end

  private
  def is_valid_url?(url_to_shorten)
    url = false
    if url_to_shorten.empty? || url_to_shorten.split(' ').count > 1 || url_to_shorten.nil?
    elsif url_to_shorten
      if url_to_shorten =~ /^#{URI::regexp}$/
        url = true
      end
      url
    end
  end

end
{'fraction_non_alphanumeric': 0.12719298245614036, 'fraction_numerical': 0.0005482456140350877, 'mean_word_length': 3.516089108910891, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '7510779', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1337, 'n_words': 248}