Sfoglia il codice sorgente

Add URL Encoding back.

pull/22/head
Greg Holmes 9 anni fa
parent
commit
0b1b21f957

+ 50
- 0
src/com/dmdirc/util/collections/URLEncodingMapFlattener.java Vedi File

@@ -0,0 +1,50 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.collections;
24
+
25
+import java.io.UnsupportedEncodingException;
26
+import java.net.URLEncoder;
27
+import java.util.Map;
28
+import java.util.function.Function;
29
+import java.util.stream.Stream;
30
+
31
+/**
32
+ * Flattens a map into a key=value pair, URLEncoding both the key and the entry.
33
+ */
34
+public class URLEncodingMapFlattener
35
+        implements Function<Map.Entry<String, String>, Stream<String>> {
36
+
37
+    @Override
38
+    public Stream<String> apply(final Map.Entry<String, String> entry) {
39
+        String key;
40
+        String value;
41
+        try {
42
+            key = URLEncoder.encode(entry.getKey(), "UTF-8");
43
+            value = URLEncoder.encode(entry.getValue(), "UTF-8");
44
+        } catch (UnsupportedEncodingException ex){
45
+            key = entry.getKey();
46
+            value = entry.getValue();
47
+        }
48
+        return Stream.of(key + '=' + value);
49
+    }
50
+}

+ 4
- 1
src/com/dmdirc/util/io/Downloader.java Vedi File

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.util.io;
24 24
 
25
+import com.dmdirc.util.collections.URLEncodingMapFlattener;
26
+
25 27
 import java.io.BufferedReader;
26 28
 import java.io.DataOutputStream;
27 29
 import java.io.IOException;
@@ -79,7 +81,8 @@ public class Downloader {
79 81
      */
80 82
     public List<String> getPage(final String url,
81 83
             final Map<String, String> postData) throws IOException {
82
-        return getPage(url, postData.entrySet().stream().map(Object::toString)
84
+        return getPage(url, postData.entrySet().stream()
85
+                .flatMap(e -> new URLEncodingMapFlattener().apply(e))
83 86
                 .collect(Collectors.joining("&")));
84 87
     }
85 88
 

+ 16
- 1
src/com/dmdirc/util/io/ListenerInputStream.java Vedi File

@@ -29,7 +29,8 @@ import java.io.InputStream;
29 29
 import javax.annotation.Nonnull;
30 30
 
31 31
 /**
32
- *
32
+ * Wrapped {@link InputStream} that provides updates to a {@link DownloadListener} as it reads a
33
+ * stream.
33 34
  */
34 35
 public class ListenerInputStream extends FilterInputStream {
35 36
 
@@ -38,6 +39,13 @@ public class ListenerInputStream extends FilterInputStream {
38 39
     private int count;
39 40
     private int mark;
40 41
 
42
+    /**
43
+     * Creates a new stream.
44
+     *
45
+     * @param in        Stream to wrap
46
+     * @param listener  Listener to gives up dates to
47
+     * @param length    Length of the stream, if -1 the listener will be indeterminate
48
+     */
41 49
     public ListenerInputStream(@Nonnull final InputStream in, final DownloadListener listener,
42 50
             final int length) {
43 51
         super(in);
@@ -98,6 +106,13 @@ public class ListenerInputStream extends FilterInputStream {
98 106
         return super.markSupported();
99 107
     }
100 108
 
109
+    /**
110
+     * Updates the listener and total read count.
111
+     *
112
+     * @param read Number of bytes further into stream.
113
+     *
114
+     * @return Returns the number of bytes read.
115
+     */
101 116
     private int update(final int read) {
102 117
         if (read > 0) {
103 118
             count += read;

+ 14
- 0
test/com/dmdirc/util/io/DownloaderTest.java Vedi File

@@ -104,6 +104,20 @@ public class DownloaderTest {
104 104
         assertTrue(postDataString1.equals(os.toString()) || postDataString2.equals(os.toString()));
105 105
     }
106 106
 
107
+    @Test
108
+    public void testGetPageMapEncoding() throws IOException {
109
+        final Map<String, String> postData = Maps.newHashMap();
110
+        postData.put("ke&y1", "value1");
111
+        postData.put("key2", "val&ue2");
112
+        new TestableDownloader().getPage("rar", postData);
113
+        verify(mockedConnection).setRequestProperty("Content-Type",
114
+                "application/x-www-form-urlencoded");
115
+
116
+        final String postDataString1 = "ke%26y1=value1&key2=val%26ue2";
117
+        final String postDataString2 = "key2=val%26ue2&ke%26y1=value1";
118
+        assertTrue(postDataString1.equals(os.toString()) || postDataString2.equals(os.toString()));
119
+    }
120
+
107 121
     @Test
108 122
     public void testDownloadPage() throws IOException {
109 123
         final Path file = fakeFS.getPath("test.txt");

Loading…
Annulla
Salva