<commit_before>package com.xqbase.tuna.proxy;

import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;

import javax.net.ssl.SSLSession;

import com.xqbase.tuna.Connection;
import com.xqbase.tuna.ConnectionHandler;
import com.xqbase.tuna.ConnectionSession;
import com.xqbase.tuna.http.HttpPacket;
import com.xqbase.tuna.http.HttpPacketException;
import com.xqbase.tuna.http.HttpStatus;
import com.xqbase.tuna.ssl.SSLConnectionSession;
import com.xqbase.tuna.ssl.SSLFilter;
import com.xqbase.tuna.util.ByteArrayQueue;
import com.xqbase.tuna.util.Bytes;
import com.xqbase.util.Log;
import com.xqbase.util.Numbers;

/** Connection for <b>CONNECT</b> */
class PeerConnection implements Connection, HttpStatus {
	private static final int LOG_DEBUG = ProxyContext.LOG_DEBUG;
	private static final int LOG_VERBOSE = ProxyContext.LOG_VERBOSE;

	private static final byte[] CONNECTION_ESTABLISHED =
			"HTTP/1.0 200 Connection Established\r\n\r\n".getBytes();

	private ProxyConnection peer;
	private ConnectionHandler peerHandler, handler;
	private boolean proxyChain, established = false;
	// For DEBUG only
	private int logLevel, port;
	private String host, local = " (0.0.0.0:0)";

	PeerConnection(ProxyConnection peer, ConnectionHandler peerHandler,
			boolean proxyChain, int logLevel, String host, int port) {
		this.peer = peer;
		this.peerHandler = peerHandler;
		this.proxyChain = proxyChain;
		this.logLevel = logLevel;
		this.host = host;
		this.port = port;
	}

	String toString(boolean resp) {
		return peer.getRemote() + (resp ? " <= " : " => ") + host + ":" + port + local;
	}

	ConnectionHandler getHandler() {
		return handler;
	}

	@Override
	public void setHandler(ConnectionHandler handler) {
		this.handler = handler;
	}

	@Override
	public void onRecv(byte[] b, int off, int len) {
		peerHandler.send(b, off, len);
	}

	@Override
	public void onQueue(int size) {
		peerHandler.setBufferSize(size == 0 ? MAX_BUFFER_SIZE : 0);
		if (logLevel >= LOG_VERBOSE) {
			Log.v((size == 0 ? "Connection Unblocked, " :
					"Connection Blocked (" + size + "), ") + toString(false));
		}
	}

	@Override
	public void onConnect(ConnectionSession session) {
		if (!proxyChain) {
			peerHandler.send(CONNECTION_ESTABLISHED);
		}
		established = true;
		local = " (" + session.getLocalAddr() + ":" + session.getLocalPort() + ")";
		if (logLevel >= LOG_VERBOSE) {
			Log.v("Connection Established, " + toString(false));
		}
	}

	@Override
	public void onDisconnect() {
		if (!established) {
			if (logLevel >= LOG_DEBUG) {
				Log.d("Connection Failed, " + toString(false));
			}
			peer.sendError(SC_GATEWAY_TIMEOUT);
		} else if (logLevel >= LOG_VERBOSE) {
			Log.v("Connection Lost, " + toString(true));
		}
		peer.disconnect();
	}
}

/** Connection for request except <b>CONNECT</b> */
class ClientConnection implements Connection, HttpStatus {
	private static final int LOG_DEBUG = ProxyContext.LOG_DEBUG;
	private static final int LOG_VERBOSE = ProxyContext.LOG_VERBOSE;

	private ProxyConnection proxy;
	private ConnectionHandler proxyHandler, handler;
	private HttpPacket request, response = new HttpPacket();
	private boolean secure, proxyChain, established = false, begun = false, chunked = false,
			requestClose = false, responseClose = false;
	private int logLevel;
	private String host, local = " (0.0.0.0:0)";

	ClientConnection(ProxyConnection proxy, ConnectionHandler proxyHandler, HttpPacket request,
			String host, boolean secure, boolean proxyChain, int logLevel) {
		this.proxy = proxy;
		this.proxyHandler = proxyHandler;
		this.request = request;
		this.host = host;
		this.secure = secure;
		this.proxyChain = proxyChain;
		this.logLevel = logLevel;
	}

	String toString(boolean resp) {
		String uri = request.getUri();
		return proxy.getRemote() + (resp ? " <= " : " => ") +
				(proxyChain ? uri + " via " + host :
				(secure ? "https://" : "http://") + host + uri) + local;
	}

	boolean isBegun() {
		return begun;
	}

