<commit_before>package org.wikimedia.html5depurate;

import org.wikimedia.html5depurate.Config;
import org.wikimedia.html5depurate.MultipartBuffer;
import org.wikimedia.html5depurate.Depurator;

import org.glassfish.grizzly.http.multipart.MultipartScanner;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.http.util.HttpStatus;
import org.glassfish.grizzly.EmptyCompletionHandler;

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.logging.Logger;
import java.util.logging.Level;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

class DepurateHandler extends HttpHandler {
	final private Config m_config;
	Logger m_logger = Logger.getLogger(this.getClass().getName());

	DepurateHandler(Config config) {
		super("depurate");
		m_config = config;
	}

	@Override
	public void service(final Request request, final Response response)
			throws Exception
	{
		response.suspend();
		request.setCharacterEncoding("UTF-8");
		final MultipartBuffer buf = new MultipartBuffer(m_config.maxPostSize);

		MultipartScanner.scan(request,
			buf,
			new EmptyCompletionHandler<Request>() {
				@Override
				public void completed(final Request request) {
					depurate(request, response, buf);
					response.resume();
				}

				@Override
				public void failed(Throwable throwable) {
					depurate(request, response, buf);
					response.resume();
				}
			}
		);
	}

	private void depurate(
			final Request request, Response response, MultipartBuffer multi)
	{
		try {
			if (multi.isTooBig()) {
				sendError(response, 400, "The POST size was too large");
				return;
			}<commit_msg>Don't repeat parse 3 times per request<commit_after>package org.wikimedia.html5depurate;

import org.wikimedia.html5depurate.Config;
import org.wikimedia.html5depurate.MultipartBuffer;
import org.wikimedia.html5depurate.Depurator;

import org.glassfish.grizzly.http.multipart.MultipartScanner;
import org.glassfish.grizzly.http.server.HttpHandler;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.http.util.HttpStatus;
import org.glassfish.grizzly.EmptyCompletionHandler;

import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.logging.Logger;
import java.util.logging.Level;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

class DepurateHandler extends HttpHandler {
	final private Config m_config;
	Logger m_logger = Logger.getLogger(this.getClass().getName());

	DepurateHandler(Config config) {
		super("depurate");
		m_config = config;
	}

	@Override
	public void service(final Request request, final Response response)
			throws Exception
	{
		m_logger.finer("Request received");

		response.suspend();
		request.setCharacterEncoding("UTF-8");
		final MultipartBuffer buf = new MultipartBuffer(m_config.maxPostSize);

		MultipartScanner.scan(request,
			buf,
			new EmptyCompletionHandler<Request>() {
				private boolean m_done = false;

				@Override
				public void completed(final Request request) {
					m_logger.finer("Multipart complete");
					continueRequest(request);
				}

				@Override
				public void failed(Throwable throwable) {
					m_logger.finer("Multipart failed");
					continueRequest(request);
				}

				private void continueRequest(final Request request) {
					// MultipartReadHandler normally calls us 3 times. This is apparently a bug.
					if (!m_done) {
						m_done = true;
						depurate(request, response, buf);
						response.resume();
					}
				}
			}
		);
	}

	private void depurate(
			final Request request, Response response, MultipartBuffer multi)
	{
		try {
			if (multi.isTooBig()) {
				sendError(response, 400, "The POST size was too large");
				return;
			}
{'fraction_non_alphanumeric': 0.10162297128589264, 'fraction_numerical': 0.0044943820224719105, 'mean_word_length': 4.563888888888889, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6035399', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1226, 'n_words': 264}