Bladeren bron

Escape extra '#' chars in URI fragments

Pass exceptions along with user errors

Fixes CLIENT-37

Change-Id: I22898e83979992d398e663d9d7d12a014350e917
Reviewed-on: http://gerrit.dmdirc.com/1528
Automatic-Compile: Greg Holmes <greg@dmdirc.com>
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.6.5b1
Chris Smith 13 jaren geleden
bovenliggende
commit
6f0437709f
1 gewijzigde bestanden met toevoegingen van 34 en 8 verwijderingen
  1. 34
    8
      src/com/dmdirc/ui/core/util/URLHandler.java

+ 34
- 8
src/com/dmdirc/ui/core/util/URLHandler.java Bestand weergeven

@@ -83,20 +83,46 @@ public class URLHandler {
83 83
      * @param urlString URL to parse
84 84
      */
85 85
     public void launchApp(final String urlString) {
86
+        final String sanitisedString = getSanitisedString(urlString);
87
+
86 88
         URI uri;
87 89
         try {
88
-            uri = new URI(urlString.replace("|", "%7C"));
90
+            uri = new URI(sanitisedString);
89 91
             if (uri.getScheme() == null) {
90
-                uri = new URI("http://" + urlString.replace("|", "%7C"));
92
+                uri = new URI("http://" + sanitisedString);
91 93
             }
92 94
         } catch (URISyntaxException ex) {
93
-            Logger.userError(ErrorLevel.LOW, "Invalid URL: " + ex.getMessage());
95
+            Logger.userError(ErrorLevel.LOW, "Invalid URI: " + ex.getMessage(), ex);
94 96
             return;
95 97
         }
96 98
 
97 99
         launchApp(uri);
98 100
     }
99 101
 
102
+    /**
103
+     * Sanitises the specified string so that it may be used as a {@link URI}.
104
+     * Sanitisation consists of:
105
+     * <ul>
106
+     * <li>replacing any pipe character with its hex escape</li>
107
+     * <li>replacing any hash character in the fragment with its hex escape</li>
108
+     * </ul>
109
+     *
110
+     * @since 0.6.5
111
+     * @param urlString The string to be sanitised
112
+     * @return A sanitised version of the specified string.
113
+     */
114
+    protected static String getSanitisedString(final String urlString) {
115
+        String sanitisedString = urlString.replace("|", "%7C");
116
+
117
+        int index = sanitisedString.indexOf('#');
118
+        if (sanitisedString.lastIndexOf('#') > index) {
119
+            sanitisedString = sanitisedString.substring(0, index + 1)
120
+                    + sanitisedString.substring(index + 1).replace("#", "%23");
121
+        }
122
+
123
+        return sanitisedString;
124
+    }
125
+
100 126
     /**
101 127
      * Launches an application for a given url.
102 128
      *
@@ -110,7 +136,7 @@ public class URLHandler {
110 136
                 uri = new URI("http://" + url.toString());
111 137
             }
112 138
         } catch (URISyntaxException ex) {
113
-            Logger.userError(ErrorLevel.LOW, "Invalid URL: " + ex.getMessage());
139
+            Logger.userError(ErrorLevel.LOW, "Invalid URL: " + ex.getMessage(), ex);
114 140
             return;
115 141
         }
116 142
 
@@ -234,8 +260,8 @@ public class URLHandler {
234 260
         try {
235 261
             Runtime.getRuntime().exec(parseArguments(command));
236 262
         } catch (IOException ex) {
237
-            Logger.userError(ErrorLevel.LOW, "Unable to run application: ",
238
-                    ex.getMessage());
263
+            Logger.userError(ErrorLevel.LOW, "Unable to run application: "
264
+                    + ex.getMessage(), ex);
239 265
         }
240 266
     }
241 267
 
@@ -292,7 +318,7 @@ public class URLHandler {
292 318
                 desktop.browse(url);
293 319
             } catch (IOException ex) {
294 320
                 Logger.userError(ErrorLevel.LOW,
295
-                        "Unable to open URL: " + ex.getMessage());
321
+                        "Unable to open URL: " + ex.getMessage(), ex);
296 322
             }
297 323
         } else {
298 324
             Logger.userError(ErrorLevel.LOW,
@@ -314,7 +340,7 @@ public class URLHandler {
314 340
                 desktop.mail(url);
315 341
             } catch (IOException ex) {
316 342
                 Logger.userError(ErrorLevel.LOW,
317
-                        "Unable to open URL: " + ex.getMessage());
343
+                        "Unable to open URL: " + ex.getMessage(), ex);
318 344
             }
319 345
         } else {
320 346
             Logger.userError(ErrorLevel.LOW,

Laden…
Annuleren
Opslaan