<commit_before>require 'securerandom'
require 'unicorn-cuba-base/stats'

module Plugin
	module ResponseHelpers
		extend Stats
		def_stats(
			:total_write_multipart,
			:total_write, 
			:total_write_part, 
			:total_write_error, 
			:total_write_error_part
		)

		def write(code, content_type, body)
			res.status = code
			res["Content-Type"] = content_type
			ResponseHelpers.stats.incr_total_write
			res.write body
		end

		def write_plain(code, msg)
			write code, 'text/plain', msg.gsub("\n", "\r\n") + "\r\n"
		end

		def write_error(code, error)
			msg = error.message
			log.warn "sending #{code} error response: #{msg}"
			ResponseHelpers.stats.incr_total_write_error
			write_plain code, msg
		end

		def write_url_list(code, urls)
			write code, 'text/uri-list', urls.join("\r\n") + "\r\n"
		end

		# Multipart
		def write_preamble(code, headers = {})
			res.status = code
			@boundary = SecureRandom.uuid
			res["Content-Type"] = "multipart/mixed; boundary=\"#{@boundary}\""
			headers.each do |key, value|
				res[key] = value
			end
			ResponseHelpers.stats.incr_total_write_multipart
		end

		def write_part(content_type, body, headers = {})
			res.write "--#{@boundary}\r\n"
			res.write "Content-Type: #{content_type}\r\n"
			headers.each_pair do |name, value|
				res.write "#{name}: #{value}\r\n"
			end
			res.write "\r\n"<commit_msg>write_plain now supports array and '\r\n' line ended strings<commit_after>require 'securerandom'
require 'unicorn-cuba-base/stats'

module Plugin
	module ResponseHelpers
		extend Stats
		def_stats(
			:total_write_multipart,
			:total_write, 
			:total_write_part, 
			:total_write_error, 
			:total_write_error_part
		)

		def write(code, content_type, body)
			res.status = code
			res["Content-Type"] = content_type
			ResponseHelpers.stats.incr_total_write
			res.write body
		end

		def write_plain(code, msg)
			msg = msg.join("\r\n") if msg.is_a? Array
			write code, 'text/plain', msg.gsub(/(?<!\r)\n/, "\r\n") + "\r\n"
		end

		def write_error(code, error)
			msg = error.message
			log.warn "sending #{code} error response: #{msg}"
			ResponseHelpers.stats.incr_total_write_error
			write_plain code, msg
		end

		def write_url_list(code, urls)
			write code, 'text/uri-list', urls.join("\r\n") + "\r\n"
		end

		# Multipart
		def write_preamble(code, headers = {})
			res.status = code
			@boundary = SecureRandom.uuid
			res["Content-Type"] = "multipart/mixed; boundary=\"#{@boundary}\""
			headers.each do |key, value|
				res[key] = value
			end
			ResponseHelpers.stats.incr_total_write_multipart
		end

		def write_part(content_type, body, headers = {})
			res.write "--#{@boundary}\r\n"
			res.write "Content-Type: #{content_type}\r\n"
			headers.each_pair do |name, value|
				res.write "#{name}: #{value}\r\n"
			end
			res.write "\r\n"
{'fraction_non_alphanumeric': 0.14087513340448238, 'fraction_numerical': 0.0, 'mean_word_length': 3.5723577235772357, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '1363082', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1030, 'n_words': 216}