<commit_before>/*******************************************************************************
 * Copyright (c) 2014 SRCC MSU
 *
 * Distributed under the MIT License - see the accompanying file LICENSE.txt.
 ******************************************************************************/

package ru.parallel.octotron.http;

import com.sun.net.httpserver.*;
import org.apache.commons.lang3.tuple.Pair;
import ru.parallel.octotron.core.primitive.exception.ExceptionParseError;
import ru.parallel.octotron.core.primitive.exception.ExceptionSystemError;
import ru.parallel.octotron.exec.GlobalSettings;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * HTTP server for requests processing<br>
 * */
public class HTTPServer
{
	private final static Logger LOGGER = Logger.getLogger("octotron");

	private final Queue<ParsedHttpRequest> requests;
	private final Queue<ParsedHttpRequest> blocking_requests;

	private HttpServer server;
	private final ExecutorService executor;

/**
 * parse request to tokens and add parsed_request to message queue
 * id the parsed_request is not blocking - close the underlying request
 * */
	private class StandardHandler implements HttpHandler
	{
		@Override
		public void handle(HttpExchange http_exchange)
			throws IOException
		{
			HTTPRequest http_request = new HTTPRequest(http_exchange);

			ParsedHttpRequest request;

			try
			{
				request = RequestParser.ParseFromHttp(http_request);
			}
			catch (ExceptionParseError e)
			{
				http_request.FinishError(e.getMessage());

				LOGGER.log(Level.WARNING, "request failed: "
					+ http_request.GetPath() + http_request.GetQuery(), e);

				return;
			}

			if(!request.GetParsedRequest().IsBlocking())
			{
				http_request.FinishString("request queued");
				requests.add(request);
			}
			else
				blocking_requests.add(request);
		}
	}

/**
 * parse request to tokens and add parsed_request to message queue
 * id the parsed_request is not blocking - close the underlying request
 * */
	private class DefaultHandler implements HttpHandler
	{
		@Override
		public void handle(HttpExchange http_exchange)
			throws IOException
		{
			HTTPRequest http_request = new HTTPRequest(http_exchange);

			http_request.FinishString("URI is not in format /request_type/operation");
		}
	}

	private static BasicAuthenticator GetAuth(String area, Pair<String, String> auth)
	{
		final String user_ref = auth.getLeft();
		final String password_ref = auth.getRight();

		return new BasicAuthenticator(area)
		{
			@Override
			public boolean checkCredentials(String user, String password)
			{
				return user.equals(user_ref) && password.equals(<PASSWORD>);
			}
		};
	}

/**
 * create and start the server, listening on /port<br>
 * messages are not guaranteed to come in fixed order<br>
 * */
	public HTTPServer(GlobalSettings settings)
		throws ExceptionSystemError
	{
		// why is it turned off by default >.<
		if(!Boolean.getBoolean("sun.net.httpserver.nodelay"))
			LOGGER.log(Level.CONFIG, "nodelay is not set to true, import will be slow. Add '-Dsun.net.httpserver.nodelay=true' as argument to java command.");

		requests = new ConcurrentLinkedQueue<>();
		blocking_requests = new ConcurrentLinkedQueue<>();

		executor = Executors.newCachedThreadPool();

		try
		{
			server = HttpServer.create(new InetSocketAddress(settings.GetPort()), 0);
		}
		catch (IOException e)<commit_msg>logging for failed auth<commit_after>/*******************************************************************************
 * Copyright (c) 2014 SRCC MSU
 *
 * Distributed under the MIT License - see the accompanying file LICENSE.txt.
 ******************************************************************************/

package ru.parallel.octotron.http;

import com.sun.net.httpserver.*;
import org.apache.commons.lang3.tuple.Pair;
import ru.parallel.octotron.core.primitive.exception.ExceptionParseError;
import ru.parallel.octotron.core.primitive.exception.ExceptionSystemError;
import ru.parallel.octotron.exec.GlobalSettings;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * HTTP server for requests processing<br>
 * */
public class HTTPServer
{
	private final static Logger LOGGER = Logger.getLogger("octotron");

	private final Queue<ParsedHttpRequest> requests;
	private final Queue<ParsedHttpRequest> blocking_requests;

	private HttpServer server;
	private final ExecutorService executor;

/**
 * parse request to tokens and add parsed_request to message queue
 * id the parsed_request is not blocking - close the underlying request
 * */
	private class StandardHandler implements HttpHandler
	{
		@Override
		public void handle(HttpExchange http_exchange)
			throws IOException
		{
			HTTPRequest http_request = new HTTPRequest(http_exchange);

			ParsedHttpRequest request;

			try
			{
				request = RequestParser.ParseFromHttp(http_request);
			}
			catch (ExceptionParseError e)
			{
				http_request.FinishError(e.getMessage());

				LOGGER.log(Level.WARNING, "request failed: "
					+ http_request.GetPath() + http_request.GetQuery(), e);

				return;
			}

			if(!request.GetParsedRequest().IsBlocking())
			{
				http_request.FinishString("request queued");
				requests.add(request);
			}
			else
				blocking_requests.add(request);
		}
	}

/**
 * parse request to tokens and add parsed_request to message queue
 * id the parsed_request is not blocking - close the underlying request
 * */
	private class DefaultHandler implements HttpHandler
	{
		@Override
		public void handle(HttpExchange http_exchange)
			throws IOException
		{
			HTTPRequest http_request = new HTTPRequest(http_exchange);

			http_request.FinishString("URI is not in format /request_type/operation");
		}
	}

	private static BasicAuthenticator GetAuth(String area, Pair<String, String> auth)
	{
		final String user_ref = auth.getLeft();
		final String password_ref = auth.getRight();

		return new BasicAuthenticator(area)
		{
			@Override
			public boolean checkCredentials(String user, String password)
			{
				boolean result = user.equals(user_ref) && password.equals(password_ref);

				if(result)
					LOGGER.log(Level.FINE, "[SUCCESS] authentication attempt, username: " + user + ", password: " + password);
				else
					LOGGER.log(Level.WARNING, "[FAIL] authentication attempt, username: " + user + ", password: " + password);

				return result;
			}
		};
	}

/**
 * create and start the server, listening on /port<br>
 * messages are not guaranteed to come in fixed order<br>
 * */
	public HTTPServer(GlobalSettings settings)
		throws ExceptionSystemError
	{
		// why is it turned off by default >.<
		if(!Boolean.getBoolean("sun.net.httpserver.nodelay"))
			LOGGER.log(Level.CONFIG, "nodelay is not set to true, import will be slow. Add '-Dsun.net.httpserver.nodelay=true' as argument to java command.");

		requests = new ConcurrentLinkedQueue<>();
		blocking_requests = new ConcurrentLinkedQueue<>();

		executor = Executors.newCachedThreadPool();

		try
		{
			server = HttpServer.create(new InetSocketAddress(settings.GetPort()), 0);
		}
		catch (IOException e)
{'fraction_non_alphanumeric': 0.13627307743392217, 'fraction_numerical': 0.00159383716296985, 'mean_word_length': 4.644677661169415, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '1213810', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2083, 'n_words': 555}