Correctly decode URI in QueryStringDecoder. See #189

This commit is contained in:
norman 2012-02-15 08:30:22 +01:00
parent 4636309597
commit c46b083c1f
2 changed files with 102 additions and 2 deletions

View File

@ -155,11 +155,21 @@ public class QueryStringDecoder {
"maxParams: " + maxParams + " (expected: a positive integer)"); "maxParams: " + maxParams + " (expected: a positive integer)");
} }
String rawPath = uri.getRawPath();
if (rawPath != null) {
hasPath = true;
} else {
rawPath ="";
hasPath = false;
}
// Also take care of cut of things like "http://localhost"
String newUri = rawPath + "?" + uri.getRawQuery();
// http://en.wikipedia.org/wiki/Query_string // http://en.wikipedia.org/wiki/Query_string
this.uri = uri.toASCIIString().replace(';', '&'); this.uri = newUri.replace(';', '&');
this.charset = charset; this.charset = charset;
this.maxParams = maxParams; this.maxParams = maxParams;
hasPath = false;
} }
/** /**

View File

@ -15,8 +15,11 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import java.net.URI;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Assert; import org.junit.Assert;
@ -165,4 +168,91 @@ public class QueryStringDecoderTest {
Assert.assertEquals(ed.getPath(), ad.getPath()); Assert.assertEquals(ed.getPath(), ad.getPath());
Assert.assertEquals(ed.getParameters(), ad.getParameters()); Assert.assertEquals(ed.getParameters(), ad.getParameters());
} }
// See #189
@Test
public void testURI() {
URI uri = URI.create("http://localhost:8080/foo?param1=value1&param2=value2&param3=value3");
QueryStringDecoder decoder = new QueryStringDecoder(uri);
Assert.assertEquals("/foo", decoder.getPath());
Map<String, List<String>> params = decoder.getParameters();
Assert.assertEquals(3, params.size());
Iterator<Entry<String, List<String>>> entries = params.entrySet().iterator();
Entry<String, List<String>> entry = entries.next();
Assert.assertEquals("param1", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value1", entry.getValue().get(0));
entry = entries.next();
Assert.assertEquals("param2", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value2", entry.getValue().get(0));
entry = entries.next();
Assert.assertEquals("param3", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value3", entry.getValue().get(0));
Assert.assertFalse(entries.hasNext());
}
// See #189
@Test
public void testURISlashPath() {
URI uri = URI.create("http://localhost:8080/?param1=value1&param2=value2&param3=value3");
QueryStringDecoder decoder = new QueryStringDecoder(uri);
Assert.assertEquals("/", decoder.getPath());
Map<String, List<String>> params = decoder.getParameters();
Assert.assertEquals(3, params.size());
Iterator<Entry<String, List<String>>> entries = params.entrySet().iterator();
Entry<String, List<String>> entry = entries.next();
Assert.assertEquals("param1", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value1", entry.getValue().get(0));
entry = entries.next();
Assert.assertEquals("param2", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value2", entry.getValue().get(0));
entry = entries.next();
Assert.assertEquals("param3", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value3", entry.getValue().get(0));
Assert.assertFalse(entries.hasNext());
}
// See #189
@Test
public void testURINoPath() {
URI uri = URI.create("http://localhost:8080?param1=value1&param2=value2&param3=value3");
QueryStringDecoder decoder = new QueryStringDecoder(uri);
Assert.assertEquals("", decoder.getPath());
Map<String, List<String>> params = decoder.getParameters();
Assert.assertEquals(3, params.size());
Iterator<Entry<String, List<String>>> entries = params.entrySet().iterator();
Entry<String, List<String>> entry = entries.next();
Assert.assertEquals("param1", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value1", entry.getValue().get(0));
entry = entries.next();
Assert.assertEquals("param2", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value2", entry.getValue().get(0));
entry = entries.next();
Assert.assertEquals("param3", entry.getKey());
Assert.assertEquals(1, entry.getValue().size());
Assert.assertEquals("value3", entry.getValue().get(0));
Assert.assertFalse(entries.hasNext());
}
} }