<commit_before>/**
 * Copyright 2017 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tools.descartes.httploadgenerator.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import tools.descartes.dlim.httploadgenerator.runner.TransactionQueueSingleton;
import tools.descartes.dlim.httploadgenerator.runner.ValidityTracker;
import tools.descartes.dlim.httploadgenerator.runner.Transaction;

/**
 * HTTP transaction sends HTML requests to a HTTP web server based on a LUA script.
 * 
 * @author <NAME>, <NAME>
 *
 */
public class HTTPTransaction extends Transaction {

	private static final String USER_AGENT = "Mozilla/5.0";
	private static final String POST_SIGNAL = "[POST]";

	/** The constant logging instance. */
	private static final Logger LOG = Logger.getLogger(HTTPTransaction.class.getName());

	/**
	 * Processes the transaction of sending a GET request to a web server.
	 * @param generator The input generator to use.
	 * @return HTML Response of the web server.
	 */
	public String process(HTTPInputGenerator generator) {
		HttpURLConnection con = null;
		BufferedReader in = null;
		CookieHandler.setDefault(generator.getCookieManager());
		URL obj;
		String url = generator.getNextInput().trim();
		try {
			String method = "GET";
			if (url.startsWith("[")) {
				if (url.startsWith(POST_SIGNAL)) {
					method = "POST";
				}
				url = url.replaceFirst("\\[.*\\]", "");
			}
			obj = new URL(url);
			con = (HttpURLConnection) obj.openConnection();

			con.setRequestMethod(method);
			con.setRequestProperty("User-Agent", USER_AGENT);
			
			if (con.getResponseCode() >= 400) {
				generator.revertLastCall();
				LOG.log(Level.FINEST, "Received error response code: " + con.getResponseCode());
			} else {
				in = new BufferedReader(
						new InputStreamReader(con.getInputStream()));
				String inputLine;
				StringBuffer response = new StringBuffer();

				while ((inputLine = in.readLine()) != null) {
					response.append(inputLine + "\n");
				}			
				
				//store result
				String html = response.toString();
				generator.resetHTMLFunctions(html);
				return response.toString();
			}

		} catch (MalformedURLException e) {
			LOG.log(Level.SEVERE, "Malformed URL: " + url);
			generator.revertLastCall();
		} catch (ProtocolException e) {
			LOG.log(Level.SEVERE, "ProtocolException: " + e.getMessage());
			generator.revertLastCall();
		} catch (IOException e) {
			LOG.log(Level.SEVERE, "General IO Exception Occured with Input @ " + url + ": " + e.getMessage());
			generator.revertLastCall();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				LOG.log(Level.SEVERE, "General IO Exception closing message reader.");
			}
			if (con != null) {
				con.disconnect();
			}
		}
		return null;
	}

	@Override
	public void run() {
		HTTPInputGenerator generator = HTTPInputGeneratorPool.getPool().takeFromPool();
		String response = this.process(generator);
		HTTPInputGeneratorPool.getPool().releaseBackToPool(generator);
		if (response == null) {
			ValidityTracker.TRACKER.incrementInvalidTransctionCount();
		}
		TransactionQueueSingleton transactionQueue = TransactionQueueSingleton.getInstance();
		transactionQueue.addQueueElement(this);
	}
}<commit_msg>stack trace<commit_after>/**
 * Copyright 2017 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tools.descartes.httploadgenerator.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import tools.descartes.dlim.httploadgenerator.runner.TransactionQueueSingleton;
import tools.descartes.dlim.httploadgenerator.runner.ValidityTracker;
import tools.descartes.dlim.httploadgenerator.runner.Transaction;

/**
 * HTTP transaction sends HTML requests to a HTTP web server based on a LUA script.
 * 
 * @author <NAME>, <NAME>
 *
 */
public class HTTPTransaction extends Transaction {

	private static final String USER_AGENT = "Mozilla/5.0";
	private static final String POST_SIGNAL = "[POST]";

	/** The constant logging instance. */
	private static final Logger LOG = Logger.getLogger(HTTPTransaction.class.getName());

	/**
	 * Processes the transaction of sending a GET request to a web server.
	 * @param generator The input generator to use.
	 * @return HTML Response of the web server.
	 */
	public String process(HTTPInputGenerator generator) {
		HttpURLConnection con = null;
		BufferedReader in = null;
		CookieHandler.setDefault(generator.getCookieManager());
		URL obj;
		String url = generator.getNextInput().trim();
		try {
			String method = "GET";
			if (url.startsWith("[")) {
				if (url.startsWith(POST_SIGNAL)) {
					method = "POST";
				}
				url = url.replaceFirst("\\[.*\\]", "");
			}
			obj = new URL(url);
			con = (HttpURLConnection) obj.openConnection();

			con.setRequestMethod(method);
			con.setRequestProperty("User-Agent", USER_AGENT);
			
			if (con.getResponseCode() >= 400) {
				generator.revertLastCall();
				LOG.log(Level.FINEST, "Received error response code: " + con.getResponseCode());
			} else {
				in = new BufferedReader(
						new InputStreamReader(con.getInputStream()));
				String inputLine;
				StringBuffer response = new StringBuffer();

				while ((inputLine = in.readLine()) != null) {
					response.append(inputLine + "\n");
				}			
				
				//store result
				String html = response.toString();
				generator.resetHTMLFunctions(html);
				return response.toString();
			}

		} catch (MalformedURLException e) {
			LOG.log(Level.SEVERE, "Malformed URL: " + url);
			generator.revertLastCall();
		} catch (ProtocolException e) {
			LOG.log(Level.SEVERE, "ProtocolException: " + e.getMessage());
			generator.revertLastCall();
		} catch (IOException e) {
			LOG.log(Level.SEVERE, "General IO Exception Occured with Input @ " + url + ": " + e.getMessage());
			generator.revertLastCall();
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				LOG.log(Level.SEVERE, "General IO Exception closing message reader.");
			}
			if (con != null) {
				con.disconnect();
			}
		}
		return null;
	}

	@Override
	public void run() {
		HTTPInputGenerator generator = HTTPInputGeneratorPool.getPool().takeFromPool();
		String response = this.process(generator);
		HTTPInputGeneratorPool.getPool().releaseBackToPool(generator);
		if (response == null) {
			ValidityTracker.TRACKER.incrementInvalidTransctionCount();
		}
		TransactionQueueSingleton transactionQueue = TransactionQueueSingleton.getInstance();
		transactionQueue.addQueueElement(this);
	}
}
{'fraction_non_alphanumeric': 0.11793733037256353, 'fraction_numerical': 0.0032075006168270415, 'mean_word_length': 3.988923076923077, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5147751', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2326, 'n_words': 648}