<commit_before>require 'sinatra/base'
require File.dirname(__FILE__) + '/config'
require File.dirname(__FILE__) + '/utils'
require File.dirname(__FILE__) + '/db_session'

module Taps
class Server < Sinatra::Default
	use Rack::Auth::Basic do |login, password|
		login == Taps::Config.login && password == Taps::Config.password
	end

	error do
		"Application error"
	end

	before do
		unless request.env['HTTP_TAPS_VERSION'] == Taps.compatible_version
			halt 417, "Taps v#{Taps.compatible_version}.x is required for this server"
		end
	end

	get '/' do
		"hello"
	end

	post '/sessions' do
		key = rand(9999999999).to_s
		database_url = Taps::Config.database_url || request.body.string

		DbSession.create(:key => key, :database_url => database_url, :started_at => Time.now, :last_access => Time.now)

		"/sessions/#{key}"
	end

	post '/sessions/:key/tables/:table' do
		session = DbSession.filter(:key => params[:key]).first
		halt 404 unless session

		gzip_data = request.body.read
		halt 412 unless Taps::Utils.valid_data?(gzip_data, request.env['HTTP_TAPS_CHECKSUM'])
<commit_msg>server: handle older clients that send the full taps version<commit_after>require 'sinatra/base'
require File.dirname(__FILE__) + '/config'
require File.dirname(__FILE__) + '/utils'
require File.dirname(__FILE__) + '/db_session'

module Taps
class Server < Sinatra::Default
	use Rack::Auth::Basic do |login, password|
		login == Taps::Config.login && password == Taps::Config.password
	end

	error do
		"Application error"
	end

	before do
		major, minor, patch = request.env['HTTP_TAPS_VERSION'].split('.') rescue []
		unless "#{major}.#{minor}" == Taps.compatible_version
			halt 417, "Taps v#{Taps.compatible_version}.x is required for this server"
		end
	end

	get '/' do
		"hello"
	end

	post '/sessions' do
		key = rand(9999999999).to_s
		database_url = Taps::Config.database_url || request.body.string

		DbSession.create(:key => key, :database_url => database_url, :started_at => Time.now, :last_access => Time.now)

		"/sessions/#{key}"
	end

	post '/sessions/:key/tables/:table' do
		session = DbSession.filter(:key => params[:key]).first
		halt 404 unless session

		gzip_data = request.body.read
		halt 412 unless Taps::Utils.valid_data?(gzip_data, request.env['HTTP_TAPS_CHECKSUM'])

{'fraction_non_alphanumeric': 0.13903508771929823, 'fraction_numerical': 0.016666666666666666, 'mean_word_length': 4.731155778894473, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3555105', 'n_tokens_mistral': 838, 'n_tokens_neox': 750, 'n_words': 186}