Explorar el Código

IrcAddress now uses URI to parse URLs. This means that encoded characters are now decoded properly.

Fixes issue 763.

git-svn-id: http://svn.dmdirc.com/trunk@3451 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith hace 16 años
padre
commit
54236284a0
Se han modificado 2 ficheros con 48 adiciones y 37 borrados
  1. 37
    37
      src/com/dmdirc/util/IrcAddress.java
  2. 11
    0
      test/com/dmdirc/util/IrcAddressTest.java

+ 37
- 37
src/com/dmdirc/util/IrcAddress.java Ver fichero

@@ -28,6 +28,8 @@ import com.dmdirc.config.Identity;
28 28
 import com.dmdirc.config.IdentityManager;
29 29
 
30 30
 import java.io.Serializable;
31
+import java.net.URI;
32
+import java.net.URISyntaxException;
31 33
 import java.util.ArrayList;
32 34
 import java.util.List;
33 35
 
@@ -64,37 +66,42 @@ public class IrcAddress implements Serializable {
64 66
      * @throws InvalidAddressException If an invalid address is passed
65 67
      */
66 68
     public IrcAddress(final String address) throws InvalidAddressException {
67
-        StringBuilder builder;
68
-
69
-        if (address.toLowerCase().startsWith("ircs://")) {
69
+        URI uri;
70
+        String myAddress;
71
+        
72
+        // Check for +ports (SSL)
73
+        myAddress = address.replaceFirst(":\\+([0-9]+)", ":$1");
74
+        if (myAddress.length() < address.length()) {
70 75
             usesSSL = true;
71
-            builder = new StringBuilder(address.substring(7));
72
-        } else if (address.toLowerCase().startsWith("irc://")) {
73
-            usesSSL = false;
74
-            builder = new StringBuilder(address.substring(6));
75
-        } else {
76
+        }
77
+        
78
+        try {
79
+            uri = new URI(myAddress);
80
+        } catch (URISyntaxException ex) {
81
+            throw new InvalidAddressException("Unable to parse URI", ex);
82
+        }
83
+        
84
+        if (uri.getScheme() != null && uri.getScheme().equalsIgnoreCase("ircs")) {
85
+            usesSSL = true;
86
+        } else if (uri.getScheme() == null || !uri.getScheme().equalsIgnoreCase("irc")) {
76 87
             throw new InvalidAddressException("Invalid protocol specified");
77 88
         }
78
-
79
-        final int atIndex = builder.indexOf("@");
80
-        if (atIndex > -1) {
81
-            doPass(builder.substring(0, atIndex));
82
-            builder.delete(0, atIndex + 1);
89
+        
90
+        if (uri.getUserInfo() != null) {
91
+            doPass(uri.getUserInfo());
83 92
         }
93
+        
94
+        doChannels(uri.getPath());
84 95
 
85
-        final int slashIndex = builder.indexOf("/");
86
-        if (slashIndex > -1) {
87
-            doChannels(builder.substring(slashIndex + 1));
88
-            builder.delete(slashIndex, builder.length());
96
+        if (uri.getPort() > -1) {
97
+            doPort(uri.getPort());
89 98
         }
90 99
 
91
-        final int colonIndex = builder.indexOf(":");
92
-        if (colonIndex > -1) {
93
-            doPort(builder.substring(colonIndex + 1));
94
-            builder.delete(colonIndex, builder.length());
100
+        if (uri.getHost() == null) {
101
+            throw new InvalidAddressException("Invalid host or port specified");
102
+        } else {
103
+            doServer(uri.getHost());
95 104
         }
96
-
97
-        doServer(builder.toString());
98 105
     }
99 106
 
100 107
     /**
@@ -112,7 +119,11 @@ public class IrcAddress implements Serializable {
112 119
      * @param channels The channels part of this address
113 120
      */
114 121
     private void doChannels(final String channels) {
115
-        for (String channel : channels.split(",")) {
122
+        if (channels == null || channels.length() == 0 || channels.charAt(0) != '/') {
123
+            return;
124
+        }
125
+        
126
+        for (String channel : channels.substring(1).split(",")) {
116 127
             if (!channel.equalsIgnoreCase("needpass") && 
117 128
                     !channel.equalsIgnoreCase("needkey") &&
118 129
                     !channel.equalsIgnoreCase("isnick") && !channel.isEmpty()) {
@@ -127,19 +138,8 @@ public class IrcAddress implements Serializable {
127 138
      * @param port The port part of this address
128 139
      * @throws InvalidAddressException if the port is non-numeric
129 140
      */
130
-    private void doPort(final String port) throws InvalidAddressException {
131
-        String actualPort = port;
132
-
133
-        if (port.charAt(0) == '+') {
134
-            usesSSL = true;
135
-            actualPort = port.substring(1);
136
-        }
137
-
138
-        try {
139
-            this.port = Integer.valueOf(actualPort);
140
-        } catch (NumberFormatException ex) {
141
-            throw new InvalidAddressException("Invalid port number", ex);
142
-        }
141
+    private void doPort(final int port) throws InvalidAddressException {
142
+        this.port = port;
143 143
     }
144 144
 
145 145
     /**

+ 11
- 0
test/com/dmdirc/util/IrcAddressTest.java Ver fichero

@@ -179,5 +179,16 @@ public class IrcAddressTest extends junit.framework.TestCase {
179 179
             assertFalse(true);
180 180
         }
181 181
     }
182
+    
183
+    @Test
184
+    public void testEncoding() {
185
+        try {
186
+            final IrcAddress address1 = new IrcAddress("irc://server/%23DMDirc");
187
+            assertEquals(1, address1.getChannels().size());
188
+            assertEquals("#DMDirc", address1.getChannels().get(0));
189
+        } catch (InvalidAddressException ex) {
190
+            assertFalse(true);
191
+        }        
192
+    }
182 193
 
183 194
 }

Loading…
Cancelar
Guardar