<commit_before>require 'uri'

require_relative './frontend.rb'
require_relative './backend.rb'

def app(server)
	# Main loop that runs eternally
	loop do

		# Receive the request
		socket = server.accept
		
		request = socket.gets.split

		headers = {}
		while line = socket.gets.split(' ', 2)
			break if line[0] == "" 
			headers[line[0].chop] = line[1].strip 
		end

		data = socket.read(headers["Content-Length"].to_i)
		# string -> array tuples (then decode them) -> flat array -> hash
		data = Hash[*data.split("&").map {
			|pair| pair.split("="). map { |item| URI.decode item }
		}.flatten]

		# Sort by type
		if request[0] == 'GET'
			Frontend.new(socket).response(request)
		else
			Backend.new(socket).response(request, data)
		end
	end
end<commit_msg>App can now fail semi-gracefully<commit_after>require 'uri'

require_relative './frontend.rb'
require_relative './backend.rb'

def app(server)
	# Main loop that runs eternally
	loop do

		# Receive the request
		socket = server.accept
		
		# Try to handle request
		begin
			request = socket.gets.split

			headers = {}
			while line = socket.gets.split(' ', 2)
				break if line[0] == "" 
				headers[line[0].chop] = line[1].strip 
			end

			data = socket.read(headers["Content-Length"].to_i)
			# string -> array tuples (then decode them) -> flat array -> hash
			data = Hash[*data.split("&").map {
				|pair| pair.split("="). map { |item| URI.decode item }
			}.flatten]

			# Sort by type
			if request[0] == 'GET'
				Frontend.new(socket).response(request)
			else
				Backend.new(socket).response(request, data)
			end
		rescue Exception => e
			puts e.message
			puts e.backtrace
			Frontend.new(socket).throw_error()
		end
	end
end
{'fraction_non_alphanumeric': 0.1443661971830986, 'fraction_numerical': 0.005868544600938967, 'mean_word_length': 3.1995073891625614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '4345754', 'n_tokens_mistral': 666, 'n_tokens_neox': 573, 'n_words': 149}