<commit_before>require 'sinatra'
require 'redis'
require 'securerandom'
require 'json'

redis = Redis.new
file = open 'config.json'
json = file.read
config = JSON.parse json

post '/' do
	key = SecureRandom::urlsafe_base64(4)
	json = JSON.parse request.body.read
	value = json['url']
	expires = if json.has_key? 'expires' and json['expires'] < 2592000 then json['expires'] else 2592000 end
	if value =~ /^https?:\/\/([a-z0-9-]+\.)+[a-z]{2,}(\/([a-zA-Z0-9-_]+(\.[a-z]+)?)?)*\??([a-z0-9_]+(=[a-z0-9_]*)?&?)*$/i
	  while redis.get("url:#{key}") do key = SecureRandom::urlsafe_base64(4) end
	  redis.set("url:#{key}", value, :ex => expires)
	  short_url = "#{request.scheme}://#{request.host_with_port}/#{key}"
	  [303, {'Location' => short_url}, '']
	else
		puts "Invalid URL: \"#{value}\""
		halt 422, {'Content-Type' => 'text/plain'}, 'Invalid URL'
  end
end

get '/:key' do
  key = 'url:' + params['key']
  value = redis.get(key)
  if value
    redirect value, 302
  else
    [404, {'Content-Type' => 'text/plain'}, "Sorry, that URL doesn't exist or has expired."]
  end<commit_msg>increased URL id to 7 characters<commit_after>require 'sinatra'
require 'redis'
require 'securerandom'
require 'json'

redis = Redis.new
file = open 'config.json'
json = file.read
config = JSON.parse json

post '/' do
	key = SecureRandom::urlsafe_base64(5)
	json = JSON.parse request.body.read
	value = json['url']
	expires = if json.has_key? 'expires' and json['expires'] < 2592000 then json['expires'] else 2592000 end
	if value =~ /^https?:\/\/([a-z0-9-]+\.)+[a-z]{2,}(\/([a-zA-Z0-9-_]+(\.[a-z]+)?)?)*\??([a-z0-9_]+(=[a-z0-9_]*)?&?)*$/i
	  while redis.get("url:#{key}") do key = SecureRandom::urlsafe_base64(5) end
	  redis.set("url:#{key}", value, :ex => expires)
	  short_url = "#{request.scheme}://#{request.host_with_port}/#{key}"
	  [303, {'Location' => short_url}, '']
	else
		puts "Invalid URL: \"#{value}\""
		halt 422, {'Content-Type' => 'text/plain'}, 'Invalid URL'
  end
end

get '/:key' do
  key = 'url:' + params['key']
  value = redis.get(key)
  if value
    redirect value, 302
  else
    [404, {'Content-Type' => 'text/plain'}, "Sorry, that URL doesn't exist or has expired."]
  end
{'fraction_non_alphanumeric': 0.22537792029317452, 'fraction_numerical': 0.038021071919377, 'mean_word_length': 4.079069767441861, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '2284257', 'n_tokens_mistral': 933, 'n_tokens_neox': 872, 'n_words': 196}