Author: toad
Date: 2006-06-09 20:16:11 +0000 (Fri, 09 Jun 2006)
New Revision: 9105

Modified:
   trunk/freenet/src/freenet/clients/http/HTTPRequest.java
   trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java
   trunk/freenet/src/freenet/config/FilePersistentConfig.java
   trunk/freenet/src/freenet/node/Version.java
   trunk/freenet/src/freenet/node/fcp/FCPConnectionInputHandler.java
   trunk/freenet/src/freenet/support/SimpleFieldSet.java
   trunk/freenet/src/freenet/support/io/LineReader.java
   trunk/freenet/src/freenet/support/io/LineReadingInputStream.java
Log:
789: Hopefully fix #433.

Modified: trunk/freenet/src/freenet/clients/http/HTTPRequest.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/HTTPRequest.java     2006-06-09 
17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/clients/http/HTTPRequest.java     2006-06-09 
20:16:11 UTC (rev 9105)
@@ -429,9 +429,9 @@
                LineReadingInputStream lis = new LineReadingInputStream(is);

                String line;
-               line = lis.readLine(100, 100);
+               line = lis.readLine(100, 100, false); // really it's US-ASCII, 
but ISO-8859-1 is close enough.
                while (is.available() > 0 && !line.equals(boundary)) {
-                       line = lis.readLine(100, 100);
+                       line = lis.readLine(100, 100, false);
                }

                boundary  = "\r\n"+boundary;
