From 55ee8f2305e688d39952fb33d8d91b9065e2ad6e Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Sun, 1 Apr 2007 03:43:42 +0000 Subject: [PATCH] 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 --- .../solr/servlet/SolrRequestParserTest.java | 26 ++++++++++++------- .../solr/servlet/SolrRequestParsers.java | 26 ++++++++++++------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/test/org/apache/solr/servlet/SolrRequestParserTest.java b/src/test/org/apache/solr/servlet/SolrRequestParserTest.java index e10390632b3..99c12616573 100644 --- a/src/test/org/apache/solr/servlet/SolrRequestParserTest.java +++ b/src/test/org/apache/solr/servlet/SolrRequestParserTest.java @@ -17,7 +17,6 @@ package org.apache.solr.servlet; -import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; @@ -25,20 +24,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.servlet.http.HttpServletRequestWrapper; - import org.apache.commons.io.IOUtils; -import org.apache.solr.core.Config; import org.apache.solr.core.SolrConfig; 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.SolrParams; -import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.util.AbstractSolrTestCase; - -import junit.framework.TestCase; +import org.apache.solr.util.ContentStream; public class SolrRequestParserTest extends AbstractSolrTestCase { @@ -119,4 +111,20 @@ public class SolrRequestParserTest extends AbstractSolrTestCase { assertEquals( 1, streams.size() ); 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" ) ); + } + } } diff --git a/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java b/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java index ee3b79f69a7..0399f299731 100644 --- a/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java +++ b/src/webapp/src/org/apache/solr/servlet/SolrRequestParsers.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Reader; +import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; @@ -178,18 +179,23 @@ public class SolrRequestParsers { Map map = new HashMap(); if( queryString != null && queryString.length() > 0 ) { - for( String kv : queryString.split( "&" ) ) { - int idx = kv.indexOf( '=' ); - if( idx > 0 ) { - String name = URLDecoder.decode( kv.substring( 0, idx )); - String value = URLDecoder.decode( kv.substring( idx+1 )); - MultiMapSolrParams.addParam( name, value, map ); - } - else { - String name = URLDecoder.decode( kv ); - MultiMapSolrParams.addParam( name, "", map ); + try { + for( String kv : queryString.split( "&" ) ) { + int idx = kv.indexOf( '=' ); + if( idx > 0 ) { + String name = URLDecoder.decode( kv.substring( 0, idx ), "UTF-8"); + String value = URLDecoder.decode( kv.substring( idx+1 ), "UTF-8"); + MultiMapSolrParams.addParam( name, value, map ); + } + else { + String name = URLDecoder.decode( kv, "UTF-8" ); + MultiMapSolrParams.addParam( name, "", map ); + } } } + catch( UnsupportedEncodingException uex ) { + throw new SolrException( 500, uex ); + } } return new MultiMapSolrParams( map ); }