diff --git a/.gitignore b/.gitignore index 1a5d150fd..95570ebb6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,6 @@ hs_err_pid*.log /libsrc/ffdec_lib/testdata/decompile/ /libsrc/ffdec_lib/testdata/recompile/ /libsrc/ffdec_lib/revision.txt -/libsrc/jpproxy/nbproject/private/ \ No newline at end of file +/libsrc/jpproxy/nbproject/private/ +/libsrc/jpproxy/dist/ +/libsrc/jpproxy/build/ \ No newline at end of file diff --git a/examples/DeobfuscatorSample.java b/examples/DeobfuscatorSample.java index 5d9bf6089..ab5e3dce2 100644 --- a/examples/DeobfuscatorSample.java +++ b/examples/DeobfuscatorSample.java @@ -6,6 +6,10 @@ import com.jpexs.decompiler.flash.helpers.SWFDecompilerListener; public class DeobfuscatorSample implements SWFDecompilerListener { + @Override + public byte[] proxyFileCatched(byte[] data) { + } + @Override public void actionListParsed(ActionList actions, SWF swf) { } diff --git a/lib/jpproxy.jar b/lib/jpproxy.jar index 082c393cc..ec749fd30 100644 Binary files a/lib/jpproxy.jar and b/lib/jpproxy.jar differ diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java index 4d980d56d..3da716237 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWF.java @@ -79,6 +79,7 @@ import com.jpexs.decompiler.flash.exporters.settings.ShapeExportSettings; import com.jpexs.decompiler.flash.exporters.settings.SoundExportSettings; import com.jpexs.decompiler.flash.exporters.settings.TextExportSettings; import com.jpexs.decompiler.flash.exporters.shape.CanvasShapeExporter; +import com.jpexs.decompiler.flash.helpers.SWFDecompilerPlugin; import com.jpexs.decompiler.flash.helpers.collections.MyEntry; import com.jpexs.decompiler.flash.tags.ABCContainerTag; import com.jpexs.decompiler.flash.tags.DefineButton2Tag; @@ -572,6 +573,8 @@ public final class SWF implements TreeItem, Timelined { assignClassesToSymbols(); findFileAttributes(); findABCTags(); + + SWFDecompilerPlugin.fireSwfParsed(this); } else { boolean hasNonUnknownTag = false; for (Tag tag : tags) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABC.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABC.java index f677005f3..42bb2bcf4 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABC.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABC.java @@ -53,6 +53,7 @@ import com.jpexs.decompiler.flash.abc.usages.MethodParamsMultinameUsage; import com.jpexs.decompiler.flash.abc.usages.MethodReturnTypeMultinameUsage; import com.jpexs.decompiler.flash.abc.usages.MultinameUsage; import com.jpexs.decompiler.flash.abc.usages.TypeNameMultinameUsage; +import com.jpexs.decompiler.flash.helpers.SWFDecompilerPlugin; import com.jpexs.decompiler.flash.helpers.collections.MyEntry; import com.jpexs.decompiler.flash.tags.ABCContainerTag; import com.jpexs.helpers.MemoryInputStream; @@ -561,6 +562,8 @@ public class ABC { method_info.get(mb.method_info).setBody(mb); bodyIdxFromMethodIdx.set(mb.method_info, i); ais.endDumpLevel(); + + SWFDecompilerPlugin.fireMethodBodyParsed(mb, swf); } loadNamespaceMap(); /*for(int i=0;i 0) - { - out.write(buffer, 0, n); - out.flush(); - } - out.flush(); - }catch(SocketException e){ - } - catch (IOException e) - { - //Ignore errors - } + try { + while ((n = in.read(buffer, 0, buffer.length)) > 0) { + out.write(buffer, 0, n); + out.flush(); + } + out.flush(); + } catch (SocketException e) { + } catch (IOException e) { + //Ignore errors + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java b/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java index 0b41b11bf..2b8b20e35 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Handler.java @@ -14,10 +14,10 @@ import java.util.Locale; import java.util.TimeZone; import javax.net.ssl.SSLHandshakeException; -class Handler implements Runnable -{ +class Handler implements Runnable { + static final boolean DEBUG = false; - + Client client = null; Socket socket = null; Request request = null; @@ -33,16 +33,15 @@ class Handler implements Runnable List catchedContentTypes; ReplacedListener replacedListener; - static int curId=0; + static int curId = 0; int id; /** * Create a Handler. */ - Handler(Socket socket,List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) - { + Handler(Socket socket, List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) { curId++; - id=curId; + id = curId; this.socket = socket; this.replacements = replacements; this.catchedListener = catchedListener; @@ -53,439 +52,346 @@ class Handler implements Runnable /** * Close all connections associated with this handler. */ - synchronized void close() - { - if (client != null) - { - client.close(); - client = null; - } - if (http != null) - { - http.close(); - http = null; - } + synchronized void close() { + if (client != null) { + client.close(); + client = null; + } + if (http != null) { + http.close(); + http = null; + } } /** * Flush all data to the client. */ - void flush() - { - if (client != null) - { - try - { - client.getOutputStream().flush(); - } - catch (IOException e) - { - - } - } - } - - public void run() - { - boolean keepAlive = false; - Exception reason = null; - - Thread.currentThread().setName("Handler(" - + socket.getInetAddress().getHostAddress() - + ")"); - - try - { - client = new Client(socket); - client.setTimeout(ProxyConfig.readTimeout); - } - catch (IOException e) - { - - - return; - } - - try - { - boolean secure=false; - int securePort=443; - String secureServer=""; - do - { - request = null; - reply = null; - idle = System.currentTimeMillis(); - - try - { - request = client.read(); - if(secure){ - request.addSecureHostToURL(secureServer); - } - } - catch(SSLHandshakeException she) - { - she.printStackTrace(); - break; - } - catch (IOException e) - { - - break; - } - - - if(request.getCommand().equals("CONNECT")){ - secureServer=request.getHost(); - securePort=request.getPort(); - if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureServer)))){ - secure=true; - reply=new Reply(); - reply.statusLine = "HTTP/1.0 200 Connection established"; - reply.setHeaderField("Proxy-agent", ProxyConfig.appName); + void flush() { + if (client != null) { try { - client.write(reply); - } catch (IOException ex) { + client.getOutputStream().flush(); + } catch (IOException e) { } - client.promoteToServerSSL(); - keepAlive=true; - continue; - } - } - - idle = 0; - - try - { - keepAlive = processRequest(secure,secureServer,securePort); - } - catch (IOException ioe) - { - reason = ioe; - keepAlive = false; - } - - if (request != null && reply != null) - { - // XXX insert the number of bytes read into the - // reply content-length for logging. - if (reply != null && currentLength > 0) - { - reply.setHeaderField("Content-length", currentLength); - } - - - } - } - while (keepAlive); - } - finally - { - - } - - if (reason != null && reason.getMessage().indexOf("Broken pipe") == -1) - { - if (client != null && request != null) - { - error(client.getOutputStream(), reason, request); - } - - } - - close(); + } } - boolean processRequest(boolean secure,String secureHost,int securePort) throws IOException - { - boolean keepAlive = false; - - while (reply == null) - { - //boolean secure = false; - boolean uncompress = false; + public void run() { + boolean keepAlive = false; + Exception reason = null; + Thread.currentThread().setName("Handler(" + + socket.getInetAddress().getHostAddress() + + ")"); - if (request.getCommand().equals("CONNECT")) - { - secure = true; - } - else if (request.getURL().startsWith("/")) - { - - } - else if (request.getURL().startsWith("https://")&&(secure)) - { + try { + client = new Client(socket); + client.setTimeout(ProxyConfig.readTimeout); + } catch (IOException e) { - } - else if (! request.getURL().startsWith("http://")) - { + return; + } - return false; - } + try { + boolean secure = false; + int securePort = 443; + String secureServer = ""; + do { + request = null; + reply = null; + idle = System.currentTimeMillis(); - /* Client wants Keep-Alive */ - if (ProxyConfig.proxyKeepAlive) - { - keepAlive = (request.containsHeaderField("Proxy-Connection") - && request.getHeaderField("Proxy-Connection").equals("Keep-Alive")); - } + try { + request = client.read(); + if (secure) { + request.addSecureHostToURL(secureServer); + } + } catch (SSLHandshakeException she) { + she.printStackTrace(); + break; + } catch (IOException e) { - /* Filter the request. */ - //deleted + break; + } - /* None found. Use http or https relay. */ - if (secure) - { - http = createHttpsRelay(secureHost,securePort); - } - else - { - http = createHttpRelay(); - } - try - { - http.sendRequest(request); - if (http instanceof Http) - { - ((Http)http).setTimeout(ProxyConfig.readTimeout); - } - reply = http.recvReply(request); - } - catch (RetryRequestException e) - { - http.close(); - http = null; - continue; /* XXX */ - } + if (request.getCommand().equals("CONNECT")) { + secureServer = request.getHost(); + securePort = request.getPort(); + if ((ProxyConfig.httpsMode == ProxyConfig.HTTPS_FILTER) || ((ProxyConfig.httpsMode == ProxyConfig.HTTPS_FILTERLIST) && (ProxyConfig.enabledHttpsServers.contains(secureServer)))) { + secure = true; + reply = new Reply(); + reply.statusLine = "HTTP/1.0 200 Connection established"; + reply.setHeaderField("Proxy-agent", ProxyConfig.appName); + try { + client.write(reply); + } catch (IOException ex) { - /* Guess content-type if there aren't any headers. - Probably an upgraded HTTP/0.9 reply. */ - if (reply.headerCount() == 0) - { - String url = request.getURL(); - if (url.endsWith("/") - || url.endsWith(".html") || url.endsWith(".htm")) - { - reply.setHeaderField("Content-type", "text/html"); - } - } + } + client.promoteToServerSSL(); + keepAlive = true; + continue; + } + } + idle = 0; + try { + keepAlive = processRequest(secure, secureServer, securePort); + } catch (IOException ioe) { + reason = ioe; + keepAlive = false; + } - /* Filter the reply. */ - if (false) - { - /* uncompress gzip encoded html so it can be filtered */ - if (!ProxyConfig.dontUncompress - && "text/html".equals(reply.getHeaderField("Content-type"))) - { - String encoding = reply.getHeaderField("Content-Encoding"); - if (encoding != null && encoding.indexOf("gzip") != -1) - { - reply.removeHeaderField("Content-Encoding"); - reply.removeHeaderField("Content-length"); - uncompress = true; - } - } + if (request != null && reply != null) { + // XXX insert the number of bytes read into the + // reply content-length for logging. + if (reply != null && currentLength > 0) { + reply.setHeaderField("Content-length", currentLength); + } - //filter(reply); - } + } + } while (keepAlive); + } finally { + } - if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))&& reply.containsHeaderField("Connection") - && reply.getHeaderField("Connection").equals("Keep-Alive")){ - keepAlive=true; - } - reply.removeHeaderField("Proxy-Connection"); - if (keepAlive && reply.containsHeaderField("Content-length")) - { - reply.setHeaderField("Proxy-Connection", "Keep-Alive"); - } - else - { - keepAlive = false; - } + if (reason != null && reason.getMessage().indexOf("Broken pipe") == -1) { + if (client != null && request != null) { + error(client.getOutputStream(), reason, request); + } - currentLength = -1; - contentLength = -1; - try - { - contentLength = Integer.parseInt(reply.getHeaderField("Content-length")); - } - catch (NumberFormatException e) - { - } + } - - - if (http instanceof HttpsThrough) - { - HttpsThrough https = (HttpsThrough) http; - int timeout = ProxyConfig.readTimeout; - - client.write(reply); - - - try - { - client.setTimeout(timeout); - https.setTimeout(timeout); - - Copy cp = new Copy(client.getInputStream(), https.getOutputStream()); - ReusableThread thread = Server.getThread(); - thread.setRunnable(cp); - flushCopy(https.getInputStream(), client.getOutputStream(), -1, true); - client.close(); - } - catch (InterruptedIOException iioe) - { - // ignore socket timeout exceptions - } - } - else if (reply.hasContent()) - { - try - { - processContent(uncompress); - } - catch (IOException e) - { - if (http instanceof Http) - { - ((Http)http).reallyClose(); - } - else - { - http.close(); - } - http = null; - - client.close(); - client = null; - - throw e; - //return false; /* XXX */ - } - /* Document contains no data. */ - if (contentLength == 0) - { - client.close(); - } - } - else - { - client.write(reply); - } - - http.close(); - } - - return keepAlive; + close(); } - HttpRelay createHttpsRelay(String secureHost,int securePort) throws IOException - { - HttpRelay http; + boolean processRequest(boolean secure, String secureHost, int securePort) throws IOException { + boolean keepAlive = false; - if((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTER)||((ProxyConfig.httpsMode==ProxyConfig.HTTPS_FILTERLIST)&&(ProxyConfig.enabledHttpsServers.contains(secureHost)))) - { - if(ProxyConfig.useHTTPSProxy){ - http=Https.open(ProxyConfig.httpsProxyHost,ProxyConfig.httpsProxyPort,true); - Request connectReq=new Request(null); - connectReq.setStatusLine("CONNECT "+secureHost+":"+securePort+" HTTP/1.1"); - connectReq.setCommand("CONNECT"); - connectReq.setURL(secureHost+":"+securePort); - connectReq.setProtocol("HTTP/1.1"); + while (reply == null) { + //boolean secure = false; + boolean uncompress = false; + + if (request.getCommand().equals("CONNECT")) { + secure = true; + } else if (request.getURL().startsWith("/")) { + + } else if (request.getURL().startsWith("https://") && (secure)) { + + } else if (!request.getURL().startsWith("http://")) { + + return false; + } + + /* Client wants Keep-Alive */ + if (ProxyConfig.proxyKeepAlive) { + keepAlive = (request.containsHeaderField("Proxy-Connection") + && request.getHeaderField("Proxy-Connection").equals("Keep-Alive")); + } + + /* Filter the request. */ + //deleted + + /* None found. Use http or https relay. */ + if (secure) { + http = createHttpsRelay(secureHost, securePort); + } else { + http = createHttpRelay(); + } try { - http.sendRequest(connectReq); - Reply rep=http.recvReply(connectReq); - } catch (RetryRequestException ex) { + http.sendRequest(request); + if (http instanceof Http) { + ((Http) http).setTimeout(ProxyConfig.readTimeout); + } + reply = http.recvReply(request); + } catch (RetryRequestException e) { + http.close(); + http = null; + continue; /* XXX */ } - ((Https)http).promoteToClientSSL(); - }else{ - http=Https.open(secureHost,securePort,false); - ((Https)http).promoteToClientSSL(); - } - /*http = new Http(request.getHost(),request.getPort(),ProxyConfig.useHTTPSProxy); - if(ProxyConfig.useHTTPSProxy){ + + /* Guess content-type if there aren't any headers. + Probably an upgraded HTTP/0.9 reply. */ + if (reply.headerCount() == 0) { + String url = request.getURL(); + if (url.endsWith("/") + || url.endsWith(".html") || url.endsWith(".htm")) { + reply.setHeaderField("Content-type", "text/html"); + } + } + + /* Filter the reply. */ + if (false) { + /* uncompress gzip encoded html so it can be filtered */ + if (!ProxyConfig.dontUncompress + && "text/html".equals(reply.getHeaderField("Content-type"))) { + String encoding = reply.getHeaderField("Content-Encoding"); + if (encoding != null && encoding.indexOf("gzip") != -1) { + reply.removeHeaderField("Content-Encoding"); + reply.removeHeaderField("Content-length"); + uncompress = true; + } + } + + //filter(reply); + } + + if (request.containsHeaderField("Connection") && (request.getHeaderField("Connection").toLowerCase().equals("keep-alive")) && reply.containsHeaderField("Connection") + && reply.getHeaderField("Connection").equals("Keep-Alive")) { + keepAlive = true; + } + reply.removeHeaderField("Proxy-Connection"); + if (keepAlive && reply.containsHeaderField("Content-length")) { + reply.setHeaderField("Proxy-Connection", "Keep-Alive"); + } else { + keepAlive = false; + } + + currentLength = -1; + contentLength = -1; + try { + contentLength = Integer.parseInt(reply.getHeaderField("Content-length")); + } catch (NumberFormatException e) { + } + + if (http instanceof HttpsThrough) { + HttpsThrough https = (HttpsThrough) http; + int timeout = ProxyConfig.readTimeout; + + client.write(reply); + + try { + client.setTimeout(timeout); + https.setTimeout(timeout); + + Copy cp = new Copy(client.getInputStream(), https.getOutputStream()); + ReusableThread thread = Server.getThread(); + thread.setRunnable(cp); + flushCopy(https.getInputStream(), client.getOutputStream(), -1, true); + client.close(); + } catch (InterruptedIOException iioe) { + // ignore socket timeout exceptions + } + } else if (reply.hasContent()) { + try { + processContent(uncompress); + } catch (IOException e) { + if (http instanceof Http) { + ((Http) http).reallyClose(); + } else { + http.close(); + } + http = null; + + client.close(); + client = null; + + throw e; + //return false; /* XXX */ + } + /* Document contains no data. */ + if (contentLength == 0) { + client.close(); + } + } else { + client.write(reply); + } + + http.close(); + } + + return keepAlive; + } + + HttpRelay createHttpsRelay(String secureHost, int securePort) throws IOException { + HttpRelay http; + + if ((ProxyConfig.httpsMode == ProxyConfig.HTTPS_FILTER) || ((ProxyConfig.httpsMode == ProxyConfig.HTTPS_FILTERLIST) && (ProxyConfig.enabledHttpsServers.contains(secureHost)))) { + if (ProxyConfig.useHTTPSProxy) { + http = Https.open(ProxyConfig.httpsProxyHost, ProxyConfig.httpsProxyPort, true); + Request connectReq = new Request(null); + connectReq.setStatusLine("CONNECT " + secureHost + ":" + securePort + " HTTP/1.1"); + connectReq.setCommand("CONNECT"); + connectReq.setURL(secureHost + ":" + securePort); + connectReq.setProtocol("HTTP/1.1"); + try { + http.sendRequest(connectReq); + Reply rep = http.recvReply(connectReq); + } catch (RetryRequestException ex) { + + } + ((Https) http).promoteToClientSSL(); + } else { + http = Https.open(secureHost, securePort, false); + ((Https) http).promoteToClientSSL(); + } + /*http = new Http(request.getHost(),request.getPort(),ProxyConfig.useHTTPSProxy); + if(ProxyConfig.useHTTPSProxy){ Request connectReq=new Request(client); connectReq.setCommand("CONNECT"); connectReq.setURL(secureHost+":"+securePort); connectReq.setProtocol("HTTP/1.1"); - try { - http.sendRequest(connectReq); - http.recvReply(connectReq); - } catch (RetryRequestException ex) { + try { + http.sendRequest(connectReq); + http.recvReply(connectReq); + } catch (RetryRequestException ex) { + } + } + ((Http)http).promoteToClientSSL();*/ + } else { + if (ProxyConfig.useHTTPSProxy) { + http = new HttpsThrough(ProxyConfig.httpsProxyHost, + ProxyConfig.httpsProxyPort, + true); + } else { + http = new HttpsThrough(secureHost, securePort); } - } - ((Http)http).promoteToClientSSL();*/ - }else{ - if(ProxyConfig.useHTTPSProxy) - { - http = new HttpsThrough(ProxyConfig.httpsProxyHost, - ProxyConfig.httpsProxyPort, - true); - }else{ - http = new HttpsThrough(secureHost,securePort); - } - } + } - - - - return http; - } - - HttpRelay createHttpRelay() throws IOException - { - HttpRelay http; - - if (ProxyConfig.useHTTPProxy) - { - http = Http.open(ProxyConfig.httpProxyHost, - ProxyConfig.httpProxyPort, - true); - } - else - { - http = Http.open(request.getHost(), request.getPort()); - } - - return http; + return http; } - InputStream readChunkedTransfer(InputStream in) throws IOException - { - ByteArrayOutputStream chunks = new ByteArrayOutputStream(8192); - int size = 0; + HttpRelay createHttpRelay() throws IOException { + HttpRelay http; - contentLength = 0; - while ((size = reply.getChunkSize(in)) > 0) - { - contentLength += size; - copy(in, chunks, size, true); - reply.readLine(in); - } - reply.getChunkedFooter(in); + if (ProxyConfig.useHTTPProxy) { + http = Http.open(ProxyConfig.httpProxyHost, + ProxyConfig.httpProxyPort, + true); + } else { + http = Http.open(request.getHost(), request.getPort()); + } - reply.removeHeaderField("Transfer-Encoding"); - reply.setHeaderField("Content-length", contentLength); - - return new ByteArrayInputStream(chunks.toByteArray()); + return http; } -private static DateFormat httpDateFormat() { + InputStream readChunkedTransfer(InputStream in) throws IOException { + ByteArrayOutputStream chunks = new ByteArrayOutputStream(8192); + int size = 0; + + contentLength = 0; + while ((size = reply.getChunkSize(in)) > 0) { + contentLength += size; + copy(in, chunks, size, true); + reply.readLine(in); + } + reply.getChunkedFooter(in); + + reply.removeHeaderField("Transfer-Encoding"); + reply.setHeaderField("Content-length", contentLength); + + return new ByteArrayInputStream(chunks.toByteArray()); + } + + private static DateFormat httpDateFormat() { DateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); httpDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); return httpDateFormat; } -void disableReplyCaching() { + void disableReplyCaching() { reply.removeHeaderField("Cache-Control"); reply.removeHeaderField("Last-Modified"); reply.removeHeaderField("Expires"); @@ -500,33 +406,26 @@ void disableReplyCaching() { reply.setHeaderField("Last-Modified", "Sat, 26 Jul 1997 05:00:00 GMT"); } -void processContent(boolean uncompress) throws IOException - { - InputStream in; - boolean chunked = false; + void processContent(boolean uncompress) throws IOException { + InputStream in; + boolean chunked = false; - if (reply.containsHeaderField("Transfer-Encoding") - && reply.getTransferEncoding().equals("chunked")) - { - in = readChunkedTransfer(reply.getContent()); - chunked = true; - } - else - { - in = reply.getContent(); - } + if (reply.containsHeaderField("Transfer-Encoding") + && reply.getTransferEncoding().equals("chunked")) { + in = readChunkedTransfer(reply.getContent()); + chunked = true; + } else { + in = reply.getContent(); + } - if (in == null) - { + if (in == null) { - return; - } - else if (uncompress) - { - in = new GZIPInputStream(in); - } + return; + } else if (uncompress) { + in = new GZIPInputStream(in); + } - String url = request.getURL(); + String url = request.getURL(); boolean replaced = false; for (Replacement r : replacements) { if (r.matches(url)) { @@ -534,7 +433,7 @@ void processContent(boolean uncompress) throws IOException ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try { FileInputStream fis = new FileInputStream(r.targetFile); - byte[] buf = new byte[2048]; + byte[] buf = new byte[4096]; int pos = 0; while ((pos = fis.read(buf)) > 0) { buffer.write(buf, 0, pos); @@ -542,7 +441,7 @@ void processContent(boolean uncompress) throws IOException fis.close(); buffer.close(); } catch (IOException ex) { - + } byte[] bytes = buffer.toByteArray(); contentLength = bytes.length; @@ -566,16 +465,23 @@ void processContent(boolean uncompress) throws IOException if (contentType != null) { for (String ct : catchedContentTypes) { String convContentType = contentType; - if (convContentType.contains(";")) + if (convContentType.contains(";")) { convContentType = convContentType.substring(0, convContentType.indexOf(";")); + } if (ct.toLowerCase().equals(convContentType.toLowerCase())) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(in, baos, contentLength, true); + byte[] data = baos.toByteArray(); if (catchedListener != null) { - catchedListener.catched(ct, request.getURL(), new ByteArrayInputStream(baos.toByteArray())); + byte[] newData = catchedListener.catched(ct, request.getURL(), new ByteArrayInputStream(data)); + if (newData != null) { + data = newData; + contentLength = data.length; + reply.setHeaderField("Content-length", contentLength); + } } - in = new ByteArrayInputStream(baos.toByteArray()); + in = new ByteArrayInputStream(data); disableReplyCaching(); break; } @@ -587,22 +493,18 @@ void processContent(boolean uncompress) throws IOException } } - - /** * Return the content length. */ - int getTotalBytes() - { - return contentLength > 0 ? contentLength : 0; + int getTotalBytes() { + return contentLength > 0 ? contentLength : 0; } /** * Return the number of bytes read so far. */ - int getCurrentBytes() - { - return currentLength > 0 ? currentLength : 0; + int getCurrentBytes() { + return currentLength > 0 ? currentLength : 0; } /** @@ -612,21 +514,17 @@ void processContent(boolean uncompress) throws IOException * @param e exception that occurred * @param r request */ - void error(OutputStream out, Exception e, Request r) - { - StringBuffer buf = new StringBuffer(); - buf.append("While trying to retrieve the URL: "+r.getURL()+"\r\n"); - buf.append("

\r\nThe following error was encountered:\r\n

\r\n"); - buf.append("

  • " + e.toString() + "
\r\n"); - String s = new HttpError(400, buf.toString()).toString(); - try - { - out.write(s.getBytes(), 0, s.length()); - out.flush(); - } - catch (Exception ex) - { - } + void error(OutputStream out, Exception e, Request r) { + StringBuffer buf = new StringBuffer(); + buf.append("While trying to retrieve the URL: " + r.getURL() + "\r\n"); + buf.append("

\r\nThe following error was encountered:\r\n

\r\n"); + buf.append("

  • " + e.toString() + "
\r\n"); + String s = new HttpError(400, buf.toString()).toString(); + try { + out.write(s.getBytes(), 0, s.length()); + out.flush(); + } catch (Exception ex) { + } } /** @@ -637,64 +535,55 @@ void processContent(boolean uncompress) throws IOException * @param monitored Update the Monitor */ void copy(InputStream in, OutputStream out, int length, boolean monitored) - throws IOException - { - if (length == 0) - { - return; - } + throws IOException { + if (length == 0) { + return; + } - int n; - byte[] buffer = new byte[8192]; - long start = System.currentTimeMillis(); - long now = 0, then = start; - - bytesPerSecond = 0; + int n; + byte[] buffer = new byte[8192]; + long start = System.currentTimeMillis(); + long now = 0, then = start; - if (monitored) - { - currentLength = 0; - } - - for (;;) - { - n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; - n = in.read(buffer, 0, n); - if (n < 0) - { - break; - } - - out.write(buffer, 0, n); + bytesPerSecond = 0; - if (monitored) - { - currentLength += n; + if (monitored) { + currentLength = 0; + } - } + for (;;) { + n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; + n = in.read(buffer, 0, n); + if (n < 0) { + break; + } - now = System.currentTimeMillis(); - bytesPerSecond = currentLength / ((now - start) / 1000.0); + out.write(buffer, 0, n); - // flush after 1 second - if (now - then > 1000) - { - out.flush(); - } + if (monitored) { + currentLength += n; - if (length != -1) - { - length -= n; - if (length == 0) - { - break; - } - } + } - then = now; - } + now = System.currentTimeMillis(); + bytesPerSecond = currentLength / ((now - start) / 1000.0); - out.flush(); + // flush after 1 second + if (now - then > 1000) { + out.flush(); + } + + if (length != -1) { + length -= n; + if (length == 0) { + break; + } + } + + then = now; + } + + out.flush(); } @@ -706,86 +595,71 @@ void processContent(boolean uncompress) throws IOException * @param monitored Update the Monitor */ void flushCopy(InputStream in, OutputStream out, int length, boolean monitored) - throws IOException - { - if (length == 0) - { - return; - } + throws IOException { + if (length == 0) { + return; + } - int n; - byte[] buffer = new byte[8192]; - long start = System.currentTimeMillis(); - bytesPerSecond = 0; - - if (monitored) - { - currentLength = 0; - } - - for (;;) - { - n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; - n = in.read(buffer, 0, n); - if (n < 0) - { - break; - } + int n; + byte[] buffer = new byte[8192]; + long start = System.currentTimeMillis(); + bytesPerSecond = 0; - out.write(buffer, 0, n); - out.flush(); - if (monitored) - { - currentLength += n; + if (monitored) { + currentLength = 0; + } - } - bytesPerSecond = currentLength / ((System.currentTimeMillis() - start) / 1000.0); - if (length != -1) - { - length -= n; - if (length == 0) - { - break; - } - } - } - out.flush(); + for (;;) { + n = (length > 0) ? Math.min(length, buffer.length) : buffer.length; + n = in.read(buffer, 0, n); + if (n < 0) { + break; + } + + out.write(buffer, 0, n); + out.flush(); + if (monitored) { + currentLength += n; + + } + bytesPerSecond = currentLength / ((System.currentTimeMillis() - start) / 1000.0); + if (length != -1) { + length -= n; + if (length == 0) { + break; + } + } + } + out.flush(); } - /** * Return a string represenation of the hander's state. */ - public String toString() - { - StringBuffer str = new StringBuffer(); - str.append("CLIENT "); - str.append(socket.getInetAddress().getHostAddress()); - str.append(":"); - str.append(socket.getPort()); - str.append(" - "); - if (request == null) - { - str.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); - } - else - { - if (reply != null && currentLength > 0) - { - str.append("("); - str.append(currentLength); - if (contentLength > 0) - { - str.append("/"); - str.append(contentLength); - } - str.append(" "); - str.append(((int)bytesPerSecond / 1024) + " kB/s"); - str.append(") "); - } - str.append(request.getURL()); - } - return str.toString(); + public String toString() { + StringBuffer str = new StringBuffer(); + str.append("CLIENT "); + str.append(socket.getInetAddress().getHostAddress()); + str.append(":"); + str.append(socket.getPort()); + str.append(" - "); + if (request == null) { + str.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); + } else { + if (reply != null && currentLength > 0) { + str.append("("); + str.append(currentLength); + if (contentLength > 0) { + str.append("/"); + str.append(contentLength); + } + str.append(" "); + str.append(((int) bytesPerSecond / 1024) + " kB/s"); + str.append(") "); + } + str.append(request.getURL()); + } + return str.toString(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Http.java b/libsrc/jpproxy/src/com/jpexs/proxy/Http.java index 19bf98417..cca9bc177 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Http.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Http.java @@ -6,15 +6,13 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; -class Http extends HttpConnection -{ +class Http extends HttpConnection { /* XXX - more than 1 should work now. */ static final int MAX_PENDING_REQUESTS = 1; - + static Hashtable cache = new Hashtable(33); private static Object httpLock = new Object(); - String host; int port; @@ -24,334 +22,263 @@ class Http extends HttpConnection long idle = 0; Vector queue = new Vector(); - public Http(String host, int port) throws IOException - { - this(host, port, false); - } - - public Http(String host, int port, boolean isProxy) throws IOException - { - super(host, port); - this.host = host; - this.port = port; - this.proxy = isProxy; + public Http(String host, int port) throws IOException { + this(host, port, false); } - public Http(String host, int port, boolean isProxy,Socket sock) throws IOException - { - super(sock); - this.host = host; - this.port = port; - this.proxy = isProxy; + public Http(String host, int port, boolean isProxy) throws IOException { + super(host, port); + this.host = host; + this.port = port; + this.proxy = isProxy; + } + + public Http(String host, int port, boolean isProxy, Socket sock) throws IOException { + super(sock); + this.host = host; + this.port = port; + this.proxy = isProxy; } public synchronized void sendRequest(Request request) - throws IOException, RetryRequestException - { - queue.addElement(request); - - try - { - send(request); - } - catch (IOException e) - { - if (persistent) - { - persistent = false; - throw new RetryRequestException(); - } - throw e; - } + throws IOException, RetryRequestException { + queue.addElement(request); + + try { + send(request); + } catch (IOException e) { + if (persistent) { + persistent = false; + throw new RetryRequestException(); + } + throw e; + } } public synchronized Reply recvReply(Request request) - throws IOException, RetryRequestException - { - while (queue.firstElement() != request) - { - try - { - wait(); - } - catch (InterruptedException e) - { - } - } + throws IOException, RetryRequestException { + while (queue.firstElement() != request) { + try { + wait(); + } catch (InterruptedException e) { + } + } - if (closed) - { - throw new RetryRequestException(); - } + if (closed) { + throw new RetryRequestException(); + } - try - { - return recv(); - } - catch (IOException e) - { - if (persistent) - { - persistent = false; - throw new RetryRequestException(); - } - throw e; - } + try { + return recv(); + } catch (IOException e) { + if (persistent) { + persistent = false; + throw new RetryRequestException(); + } + throw e; + } } - public void reallyClose() - { - persistent = false; - close(); + public void reallyClose() { + persistent = false; + close(); } - public synchronized void close() - { - if (persistent) - { - idle = System.currentTimeMillis(); - } - else - { - cacheRemove(host, port, this); - super.close(); - closed = true; - } + public synchronized void close() { + if (persistent) { + idle = System.currentTimeMillis(); + } else { + cacheRemove(host, port, this); + super.close(); + closed = true; + } - if (queue.size() > 0) - { - queue.removeElementAt(0); - - notify(); - } + if (queue.size() > 0) { + queue.removeElementAt(0); + + notify(); + } } - private void send(Request request) throws IOException - { - - /* Prepare HTTP/1.1 request */ - - request.removeHeaderField("Proxy-Connection"); - + private void send(Request request) throws IOException { - if(!proxy){ - if(request.containsHeaderField("Connection")&&(request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))){ + /* Prepare HTTP/1.1 request */ + request.removeHeaderField("Proxy-Connection"); - }else{ - request.setHeaderField("Connection", "open"); - } - if (!request.containsHeaderField("Host")) - { - request.setHeaderField("Host", request.getHost()); - } - } + if (!proxy) { + if (request.containsHeaderField("Connection") && (request.getHeaderField("Connection").toLowerCase().equals("keep-alive"))) { - if (proxy) - { - request.write(getOutputStream()); - } - else - { - String oldStatusLine = request.statusLine; - StringBuffer head = new StringBuffer(); - head.append(request.getCommand()); - head.append(" "); - head.append(request.getPath()); - head.append(" "); - head.append("HTTP/1.0"); - request.statusLine = head.toString(); + } else { + request.setHeaderField("Connection", "open"); + } + if (!request.containsHeaderField("Host")) { + request.setHeaderField("Host", request.getHost()); + } + } - request.write(getOutputStream()); + if (proxy) { + request.write(getOutputStream()); + } else { + String oldStatusLine = request.statusLine; + StringBuffer head = new StringBuffer(); + head.append(request.getCommand()); + head.append(" "); + head.append(request.getPath()); + head.append(" "); + head.append("HTTP/1.0"); + request.statusLine = head.toString(); - /* flush? */ - - request.statusLine = oldStatusLine; - } + request.write(getOutputStream()); + + /* flush? */ + request.statusLine = oldStatusLine; + } } - private Reply recv() throws IOException - { - Reply reply = new Reply(getInputStream()); - reply.read(); + private Reply recv() throws IOException { + Reply reply = new Reply(getInputStream()); + reply.read(); - String conn = reply.getHeaderField("Connection"); + String conn = reply.getHeaderField("Connection"); - - if (reply.containsHeaderField("Connection") - && reply.getHeaderField("Connection").equals("close")) - { - persistent = false; - } - else if (reply.getProtocol().equals("HTTP/1.1")) - { - persistent = true; - } - else - { - persistent = false; - } + if (reply.containsHeaderField("Connection") + && reply.getHeaderField("Connection").equals("close")) { + persistent = false; + } else if (reply.getProtocol().equals("HTTP/1.1")) { + persistent = true; + } else { + persistent = false; + } - /* Received HTTP/1.1 "Continue". Read another Reply. */ - if (reply.getStatusCode() == 100) - { - reply = recv(); - } + /* Received HTTP/1.1 "Continue". Read another Reply. */ + if (reply.getStatusCode() == 100) { + reply = recv(); + } - return reply; + return reply; } - protected boolean isBusy() - { - return queue.size() >= MAX_PENDING_REQUESTS; + protected boolean isBusy() { + return queue.size() >= MAX_PENDING_REQUESTS; } - protected boolean isPersistent() - { - return persistent; + protected boolean isPersistent() { + return persistent; } - private static String cacheKey(String host, int port) - { - return host.toLowerCase() + ":" + port; + private static String cacheKey(String host, int port) { + return host.toLowerCase() + ":" + port; } - private static Vector cacheLookup(String host, int port) - { - Vector v = (Vector) cache.get(cacheKey(host, port)); - return v; + private static Vector cacheLookup(String host, int port) { + Vector v = (Vector) cache.get(cacheKey(host, port)); + return v; } - private static boolean cacheContains(Http http) - { - Vector v = (Vector) cache.get(cacheKey(http.host, http.port)); - return v != null ? v.contains(http) : false; + private static boolean cacheContains(Http 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, Http 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 cacheInsert(String host, int port, Http 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, Http 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 cacheRemove(String host, int port, Http 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++) - { - Http http = (Http) v.elementAt(i); - if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */ - { - http.persistent = false; - http.close(); - } - } - } + 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++) { + Http http = (Http) v.elementAt(i); + if (http.idle > 0 && now - http.idle > 30000) /* 30 seconds */ { + http.persistent = false; + http.close(); + } + } + } } static Http open(String host, int port, boolean isProxy) - throws IOException - { - Http http = null; + throws IOException { + Http http = null; - synchronized (httpLock) - { - Vector v = cacheLookup(host, port); - if (v != null) - { - for (int i = 0; i < v.size(); i++) - { - Http pick = (Http) v.elementAt(i); + synchronized (httpLock) { + Vector v = cacheLookup(host, port); + if (v != null) { + for (int i = 0; i < v.size(); i++) { + Http pick = (Http) v.elementAt(i); - /* find an http connection that isn't busy */ - if (pick.isPersistent() && !pick.isBusy()) - { - http = pick; - break; - } - } + /* 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 Http(host, port, isProxy); - cacheInsert(host, port, http); - } - - return http; + if (http != null) { + http.idle = 0; + } + } + } + + if (http == null) { + http = new Http(host, port, isProxy); + cacheInsert(host, port, http); + } + + return http; } - static Http open(String host, int port) throws IOException - { - return open(host, port, false); + static Http 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 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(); + static synchronized void clean() { + cacheClean(); } - public String toString() - { - StringBuffer buf = new StringBuffer(); - buf.append("SERVER "); - buf.append(super.toString()); - if (isPersistent()) - { - buf.append(" - "); - if (queue.size() > 0) - { - buf.append(queue.size()); - buf.append(" pending"); - } - else - { - buf.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); - } - } - return buf.toString(); + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("SERVER "); + buf.append(super.toString()); + if (isPersistent()) { + buf.append(" - "); + if (queue.size() > 0) { + buf.append(queue.size()); + buf.append(" pending"); + } else { + buf.append("idle " + ((System.currentTimeMillis() - idle) / 1000.0) + " sec"); + } + } + return buf.toString(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java index 685c68dfb..14e944fd5 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpConnection.java @@ -5,54 +5,45 @@ import java.io.OutputStream; import java.io.IOException; import java.net.Socket; -abstract class HttpConnection extends Connection implements HttpRelay -{ - HttpConnection(String host, int port) throws IOException - { - super(host, port); +abstract class HttpConnection extends Connection implements HttpRelay { + + HttpConnection(String host, int port) throws IOException { + super(host, port); } - HttpConnection(Socket s) throws IOException - { - super(s); + HttpConnection(Socket s) throws IOException { + super(s); } public void sendRequest(Request request) - throws IOException, RetryRequestException - { - request.write(getOutputStream()); + throws IOException, RetryRequestException { + request.write(getOutputStream()); } - + public Reply recvReply(Request request) - throws IOException, RetryRequestException - { - Reply reply = new Reply(getInputStream()); - reply.read(); - return reply; - } - - public void setInputStream(InputStream in) - { - super.setInputStream(in); - } - - public void setOutputStream(OutputStream out) - { - super.setOutputStream(out); + throws IOException, RetryRequestException { + Reply reply = new Reply(getInputStream()); + reply.read(); + return reply; } - public InputStream getInputStream() - { - return super.getInputStream(); - } - - public OutputStream getOutputStream() - { - return super.getOutputStream(); + public void setInputStream(InputStream in) { + super.setInputStream(in); } - public void close() - { - super.close(); + public void setOutputStream(OutputStream out) { + super.setOutputStream(out); + } + + public InputStream getInputStream() { + return super.getInputStream(); + } + + public OutputStream getOutputStream() { + return super.getOutputStream(); + } + + public void close() { + super.close(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java index ec0dc8c64..f136473d6 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpError.java @@ -1,70 +1,62 @@ package com.jpexs.proxy; -class HttpError -{ +class HttpError { + StringBuffer content = null; Reply reply = null; - - HttpError(int code, String message) - { - String error; - switch (code) - { - case 400: - error = "Bad Request"; - break; - case 403: - error = "Forbidden"; - break; + HttpError(int code, String message) { + String error; + switch (code) { + case 400: + error = "Bad Request"; + break; - case 404: - error = "Not found"; - break; + case 403: + error = "Forbidden"; + break; - case 503: - error = "Service Unavailable"; - break; + case 404: + error = "Not found"; + break; - default: - error = "Error"; - break; - } + case 503: + error = "Service Unavailable"; + break; - reply = new Reply(); - reply.statusLine = "HTTP/1.0 " + code + " " + error; - reply.setHeaderField("Content-type", "text/html"); - reply.setHeaderField("Server", ProxyConfig.appName+"/" +ProxyConfig.appVersion); + default: + error = "Error"; + break; + } - content = new StringBuffer(); - content.append(message); + reply = new Reply(); + reply.statusLine = "HTTP/1.0 " + code + " " + error; + reply.setHeaderField("Content-type", "text/html"); + reply.setHeaderField("Server", ProxyConfig.appName + "/" + ProxyConfig.appVersion); + + content = new StringBuffer(); + content.append(message); } - Reply getReply() - { - return reply; + Reply getReply() { + return reply; } - String getContent() - { - if (content == null) - { - return null; - } - return content.toString(); + String getContent() { + if (content == null) { + return null; + } + return content.toString(); } - public String toString() - { - StringBuffer buf = new StringBuffer(); - if (reply != null) - { - buf.append(reply.toString()); - } - if (content != null) - { - buf.append(content.toString()); - } - return buf.toString(); + public String toString() { + StringBuffer buf = new StringBuffer(); + if (reply != null) { + buf.append(reply.toString()); + } + if (content != null) { + buf.append(content.toString()); + } + return buf.toString(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java index f2808e42c..aed61531c 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpRelay.java @@ -4,9 +4,11 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; -public interface HttpRelay -{ +public interface HttpRelay { + void sendRequest(Request request) throws IOException, RetryRequestException; + Reply recvReply(Request request) throws IOException, RetryRequestException; + void close(); } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Https.java b/libsrc/jpproxy/src/com/jpexs/proxy/Https.java index 0bb7b7571..27da62bd8 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Https.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Https.java @@ -10,143 +10,118 @@ import java.util.Vector; * @author JPEXS */ public class Https extends Http { - /* XXX - more than 1 should work now. */ + /* XXX - more than 1 should work now. */ + static final int MAX_PENDING_REQUESTS = 1; static Hashtable cache = new Hashtable(33); private static Object httpLock = new Object(); - public Https(String host, int port) throws IOException - { - this(host, port, false); + public Https(String host, int port) throws IOException { + this(host, port, false); } - public Https(String host, int port, boolean isProxy) throws IOException - { - super(host, port,isProxy); + public Https(String host, int port, boolean isProxy) throws IOException { + super(host, port, isProxy); } - private static String cacheKey(String host, int port) - { - return host.toLowerCase() + ":" + port; + private static String cacheKey(String host, int port) { + return host.toLowerCase() + ":" + port; } - private static Vector cacheLookup(String host, int port) - { - Vector v = (Vector) cache.get(cacheKey(host, port)); - return v; + 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 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 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 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(); - } - } - } + 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; + 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); + 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; - } - } + /* 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.idle = 0; + } + } + } - if (http == null) - { - http = new Https(host, port, isProxy); - cacheInsert(host, port, http); - } + if (http == null) { + http = new Https(host, port, isProxy); + cacheInsert(host, port, http); + } - return http; + return http; } - static Https open(String host, int port) throws IOException - { - return open(host, port, false); + 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 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(); + static synchronized void clean() { + cacheClean(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java b/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java index 1dff75fec..ebf3fa6ef 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/HttpsThrough.java @@ -4,36 +4,36 @@ import java.io.IOException; class HttpsThrough extends HttpConnection { - boolean proxy = false; + boolean proxy = false; - HttpsThrough(String host, int port) throws IOException { - super(host, port); - } + HttpsThrough(String host, int port) throws IOException { + super(host, port); + } - HttpsThrough(String host, int port, boolean isProxy) throws IOException { - this(host, port); - proxy = isProxy; - } + HttpsThrough(String host, int port, boolean isProxy) throws IOException { + this(host, port); + proxy = isProxy; + } - public void sendRequest(Request request) - throws IOException, RetryRequestException { - if (proxy) { - super.sendRequest(request); - } else { - /* nothing */ - } - } + public void sendRequest(Request request) + throws IOException, RetryRequestException { + if (proxy) { + super.sendRequest(request); + } else { + /* nothing */ + } + } - 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); - } + 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); + } - return reply; - } + return reply; + } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java b/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java index de73453e7..53b2382bb 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Janitor.java @@ -3,39 +3,32 @@ package com.jpexs.proxy; import java.util.Enumeration; import java.util.Vector; -class Janitor implements Runnable -{ +class Janitor implements Runnable { + private Vector cleanable = new Vector(); - - public void add(Cleanable c) - { - cleanable.addElement(c); + + public void add(Cleanable c) { + cleanable.addElement(c); } - - public void run() - { - Thread.currentThread().setName("Janitor"); - - for (;;) - { - try - { - Thread.sleep(30 * 1000); /* 30 seconds */ - } - catch (Exception e) - { - - } - for (Enumeration e = cleanable.elements(); - e.hasMoreElements(); ) - { - ((Cleanable)e.nextElement()).clean(); - } + public void run() { + Thread.currentThread().setName("Janitor"); - Http.clean(); + for (;;) { + try { + Thread.sleep(30 * 1000); /* 30 seconds */ - - } + } catch (Exception e) { + + } + + for (Enumeration e = cleanable.elements(); + e.hasMoreElements();) { + ((Cleanable) e.nextElement()).clean(); + } + + Http.clean(); + + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Key.java b/libsrc/jpproxy/src/com/jpexs/proxy/Key.java index 79c552550..18fca5321 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Key.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Key.java @@ -1,6 +1,7 @@ package com.jpexs.proxy; class Key { + private String name = null; /** @@ -31,4 +32,4 @@ class Key { public String toString() { return name; } -} \ No newline at end of file +} diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Main.java b/libsrc/jpproxy/src/com/jpexs/proxy/Main.java index 2b41fa0c1..f690e36b6 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Main.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Main.java @@ -8,22 +8,22 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; -public class Main -{ +public class Main { public static final String REPLACEMENTSFILE = "." + File.separator + "config" + File.separator + "replacements.ini"; public static boolean DEBUG_MODE = false; - public static void main(String[] argv) - { - List replacements = new ArrayList(); + public static void main(String[] argv) { + List replacements = new ArrayList(); if ((new File(REPLACEMENTSFILE)).exists()) { try { BufferedReader br = new BufferedReader(new FileReader(REPLACEMENTSFILE)); String s = ""; while ((s = br.readLine()) != null) { String fileName = br.readLine(); - if (fileName == null) break; + if (fileName == null) { + break; + } fileName = fileName.replaceAll("[\\\\/]", File.separator); Replacement r = new Replacement(s, fileName); if (DEBUG_MODE) { @@ -40,25 +40,26 @@ public class Main System.out.println("WARNING:REPLACEMENTS FILE NOT FOUND."); } } - Server.startServer(ProxyConfig.port, replacements, new ArrayList(), new CatchedListener() { + Server.startServer(ProxyConfig.port, replacements, new ArrayList(), new CatchedListener() { /** * Method called when specified contentType is received * * @param contentType Content type - * @param url URL of the method - * @param data Data stream + * @param url URL of the method + * @param data Data stream */ - public void catched(String contentType, String url, InputStream data) { + public byte[] catched(String contentType, String url, InputStream data) { + return null; } - }, new ReplacedListener() { + }, new ReplacedListener() { public void replaced(Replacement replacement, String url, String contentType) { - if (DEBUG_MODE) { - System.out.println("REPLACED:" + url + " (Content-type:" + contentType + ") WITH FILE " + replacement.targetFile); - } + if (DEBUG_MODE) { + System.out.println("REPLACED:" + url + " (Content-type:" + contentType + ") WITH FILE " + replacement.targetFile); + } } - }); + }); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Message.java b/libsrc/jpproxy/src/com/jpexs/proxy/Message.java index b3cca4456..42c34aabb 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Message.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Message.java @@ -5,8 +5,8 @@ import java.util.Enumeration; import java.util.Vector; import java.io.*; -public abstract class Message -{ +public abstract class Message { + /** * Hashtable used to store message headers. */ @@ -17,199 +17,155 @@ public abstract class Message */ String statusLine = null; - public String readLine(InputStream in) throws IOException - { - char[] buf = new char[128]; - int offset = 0; - int ch; + public String readLine(InputStream in) throws IOException { + char[] buf = new char[128]; + int offset = 0; + int ch; - for (;;) - { - ch = in.read(); - if (ch == -1 || ch == '\n') - { - break; - } - else if (ch == '\r') - { - int tmpch = in.read(); - if (tmpch != '\n') - { - if (! (in instanceof PushbackInputStream)) - { - in = new PushbackInputStream(in); - } - ((PushbackInputStream) in).unread(tmpch); - } - break; - } - else - { - if (offset == buf.length) - { - char[] tmpbuf = buf; - buf = new char[tmpbuf.length * 2]; - System.arraycopy(tmpbuf, 0, buf, 0, offset); - } - buf[offset++] = (char) ch; - } - } - return String.copyValueOf(buf, 0, offset); + for (;;) { + ch = in.read(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch == '\r') { + int tmpch = in.read(); + if (tmpch != '\n') { + if (!(in instanceof PushbackInputStream)) { + in = new PushbackInputStream(in); + } + ((PushbackInputStream) in).unread(tmpch); + } + break; + } else { + if (offset == buf.length) { + char[] tmpbuf = buf; + buf = new char[tmpbuf.length * 2]; + System.arraycopy(tmpbuf, 0, buf, 0, offset); + } + buf[offset++] = (char) ch; + } + } + return String.copyValueOf(buf, 0, offset); } /** * Read headers and store them in the hashtable. */ - void readHeaders(InputStream in) throws IOException - { - int i; - Key key = null; - - for (;;) - { - String s = readLine(in); - if (s == null) - { - break; - } - i = s.indexOf(':'); - if (i == -1) - { - /* end of header */ - if (s.length() == 0) - { - break; - } - /* multi-line headers */ - else if (key != null - && (s.startsWith(" ") || s.startsWith("\t"))) - { - int index = getHeaderValueCount(key.toString()); - index--; - Vector v = (Vector) headers.get(key); - v.setElementAt(v.elementAt(index) + "\n" + s, index); - } - } - else - { - key = new Key(s.substring(0, i)); - Vector v; - if (headers.containsKey(key)) - { - v = (Vector) headers.get(key); - } - else - { - v = new Vector(); - } - v.addElement(s.substring(i+1).trim()); - headers.put(key, v); - } - } + void readHeaders(InputStream in) throws IOException { + int i; + Key key = null; + + for (;;) { + String s = readLine(in); + if (s == null) { + break; + } + i = s.indexOf(':'); + if (i == -1) { + /* end of header */ + if (s.length() == 0) { + break; + } /* multi-line headers */ else if (key != null + && (s.startsWith(" ") || s.startsWith("\t"))) { + int index = getHeaderValueCount(key.toString()); + index--; + Vector v = (Vector) headers.get(key); + v.setElementAt(v.elementAt(index) + "\n" + s, index); + } + } else { + key = new Key(s.substring(0, i)); + Vector v; + if (headers.containsKey(key)) { + v = (Vector) headers.get(key); + } else { + v = new Vector(); + } + v.addElement(s.substring(i + 1).trim()); + headers.put(key, v); + } + } } - public int headerCount() - { - return headers.size(); + public int headerCount() { + return headers.size(); } /** * Set the Status line. */ - public void setStatusLine(String l) - { + public void setStatusLine(String l) { statusLine = l; } - public int getHeaderValueCount(String name) - { - Vector v = (Vector) headers.get(new Key(name)); - return v.size(); - } - - public String getHeaderField(String name) - { - return getHeaderField(name, 0); - } - - public String getHeaderField(String name, int index) - { - Vector v = (Vector) headers.get(new Key(name)); - if (v == null) - { - return null; - } - return (String) v.elementAt(index); + public int getHeaderValueCount(String name) { + Vector v = (Vector) headers.get(new Key(name)); + return v.size(); } - public void setHeaderField(String name, String value) - { - setHeaderField(name, value, 0); + public String getHeaderField(String name) { + return getHeaderField(name, 0); } - public void setHeaderField(String name, String value, int index) - { - Vector v; - Key key = new Key(name); - - if (headers.containsKey(key)) - { - v = (Vector) headers.get(key); - } - else - { - v = new Vector(); - if (index == 0) - { - v.addElement(""); - } - headers.put(key, v); - } - v.setElementAt(value, index); - } - - public void setHeaderField(String name, int value) - { - setHeaderField(name, value, 0); + public String getHeaderField(String name, int index) { + Vector v = (Vector) headers.get(new Key(name)); + if (v == null) { + return null; + } + return (String) v.elementAt(index); } - public void setHeaderField(String name, int value, int index) - { - setHeaderField(name, new Integer(value).toString(), index); + public void setHeaderField(String name, String value) { + setHeaderField(name, value, 0); + } + + public void setHeaderField(String name, String value, int index) { + Vector v; + Key key = new Key(name); + + if (headers.containsKey(key)) { + v = (Vector) headers.get(key); + } else { + v = new Vector(); + if (index == 0) { + v.addElement(""); + } + headers.put(key, v); + } + v.setElementAt(value, index); + } + + public void setHeaderField(String name, int value) { + setHeaderField(name, value, 0); + } + + public void setHeaderField(String name, int value, int index) { + setHeaderField(name, new Integer(value).toString(), index); } /** - * Set all header fields with the give name to the - * specified value. + * Set all header fields with the give name to the specified value. */ - public void setHeaderFields(String name, String value) - { - Vector v; - Key key = new Key(name); + public void setHeaderFields(String name, String value) { + Vector v; + Key key = new Key(name); - v = (Vector) headers.get(key); - if (v != null) - { - for (int i = 0; i < v.size(); i++) - { - v.setElementAt(value, i); - } - } + v = (Vector) headers.get(key); + if (v != null) { + for (int i = 0; i < v.size(); i++) { + v.setElementAt(value, i); + } + } } - public void appendHeaderField(String name, String value) - { - appendHeaderField(name, value, 0); + public void appendHeaderField(String name, String value) { + appendHeaderField(name, value, 0); } - - public void appendHeaderField(String name, String value, int index) - { - setHeaderField(name, getHeaderField(name, index) + value, index); + + public void appendHeaderField(String name, String value, int index) { + setHeaderField(name, getHeaderField(name, index) + value, index); } - - public void removeHeaderField(String name) - { - headers.remove(new Key(name)); + + public void removeHeaderField(String name) { + headers.remove(new Key(name)); } /** @@ -217,81 +173,70 @@ public abstract class Message * * @param name header name */ - public boolean containsHeaderField(String name) - { - return headers.containsKey(new Key(name)); + public boolean containsHeaderField(String name) { + return headers.containsKey(new Key(name)); } /** * @return an Enumeration of Strings */ - public Enumeration getHeaders() - { - Vector v = new Vector(); + public Enumeration getHeaders() { + Vector v = new Vector(); - for (Enumeration e = headers.keys(); e.hasMoreElements(); ) - { - v.addElement(e.nextElement().toString()); - } + for (Enumeration e = headers.keys(); e.hasMoreElements();) { + v.addElement(e.nextElement().toString()); + } - return v.elements(); + return v.elements(); } private final static byte[] COLON_SPACE = ": ".getBytes(); private final static byte[] CRLF = "\r\n".getBytes(); - - private ByteArray toByteArray(byte[] sep) - { - ByteArray buf = new ByteArray(); - Key key; - String value; - Vector v; - int i = 0; - - buf.append(statusLine); - buf.append(sep); - - for (Enumeration e = headers.keys(); e.hasMoreElements(); ) - { - key = (Key) e.nextElement(); - v = (Vector) headers.get(key); - for (i = 0; i < v.size(); i++) - { - buf.append(key.toString()); - buf.append(COLON_SPACE); - buf.append(v.elementAt(i).toString()); - buf.append(sep); - } - } - buf.append(sep); - return buf; + private ByteArray toByteArray(byte[] sep) { + ByteArray buf = new ByteArray(); + Key key; + String value; + Vector v; + int i = 0; + + buf.append(statusLine); + buf.append(sep); + + for (Enumeration e = headers.keys(); e.hasMoreElements();) { + key = (Key) e.nextElement(); + v = (Vector) headers.get(key); + for (i = 0; i < v.size(); i++) { + buf.append(key.toString()); + buf.append(COLON_SPACE); + buf.append(v.elementAt(i).toString()); + buf.append(sep); + } + } + buf.append(sep); + + return buf; } - private ByteArray toByteArray() - { - return toByteArray(CRLF); + private ByteArray toByteArray() { + return toByteArray(CRLF); } - private ByteArray toByteArray(String sep) - { - return toByteArray(sep.getBytes()); + private ByteArray toByteArray(String sep) { + return toByteArray(sep.getBytes()); } - - public String toString() - { - return toByteArray().toString(); + + public String toString() { + return toByteArray().toString(); } - - public String toString(String sep) - { - return toByteArray(sep).toString(); + + public String toString(String sep) { + return toByteArray(sep).toString(); } public void write(OutputStream out) - throws IOException - { - toByteArray().writeTo(out); - out.flush(); + throws IOException { + toByteArray().writeTo(out); + out.flush(); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java b/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java index f470d1bf6..4b3ac637d 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ProxyConfig.java @@ -9,35 +9,35 @@ import java.util.List; */ public class ProxyConfig { - public static boolean dontLogFilters=false; + public static boolean dontLogFilters = false; - public static String appVersion="1.1"; - public static String appName="JPProxy"; - public static int port=55555; - public static String bindAddress; + public static String appVersion = "1.1"; + public static String appName = "JPProxy"; + public static int port = 55555; + public static String bindAddress; - public static int readTimeout=50000; - public static boolean proxyKeepAlive=false; - public static boolean dontUncompress=false; + public static int readTimeout = 50000; + public static boolean proxyKeepAlive = false; + public static boolean dontUncompress = false; - public static final int HTTPS_PASSTHRU=0; - public static final int HTTPS_FILTER=1; - public static final int HTTPS_FILTERLIST=2; + public static final int HTTPS_PASSTHRU = 0; + public static final int HTTPS_FILTER = 1; + public static final int HTTPS_FILTERLIST = 2; - public static int httpsMode=HTTPS_PASSTHRU; + public static int httpsMode = HTTPS_PASSTHRU; - public static List enabledHttpsServers=new ArrayList(); + public static List enabledHttpsServers = new ArrayList(); - public static boolean useHTTPSProxy=false; - public static String httpsProxyHost=""; - public static int httpsProxyPort=0; + public static boolean useHTTPSProxy = false; + public static String httpsProxyHost = ""; + public static int httpsProxyPort = 0; - public static boolean useHTTPProxy=false; - public static String httpProxyHost=""; - public static int httpProxyPort=0; + public static boolean useHTTPProxy = false; + public static String httpProxyHost = ""; + public static int httpProxyPort = 0; - public static String httpsKeyStoreFile=null; - public static String httpsKeyStorePass=null; - public static String httpsKeyPass=null; + public static String httpsKeyStoreFile = null; + public static String httpsKeyStorePass = null; + public static String httpsKeyPass = null; } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java b/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java index b23576348..3304e76e4 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ReplacedListener.java @@ -1,5 +1,6 @@ -package com.jpexs.proxy; - -public interface ReplacedListener { - public void replaced(Replacement replacement, String url, String contentType); -} +package com.jpexs.proxy; + +public interface ReplacedListener { + + public void replaced(Replacement replacement, String url, String contentType); +} diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java b/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java index 0d7fc4da8..ce1f6b8d8 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Reply.java @@ -8,238 +8,195 @@ import java.io.SequenceInputStream; import java.util.Hashtable; import java.util.StringTokenizer; -public class Reply extends Message -{ +public class Reply extends Message { + InputStream in = null; int statusCode = -1; - public Reply() - { + public Reply() { } - public Reply(InputStream in) - { - setContent(in); + public Reply(InputStream in) { + setContent(in); } - public void setContent(InputStream in) - { - this.in = in; + public void setContent(InputStream in) { + this.in = in; } - public InputStream getContent() - { - return in; - } - - void read() throws IOException - { - if (in != null) - { - read(in); - } - } - - void read(InputStream in) throws IOException - { - statusLine = readLine(in); - if (statusLine == null || statusLine.length() == 0) - { - throw new IOException("Missing HTTP status line"); - } - - /* Look for HTTP/0.9 */ - if (!statusLine.startsWith("HTTP")) - { - /* Put back the line */ - if (this.in != null) - { - String putback = new String(statusLine + "\n"); - this.in = new SequenceInputStream(new StringBufferInputStream(putback), in); - } - /* Fake a status line and upgrade to HTTP/1.0 */ - statusLine = "HTTP/1.0 200 OK"; - return; - } - - readHeaders(in); - int code = getStatusCode(); - - /* RFC 2068: 204 and 304 MUST NOT contain a message body. */ - switch (code) - { - case 204: /* No Content */ - case 304: /* Not Modified */ - /* Ignore the message body if it exists */ - if (containsHeaderField("Content-length")) - { - int contentLength = 0; - try - { - contentLength = Integer.parseInt(getHeaderField("Content-length")); - } - catch (NumberFormatException e) - { - } - int n; - byte[] buffer = new byte[8192]; - while ((n = in.read(buffer, 0, buffer.length)) > 0) - { - /* ignore */ - } - removeHeaderField("Content-length"); - } - break; - } + public InputStream getContent() { + return in; } - public boolean hasContent() - { - switch (getStatusCode()) - { - case 204: - case 304: - return false; - - default: - return true; - } + void read() throws IOException { + if (in != null) { + read(in); + } } - public String getProtocol() - { - StringTokenizer st = new StringTokenizer(statusLine); - String protocol = (String) st.nextToken(); - return protocol; + void read(InputStream in) throws IOException { + statusLine = readLine(in); + if (statusLine == null || statusLine.length() == 0) { + throw new IOException("Missing HTTP status line"); + } + + /* Look for HTTP/0.9 */ + if (!statusLine.startsWith("HTTP")) { + /* Put back the line */ + if (this.in != null) { + String putback = new String(statusLine + "\n"); + this.in = new SequenceInputStream(new StringBufferInputStream(putback), in); + } + /* Fake a status line and upgrade to HTTP/1.0 */ + statusLine = "HTTP/1.0 200 OK"; + return; + } + + readHeaders(in); + int code = getStatusCode(); + + /* RFC 2068: 204 and 304 MUST NOT contain a message body. */ + switch (code) { + case 204: /* No Content */ + + case 304: /* Not Modified */ + /* Ignore the message body if it exists */ + + if (containsHeaderField("Content-length")) { + int contentLength = 0; + try { + contentLength = Integer.parseInt(getHeaderField("Content-length")); + } catch (NumberFormatException e) { + } + int n; + byte[] buffer = new byte[8192]; + while ((n = in.read(buffer, 0, buffer.length)) > 0) { + /* ignore */ + } + removeHeaderField("Content-length"); + } + break; + } } - public int getStatusCode() - { - if (statusCode == -1) - { - StringTokenizer st = new StringTokenizer(statusLine); - String protocol = (String) st.nextToken(); - String status = (String) st.nextToken(); + public boolean hasContent() { + switch (getStatusCode()) { + case 204: + case 304: + return false; - try - { - statusCode = Integer.parseInt(status); - } - catch (NumberFormatException e) - { - System.out.println("Malformed or missing status code"); - statusCode = 0; - } - } - - return statusCode; + default: + return true; + } } - private Hashtable headerParser(String header) - { - Hashtable table = new Hashtable(); - String type = getHeaderField(header); - if (type == null) - { - return table; - } - - StringTokenizer st = new StringTokenizer(type, ";"); - int count = 0; - while (st.hasMoreTokens()) - { - String token = st.nextToken(); - token = token.trim(); - String name; - String value; - int i = token.indexOf('='); - if (i != -1) - { - name = token.substring(0, i); - value = token.substring(i+1); - } - else - { - name = token; - value = ""; - } - - if (count == 0) - { - table.put(header, name); - } - else - { - table.put(name, value); - } - - count++; - } - - return table; + public String getProtocol() { + StringTokenizer st = new StringTokenizer(statusLine); + String protocol = (String) st.nextToken(); + return protocol; } - public String getContentType() - { - Hashtable table = headerParser("Content-type"); - return(String) table.get("Content-type"); + public int getStatusCode() { + if (statusCode == -1) { + StringTokenizer st = new StringTokenizer(statusLine); + String protocol = (String) st.nextToken(); + String status = (String) st.nextToken(); + + try { + statusCode = Integer.parseInt(status); + } catch (NumberFormatException e) { + System.out.println("Malformed or missing status code"); + statusCode = 0; + } + } + + return statusCode; } - public String getBoundary() - { - Hashtable table = headerParser("Content-type"); - return(String) table.get("boundary"); + private Hashtable headerParser(String header) { + Hashtable table = new Hashtable(); + String type = getHeaderField(header); + if (type == null) { + return table; + } + + StringTokenizer st = new StringTokenizer(type, ";"); + int count = 0; + while (st.hasMoreTokens()) { + String token = st.nextToken(); + token = token.trim(); + String name; + String value; + int i = token.indexOf('='); + if (i != -1) { + name = token.substring(0, i); + value = token.substring(i + 1); + } else { + name = token; + value = ""; + } + + if (count == 0) { + table.put(header, name); + } else { + table.put(name, value); + } + + count++; + } + + return table; } - public String getTransferEncoding() - { - Hashtable table = headerParser("Transfer-Encoding"); - return(String) table.get("Transfer-Encoding"); + public String getContentType() { + Hashtable table = headerParser("Content-type"); + return (String) table.get("Content-type"); } - public int getChunkSize(InputStream in) throws IOException - { - String line = readLine(in); - line = line.trim(); /* apache can have trailing spaces */ - int size = -1; - try - { - size = Integer.valueOf(line, 16).intValue(); - } - catch (NumberFormatException e) - { - System.out.println(e); - } - return size; + public String getBoundary() { + Hashtable table = headerParser("Content-type"); + return (String) table.get("boundary"); } - public void getChunkedFooter(InputStream in) throws IOException - { - for (;;) - { - String line = readLine(in); - if (line == null) - { - break; - } - int i = line.indexOf(':'); - if (i == -1) - { - break; - } - } + public String getTransferEncoding() { + Hashtable table = headerParser("Transfer-Encoding"); + return (String) table.get("Transfer-Encoding"); } - public void setStatusLine(String line) - { - this.statusLine = line; + public int getChunkSize(InputStream in) throws IOException { + String line = readLine(in); + line = line.trim(); /* apache can have trailing spaces */ + + int size = -1; + try { + size = Integer.valueOf(line, 16).intValue(); + } catch (NumberFormatException e) { + System.out.println(e); + } + return size; } - public static Reply createRedirect(String url) - { - Reply r = new Reply(); - r.setStatusLine("HTTP/1.0 302 Moved Temporarily"); - r.setHeaderField("Location", url); - return r; + public void getChunkedFooter(InputStream in) throws IOException { + for (;;) { + String line = readLine(in); + if (line == null) { + break; + } + int i = line.indexOf(':'); + if (i == -1) { + break; + } + } + } + + public void setStatusLine(String line) { + this.statusLine = line; + } + + public static Reply createRedirect(String url) { + Reply r = new Reply(); + r.setStatusLine("HTTP/1.0 302 Moved Temporarily"); + r.setHeaderField("Location", url); + return r; } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Request.java b/libsrc/jpproxy/src/com/jpexs/proxy/Request.java index e227a96a4..b7630e106 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Request.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Request.java @@ -8,9 +8,8 @@ import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; -public class Request extends Message -{ - +public class Request extends Message { + private String command = null; private String url = null; private String protocol = null; @@ -18,264 +17,207 @@ public class Request extends Message private Client client = null; private Hashtable log; private Vector logHeaders; - public boolean hadKeepalive=false; + public boolean hadKeepalive = false; - - Request(Client c) - { - client = c; + Request(Client c) { + client = c; } - void read(InputStream in) throws IOException - { - statusLine = readLine(in); - if (statusLine == null || statusLine.length() == 0) - { - throw new IOException("Empty request"); - } + void read(InputStream in) throws IOException { + statusLine = readLine(in); + if (statusLine == null || statusLine.length() == 0) { + throw new IOException("Empty request"); + } - StringTokenizer st = new StringTokenizer(statusLine); - command = (String) st.nextToken(); - url = (String) st.nextToken(); - protocol = (String) st.nextToken(); + StringTokenizer st = new StringTokenizer(statusLine); + command = (String) st.nextToken(); + url = (String) st.nextToken(); + protocol = (String) st.nextToken(); - if (!url.startsWith("http")) - { - //TODO do something here - } + if (!url.startsWith("http")) { + //TODO do something here + } - readHeaders(in); + readHeaders(in); - if ("POST".equals(command) || "PUT".equals(command)) - { - try - { - int n = Integer.parseInt(getHeaderField("Content-length")); - data = new byte[n]; - int offset = 0; - while (offset < data.length) - { - n = in.read(data, offset, data.length - offset); - if (n < 0) - { - throw new IOException("Not enough " + command + " data"); - } - offset += n; - } - } - catch (NumberFormatException e) - { - - } - } + if ("POST".equals(command) || "PUT".equals(command)) { + try { + int n = Integer.parseInt(getHeaderField("Content-length")); + data = new byte[n]; + int offset = 0; + while (offset < data.length) { + n = in.read(data, offset, data.length - offset); + if (n < 0) { + throw new IOException("Not enough " + command + " data"); + } + offset += n; + } + } catch (NumberFormatException e) { + + } + } } public void write(OutputStream out) - throws IOException - { - super.write(out); - if (data != null) - { - out.write(data); - out.flush(); - } + throws IOException { + super.write(out); + if (data != null) { + out.write(data); + out.flush(); + } } - public String getRequest() - { - return statusLine; + public String getRequest() { + return statusLine; } - public String getCommand() - { - return command; - } - - public void setCommand(String command) - { - this.command = command; + public String getCommand() { + return command; } - public String getURL() - { - return url; + public void setCommand(String command) { + this.command = command; } - public void setURL(String url) - { - this.url = url; + public String getURL() { + return url; } - public String getProtocol() - { - return protocol; + public void setURL(String url) { + this.url = url; } - public void setProtocol(String protocol) - { - this.protocol = protocol; + public String getProtocol() { + return protocol; } - public void addSecureHostToURL(String host){ - url="https://"+host+url; + public void setProtocol(String protocol) { + this.protocol = protocol; } - public String getHost() - { - String url = getURL(); - String s; - - if (url.startsWith("http://")) - { - s = url.substring(7, url.indexOf('/', 7)); - } - else if (url.startsWith("https://")) - { - s = url.substring(8, url.indexOf('/', 8)); - } - else - { - s = url; - } - - int at = s.indexOf('@'); - if (at != -1 ) - { - s = s.substring(at+1); - } - - if (s.indexOf(':') != -1) - { - return s.substring(0, s.indexOf(':')); - } - - return s; + public void addSecureHostToURL(String host) { + url = "https://" + host + url; } - public int getPort() - { - int port = 80; - String url = getURL(); - String s; + public String getHost() { + String url = getURL(); + String s; - if (url.startsWith("http://")) - { - s = url.substring(7, url.indexOf('/', 7)); - } - else if (url.startsWith("https://")) - { - s = url.substring(8, url.indexOf('/', 8)); - port=443; - } - else - { - s = url; - } + if (url.startsWith("http://")) { + s = url.substring(7, url.indexOf('/', 7)); + } else if (url.startsWith("https://")) { + s = url.substring(8, url.indexOf('/', 8)); + } else { + s = url; + } - int at = s.indexOf('@'); - if (at != -1 ) - { - s = s.substring(at+1); - } + int at = s.indexOf('@'); + if (at != -1) { + s = s.substring(at + 1); + } - if (s.indexOf(':') != -1) - { - try - { - port = Integer.parseInt(s.substring(s.indexOf(':') + 1)); - } - catch (NumberFormatException e) - { - - } - } - return port; + if (s.indexOf(':') != -1) { + return s.substring(0, s.indexOf(':')); + } + + return s; } - public String getData() - { - if (data == null) - { - return null; - } - return new String(data); + public int getPort() { + int port = 80; + String url = getURL(); + String s; + + if (url.startsWith("http://")) { + s = url.substring(7, url.indexOf('/', 7)); + } else if (url.startsWith("https://")) { + s = url.substring(8, url.indexOf('/', 8)); + port = 443; + } else { + s = url; + } + + int at = s.indexOf('@'); + if (at != -1) { + s = s.substring(at + 1); + } + + if (s.indexOf(':') != -1) { + try { + port = Integer.parseInt(s.substring(s.indexOf(':') + 1)); + } catch (NumberFormatException e) { + + } + } + return port; } - public String getPath() - { - String str = getURL(); - int pos = 0; - for (int i = 0; i < 3; i++) - { - pos = str.indexOf('/', pos); - pos++; - } - pos--; - return str.substring(pos); - } - - public String getDocument() - { - String path = getPath(); - int n = path.lastIndexOf('/'); - if (n == path.length() - 1) - { - n = path.lastIndexOf('/', n - 1); - } - if (n < 0) - { - return "/"; - } - else - { - return path.substring(n + 1); - } + public String getData() { + if (data == null) { + return null; + } + return new String(data); } - public Client getClient() - { - return client; + public String getPath() { + String str = getURL(); + int pos = 0; + for (int i = 0; i < 3; i++) { + pos = str.indexOf('/', pos); + pos++; + } + pos--; + return str.substring(pos); } - public String getQueryString() - { - String path = getPath(); - int n = path.indexOf('?'); - if (n < 0) - { - return null; - } - return path.substring(n + 1); + public String getDocument() { + String path = getPath(); + int n = path.lastIndexOf('/'); + if (n == path.length() - 1) { + n = path.lastIndexOf('/', n - 1); + } + if (n < 0) { + return "/"; + } else { + return path.substring(n + 1); + } + } + + public Client getClient() { + return client; + } + + public String getQueryString() { + String path = getPath(); + int n = path.indexOf('?'); + if (n < 0) { + return null; + } + return path.substring(n + 1); } public synchronized void addLogEntry(String header, - String message) - { - if (log == null) - { - log = new Hashtable(); - logHeaders = new Vector(); - } + String message) { + if (log == null) { + log = new Hashtable(); + logHeaders = new Vector(); + } - Vector v = (Vector)log.get(header); - if (log.get(header) == null) - { - v = new Vector(); - log.put(header, v); - logHeaders.addElement(header); - } - v.addElement(message); + Vector v = (Vector) log.get(header); + if (log.get(header) == null) { + v = new Vector(); + log.put(header, v); + logHeaders.addElement(header); + } + v.addElement(message); } - public Enumeration getLogHeaders() - { - return logHeaders != null ? logHeaders.elements() : null; + public Enumeration getLogHeaders() { + return logHeaders != null ? logHeaders.elements() : null; } - public Enumeration getLogEntries(String header) - { - return log != null ? ((Vector)log.get(header)).elements() : null; + public Enumeration getLogEntries(String header) { + return log != null ? ((Vector) log.get(header)).elements() : null; } - } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java b/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java index 9d2d20235..873b5eb7e 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/RetryRequestException.java @@ -1,14 +1,12 @@ package com.jpexs.proxy; -class RetryRequestException extends Exception -{ - RetryRequestException() - { - super(); +class RetryRequestException extends Exception { + + RetryRequestException() { + super(); } - - RetryRequestException(String message) - { - super(message); + + RetryRequestException(String message) { + super(message); } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java b/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java index c2621772c..090ec7888 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ReusableThread.java @@ -1,70 +1,57 @@ package com.jpexs.proxy; -public class ReusableThread extends Thread -{ +public class ReusableThread extends Thread { + private ThreadPool pool = null; private Runnable runnable = null; private boolean alive = true; private long lastrun = 0; private int used = 0; - public ReusableThread(ThreadPool pool) - { - this.pool = pool; + public ReusableThread(ThreadPool pool) { + this.pool = pool; } - public synchronized void setRunnable(Runnable runnable) - { - this.runnable = runnable; - notify(); + public synchronized void setRunnable(Runnable runnable) { + this.runnable = runnable; + notify(); } - public synchronized void terminate() - { - alive = false; - notify(); + public synchronized void terminate() { + alive = false; + notify(); } - public long getLastRunTime() - { - return lastrun; + public long getLastRunTime() { + return lastrun; } - public int useCount() - { - return used; + public int useCount() { + return used; } - - public void run() - { - while (alive) - { - setName("ReusableThread: idle"); - - while (runnable == null && alive) - { - synchronized (this) - { - try - { - wait(); - } - catch (InterruptedException ie) - { - } - } - } - if (alive) - { - setName("ReusableThread: busy"); - setPriority(Thread.NORM_PRIORITY); - lastrun = System.currentTimeMillis(); - used++; - runnable.run(); - runnable = null; - pool.put(this); - } - } + public void run() { + while (alive) { + setName("ReusableThread: idle"); + + while (runnable == null && alive) { + synchronized (this) { + try { + wait(); + } catch (InterruptedException ie) { + } + } + } + + if (alive) { + setName("ReusableThread: busy"); + setPriority(Thread.NORM_PRIORITY); + lastrun = System.currentTimeMillis(); + used++; + runnable.run(); + runnable = null; + pool.put(this); + } + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/Server.java b/libsrc/jpproxy/src/com/jpexs/proxy/Server.java index 70d9f69e8..00ba753a7 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/Server.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/Server.java @@ -7,8 +7,8 @@ import java.io.IOException; import java.io.DataOutputStream; import java.util.List; -public class Server implements Runnable -{ +public class Server implements Runnable { + ServerSocket server = null; boolean running = false; @@ -17,145 +17,121 @@ public class Server implements Runnable private ReplacedListener replacedListener; private List replacements; - static ThreadPool pool; + static ThreadPool pool; static Server myServer; - static boolean serverRunning=false; + static boolean serverRunning = false; - static boolean stopping=false; + static boolean stopping = false; - public static ReusableThread getThread() - { - return pool.get(); + public static ReusableThread getThread() { + return pool.get(); } /** * Starts proxy server * - * @param port Listening port - * @param replacements List of replacements + * @param port Listening port + * @param replacements List of replacements * @param catchedContentTypes Content types to sniff - * @param catchedListener Catched listener + * @param catchedListener Catched listener */ public static boolean startServer(int port, List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) { stopServer(); - try { - myServer = new Server(port, replacements, catchedContentTypes, catchedListener, replacedListener); - } catch (IOException ex) { - return false; - } - pool = new ThreadPool(ProxyConfig.appName+" Threads"); + try { + myServer = new Server(port, replacements, catchedContentTypes, catchedListener, replacedListener); + } catch (IOException ex) { + return false; + } + pool = new ThreadPool(ProxyConfig.appName + " Threads"); /* Startup the Janitor */ - Janitor j = new Janitor(); - j.add(pool); - getThread().setRunnable(j); - serverRunning=true; - getThread().setRunnable(myServer); - return true; + Janitor j = new Janitor(); + j.add(pool); + getThread().setRunnable(j); + serverRunning = true; + getThread().setRunnable(myServer); + return true; } + public static void stopServer() { + if (serverRunning) { + serverRunning = false; + try { + myServer.server.close(); + } catch (IOException ex) { - public static void stopServer() - { - if(serverRunning){ - serverRunning=false; - try { - myServer.server.close(); - } catch (IOException ex) { - - } - pool.clean(); - } + } + pool.clean(); + } } - Server(int port,List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) throws IOException - { - this.replacements = replacements; - this.catchedContentTypes = catchedContentTypes; + Server(int port, List replacements, List catchedContentTypes, CatchedListener catchedListener, ReplacedListener replacedListener) throws IOException { + + this.replacements = replacements; + this.catchedContentTypes = catchedContentTypes; this.catchedListener = catchedListener; this.replacedListener = replacedListener; - - try - { - String bindaddr = ProxyConfig.bindAddress; - if (bindaddr != null && bindaddr.length() > 0) - { - server = new ServerSocket(port, 512, - InetAddress.getByName(bindaddr)); - } - else - { - server = new ServerSocket(port, 512); - } - } - catch (IOException e) - { - throw e; - } - /* Initialize internal Httpd */ + try { + String bindaddr = ProxyConfig.bindAddress; + if (bindaddr != null && bindaddr.length() > 0) { + server = new ServerSocket(port, 512, + InetAddress.getByName(bindaddr)); + } else { + server = new ServerSocket(port, 512); + } + } catch (IOException e) { + throw e; + } + + /* Initialize internal Httpd */ } - synchronized void suspend() - { - running = false; + synchronized void suspend() { + running = false; } - synchronized void resume() - { - running = true; + synchronized void resume() { + running = true; } + public void run() { + Thread.currentThread().setName(ProxyConfig.appName + " Server"); + running = true; + for (;;) { + Socket socket; - public void run() - { - Thread.currentThread().setName(ProxyConfig.appName+" Server"); - running = true; - for (;;) - { - Socket socket; + try { + socket = server.accept(); + } catch (IOException e) { + if (stopping) { + break; + } + continue; + } - try - { - socket = server.accept(); - } - catch (IOException e) - { - if(stopping){ - break; - } - continue; - } + if (stopping) { + break; + } - if(stopping){ - break; - } - - if (running) - { - Handler h = new Handler(socket,replacements,catchedContentTypes,catchedListener,replacedListener); - ReusableThread rt = getThread(); - rt.setRunnable(h); - } - else - { - error(socket, 503, ProxyConfig.appName+" proxy service is suspended."); - } - } + if (running) { + Handler h = new Handler(socket, replacements, catchedContentTypes, catchedListener, replacedListener); + ReusableThread rt = getThread(); + rt.setRunnable(h); + } else { + error(socket, 503, ProxyConfig.appName + " proxy service is suspended."); + } + } } - void error(Socket socket, int code, String message) - { - try - { - DataOutputStream out = new DataOutputStream(socket.getOutputStream()); - out.writeBytes((new HttpError(code, message)).toString()); - out.close(); - socket.close(); - } - catch (IOException e) - { - e.printStackTrace(); - } + void error(Socket socket, int code, String message) { + try { + DataOutputStream out = new DataOutputStream(socket.getOutputStream()); + out.writeBytes((new HttpError(code, message)).toString()); + out.close(); + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java b/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java index 1568f7000..86409ddcb 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/ThreadPool.java @@ -2,52 +2,44 @@ package com.jpexs.proxy; import java.util.*; -public class ThreadPool implements Cleanable -{ +public class ThreadPool implements Cleanable { + private String name; private Vector pool = new Vector(); - - public ThreadPool(String name) - { - this.name = name; + + public ThreadPool(String name) { + this.name = name; } - public synchronized ReusableThread get() - { - ReusableThread rt = null; + public synchronized ReusableThread get() { + ReusableThread rt = null; - if (pool.size() > 0) - { - rt = (ReusableThread)pool.firstElement(); - pool.removeElement(rt); - } + if (pool.size() > 0) { + rt = (ReusableThread) pool.firstElement(); + pool.removeElement(rt); + } - if (rt == null) - { - rt = new ReusableThread(this); - rt.start(); - } + if (rt == null) { + rt = new ReusableThread(this); + rt.start(); + } - return rt; + return rt; } - public synchronized void put(ReusableThread rt) - { - pool.addElement(rt); + public synchronized void put(ReusableThread rt) { + pool.addElement(rt); } - public synchronized void clean() - { - long now = System.currentTimeMillis(); - - for (Enumeration e = pool.elements(); e.hasMoreElements(); ) - { - ReusableThread rt = (ReusableThread) e.nextElement(); - if (now - rt.getLastRunTime() >= 30000) - { - rt.terminate(); - pool.removeElement(rt); - } - } + public synchronized void clean() { + long now = System.currentTimeMillis(); + + for (Enumeration e = pool.elements(); e.hasMoreElements();) { + ReusableThread rt = (ReusableThread) e.nextElement(); + if (now - rt.getLastRunTime() >= 30000) { + rt.terminate(); + pool.removeElement(rt); + } + } } } diff --git a/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java b/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java index 991dfb885..ddc2d4075 100644 --- a/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java +++ b/libsrc/jpproxy/src/com/jpexs/proxy/WorkerThread.java @@ -2,132 +2,133 @@ package com.jpexs.proxy; import java.util.*; - public class WorkerThread extends Thread { -private Runnable task; -private String name; + private Runnable task; + private String name; -private static int nactive = 0; -private static Vector list = new Vector(); -private static int max = 10; -private static long lifetime = 900 * 1000; /* 15 minute default */ + private static int nactive = 0; + private static Vector list = new Vector(); + private static int max = 10; + private static long lifetime = 900 * 1000; /* 15 minute default */ -private -WorkerThread() { - setDaemon(true); -} -/** - * Sets the lifetime of an idle WorkerThread (in ms). A WorkerThread - * will remain on the idle list for this much time before exiting. This - * does not affect WorkerThreads currently idling. - */ -synchronized static void -setLifetime(long time) { - lifetime = time; -} + private + WorkerThread() { + setDaemon(true); + } -/** - * Sets the maximum number of WorkerThreads that can exist at any given - * time. If this value is decreased below the current number of - * WorkerThreads, this will not take effect immediately. - */ -synchronized static void -setMaxThreads(int maxThreads) { - max = maxThreads; -} + /** + * Sets the lifetime of an idle WorkerThread (in ms). A WorkerThread will + * remain on the idle list for this much time before exiting. This does not + * affect WorkerThreads currently idling. + */ + synchronized static void + setLifetime(long time) { + lifetime = time; + } -/** - * Obtains a WorkerThread to which a task can be assigned. If an idle - * WorkerThread is present, it is removed from the idle list and returned. - * If not, and the maximum number of WorkerThreads has not been reached, - * a new WorkerThread is created. If the maximum number has been reached, - * this blocks until a WorkerThread is free. - */ -static WorkerThread -getThread() { - WorkerThread t; - synchronized (list) { - if (list.size() > 0) { - t = (WorkerThread) list.firstElement(); - list.removeElement(t); - } - else if (nactive >= max) { - while (true) { - try { - list.wait(); - } - catch (InterruptedException e) { - } - if (list.size() == 0) - continue; - t = (WorkerThread) list.firstElement(); - list.removeElement(t); - break; - } - } - else - t = new WorkerThread(); - nactive++; - } - return t; -} + /** + * Sets the maximum number of WorkerThreads that can exist at any given + * time. If this value is decreased below the current number of + * WorkerThreads, this will not take effect immediately. + */ + synchronized static void + setMaxThreads(int maxThreads) { + max = maxThreads; + } -/** - * Assigns a task to a WorkerThread - * @param task The task to be run - * @param name The name of the task - */ -public static void -assignThread(Runnable task, String name) { - while (true) { - try { - WorkerThread t = getThread(); - synchronized (t) { - t.task = task; - t.name = name; - if (!t.isAlive()) - t.start(); - else - t.notify(); - } - return; - } - catch (IllegalThreadStateException e) { - } - } -} + /** + * Obtains a WorkerThread to which a task can be assigned. If an idle + * WorkerThread is present, it is removed from the idle list and returned. + * If not, and the maximum number of WorkerThreads has not been reached, a + * new WorkerThread is created. If the maximum number has been reached, this + * blocks until a WorkerThread is free. + */ + static WorkerThread + getThread() { + WorkerThread t; + synchronized (list) { + if (list.size() > 0) { + t = (WorkerThread) list.firstElement(); + list.removeElement(t); + } else if (nactive >= max) { + while (true) { + try { + list.wait(); + } catch (InterruptedException e) { + } + if (list.size() == 0) { + continue; + } + t = (WorkerThread) list.firstElement(); + list.removeElement(t); + break; + } + } else { + t = new WorkerThread(); + } + nactive++; + } + return t; + } -/** Performs the task */ -synchronized public void -run() { - while (true) { - setName(name); - try { - task.run(); - } - catch (Throwable t) { - System.err.println(t); - } - setName("idle thread"); - synchronized (list) { - list.addElement(this); - if (nactive >= max) - list.notify(); - nactive--; - } - task = null; - try { - wait(lifetime); - } - catch (InterruptedException e) { - } - if (task == null) { - list.removeElement(this); - return; - } - } -} + /** + * Assigns a task to a WorkerThread + * + * @param task The task to be run + * @param name The name of the task + */ + public static void + assignThread(Runnable task, String name) { + while (true) { + try { + WorkerThread t = getThread(); + synchronized (t) { + t.task = task; + t.name = name; + if (!t.isAlive()) { + t.start(); + } else { + t.notify(); + } + } + return; + } catch (IllegalThreadStateException e) { + } + } + } + + /** + * Performs the task + */ + synchronized public void + run() { + while (true) { + setName(name); + try { + task.run(); + } catch (Throwable t) { + System.err.println(t); + } + setName("idle thread"); + synchronized (list) { + list.addElement(this); + if (nactive >= max) { + list.notify(); + } + nactive--; + } + task = null; + try { + wait(lifetime); + } catch (InterruptedException e) { + } + if (task == null) { + list.removeElement(this); + return; + } + } + } } diff --git a/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java b/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java index a3439c8a0..d11184d41 100644 --- a/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java +++ b/src/com/jpexs/decompiler/flash/gui/proxy/ProxyFrame.java @@ -21,6 +21,8 @@ import com.jpexs.decompiler.flash.gui.AppFrame; import com.jpexs.decompiler.flash.gui.Main; import com.jpexs.decompiler.flash.gui.MainFrame; import com.jpexs.decompiler.flash.gui.View; +import com.jpexs.decompiler.flash.helpers.SWFDecompilerPlugin; +import com.jpexs.helpers.Helper; import com.jpexs.proxy.CatchedListener; import com.jpexs.proxy.ReplacedListener; import com.jpexs.proxy.Replacement; @@ -347,50 +349,55 @@ public class ProxyFrame extends AppFrame implements ActionListener, CatchedListe * @param contentType Content type * @param url URL of the method * @param data Data stream + * @return replacement data */ @Override - public void catched(String contentType, String url, InputStream data) { + public byte[] catched(String contentType, String url, InputStream data) { boolean swfOnly = false; if (contentType.contains(";")) { contentType = contentType.substring(0, contentType.indexOf(';')); } if ((!sniffSWFCheckBox.isSelected()) && (contentType.equals("application/x-shockwave-flash"))) { - return; + return null; } if ((!sniffJSCheckBox.isSelected()) && (contentType.equals("application/javascript") || contentType.equals("application/x-javascript") || contentType.equals("text/javascript") || contentType.equals("application/json"))) { - return; + return null; } if ((!sniffXMLCheckBox.isSelected()) && (contentType.equals("application/xml") || contentType.equals("text/xml"))) { - return; + return null; } if ((!sniffOSCheckBox.isSelected()) && (contentType.equals("application/octet-stream"))) { - return; + return null; } + + byte[] result = null; + if (!listModel.contains(url)) { try { byte[] hdr = new byte[3]; data.read(hdr); String shdr = new String(hdr); - if ((swfOnly) && ((!shdr.equals("FWS")) && (!shdr.equals("CWS")))) { - return; //NOT SWF + if (swfOnly && ((!shdr.equals("FWS")) && (!shdr.equals("CWS")) && (!shdr.equals("ZWS")))) { + return null; //NOT SWF } - File f = new File(Main.tempFile(url)); - try (FileOutputStream fos = new FileOutputStream(f)) { - fos.write(hdr); - byte[] buf = new byte[2048]; - int count; - while ((count = data.read(buf)) > 0) { - fos.write(buf, 0, count); - } + String tempFilePath = Main.tempFile(url); + data.reset(); + byte[] dataArray = Helper.readStream(data); + try (FileOutputStream fos = new FileOutputStream(new File(tempFilePath))) { + fos.write(dataArray); } - Replacement r = new Replacement(url, Main.tempFile(url)); + + result = SWFDecompilerPlugin.fireProxyFileCatched(dataArray); + + Replacement r = new Replacement(url, tempFilePath); r.lastAccess = Calendar.getInstance(); listModel.addURL(r); } catch (IOException e) { } - } + + return result; } /**