@@ -442,7 +442,7 @@
                while(is.available() > 0) {
                        name = null;
                        // chomp headers
-                       while( (line = lis.readLine(100, 100)) != null) {
+                       while( (line = lis.readLine(100, 100, false)) /* see 
above */ != null) {
                                if (line.length() == 0) break;

                                String[] lineparts = line.split(":");

Modified: trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java      
2006-06-09 17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/clients/http/ToadletContextImpl.java      
2006-06-09 20:16:11 UTC (rev 9105)
@@ -176,7 +176,7 @@

                        while(true) {

-                               String firstLine = lis.readLine(32768, 128);
+                               String firstLine = lis.readLine(32768, 128, 
false); // ISO-8859-1 or US-ASCII, _not_ UTF-8
                                if (firstLine == null) {
                                        sock.close();
                                        return;
@@ -213,7 +213,7 @@
                                MultiValueTable headers = new MultiValueTable();

                                while(true) {
-                                       String line = lis.readLine(32768, 128);
+                                       String line = lis.readLine(32768, 128, 
false); // ISO-8859 or US-ASCII, not UTF-8
                                        if (line == null) {
                                                sock.close();
                                                return;

Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
===================================================================
--- trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-06-09 
17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-06-09 
20:16:11 UTC (rev 9105)
@@ -80,7 +80,8 @@
                BufferedInputStream bis = new BufferedInputStream(fis);
                try {
                        LineReadingInputStream lis = new 
LineReadingInputStream(bis);
-                       origConfigFileContents = new SimpleFieldSet(lis, 4096, 
256, true, true);
+                       // Config file is UTF-8 too!
+                       origConfigFileContents = new SimpleFieldSet(lis, 4096, 
256, true, true, true);
                } finally {
                        try {
                                fis.close();
@@ -122,7 +123,7 @@
                SimpleFieldSet fs = exportFieldSet();
                Logger.minor(this, "fs = "+fs);
                FileOutputStream fos = new FileOutputStream(tempFilename);
-               BufferedWriter bw = new BufferedWriter(new 
OutputStreamWriter(fos));
+               BufferedWriter bw = new BufferedWriter(new 
OutputStreamWriter(fos, "UTF-8"));
                synchronized(this) {
                        fs.writeTo(bw);
                }

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-06-09 17:38:49 UTC (rev 
9104)
+++ trunk/freenet/src/freenet/node/Version.java 2006-06-09 20:16:11 UTC (rev 
9105)
@@ -18,7 +18,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 788;
+       private static final int buildNumber = 789;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 765;

Modified: trunk/freenet/src/freenet/node/fcp/FCPConnectionInputHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPConnectionInputHandler.java   
2006-06-09 17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/node/fcp/FCPConnectionInputHandler.java   
2006-06-09 20:16:11 UTC (rev 9105)
@@ -43,13 +43,13 @@
                while(true) {
                        SimpleFieldSet fs;
                        // Read a message
-                       String messageType = lis.readLine(64, 64);
+                       String messageType = lis.readLine(64, 64, true);
                        if(messageType == null) {
                                is.close();
                                return;
                        }
                        if(messageType.equals("")) continue;
-                       fs = new SimpleFieldSet(lis, 4096, 128, true, false);
+                       fs = new SimpleFieldSet(lis, 4096, 128, true, false, 
true);
                        FCPMessage msg;
                        try {
                                msg = FCPMessage.create(messageType, fs, 
handler.bf, handler.server.node.persistentTempBucketFactory);

Modified: trunk/freenet/src/freenet/support/SimpleFieldSet.java
===================================================================
--- trunk/freenet/src/freenet/support/SimpleFieldSet.java       2006-06-09 
17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/support/SimpleFieldSet.java       2006-06-09 
20:16:11 UTC (rev 9105)
@@ -33,10 +33,10 @@
         read(br);
     }

-    public SimpleFieldSet(LineReader lis, int maxLineLength, int 
lineBufferSize, boolean multiLevel, boolean tolerant) throws IOException {
+    public SimpleFieldSet(LineReader lis, int maxLineLength, int 
lineBufferSize, boolean multiLevel, boolean tolerant, boolean utf8OrIso88591) 
throws IOException {
        map = new HashMap();
        this.multiLevel = multiLevel;
-       read(lis, maxLineLength, lineBufferSize, tolerant);
+       read(lis, maxLineLength, lineBufferSize, tolerant, utf8OrIso88591);
     }

     /**
@@ -95,11 +95,12 @@
      * blah=blah
      * blah=blah
      * End
+     * @param utfOrIso88591 If true, read as UTF-8, otherwise read as 
ISO-8859-1.
      */
-    private void read(LineReader br, int maxLength, int bufferSize, boolean 
tolerant) throws IOException {
+    private void read(LineReader br, int maxLength, int bufferSize, boolean 
tolerant, boolean utfOrIso88591) throws IOException {
         boolean firstLine = true;
         while(true) {
-            String line = br.readLine(maxLength, bufferSize);
+            String line = br.readLine(maxLength, bufferSize, utfOrIso88591);
             if(line == null) {
                 if(firstLine) throw new EOFException();
                 if(tolerant)

Modified: trunk/freenet/src/freenet/support/io/LineReader.java
===================================================================
--- trunk/freenet/src/freenet/support/io/LineReader.java        2006-06-09 
17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/support/io/LineReader.java        2006-06-09 
20:16:11 UTC (rev 9105)
@@ -4,6 +4,9 @@

 public interface LineReader {

-       public String readLine(int maxLength, int bufferSize) throws 
IOException;
+       /**
+        * Read a \n or \r\n terminated line of UTF-8 or ISO-8859-1.
+        */
+       public String readLine(int maxLength, int bufferSize, boolean utf) 
throws IOException;

 }

Modified: trunk/freenet/src/freenet/support/io/LineReadingInputStream.java
===================================================================
--- trunk/freenet/src/freenet/support/io/LineReadingInputStream.java    
2006-06-09 17:38:49 UTC (rev 9104)
+++ trunk/freenet/src/freenet/support/io/LineReadingInputStream.java    
2006-06-09 20:16:11 UTC (rev 9105)
@@ -13,28 +13,34 @@
                super(in);
        }

+       private byte[] buf;
+
        /**
-        * Read a line of US-ASCII. Used for e.g. HTTP. @return Null if end of 
file.
+        * Read a \n or \r\n terminated line of UTF-8 or ISO-8859-1.
         */
-       public String readLine(int maxLength, int bufferSize) throws 
IOException {
-               StringBuffer sb = new StringBuffer(bufferSize);
+       public String readLine(int maxLength, int bufferSize, boolean utf) 
throws IOException {
+               if(buf == null)
+                       buf = new byte[Math.max(128, Math.min(1024, 
bufferSize))];
+               int ctr = 0;
                while(true) {
                        int x = read();
                        if(x == -1) {
-                               if(sb.length() == 0) return null;
-                               return sb.toString();
+                               if(ctr == 0) return null;
+                               return new String(buf, 0, ctr, utf ? "UTF-8" : 
"ISO-8859-1");
                        }
-                       char c = (char) x;
-                       if(c == '\n') {
-                               if(sb.length() > 0) {
-                                       if(sb.charAt(sb.length()-1) == '\r')
-                                               sb.setLength(sb.length()-1);
-                               }
-                               return sb.toString();
+                       // REDFLAG this is definitely safe with the above 
charsets, it may not be safe with some wierd ones. 
+                       if(x == (int)'\n') {
+                               if(ctr == 0) return "";
+                               if(buf[ctr-1] == '\r') ctr--;
+                               return new String(buf, 0, ctr, utf ? "UTF-8" : 
"ISO-8859-1");
                        }
-                       sb.append(c);
-                       if(sb.length() >= maxLength)
-                               throw new TooLongException();
+                       if(ctr >= buf.length) {
+                               if(buf.length == bufferSize) throw new 
TooLongException();
+                               byte[] newBuf = new byte[Math.min(buf.length * 
2, bufferSize)];
+                               System.arraycopy(buf, 0, newBuf, 0, buf.length);
+                               buf = newBuf;
+                       }
+                       buf[ctr++] = (byte)x;
                }
        }



Reply via email to