JPProxy update - Https support

This commit is contained in:
Jindra Pet��k
2011-07-17 11:13:51 +02:00
parent 49a8fd03bf
commit 940a57f5d8
11 changed files with 389 additions and 291 deletions
@@ -1,39 +1,165 @@
package com.jpexs.proxy;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
class Https extends HttpConnection {
/**
*
* @author JPEXS
*/
public class Https extends Http {
/* XXX - more than 1 should work now. */
static final int MAX_PENDING_REQUESTS = 1;
boolean proxy = false;
static Hashtable cache = new Hashtable(33);
private static Object httpLock = new Object();
Https(String host, int port) throws IOException {
super(host, port);
}
public Https(String host, int port) throws IOException
{
this(host, port, false);
}
Https(String host, int port, boolean isProxy) throws IOException {
this(host, port);
proxy = isProxy;
}
public Https(String host, int port, boolean isProxy) throws IOException
{
super(host, port,isProxy);
if(isProxy){
Request connectReq=new Request(null);
connectReq.setCommand("CONNECT");
connectReq.setURL(host+":"+port);
connectReq.setProtocol("HTTP/1.1");
try {
sendRequest(connectReq);
recvReply(connectReq);
} catch (RetryRequestException ex) {
public void sendRequest(Request request)
throws IOException, RetryRequestException {
if (proxy) {
super.sendRequest(request);
} else {
/* nothing */
}
}
}
}
promoteToClientSSL();
}
public Reply recvReply(Request request)
throws java.io.IOException, RetryRequestException {
Reply reply = new Reply(getInputStream());
if (proxy) {
reply.read();
} else {
reply.statusLine = "HTTP/1.0 200 Connection established";
reply.setHeaderField("Proxy-agent", ProxyConfig.appName);
}
private static String cacheKey(String host, int port)
{
return host.toLowerCase() + ":" + port;
}
return reply;
}
private static Vector cacheLookup(String host, int port)
{
Vector v = (Vector) cache.get(cacheKey(host, port));
return v;
}
private static boolean cacheContains(Https http)
{
Vector v = (Vector) cache.get(cacheKey(http.host, http.port));
return v != null ? v.contains(http) : false;
}
private static void cacheInsert(String host, int port, Https http)
{
String key = cacheKey(host, port);
Vector v = (Vector) cache.get(key);
if (v == null)
{
v = new Vector();
}
v.addElement(http);
cache.put(key, v);
}
private static void cacheRemove(String host, int port, Https http)
{
Vector v = (Vector) cache.get(cacheKey(host, port));
if (v != null)
{
v.removeElement(http);
if (v.isEmpty())
{
cache.remove(cacheKey(host, port));
}
}
}
private static void cacheClean()
{
long now = System.currentTimeMillis();
Enumeration e = cache.keys();
while (e.hasMoreElements())
{
Vector v = (Vector) cache.get(e.nextElement());
for (int i = 0; i < v.size(); i++)
{
Https http = (Https) v.elementAt(i);
if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */
{
http.persistent = false;
http.close();
}
}
}
}
static Https open(String host, int port, boolean isProxy)
throws IOException
{
Https http = null;
synchronized (httpLock)
{
Vector v = cacheLookup(host, port);
if (v != null)
{
for (int i = 0; i < v.size(); i++)
{
Https pick = (Https) v.elementAt(i);
/* find an http connection that isn't busy */
if (pick.isPersistent() && !pick.isBusy())
{
http = pick;
break;
}
}
if (http != null)
{
http.idle = 0;
}
}
}
if (http == null)
{
http = new Https(host, port, isProxy);
cacheInsert(host, port, http);
}
return http;
}
static Https open(String host, int port) throws IOException
{
return open(host, port, false);
}
static Enumeration enumerate()
{
Vector list = new Vector();
Enumeration e = cache.keys();
while (e.hasMoreElements())
{
Vector v = (Vector) cache.get(e.nextElement());
for (int i = 0; i < v.size(); i++)
{
list.addElement(v.elementAt(i));
}
}
return list.elements();
}
static synchronized void clean()
{
cacheClean();
}
}