From 3be98f194d73bc8dfc7741b4f3a7b3fa6a2b7b98 Mon Sep 17 00:00:00 2001 From: orbiter Date: Sat, 18 Jun 2005 01:48:11 +0000 Subject: [PATCH] tried to find the socket bug git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@300 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/http/httpc.java | 1298 +++++++++---------- source/de/anomic/kelondro/kelondroTree.java | 10 +- yacy.init | 6 +- 3 files changed, 651 insertions(+), 663 deletions(-) diff --git a/source/de/anomic/http/httpc.java b/source/de/anomic/http/httpc.java index 76a2425c7..ddc50544a 100644 --- a/source/de/anomic/http/httpc.java +++ b/source/de/anomic/http/httpc.java @@ -1,4 +1,4 @@ -// httpc.java +// httpc.java // ------------------------------------- // (C) by Michael Peter Christen; mc@anomic.de // first published on http://www.anomic.de @@ -45,7 +45,7 @@ necessary to implement the network interface since otherwise there is no access to the HTTP/1.0 / HTTP/1.1 header information that comes along each connection. -*/ + */ package de.anomic.http; @@ -87,7 +87,7 @@ import de.anomic.server.serverCore.Session; public final class httpc { - + // statics private static final String vDATE = "20040602"; private static String userAgent; @@ -99,9 +99,19 @@ public final class httpc { private static final SimpleDateFormat HTTPGMTFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); private static final SimpleDateFormat EMLFormatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US); private static final SimpleDateFormat ShortFormatter = new SimpleDateFormat("yyyyMMddHHmmss"); - //Mo 06 Sep 2004 23:32 + //Mo 06 Sep 2004 23:32 private static final HashMap reverseMappingCache = new HashMap(); - + + // the dns cache + private static final HashMap nameCacheHit = new HashMap(); + //private static HashSet nameCacheMiss = new HashSet(); + + /** + * A Object Pool containing all pooled httpc-objects. + * @see httpcPool + */ + private static final httpcPool theHttpcPool; + // class variables private Socket socket = null; // client socket for commands private String host = null; @@ -109,31 +119,19 @@ public final class httpc { private long handle; // output and input streams for client control connection - PushbackInputStream clientInput = null; - OutputStream clientOutput = null; - + private PushbackInputStream clientInput = null; + private OutputStream clientOutput = null; + private boolean remoteProxyUse = false; private String savedRemoteHost = null; private String requestPath = null; - - // the dns cache - private static final HashMap nameCacheHit = new HashMap(); - //private static HashSet nameCacheMiss = new HashSet(); static { - // set time-out of InetAddress.getByName cache ttl - java.security.Security.setProperty("networkaddress.cache.ttl" , "60"); + // set time-out of InetAddress.getByName cache ttl + java.security.Security.setProperty("networkaddress.cache.ttl" , "60"); java.security.Security.setProperty("networkaddress.cache.negative.ttl" , "0"); } - - - /** - * A Object Pool containing all pooled httpc-objects. - * @see httpcPool - */ - private static final httpcPool theHttpcPool; - /** * Indicates if the current object was removed from pool because the maximum limit * was exceeded. @@ -150,16 +148,16 @@ public final class httpc { config.maxActive = 150; // The maximum number of idle connections connections in the pool - // 0 = no limit. + // 0 = no limit. config.maxIdle = 75; - config.minIdle = 10; + config.minIdle = 10; - config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; - config.minEvictableIdleTimeMillis = 30000; + config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; + config.minEvictableIdleTimeMillis = 30000; - theHttpcPool = new httpcPool(new httpcFactory(),config); + theHttpcPool = new httpcPool(new httpcFactory(),config); } - + /** * A reusable readline buffer * @see serverByteBuffer @@ -167,29 +165,29 @@ public final class httpc { final serverByteBuffer readLineBuffer = new serverByteBuffer(100); public String toString() { - return (this.savedRemoteHost == null) ? "Disconnected" : "Connected to " + this.savedRemoteHost + - ((this.remoteProxyUse) ? " via " + this.host : ""); + return (this.savedRemoteHost == null) ? "Disconnected" : "Connected to " + this.savedRemoteHost + + ((this.remoteProxyUse) ? " via " + this.host : ""); } - + public static httpc getInstance( - String server, - int port, - int timeout, + String server, + int port, + int timeout, boolean ssl, - String remoteProxyHost, + String remoteProxyHost, int remoteProxyPort - ) throws IOException { + ) throws IOException { httpc newHttpc; try { // fetching a new httpc from the object pool - newHttpc = (httpc) httpc.theHttpcPool.borrowObject(); + newHttpc = (httpc) httpc.theHttpcPool.borrowObject(); } catch (Exception e) { throw new IOException("Unable to initialize a new httpc. " + e.getMessage()); - } - + } + // initialize it - newHttpc.init(server,port,timeout,ssl,remoteProxyHost, remoteProxyPort); + newHttpc.init(server,port,timeout,ssl,remoteProxyHost, remoteProxyPort); return newHttpc; } @@ -202,35 +200,27 @@ public final class httpc { } catch (Exception e) { throw new IOException("Unable to fetch a new httpc from pool. " + e.getMessage()); - } + } // initialize it try { - newHttpc.init(server,port,timeout,ssl); + newHttpc.init(server,port,timeout,ssl); } catch (IOException e) { try{ httpc.theHttpcPool.returnObject(newHttpc); } catch (Exception e1) {} throw e; } return newHttpc; - - - } + + + } public static void returnInstance(httpc theHttpc) { try { theHttpc.reset(); httpc.theHttpcPool.returnObject(theHttpc); } catch (Exception e) { - // we could ignore this error - } - } - - protected void finalize() throws Throwable { - if (!this.removedFromPool) { - System.err.println("Httpc object was not returned to object pool."); - httpc.theHttpcPool.invalidateObject(this); + // we could ignore this error } - this.reset(); } public static String dnsResolve(String host) { @@ -251,7 +241,7 @@ public final class httpc { } return null; } - + public static boolean dnsFetch(String host) { // looks for the ip of host and returns false if the host was in the cache // if it is not in the cache the ip is fetched and this resturns true @@ -269,55 +259,45 @@ public final class httpc { return false; } } + + // provide HTTP date handling static methods + public static String dateString(Date date) { + if (date == null) return ""; else return HTTPGMTFormatter.format(date); + } - void reset() { - try { - if (this.clientInput != null) { - this.clientInput.close(); - this.clientInput = null; - } - if (this.clientOutput != null) { - this.clientOutput.close(); - this.clientOutput = null; - } - if (this.socket != null) { - this.socket.close(); - this.socket = null; - } - - this.host = null; - this.timeout = 0; - this.handle = 0; - - this.remoteProxyUse = false; - this.savedRemoteHost = null; - this.requestPath = null; - - - // shrink readlinebuffer if it is to large - this.readLineBuffer.reset(80); - } catch (Exception e) { - // we could ignore this ... - } + public static Date nowDate() { + return new GregorianCalendar(GMTTimeZone).getTime(); } - + static { + // provide system information for client identification + String loc = System.getProperty("user.timezone", "nowhere"); + int p = loc.indexOf("/"); + if (p > 0) loc = loc.substring(0,p); + loc = loc + "/" + System.getProperty("user.language", "dumb"); + systemOST = + System.getProperty("os.arch", "no-os-arch") + " " + System.getProperty("os.name", "no-os-arch") + " " + + System.getProperty("os.version", "no-os-version") + "; " + + "java " + System.getProperty("java.version", "no-java-version") + "; " + loc; + userAgent = "yacy (www.yacy.net; v" + vDATE + "; " + systemOST + ")"; + } + // http client void init(String server, int port, int timeout, boolean ssl, - String remoteProxyHost, int remoteProxyPort) throws IOException { - this.init(remoteProxyHost, remoteProxyPort, timeout, ssl); - this.remoteProxyUse = true; - this.savedRemoteHost = server + ((port == 80) ? "" : (":" + port)); + String remoteProxyHost, int remoteProxyPort) throws IOException { + this.init(remoteProxyHost, remoteProxyPort, timeout, ssl); + this.remoteProxyUse = true; + this.savedRemoteHost = server + ((port == 80) ? "" : (":" + port)); } - + void init(String server, int port, int timeout, boolean ssl) throws IOException { handle = System.currentTimeMillis(); //serverLog.logDebug("HTTPC", handle + " initialized"); this.remoteProxyUse = false; this.timeout = timeout; this.savedRemoteHost = server; - + try { this.host = server + ((port == 80) ? "" : (":" + port)); String hostip; @@ -341,225 +321,233 @@ public final class httpc { throw new IOException("unknown host: " + server); } } - - // provide HTTP date handling static methods - public static String dateString(Date date) { - if (date == null) return ""; else return HTTPGMTFormatter.format(date); + + void reset() { + if (this.clientInput != null) { + try {this.clientInput.close();} catch (Exception e) {} + this.clientInput = null; + } + if (this.clientOutput != null) { + try {this.clientOutput.close();} catch (Exception e) {} + this.clientOutput = null; + } + if (this.socket != null) { + try {this.socket.close();} catch (Exception e) {} + this.socket = null; + } + + this.host = null; + this.timeout = 0; + this.handle = 0; + + this.remoteProxyUse = false; + this.savedRemoteHost = null; + this.requestPath = null; + + + // shrink readlinebuffer if it is to large + this.readLineBuffer.reset(80); } - public static Date nowDate() { - return new GregorianCalendar(GMTTimeZone).getTime(); + public void close() { + reset(); } - static { - // provide system information for client identification - String loc = System.getProperty("user.timezone", "nowhere"); - int p = loc.indexOf("/"); - if (p > 0) loc = loc.substring(0,p); - loc = loc + "/" + System.getProperty("user.language", "dumb"); - systemOST = - System.getProperty("os.arch", "no-os-arch") + " " + System.getProperty("os.name", "no-os-arch") + " " + - System.getProperty("os.version", "no-os-version") + "; " + - "java " + System.getProperty("java.version", "no-java-version") + "; " + loc; - userAgent = "yacy (www.yacy.net; v" + vDATE + "; " + systemOST + ")"; + protected void finalize() throws Throwable { + if (!(this.removedFromPool)) { + System.err.println("Httpc object was not returned to object pool."); + httpc.theHttpcPool.invalidateObject(this); + } + this.reset(); } - - public final class response { - // Response-Header = Date | Pragma | Allow | Content-Encoding | Content-Length | Content-Type | - // Expires | Last-Modified | HTTP-header - /* - Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF - 1xx: Informational - Not used, but reserved for future use - 2xx: Success - The action was successfully received, understood, and accepted. - 3xx: Redirection - Further action must be taken in order to complete the request - 4xx: Client Error - The request contains bad syntax or cannot be fulfilled - 5xx: Server Error - The server failed to fulfill an apparently valid request - */ - - // header information - public httpHeader responseHeader = null; - public String httpVer = "HTTP/0.9"; - public String status; // the success/failure response string starting with status-code - private boolean gzip; // for gunzipping on-the-fly - private String encoding; - public response(boolean zipped) throws IOException { - - // lets start with worst-case attributes as set-up - responseHeader = new httpHeader(reverseMappingCache); - status = "503 internal error"; - gzip = false; - - // check connection status - if (clientInput == null) { - // the server has meanwhile disconnected - status = "503 lost connection to server"; - return; // in bad mood - } - - // reads in the http header, right now, right here - byte[] b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false); - if (b == null) { - // the server has meanwhile disconnected - status = "503 server has closed connection"; - return; // in bad mood - } - String buffer = new String(b); // this is the status response line + public final class response { + // Response-Header = Date | Pragma | Allow | Content-Encoding | Content-Length | Content-Type | + // Expires | Last-Modified | HTTP-header + /* + Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF + 1xx: Informational - Not used, but reserved for future use + 2xx: Success - The action was successfully received, understood, and accepted. + 3xx: Redirection - Further action must be taken in order to complete the request + 4xx: Client Error - The request contains bad syntax or cannot be fulfilled + 5xx: Server Error - The server failed to fulfill an apparently valid request + */ + + // header information + public httpHeader responseHeader = null; + public String httpVer = "HTTP/0.9"; + public String status; // the success/failure response string starting with status-code + private boolean gzip; // for gunzipping on-the-fly + private String encoding; + + public response(boolean zipped) throws IOException { + + // lets start with worst-case attributes as set-up + responseHeader = new httpHeader(reverseMappingCache); + status = "503 internal error"; + gzip = false; + + // check connection status + if (clientInput == null) { + // the server has meanwhile disconnected + status = "503 lost connection to server"; + return; // in bad mood + } + + // reads in the http header, right now, right here + byte[] b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false); + if (b == null) { + // the server has meanwhile disconnected + status = "503 server has closed connection"; + return; // in bad mood + } + String buffer = new String(b); // this is the status response line //System.out.println("#S#" + buffer); int p = buffer.indexOf(" "); - if (p < 0) { - status = "500 status line parse error"; - // flush in anything that comes without parsing - while ((b != null) && (b.length != 0)) b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false); - return; // in bad mood - } - // the http version reported by the server - this.httpVer = buffer.substring(0,p); + if (p < 0) { + status = "500 status line parse error"; + // flush in anything that comes without parsing + while ((b != null) && (b.length != 0)) b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false); + return; // in bad mood + } + // the http version reported by the server + this.httpVer = buffer.substring(0,p); + + // we have a status + status = buffer.substring(p + 1).trim(); // the status code plus reason-phrase + + // check validity + if (status.startsWith("400")) { + // bad request + // flush in anything that comes without parsing + while ((b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false)).length != 0) {} + return; // in bad mood + } + + // at this point we should have a valid response. read in the header properties + String key = ""; + while ((b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false)) != null) { + if (b.length == 0) break; + buffer = new String(b); + //System.out.println("#H#" + buffer); // debug + if (buffer.charAt(0) <= 32) { + // use old entry + if (key.length() == 0) throw new IOException("header corrupted - input error"); + // attach new line + if (!(responseHeader.containsKey(key))) throw new IOException("header corrupted - internal error"); + responseHeader.put(key, (String) responseHeader.get(key) + " " + buffer.trim()); + } else { + // create new entry + p = buffer.indexOf(":"); + if (p > 0) { + responseHeader.add(buffer.substring(0, p).trim(), buffer.substring(p + 1).trim()); + } else { + serverLog.logError("HTTPC", "RESPONSE PARSE ERROR: HOST='" + host + "', PATH='" + requestPath + "', STATUS='" + status + "'"); + serverLog.logError("HTTPC", "..............BUFFER: " + buffer); + } + } + } + // finished with reading header + + // we will now manipulate the header if the content is gzip encoded, because + // reading the content with "writeContent" will gunzip on-the-fly + gzip = ((zipped) && (responseHeader.gzip())); + + if (gzip) { + responseHeader.remove("CONTENT-ENCODING"); // we fake that we don't have encoding, since what comes out does not have gzip and we also don't know what was encoded + responseHeader.remove("CONTENT-LENGTH"); // we cannot use the length during gunzippig yet; still we can hope that it works + } + } - // we have a status - status = buffer.substring(p + 1).trim(); // the status code plus reason-phrase - - // check validity - if (status.startsWith("400")) { - // bad request - // flush in anything that comes without parsing - while ((b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false)).length != 0) {} - return; // in bad mood - } - - // at this point we should have a valid response. read in the header properties - String key = ""; - while ((b = serverCore.receive(clientInput, readLineBuffer, terminalMaxLength, false)) != null) { - if (b.length == 0) break; - buffer = new String(b); - //System.out.println("#H#" + buffer); // debug - if (buffer.charAt(0) <= 32) { - // use old entry - if (key.length() == 0) throw new IOException("header corrupted - input error"); - // attach new line - if (!(responseHeader.containsKey(key))) throw new IOException("header corrupted - internal error"); - responseHeader.put(key, (String) responseHeader.get(key) + " " + buffer.trim()); - } else { - // create new entry - p = buffer.indexOf(":"); - if (p > 0) { - responseHeader.add(buffer.substring(0, p).trim(), buffer.substring(p + 1).trim()); - } else { - serverLog.logError("HTTPC", "RESPONSE PARSE ERROR: HOST='" + host + "', PATH='" + requestPath + "', STATUS='" + status + "'"); - serverLog.logError("HTTPC", "..............BUFFER: " + buffer); - } - } - } - // finished with reading header - - // we will now manipulate the header if the content is gzip encoded, because - // reading the content with "writeContent" will gunzip on-the-fly - gzip = ((zipped) && (responseHeader.gzip())); - - if (gzip) { - responseHeader.remove("CONTENT-ENCODING"); // we fake that we don't have encoding, since what comes out does not have gzip and we also don't know what was encoded - responseHeader.remove("CONTENT-LENGTH"); // we cannot use the length during gunzippig yet; still we can hope that it works - } - } - - public String toString() { - StringBuffer toStringBuffer = new StringBuffer(); - toStringBuffer.append((this.status == null) ? "Status: Unknown" : "Status: " + this.status) - .append(" | Headers: ") - .append((this.responseHeader == null) ? "none" : this.responseHeader.toString()); - return toStringBuffer.toString(); - } - - public boolean success() { - return ((status.charAt(0) == '2') || (status.charAt(0) == '3')); - } - - public byte[] writeContent(OutputStream procOS) throws IOException { - int contentLength = (int) this.responseHeader.contentLength(); - serverByteBuffer sbb = new serverByteBuffer((contentLength==-1)?8192:contentLength); - writeContentX(procOS, sbb, httpc.this.clientInput); - return sbb.getBytes(); - } - - public void writeContent(OutputStream procOS, File file) throws IOException { - // this writes the input stream to either another output stream or - // a file or both. - FileOutputStream bufferOS = null; - try { - if (file != null) bufferOS = new FileOutputStream(file); - writeContentX(procOS, bufferOS, httpc.this.clientInput); - } finally { - if (bufferOS != null) { - bufferOS.close(); - if (file.length() == 0) file.delete(); - } + public String toString() { + StringBuffer toStringBuffer = new StringBuffer(); + toStringBuffer.append((this.status == null) ? "Status: Unknown" : "Status: " + this.status) + .append(" | Headers: ") + .append((this.responseHeader == null) ? "none" : this.responseHeader.toString()); + return toStringBuffer.toString(); } - } - - public void writeContentX(OutputStream procOS, OutputStream bufferOS, InputStream clientInput) throws IOException { - // we write length bytes, but if length == -1 (or < 0) then we - // write until the input stream closes - // procOS == null -> no write to procOS - // file == null -> no write to file - // If the Content-Encoding is gzip, we gunzip on-the-fly - // and change the Content-Encoding and Content-Length attributes in the header - byte[] buffer = new byte[2048]; - int l; - long len = 0; - // find out length - long length = this.responseHeader.contentLength(); + public boolean success() { + return ((status.charAt(0) == '2') || (status.charAt(0) == '3')); + } - // using the proper intput stream - InputStream dis = (this.gzip) ? (InputStream) new GZIPInputStream(clientInput) : (InputStream) clientInput; + public byte[] writeContent(OutputStream procOS) throws IOException { + int contentLength = (int) this.responseHeader.contentLength(); + serverByteBuffer sbb = new serverByteBuffer((contentLength==-1)?8192:contentLength); + writeContentX(procOS, sbb, httpc.this.clientInput); + return sbb.getBytes(); + } - // we have three methods of reading: length-based, length-based gzip and connection-close-based - try { - if (length > 0) { - // we read exactly 'length' bytes - while ((len < length) && ((l = dis.read(buffer)) >= 0)) { - if (procOS != null) procOS.write(buffer, 0, l); - if (bufferOS != null) bufferOS.write(buffer, 0, l); - len += l; - } - } else { - // no content-length was given, thus we read until the connection closes - while ((l = dis.read(buffer, 0, buffer.length)) >= 0) { - if (procOS != null) procOS.write(buffer, 0, l); - if (bufferOS != null) bufferOS.write(buffer, 0, l); + public void writeContent(OutputStream procOS, File file) throws IOException { + // this writes the input stream to either another output stream or + // a file or both. + FileOutputStream bufferOS = null; + try { + if (file != null) bufferOS = new FileOutputStream(file); + writeContentX(procOS, bufferOS, httpc.this.clientInput); + } finally { + if (bufferOS != null) { + bufferOS.close(); + if (file.length() == 0) file.delete(); } } - } catch (java.net.SocketException e) { - throw new IOException("Socket exception: " + e.getMessage()); - } catch (java.net.SocketTimeoutException e) { - throw new IOException("Socket time-out: " + e.getMessage()); - } finally { - // close the streams - if (procOS != null) { - if (procOS instanceof httpChunkedOutputStream) - ((httpChunkedOutputStream)procOS).finish(); - procOS.flush(); + } + + public void writeContentX(OutputStream procOS, OutputStream bufferOS, InputStream clientInput) throws IOException { + // we write length bytes, but if length == -1 (or < 0) then we + // write until the input stream closes + // procOS == null -> no write to procOS + // file == null -> no write to file + // If the Content-Encoding is gzip, we gunzip on-the-fly + // and change the Content-Encoding and Content-Length attributes in the header + byte[] buffer = new byte[2048]; + int l; + long len = 0; + + // find out length + long length = this.responseHeader.contentLength(); + + // using the proper intput stream + InputStream dis = (this.gzip) ? (InputStream) new GZIPInputStream(clientInput) : (InputStream) clientInput; + + // we have three methods of reading: length-based, length-based gzip and connection-close-based + try { + if (length > 0) { + // we read exactly 'length' bytes + while ((len < length) && ((l = dis.read(buffer)) >= 0)) { + if (procOS != null) procOS.write(buffer, 0, l); + if (bufferOS != null) bufferOS.write(buffer, 0, l); + len += l; + } + } else { + // no content-length was given, thus we read until the connection closes + while ((l = dis.read(buffer, 0, buffer.length)) >= 0) { + if (procOS != null) procOS.write(buffer, 0, l); + if (bufferOS != null) bufferOS.write(buffer, 0, l); + } + } + } catch (java.net.SocketException e) { + throw new IOException("Socket exception: " + e.getMessage()); + } catch (java.net.SocketTimeoutException e) { + throw new IOException("Socket time-out: " + e.getMessage()); + } finally { + // close the streams + if (procOS != null) { + if (procOS instanceof httpChunkedOutputStream) + ((httpChunkedOutputStream)procOS).finish(); + procOS.flush(); + } + if (bufferOS != null) bufferOS.flush(); + buffer = null; } - if (bufferOS != null) bufferOS.flush(); - buffer = null; } + + public void print() { + serverLog.logInfo("HTTPC", "RESPONSE: status=" + status + ", header=" + responseHeader.toString()); + } + } - - public void print() { - serverLog.logInfo("HTTPC", "RESPONSE: status=" + status + ", header=" + responseHeader.toString()); - } - - } - - public void close() { - // closes the connection - try { - this.clientInput.close(); - this.clientOutput.close(); - this.socket.close(); - } catch (IOException e) {} - } - + // method is either GET, HEAD or POST private void send(String method, String path, httpHeader header, boolean zipped) throws IOException { // scheduled request through request-response objects/threads @@ -605,8 +593,8 @@ public final class httpc { if ((!(header.containsKey(httpHeader.REFERER))) || (((String) header.get(httpHeader.REFERER)).trim().length() == 0)) { header.put(httpHeader.REFERER, (((System.currentTimeMillis() >> 10) & 1) == 0) ? - "http://www.anomic.de" : - "http://www.yacy.net/yacy"); + "http://www.anomic.de" : + "http://www.yacy.net/yacy"); } // stimulate zipping or not @@ -662,7 +650,7 @@ public final class httpc { // this is the place where www.stern.de refuses to answer ..??? } - + public response GET(String path, httpHeader requestHeader) throws IOException { //serverLog.logDebug("HTTPC", handle + " requested GET '" + path + "', time = " + (System.currentTimeMillis() - handle)); try { @@ -675,165 +663,165 @@ public final class httpc { throw new IOException(e.getMessage()); } } - + public response HEAD(String path, httpHeader requestHeader) throws IOException { - try { - send(httpHeader.METHOD_HEAD, path, requestHeader, false); - return new response(false); - // in this case the caller should not read the response body, - // since there is none... - } catch (SocketException e) { - throw new IOException(e.getMessage()); - } + try { + send(httpHeader.METHOD_HEAD, path, requestHeader, false); + return new response(false); + // in this case the caller should not read the response body, + // since there is none... + } catch (SocketException e) { + throw new IOException(e.getMessage()); + } } - + public response POST(String path, httpHeader requestHeader, InputStream ins) throws IOException { - try { - send(httpHeader.METHOD_POST, path, requestHeader, false); - // if there is a body to the call, we would have a CONTENT-LENGTH tag in the requestHeader - String cl = (String) requestHeader.get(httpHeader.CONTENT_LENGTH); + try { + send(httpHeader.METHOD_POST, path, requestHeader, false); + // if there is a body to the call, we would have a CONTENT-LENGTH tag in the requestHeader + String cl = (String) requestHeader.get(httpHeader.CONTENT_LENGTH); int len, c; byte[] buffer = new byte[512]; - if (cl != null) { - len = Integer.parseInt(cl); - // transfer len bytes from ins to the server - while ((len > 0) && ((c = ins.read(buffer)) >= 0)) { + if (cl != null) { + len = Integer.parseInt(cl); + // transfer len bytes from ins to the server + while ((len > 0) && ((c = ins.read(buffer)) >= 0)) { clientOutput.write(buffer, 0, c); len -= c; } - } else { + } else { len = 0; - while ((c = ins.read(buffer)) >= 0) { + while ((c = ins.read(buffer)) >= 0) { clientOutput.write(buffer, 0, c); len += c; } requestHeader.put(httpHeader.CONTENT_LENGTH, "" + len); } clientOutput.flush(); - return new response(false); - } catch (SocketException e) { - throw new IOException(e.getMessage()); - } + return new response(false); + } catch (SocketException e) { + throw new IOException(e.getMessage()); + } } - + public response CONNECT(String host, int port, httpHeader requestHeader) throws IOException { - try { - send(httpHeader.METHOD_CONNECT, host + ":" + port, requestHeader, false); - return new response(false); - } catch (SocketException e) { - throw new IOException(e.getMessage()); - } + try { + send(httpHeader.METHOD_CONNECT, host + ":" + port, requestHeader, false); + return new response(false); + } catch (SocketException e) { + throw new IOException(e.getMessage()); + } } - - + + public response POST(String path, httpHeader requestHeader, serverObjects args, Hashtable files) throws IOException { - // make shure, the header has a boundary information like - // CONTENT-TYPE=multipart/form-data; boundary=----------0xKhTmLbOuNdArY - if (requestHeader == null) requestHeader = new httpHeader(); - String boundary = (String) requestHeader.get(httpHeader.CONTENT_TYPE); - if (boundary == null) { - // create a boundary - boundary = "multipart/form-data; boundary=----------" + java.lang.System.currentTimeMillis(); - requestHeader.put(httpHeader.CONTENT_TYPE, boundary); - } - // extract the boundary string - int pos = boundary.toUpperCase().indexOf("BOUNDARY="); - if (pos < 0) { - // again, create a boundary - boundary = "multipart/form-data; boundary=----------" + java.lang.System.currentTimeMillis(); - requestHeader.put(httpHeader.CONTENT_TYPE, boundary); - pos = boundary.indexOf("boundary="); - } - boundary = "--" + boundary.substring(pos + "boundary=".length()); - - ByteArrayOutputStream buf = new ByteArrayOutputStream(); - // in contrast to GET and HEAD, this method also transports a message body - // the body consists of repeated boundaries and values in between - if (args.size() != 0) { - // we have values for the POST, start with one boundary - String key, value; - Enumeration e = args.keys(); - while (e.hasMoreElements()) { - // start with a boundary - buf.write(boundary.getBytes()); - buf.write(serverCore.crlf); + // make shure, the header has a boundary information like + // CONTENT-TYPE=multipart/form-data; boundary=----------0xKhTmLbOuNdArY + if (requestHeader == null) requestHeader = new httpHeader(); + String boundary = (String) requestHeader.get(httpHeader.CONTENT_TYPE); + if (boundary == null) { + // create a boundary + boundary = "multipart/form-data; boundary=----------" + java.lang.System.currentTimeMillis(); + requestHeader.put(httpHeader.CONTENT_TYPE, boundary); + } + // extract the boundary string + int pos = boundary.toUpperCase().indexOf("BOUNDARY="); + if (pos < 0) { + // again, create a boundary + boundary = "multipart/form-data; boundary=----------" + java.lang.System.currentTimeMillis(); + requestHeader.put(httpHeader.CONTENT_TYPE, boundary); + pos = boundary.indexOf("boundary="); + } + boundary = "--" + boundary.substring(pos + "boundary=".length()); + + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + // in contrast to GET and HEAD, this method also transports a message body + // the body consists of repeated boundaries and values in between + if (args.size() != 0) { + // we have values for the POST, start with one boundary + String key, value; + Enumeration e = args.keys(); + while (e.hasMoreElements()) { + // start with a boundary + buf.write(boundary.getBytes()); + buf.write(serverCore.crlf); // write value - key = (String) e.nextElement(); - value = (String) args.get(key, ""); - if ((files != null) && (files.containsKey(key))) { - // we are about to write a file - buf.write(("Content-Disposition: form-data; name=" + '"' + key + '"' + "; filename=" + '"' + value + '"').getBytes()); + key = (String) e.nextElement(); + value = (String) args.get(key, ""); + if ((files != null) && (files.containsKey(key))) { + // we are about to write a file + buf.write(("Content-Disposition: form-data; name=" + '"' + key + '"' + "; filename=" + '"' + value + '"').getBytes()); + buf.write(serverCore.crlf); + buf.write(serverCore.crlf); + buf.write((byte[]) files.get(key)); + buf.write(serverCore.crlf); + } else { + // write a single value + buf.write(("Content-Disposition: form-data; name=" + '"' + key + '"').getBytes()); + buf.write(serverCore.crlf); buf.write(serverCore.crlf); - buf.write(serverCore.crlf); - buf.write((byte[]) files.get(key)); - buf.write(serverCore.crlf); - } else { - // write a single value - buf.write(("Content-Disposition: form-data; name=" + '"' + key + '"').getBytes()); - buf.write(serverCore.crlf); - buf.write(serverCore.crlf); buf.write(value.getBytes()); - buf.write(serverCore.crlf); + buf.write(serverCore.crlf); } - } - // finish with a boundary - buf.write(boundary.getBytes()); + } + // finish with a boundary + buf.write(boundary.getBytes()); buf.write(serverCore.crlf); - //buf.write("" + serverCore.crlfString); - } + //buf.write("" + serverCore.crlfString); + } // create body array buf.close(); byte[] body = buf.toByteArray(); //System.out.println("DEBUG: PUT BODY=" + new String(body)); // size of that body - requestHeader.put(httpHeader.CONTENT_LENGTH, "" + body.length); - // send the header - //System.out.println("header=" + requestHeader); - send(httpHeader.METHOD_POST, path, requestHeader, false); - // send the body - //System.out.println("body=" + buf.toString()); - serverCore.send(clientOutput, body); - - return new response(false); + requestHeader.put(httpHeader.CONTENT_LENGTH, "" + body.length); + // send the header + //System.out.println("header=" + requestHeader); + send(httpHeader.METHOD_POST, path, requestHeader, false); + // send the body + //System.out.println("body=" + buf.toString()); + serverCore.send(clientOutput, body); + + return new response(false); } - + /* DEBUG: PUT BODY=------------1090358578442 Content-Disposition: form-data; name="youare" - + Ty2F86ekSWM5 ------------1090358578442 Content-Disposition: form-data; name="key" - + 6EkPPOl7 ------------1090358578442 Content-Disposition: form-data; name="iam" - + HnTvzwV7SCJR ------------1090358578442 Content-Disposition: form-data; name="process" - + permission ------------1090358578442 - + */ /* ------------0xKhTmLbOuNdArY Content-Disposition: form-data; name="file1"; filename="dir.gif" Content-Type: image/gif - + GIF89 ------------0xKhTmLbOuNdArY Content-Disposition: form-data; name="file2"; filename="" - - + + ------------0xKhTmLbOuNdArY Content-Disposition: form-data; name="upload" - + do upload ------------0xKhTmLbOuNdArY-- - + ###### Listing Properties ###### # METHOD=POST ### Header Values: @@ -849,35 +837,35 @@ do upload # USER-AGENT=Mozilla/5.0 (Macintosh; U; PPC Mac OS X; de-de) AppleWebKit/103u (KHTML, like Gecko) Safari/100.1 ### Call Properties: ###### End OfList ###### - */ - + */ + public static byte[] singleGET(String host, int port, String path, int timeout, - String user, String password, boolean ssl, - String proxyHost, int proxyPort, - httpHeader requestHeader) throws IOException { - if (requestHeader == null) requestHeader = new httpHeader(); - if ((user != null) && (password != null) && (user.length() != 0)) { - requestHeader.put(httpHeader.AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password)); - } + String user, String password, boolean ssl, + String proxyHost, int proxyPort, + httpHeader requestHeader) throws IOException { + if (requestHeader == null) requestHeader = new httpHeader(); + if ((user != null) && (password != null) && (user.length() != 0)) { + requestHeader.put(httpHeader.AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password)); + } httpc con = null; - try { - + try { + if ((proxyHost == null) || (proxyPort == 0)) { con = httpc.getInstance(host, port, timeout, ssl); - } else { + } else { con = httpc.getInstance(host, port, timeout, ssl, proxyHost, proxyPort); } - - httpc.response res = con.GET(path, null); - if (res.status.startsWith("2")) { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - res.writeContent(bos, null); - con.close(); - return bos.toByteArray(); - } else { - return res.status.getBytes(); - } + + httpc.response res = con.GET(path, null); + if (res.status.startsWith("2")) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + res.writeContent(bos, null); + con.close(); + return bos.toByteArray(); + } else { + return res.status.getBytes(); + } } catch (Exception e) { throw new IOException(e.getMessage()); } finally { @@ -885,56 +873,56 @@ do upload } } - + public static byte[] singleGET(URL u, int timeout, - String user, String password, - String proxyHost, int proxyPort) throws IOException { - int port = u.getPort(); + String user, String password, + String proxyHost, int proxyPort) throws IOException { + int port = u.getPort(); boolean ssl = u.getProtocol().equals("https"); - if (port < 0) port = (ssl) ? 443: 80; - String path = u.getPath(); - String query = u.getQuery(); - if ((query != null) && (query.length() > 0)) path = path + "?" + query; - return singleGET(u.getHost(), port, path, timeout, user, password, ssl, proxyHost, proxyPort, null); + if (port < 0) port = (ssl) ? 443: 80; + String path = u.getPath(); + String query = u.getQuery(); + if ((query != null) && (query.length() > 0)) path = path + "?" + query; + return singleGET(u.getHost(), port, path, timeout, user, password, ssl, proxyHost, proxyPort, null); } - + /* public static byte[] singleGET(String url, int timeout) throws IOException { - try { - return singleGET(new URL(url), timeout, null, null, null, 0); - } catch (MalformedURLException e) { - throw new IOException("Malformed URL: " + e.getMessage()); - } + try { + return singleGET(new URL(url), timeout, null, null, null, 0); + } catch (MalformedURLException e) { + throw new IOException("Malformed URL: " + e.getMessage()); + } } - */ + */ public static byte[] singlePOST(String host, int port, String path, int timeout, - String user, String password, boolean ssl, - String proxyHost, int proxyPort, - httpHeader requestHeader, serverObjects props) throws IOException { + String user, String password, boolean ssl, + String proxyHost, int proxyPort, + httpHeader requestHeader, serverObjects props) throws IOException { - if (requestHeader == null) requestHeader = new httpHeader(); - if ((user != null) && (password != null) && (user.length() != 0)) { - requestHeader.put(httpHeader.AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password)); - } + if (requestHeader == null) requestHeader = new httpHeader(); + if ((user != null) && (password != null) && (user.length() != 0)) { + requestHeader.put(httpHeader.AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password)); + } - httpc con = null; + httpc con = null; try { - if ((proxyHost == null) || (proxyPort == 0)) - con = httpc.getInstance(host, port, timeout, ssl); - else - con = httpc.getInstance(host, port, timeout, ssl, proxyHost, proxyPort); - httpc.response res = con.POST(path, null, props, null); + if ((proxyHost == null) || (proxyPort == 0)) + con = httpc.getInstance(host, port, timeout, ssl); + else + con = httpc.getInstance(host, port, timeout, ssl, proxyHost, proxyPort); + httpc.response res = con.POST(path, null, props, null); - //System.out.println("response=" + res.toString()); - if (res.status.startsWith("2")) { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - res.writeContent(bos, null); - con.close(); - return bos.toByteArray(); - } else { - return res.status.getBytes(); - } + //System.out.println("response=" + res.toString()); + if (res.status.startsWith("2")) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + res.writeContent(bos, null); + con.close(); + return bos.toByteArray(); + } else { + return res.status.getBytes(); + } } catch (Exception e) { throw new IOException(e.getMessage()); } finally { @@ -942,128 +930,128 @@ do upload } } - + public static byte[] singlePOST(URL u, int timeout, - String user, String password, - String proxyHost, int proxyPort, - serverObjects props) throws IOException { - int port = u.getPort(); + String user, String password, + String proxyHost, int proxyPort, + serverObjects props) throws IOException { + int port = u.getPort(); boolean ssl = u.getProtocol().equals("https"); if (port < 0) port = (ssl) ? 443 : 80; - String path = u.getPath(); - String query = u.getQuery(); - if ((query != null) && (query.length() > 0)) path = path + "?" + query; - return singlePOST(u.getHost(), port, path, timeout, user, password, ssl, proxyHost, proxyPort, null, props); + String path = u.getPath(); + String query = u.getQuery(); + if ((query != null) && (query.length() > 0)) path = path + "?" + query; + return singlePOST(u.getHost(), port, path, timeout, user, password, ssl, proxyHost, proxyPort, null, props); } - + public static byte[] singlePOST(String url, int timeout, serverObjects props) throws IOException { - try { - return singlePOST(new URL(url), timeout, null, null, null, 0, props); - } catch (MalformedURLException e) { - throw new IOException("Malformed URL: " + e.getMessage()); - } + try { + return singlePOST(new URL(url), timeout, null, null, null, 0, props); + } catch (MalformedURLException e) { + throw new IOException("Malformed URL: " + e.getMessage()); + } } - + public static Vector wget(URL url, int timeout, String user, String password, String proxyHost, int proxyPort) throws IOException { - // splitting of the byte array into lines - byte[] a = singleGET(url, timeout, user, password, proxyHost, proxyPort); + // splitting of the byte array into lines + byte[] a = singleGET(url, timeout, user, password, proxyHost, proxyPort); if (a == null) return null; - int s = 0; - int e; - Vector v = new Vector(); - while (s < a.length) { - e = s; while (e < a.length) if (a[e++] < 32) {e--; break;} - v.add(new String(a, s, e - s)); - s = e; while (s < a.length) if (a[s++] >= 32) {s--; break;} - } - return v; + int s = 0; + int e; + Vector v = new Vector(); + while (s < a.length) { + e = s; while (e < a.length) if (a[e++] < 32) {e--; break;} + v.add(new String(a, s, e - s)); + s = e; while (s < a.length) if (a[s++] >= 32) {s--; break;} + } + return v; } public static httpHeader whead(URL url, int timeout, String user, String password, String proxyHost, int proxyPort) throws IOException { // generate request header httpHeader requestHeader = new httpHeader(); - if ((user != null) && (password != null) && (user.length() != 0)) { - requestHeader.put(httpHeader.AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password)); - } + if ((user != null) && (password != null) && (user.length() != 0)) { + requestHeader.put(httpHeader.AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password)); + } // parse query int port = url.getPort(); boolean ssl = url.getProtocol().equals("https"); - if (port < 0) port = (ssl) ? 443 : 80; - String path = url.getPath(); - String query = url.getQuery(); - if ((query != null) && (query.length() > 0)) path = path + "?" + query; - String host = url.getHost(); + if (port < 0) port = (ssl) ? 443 : 80; + String path = url.getPath(); + String query = url.getQuery(); + if ((query != null) && (query.length() > 0)) path = path + "?" + query; + String host = url.getHost(); // start connection httpc con = null; - try { + try { if ((proxyHost == null) || (proxyPort == 0)) - con = httpc.getInstance(host, port, timeout, ssl); + con = httpc.getInstance(host, port, timeout, ssl); else con = httpc.getInstance(host, port, timeout, ssl, proxyHost, proxyPort); - httpc.response res = con.HEAD(path, requestHeader); - if (res.status.startsWith("2")) { - // success - return res.responseHeader; - } else { - // fail - return res.responseHeader; - } + httpc.response res = con.HEAD(path, requestHeader); + if (res.status.startsWith("2")) { + // success + return res.responseHeader; + } else { + // fail + return res.responseHeader; + } } catch (Exception e) { throw new IOException(e.getMessage()); } finally { if (con != null) httpc.returnInstance(con); } } - + /* public static Vector wget(String url) { - try { - return wget(new URL(url), 5000, null, null, null, 0); - } catch (IOException e) { - Vector ll = new Vector(); - ll.add("503 " + e.getMessage()); - return ll; - } + try { + return wget(new URL(url), 5000, null, null, null, 0); + } catch (IOException e) { + Vector ll = new Vector(); + ll.add("503 " + e.getMessage()); + return ll; + } } - */ - + */ + public static Vector wput(URL url, int timeout, String user, String password, String proxyHost, int proxyPort, serverObjects props) throws IOException { - // splitting of the byte array into lines - byte[] a = singlePOST(url, timeout, user, password, proxyHost, proxyPort, props); - //System.out.println("wput-out=" + new String(a)); - int s = 0; - int e; - Vector v = new Vector(); - while (s < a.length) { - e = s; while (e < a.length) if (a[e++] < 32) {e--; break;} - v.add(new String(a, s, e - s)); - s = e; while (s < a.length) if (a[s++] >= 32) {s--; break;} - } - return v; + // splitting of the byte array into lines + byte[] a = singlePOST(url, timeout, user, password, proxyHost, proxyPort, props); + //System.out.println("wput-out=" + new String(a)); + int s = 0; + int e; + Vector v = new Vector(); + while (s < a.length) { + e = s; while (e < a.length) if (a[e++] < 32) {e--; break;} + v.add(new String(a, s, e - s)); + s = e; while (s < a.length) if (a[s++] >= 32) {s--; break;} + } + return v; } - + /* public static Vector wput(String url, serverObjects props) { - try { - return wput(url, 5000, null, null, null, 0, props); - } catch (IOException e) { - serverLog.logError("HTTPC", "wput exception for url " + url + ": " + e.getMessage()); - e.printStackTrace(); - Vector ll = new Vector(); - ll.add("503 " + e.getMessage()); - return ll; - } + try { + return wput(url, 5000, null, null, null, 0, props); + } catch (IOException e) { + serverLog.logError("HTTPC", "wput exception for url " + url + ": " + e.getMessage()); + e.printStackTrace(); + Vector ll = new Vector(); + ll.add("503 " + e.getMessage()); + return ll; + } } - */ + */ public static void main(String[] args) { - System.out.println("ANOMIC.DE HTTP CLIENT v" + vDATE); - String url = args[0]; - if (!(url.toUpperCase().startsWith("HTTP://"))) url = "http://" + url; - Vector text = new Vector(); - if (args.length == 4) { + System.out.println("ANOMIC.DE HTTP CLIENT v" + vDATE); + String url = args[0]; + if (!(url.toUpperCase().startsWith("HTTP://"))) url = "http://" + url; + Vector text = new Vector(); + if (args.length == 4) { int timeout = Integer.parseInt(args[1]); String proxyHost = args[2]; int proxyPort = Integer.parseInt(args[3]); @@ -1074,22 +1062,22 @@ do upload } catch (IOException e) { System.out.println("Error loading url '" + url + "': " + e.getMessage()); } - } /*else { - serverObjects post = new serverObjects(); - int p; - for (int i = 1; i < args.length; i++) { - p = args[i].indexOf("="); - if (p > 0) post.put(args[i].substring(0, p), args[i].substring(p + 1)); - } - text = wput(url, post); - }*/ - Enumeration i = text.elements(); - while (i.hasMoreElements()) System.out.println((String) i.nextElement()); + } /*else { + serverObjects post = new serverObjects(); + int p; + for (int i = 1; i < args.length; i++) { + p = args[i].indexOf("="); + if (p > 0) post.put(args[i].substring(0, p), args[i].substring(p + 1)); + } + text = wput(url, post); + }*/ + Enumeration i = text.elements(); + while (i.hasMoreElements()) System.out.println((String) i.nextElement()); } - + } /* @@ -1098,100 +1086,100 @@ import java.io.*; import javax.net.ssl.*; import javax.security.cert.X509Certificate; import java.security.KeyStore; - - + + //The application can be modified to connect to a server outside //the firewall by following SSLSocketClientWithTunneling.java. - + public class SSLSocketClientWithClientAuth { - + public static void main(String[] args) throws Exception { - String host = null; - int port = -1; - String path = null; - for (int i = 0; i < args.length; i++) - System.out.println(args[i]); - - if (args.length < 3) { - System.out.println( - "USAGE: java SSLSocketClientWithClientAuth " + - "host port requestedfilepath"); - System.exit(-1); - } - - try { - host = args[0]; - port = Integer.parseInt(args[1]); - path = args[2]; - } catch (IllegalArgumentException e) { - System.out.println("USAGE: java SSLSocketClientWithClientAuth " + - "host port requestedfilepath"); - System.exit(-1); - } - - try { - - SSLSocketFactory factory = null; - try { - SSLContext ctx; - KeyManagerFactory kmf; - KeyStore ks; - char[] passphrase = "passphrase".toCharArray(); - - ctx = SSLContext.getInstance("TLS"); - kmf = KeyManagerFactory.getInstance("SunX509"); - ks = KeyStore.getInstance("JKS"); - - ks.load(new FileInputStream("testkeys"), passphrase); - - kmf.init(ks, passphrase); - ctx.init(kmf.getKeyManagers(), null, null); - - factory = ctx.getSocketFactory(); - } catch (Exception e) { - throw new IOException(e.getMessage()); - } - - SSLSocket socket = (SSLSocket)factory.createSocket(host, port); - - socket.startHandshake(); - - PrintWriter out = new PrintWriter( - new BufferedWriter( - new OutputStreamWriter( - socket.getOutputStream()))); - out.println("GET " + path + " HTTP/1.1"); - out.println(); - out.flush(); - - if (out.checkError()) - System.out.println( - "SSLSocketClient: java.io.PrintWriter error"); - - BufferedReader in = new BufferedReader( - new InputStreamReader( - socket.getInputStream())); - - String inputLine; - - while ((inputLine = in.readLine()) != null) - System.out.println(inputLine); - - in.close(); - out.close(); - socket.close(); - - } catch (Exception e) { - e.printStackTrace(); - } + String host = null; + int port = -1; + String path = null; + for (int i = 0; i < args.length; i++) + System.out.println(args[i]); + + if (args.length < 3) { + System.out.println( + "USAGE: java SSLSocketClientWithClientAuth " + + "host port requestedfilepath"); + System.exit(-1); + } + + try { + host = args[0]; + port = Integer.parseInt(args[1]); + path = args[2]; + } catch (IllegalArgumentException e) { + System.out.println("USAGE: java SSLSocketClientWithClientAuth " + + "host port requestedfilepath"); + System.exit(-1); + } + + try { + + SSLSocketFactory factory = null; + try { + SSLContext ctx; + KeyManagerFactory kmf; + KeyStore ks; + char[] passphrase = "passphrase".toCharArray(); + + ctx = SSLContext.getInstance("TLS"); + kmf = KeyManagerFactory.getInstance("SunX509"); + ks = KeyStore.getInstance("JKS"); + + ks.load(new FileInputStream("testkeys"), passphrase); + + kmf.init(ks, passphrase); + ctx.init(kmf.getKeyManagers(), null, null); + + factory = ctx.getSocketFactory(); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + + SSLSocket socket = (SSLSocket)factory.createSocket(host, port); + + socket.startHandshake(); + + PrintWriter out = new PrintWriter( + new BufferedWriter( + new OutputStreamWriter( + socket.getOutputStream()))); + out.println("GET " + path + " HTTP/1.1"); + out.println(); + out.flush(); + + if (out.checkError()) + System.out.println( + "SSLSocketClient: java.io.PrintWriter error"); + + BufferedReader in = new BufferedReader( + new InputStreamReader( + socket.getInputStream())); + + String inputLine; + + while ((inputLine = in.readLine()) != null) + System.out.println(inputLine); + + in.close(); + out.close(); + socket.close(); + + } catch (Exception e) { + e.printStackTrace(); + } } } -*/ + */ final class httpcFactory implements org.apache.commons.pool.PoolableObjectFactory { - + public httpcFactory() { - super(); + super(); } /** @@ -1199,14 +1187,15 @@ final class httpcFactory implements org.apache.commons.pool.PoolableObjectFactor */ public Object makeObject() throws Exception { return new httpc(); - } + } - /** + /** * @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang.Object) */ public void destroyObject(Object obj) { if (obj instanceof httpc) { httpc theHttpc = (httpc) obj; + theHttpc.removedFromPool = true; } } @@ -1215,8 +1204,7 @@ final class httpcFactory implements org.apache.commons.pool.PoolableObjectFactor * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object) */ public boolean validateObject(Object obj) { - if (obj instanceof httpc) - { + if (obj instanceof httpc) { httpc theHttpc = (httpc) obj; return true; } @@ -1224,30 +1212,30 @@ final class httpcFactory implements org.apache.commons.pool.PoolableObjectFactor } /** - * @param obj - * + * @param obj + * */ public void activateObject(Object obj) { //log.debug(" activateObject..."); } - + /** - * @param obj - * + * @param obj + * */ - public void passivateObject(Object obj) { + public void passivateObject(Object obj) { //log.debug(" passivateObject..." + obj); if (obj instanceof Session) { - httpc theHttpc = (httpc) obj; + httpc theHttpc = (httpc) obj; } } -} +} final class httpcPool extends GenericObjectPool { /** * First constructor. * @param objFactory - */ + */ public httpcPool(httpcFactory objFactory) { super(objFactory); this.setMaxIdle(75); // Maximum idle threads. @@ -1257,7 +1245,7 @@ final class httpcPool extends GenericObjectPool { } public httpcPool(httpcFactory objFactory, - GenericObjectPool.Config config) { + GenericObjectPool.Config config) { super(objFactory, config); } @@ -1265,13 +1253,13 @@ final class httpcPool extends GenericObjectPool { * @see org.apache.commons.pool.impl.GenericObjectPool#borrowObject() */ public Object borrowObject() throws Exception { - return super.borrowObject(); + return super.borrowObject(); } - + /** * @see org.apache.commons.pool.impl.GenericObjectPool#returnObject(java.lang.Object) */ public void returnObject(Object obj) throws Exception { super.returnObject(obj); - } -} + } +} diff --git a/source/de/anomic/kelondro/kelondroTree.java b/source/de/anomic/kelondro/kelondroTree.java index a7bff903a..a8c263b8c 100644 --- a/source/de/anomic/kelondro/kelondroTree.java +++ b/source/de/anomic/kelondro/kelondroTree.java @@ -125,7 +125,7 @@ public class kelondroTree extends kelondroRecords implements Comparator { } // Returns the value to which this map maps the specified key. - public byte[][] get(byte[] key) throws IOException { + public synchronized byte[][] get(byte[] key) throws IOException { //System.out.println("kelondroTree.get " + new String(key) + " in " + filename); Search search = new Search(key); if (search.found()) { @@ -298,7 +298,7 @@ public class kelondroTree extends kelondroRecords implements Comparator { } // Associates the specified value with the specified key in this map - public byte[][] put(byte[][] newrow) throws IOException { + public synchronized byte[][] put(byte[][] newrow) throws IOException { if (newrow.length != columns()) throw new IllegalArgumentException("put: wrong row length " + newrow.length + "; must be " + columns()); // first try to find the key element in the database Search searchResult = new Search(newrow[0]); @@ -546,7 +546,7 @@ public class kelondroTree extends kelondroRecords implements Comparator { } // Associates the specified value with the specified key in this map - public synchronized byte[] put(byte[] key, byte[] value) throws IOException { + public byte[] put(byte[] key, byte[] value) throws IOException { byte[][] row = new byte[2][]; row[0] = key; row[1] = value; @@ -571,7 +571,7 @@ public class kelondroTree extends kelondroRecords implements Comparator { while (size() > 0) remove(lastNode(), null); } - public void remove(Node node, Node parentOfNode) throws IOException { + private void remove(Node node, Node parentOfNode) throws IOException { // there are three cases when removing a node // - the node is a leaf - it can be removed easily // - the node has one child - the child replaces the node @@ -1214,7 +1214,7 @@ public class kelondroTree extends kelondroRecords implements Comparator { // Returns -1, 0, or 1 as the first argument // is less than, equal to, or greater than the second. // two arrays are also equal if one array is a subset of the other's array with filled-up char(0)-values - public synchronized int compare(byte[] a, byte[] b) { + public int compare(byte[] a, byte[] b) { int i = 0; int al = a.length; int bl = b.length; diff --git a/yacy.init b/yacy.init index eabcf1f1b..1013bd542 100644 --- a/yacy.init +++ b/yacy.init @@ -402,11 +402,11 @@ xpstopw=true 50_localcrawl_busysleep=200 61_globalcrawltrigger_idlesleep=10000 61_globalcrawltrigger_busysleep=200 -62_remotetriggeredcrawl_idlesleep=20000 +62_remotetriggeredcrawl_idlesleep=10000 62_remotetriggeredcrawl_busysleep=200 -70_cachemanager_idlesleep=10000 +70_cachemanager_idlesleep=1000 70_cachemanager_busysleep=0 -80_indexing_idlesleep=10000 +80_indexing_idlesleep=1000 80_indexing_busysleep=0 90_cleanup_idlesleep=300000 90_cleanup_busysleep=300000