protection against strange answers from remote peers during search

pull/1/head
Michael Peter Christen 13 years ago
parent 9c51db4243
commit c639248c23

@ -78,7 +78,7 @@ public final class FileUtils
/** /**
* Copies an InputStream to an OutputStream. * Copies an InputStream to an OutputStream.
* *
* @param source InputStream * @param source InputStream
* @param dest OutputStream * @param dest OutputStream
* @param count the total amount of bytes to copy (-1 for all, else must be greater than zero) * @param count the total amount of bytes to copy (-1 for all, else must be greater than zero)
@ -185,7 +185,7 @@ public final class FileUtils
/** /**
* Copies an InputStream to a File. * Copies an InputStream to a File.
* *
* @param source InputStream * @param source InputStream
* @param dest File * @param dest File
* @param count the amount of bytes to copy * @param count the amount of bytes to copy
@ -219,7 +219,7 @@ public final class FileUtils
/** /**
* Copies a part of a File to an OutputStream. * Copies a part of a File to an OutputStream.
* *
* @param source File * @param source File
* @param dest OutputStream * @param dest OutputStream
* @param start Number of bytes to skip from the beginning of the File * @param start Number of bytes to skip from the beginning of the File
@ -255,7 +255,7 @@ public final class FileUtils
/** /**
* Copies a File to an OutputStream. * Copies a File to an OutputStream.
* *
* @param source File * @param source File
* @param dest OutputStream * @param dest OutputStream
* @throws IOException * @throws IOException
@ -281,7 +281,7 @@ public final class FileUtils
/** /**
* Copies a File to a File. * Copies a File to a File.
* *
* @param source File * @param source File
* @param dest File * @param dest File
* @param count the amount of bytes to copy * @param count the amount of bytes to copy
@ -424,7 +424,7 @@ public final class FileUtils
/** /**
* This function determines if a byte array is gzip compressed and uncompress it * This function determines if a byte array is gzip compressed and uncompress it
* *
* @param source properly gzip compressed byte array * @param source properly gzip compressed byte array
* @return uncompressed byte array * @return uncompressed byte array
* @throws IOException * @throws IOException
@ -666,6 +666,8 @@ public final class FileUtils
} }
public static Map<String, String> table(final byte[] a) { public static Map<String, String> table(final byte[] a) {
if (a == null) return new ConcurrentHashMap<String, String>();
//System.out.println("***TABLE: a.size = " + a.length);
return table(strings(a)); return table(strings(a));
} }
@ -674,9 +676,7 @@ public final class FileUtils
return new ArrayList<String>().iterator(); return new ArrayList<String>().iterator();
} }
try { try {
return new StringsIterator(new BufferedReader(new InputStreamReader( return new StringsIterator(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(a), "UTF-8")));
new ByteArrayInputStream(a),
"UTF-8")));
} catch ( final UnsupportedEncodingException e ) { } catch ( final UnsupportedEncodingException e ) {
return null; return null;
} }
@ -684,7 +684,7 @@ public final class FileUtils
/** /**
* Read lines of a file into an ArrayList. * Read lines of a file into an ArrayList.
* *
* @param listFile the file * @param listFile the file
* @return the resulting array as an ArrayList * @return the resulting array as an ArrayList
*/ */
@ -714,7 +714,7 @@ public final class FileUtils
/** /**
* Write a String to a file (used for string representation of lists). * Write a String to a file (used for string representation of lists).
* *
* @param listFile the file to write to * @param listFile the file to write to
* @param out the String to write * @param out the String to write
* @return returns <code>true</code> if successful, <code>false</code> otherwise * @return returns <code>true</code> if successful, <code>false</code> otherwise
@ -743,7 +743,7 @@ public final class FileUtils
/** /**
* Read lines of a text file into a String, optionally ignoring comments. * Read lines of a text file into a String, optionally ignoring comments.
* *
* @param listFile the File to read from. * @param listFile the File to read from.
* @param withcomments If <code>false</code> ignore lines starting with '#'. * @param withcomments If <code>false</code> ignore lines starting with '#'.
* @return String representation of the file content. * @return String representation of the file content.
@ -783,7 +783,7 @@ public final class FileUtils
/** /**
* Read content of a directory into a String array of file names. * Read content of a directory into a String array of file names.
* *
* @param dirname The directory to get the file listing from. If it doesn't exist yet, it will be created. * @param dirname The directory to get the file listing from. If it doesn't exist yet, it will be created.
* @return array of file names * @return array of file names
*/ */
@ -793,7 +793,7 @@ public final class FileUtils
/** /**
* Read content of a directory into a String array of file names. * Read content of a directory into a String array of file names.
* *
* @param dirname The directory to get the file listing from. If it doesn't exist yet, it will be created. * @param dirname The directory to get the file listing from. If it doesn't exist yet, it will be created.
* @param filter String which contains a regular expression which has to be matched by file names in order * @param filter String which contains a regular expression which has to be matched by file names in order
* to appear in returned array. All file names will be returned if filter is null. * to appear in returned array. All file names will be returned if filter is null.
@ -805,7 +805,7 @@ public final class FileUtils
/** /**
* Read content of a directory into a String array of file names. * Read content of a directory into a String array of file names.
* *
* @param dir The directory to get the file listing from. If it doesn't exist yet, it will be created. * @param dir The directory to get the file listing from. If it doesn't exist yet, it will be created.
* @return array of file names * @return array of file names
*/ */
@ -815,7 +815,7 @@ public final class FileUtils
/** /**
* Read content of a directory into a String array of file names. * Read content of a directory into a String array of file names.
* *
* @param dir The directory to get the file listing from. If it doesn't exist yet, it will be created. * @param dir The directory to get the file listing from. If it doesn't exist yet, it will be created.
* @param filter String which contains a regular expression which has to be matched by file names in order * @param filter String which contains a regular expression which has to be matched by file names in order
* to appear in returned array. All file names will be returned if filter is null. * to appear in returned array. All file names will be returned if filter is null.
@ -872,7 +872,7 @@ public final class FileUtils
/** /**
* Write elements of an Array of Strings to a file (one element per line). * Write elements of an Array of Strings to a file (one element per line).
* *
* @param listFile the file to write to * @param listFile the file to write to
* @param list the Array to write * @param list the Array to write
* @return returns <code>true</code> if successful, <code>false</code> otherwise * @return returns <code>true</code> if successful, <code>false</code> otherwise
@ -887,7 +887,7 @@ public final class FileUtils
public static class StringsIterator implements Iterator<String> public static class StringsIterator implements Iterator<String>
{ {
private final BufferedReader reader; private BufferedReader reader;
private String nextLine; private String nextLine;
public StringsIterator(final BufferedReader reader) { public StringsIterator(final BufferedReader reader) {
@ -904,7 +904,7 @@ public final class FileUtils
@Override @Override
public String next() { public String next() {
final String line = this.nextLine; final String line = this.nextLine;
try { if (this.reader != null) try {
while ( (this.nextLine = this.reader.readLine()) != null ) { while ( (this.nextLine = this.reader.readLine()) != null ) {
this.nextLine = this.nextLine.trim(); this.nextLine = this.nextLine.trim();
if ( this.nextLine.length() > 0 ) { if ( this.nextLine.length() > 0 ) {
@ -917,6 +917,13 @@ public final class FileUtils
Log.logException(e); Log.logException(e);
this.nextLine = null; this.nextLine = null;
} }
if (this.nextLine == null && this.reader != null) {
try {
this.reader.close();
} catch (IOException e) {} finally {
this.reader = null;
}
}
return line; return line;
} }
@ -942,7 +949,7 @@ public final class FileUtils
/** /**
* Moves all files from a directory to another. * Moves all files from a directory to another.
* *
* @param from_dir Directory which contents will be moved. * @param from_dir Directory which contents will be moved.
* @param to_dir Directory to move into. It must exist already. * @param to_dir Directory to move into. It must exist already.
*/ */
@ -1010,7 +1017,7 @@ public final class FileUtils
/** /**
* copies the input stream to one output stream (byte per byte) * copies the input stream to one output stream (byte per byte)
* *
* @param in * @param in
* @param out * @param out
* @return number of copies bytes * @return number of copies bytes
@ -1031,7 +1038,7 @@ public final class FileUtils
/** /**
* copies the input stream to both output streams (byte per byte) * copies the input stream to both output streams (byte per byte)
* *
* @param in * @param in
* @param out0 * @param out0
* @param out1 * @param out1
@ -1060,7 +1067,7 @@ public final class FileUtils
/** /**
* copies the input stream to all writers (byte per byte) * copies the input stream to all writers (byte per byte)
* *
* @param data * @param data
* @param writer * @param writer
* @param charSet * @param charSet
@ -1111,7 +1118,7 @@ public final class FileUtils
/** /**
* delete files and directories if a directory is not empty, delete also everything inside because * delete files and directories if a directory is not empty, delete also everything inside because
* deletion sometimes fails on windows, there is also a windows exec included * deletion sometimes fails on windows, there is also a windows exec included
* *
* @param path * @param path
*/ */
public static void deletedelete(final File path) { public static void deletedelete(final File path) {

@ -159,7 +159,7 @@ public final class Protocol
* exceptional failure case is when we know the other's peers hash, the other peers responds correctly but * exceptional failure case is when we know the other's peers hash, the other peers responds correctly but
* they appear to be another peer by comparisment of the other peer's hash this works of course only if we * they appear to be another peer by comparisment of the other peer's hash this works of course only if we
* know the other peer's hash. * know the other peer's hash.
* *
* @return the number of new seeds * @return the number of new seeds
*/ */
public static int hello( public static int hello(
@ -424,7 +424,7 @@ public final class Protocol
/** /**
* check the status of a remote peer * check the status of a remote peer
* *
* @param target * @param target
* @return an array of two long: [0] is the count of urls, [1] is a magic * @return an array of two long: [0] is the count of urls, [1] is a magic
*/ */
@ -956,10 +956,12 @@ public final class Protocol
} }
final HTTPClient httpClient = new HTTPClient(ClientIdentification.getUserAgent(), 8000); final HTTPClient httpClient = new HTTPClient(ClientIdentification.getUserAgent(), 8000);
resultMap = byte[] a = httpClient.POSTbytes(new MultiProtocolURI("http://" + hostaddress + "/yacy/search.html"), hostname, parts, false);
FileUtils.table(httpClient.POSTbytes(new MultiProtocolURI("http://" if (a != null && a.length > 200000) {
+ hostaddress // there is something wrong. This is too large, maybe a hack on the other side?
+ "/yacy/search.html"), hostname, parts, false)); a = null;
}
resultMap = FileUtils.table(a);
// evaluate request result // evaluate request result
if ( resultMap == null || resultMap.isEmpty() ) { if ( resultMap == null || resultMap.isEmpty() ) {
@ -1139,7 +1141,7 @@ public final class Protocol
/** /**
* transfer the index. If the transmission fails, return a string describing the cause. If everything is * transfer the index. If the transmission fails, return a string describing the cause. If everything is
* ok, return null. * ok, return null.
* *
* @param targetSeed * @param targetSeed
* @param indexes * @param indexes
* @param urlCache * @param urlCache

Loading…
Cancel
Save