fix queryString parsing to handle UTF8 (percent encoded): SOLR-202

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@524524 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2007-04-01 03:43:42 +00:00
parent 6b58a8536b
commit 55ee8f2305
2 changed files with 33 additions and 19 deletions

View File

@ -17,7 +17,6 @@
package org.apache.solr.servlet; package org.apache.solr.servlet;
import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -25,20 +24,13 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.solr.core.Config;
import org.apache.solr.core.SolrConfig; import org.apache.solr.core.SolrConfig;
import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrCore;
import org.apache.solr.util.ContentStream;
import org.apache.solr.request.MapSolrParams;
import org.apache.solr.request.MultiMapSolrParams; import org.apache.solr.request.MultiMapSolrParams;
import org.apache.solr.request.SolrParams; import org.apache.solr.request.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.util.AbstractSolrTestCase; import org.apache.solr.util.AbstractSolrTestCase;
import org.apache.solr.util.ContentStream;
import junit.framework.TestCase;
public class SolrRequestParserTest extends AbstractSolrTestCase { public class SolrRequestParserTest extends AbstractSolrTestCase {
@ -119,4 +111,20 @@ public class SolrRequestParserTest extends AbstractSolrTestCase {
assertEquals( 1, streams.size() ); assertEquals( 1, streams.size() );
assertEquals( txt, IOUtils.toString( streams.get(0).getStream() ) ); assertEquals( txt, IOUtils.toString( streams.get(0).getStream() ) );
} }
public void testUrlParamParsing()
{
String[][] teststr = new String[][] {
{ "this is simple", "this%20is%20simple" },
{ "this is simple", "this+is+simple" },
{ "\u00FC", "%C3%BC" }, // lower-case "u" with diaeresis/umlaut
{ "\u0026", "%26" }, // &
{ "\u20AC", "%E2%82%AC" } // euro
};
for( String[] tst : teststr ) {
MultiMapSolrParams params = SolrRequestParsers.parseQueryString( "val="+tst[1] );
assertEquals( tst[0], params.get( "val" ) );
}
}
} }

View File

@ -21,6 +21,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.ArrayList; import java.util.ArrayList;
@ -178,18 +179,23 @@ public class SolrRequestParsers
{ {
Map<String,String[]> map = new HashMap<String, String[]>(); Map<String,String[]> map = new HashMap<String, String[]>();
if( queryString != null && queryString.length() > 0 ) { if( queryString != null && queryString.length() > 0 ) {
for( String kv : queryString.split( "&" ) ) { try {
int idx = kv.indexOf( '=' ); for( String kv : queryString.split( "&" ) ) {
if( idx > 0 ) { int idx = kv.indexOf( '=' );
String name = URLDecoder.decode( kv.substring( 0, idx )); if( idx > 0 ) {
String value = URLDecoder.decode( kv.substring( idx+1 )); String name = URLDecoder.decode( kv.substring( 0, idx ), "UTF-8");
MultiMapSolrParams.addParam( name, value, map ); String value = URLDecoder.decode( kv.substring( idx+1 ), "UTF-8");
} MultiMapSolrParams.addParam( name, value, map );
else { }
String name = URLDecoder.decode( kv ); else {
MultiMapSolrParams.addParam( name, "", map ); String name = URLDecoder.decode( kv, "UTF-8" );
MultiMapSolrParams.addParam( name, "", map );
}
} }
} }
catch( UnsupportedEncodingException uex ) {
throw new SolrException( 500, uex );
}
} }
return new MultiMapSolrParams( map ); return new MultiMapSolrParams( map );
} }