	void begin(boolean head, boolean connectionClose) {
		begun = chunked = false;
		response.setType(head ? HttpPacket.TYPE_RESPONSE_HEAD : request.isHttp10() ?
				HttpPacket.TYPE_RESPONSE_HTTP10 : HttpPacket.TYPE_RESPONSE);
		requestClose = connectionClose;<commit_msg>log bugfix<commit_after>package com.xqbase.tuna.proxy;

import java.io.IOException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;

import javax.net.ssl.SSLSession;

import com.xqbase.tuna.Connection;
import com.xqbase.tuna.ConnectionHandler;
import com.xqbase.tuna.ConnectionSession;
import com.xqbase.tuna.http.HttpPacket;
import com.xqbase.tuna.http.HttpPacketException;
import com.xqbase.tuna.http.HttpStatus;
import com.xqbase.tuna.ssl.SSLConnectionSession;
import com.xqbase.tuna.ssl.SSLFilter;
import com.xqbase.tuna.util.ByteArrayQueue;
import com.xqbase.tuna.util.Bytes;
import com.xqbase.util.Log;
import com.xqbase.util.Numbers;

/** Connection for <b>CONNECT</b> */
class PeerConnection implements Connection, HttpStatus {
	private static final int LOG_DEBUG = ProxyContext.LOG_DEBUG;
	private static final int LOG_VERBOSE = ProxyContext.LOG_VERBOSE;

	private static final byte[] CONNECTION_ESTABLISHED =
			"HTTP/1.0 200 Connection Established\r\n\r\n".getBytes();

	private ProxyConnection peer;
	private ConnectionHandler peerHandler, handler;
	private boolean proxyChain, established = false;
	// For DEBUG only
	private int logLevel, port;
	private String host, local = " (0.0.0.0:0)";

	PeerConnection(ProxyConnection peer, ConnectionHandler peerHandler,
			boolean proxyChain, int logLevel, String host, int port) {
		this.peer = peer;
		this.peerHandler = peerHandler;
		this.proxyChain = proxyChain;
		this.logLevel = logLevel;
		this.host = host;
		this.port = port;
	}

	String toString(boolean resp) {
		return peer.getRemote() + (resp ? " <= " : " => ") + host + ":" + port + local;
	}

	ConnectionHandler getHandler() {
		return handler;
	}

	@Override
	public void setHandler(ConnectionHandler handler) {
		this.handler = handler;
	}

	@Override
	public void onRecv(byte[] b, int off, int len) {
		peerHandler.send(b, off, len);
	}

	@Override
	public void onQueue(int size) {
		peerHandler.setBufferSize(size == 0 ? MAX_BUFFER_SIZE : 0);
		if (logLevel >= LOG_VERBOSE) {
			Log.v((size == 0 ? "Connection Unblocked, " :
					"Connection Blocked (" + size + "), ") + toString(false));
		}
	}

	@Override
	public void onConnect(ConnectionSession session) {
		if (!proxyChain) {
			peerHandler.send(CONNECTION_ESTABLISHED);
		}
		established = true;
		local = " (" + session.getLocalAddr() + ":" + session.getLocalPort() + ")";
		if (logLevel >= LOG_VERBOSE) {
			Log.v("Connection Established, " + toString(false));
		}
	}

	@Override
	public void onDisconnect() {
		if (!established) {
			if (logLevel >= LOG_DEBUG) {
				Log.d("Connection Failed, " + toString(false));
			}
			peer.sendError(SC_GATEWAY_TIMEOUT);
		} else if (logLevel >= LOG_VERBOSE) {
			Log.v("Connection Lost, " + toString(true));
		}
		peer.disconnect();
	}
}

/** Connection for request except <b>CONNECT</b> */
class ClientConnection implements Connection, HttpStatus {
	private static final int LOG_DEBUG = ProxyContext.LOG_DEBUG;
	private static final int LOG_VERBOSE = ProxyContext.LOG_VERBOSE;

	private ProxyConnection proxy;
	private ConnectionHandler proxyHandler, handler;
	private HttpPacket request, response = new HttpPacket();
	private boolean secure, proxyChain, established = false, begun = false, chunked = false,
			requestClose = false, responseClose = false;
	private int logLevel;
	private String host, local = " (0.0.0.0:0)";

	ClientConnection(ProxyConnection proxy, ConnectionHandler proxyHandler, HttpPacket request,
			String host, boolean secure, boolean proxyChain, int logLevel) {
		this.proxy = proxy;
		this.proxyHandler = proxyHandler;
		this.request = request;
		this.host = host;
		this.secure = secure;
		this.proxyChain = proxyChain;
		this.logLevel = logLevel;
	}

	String toString(boolean resp) {
		String uri = request.getUri();
		return proxy.getRemote() + (resp ? " <= " : " => ") +
				(proxyChain ? uri + " via " + host :
				(secure ? "https://" : "http://") + host +
				(uri == null ? "" : uri)) + local;
	}

	boolean isBegun() {
		return begun;
	}

	void begin(boolean head, boolean connectionClose) {
		begun = chunked = false;
		response.setType(head ? HttpPacket.TYPE_RESPONSE_HEAD : request.isHttp10() ?
				HttpPacket.TYPE_RESPONSE_HTTP10 : HttpPacket.TYPE_RESPONSE);
		requestClose = connectionClose;
{'fraction_non_alphanumeric': 0.12251356238698011, 'fraction_numerical': 0.0054249547920434, 'mean_word_length': 4.462345679012346, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5450480', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2862, 'n_words': 674}