StringUtil.csvSplit(String)

Conflicts:
	jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractJettyMojo.java
	jetty-security/src/main/java/org/eclipse/jetty/security/PropertyUserStore.java
	jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java
	jetty-servlets/src/main/java/org/eclipse/jetty/servlets/PushCacheFilter.java
	jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java
	jetty-xml/src/main/java/org/eclipse/jetty/xml/XmlConfiguration.java
This commit is contained in:
Greg Wilkins 2015-06-19 16:48:53 +10:00 committed by Joakim Erdfelt
parent e6f23a692e
commit 3527c6a71b
19 changed files with 262 additions and 31 deletions

View File

@ -46,6 +46,7 @@ import org.eclipse.jetty.annotations.AnnotationParser.Handler;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.util.ConcurrentHashSet;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -274,7 +275,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
{
_ordering = ordering;
String[] tmp = ordering.split(",");
String[] tmp = StringUtil.csvSplit(ordering);
for (int i=0; i<tmp.length; i++)
{

View File

@ -38,6 +38,7 @@ import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.TypeUtil;
/**
@ -100,7 +101,7 @@ public class DigestAuthentication implements Authentication
String clientQOP = null;
if (serverQOP != null)
{
List<String> serverQOPValues = Arrays.asList(serverQOP.split(","));
List<String> serverQOPValues = StringUtil.csvSplit(null,serverQOP,0,serverQOP.length());
if (serverQOPValues.contains("auth"))
clientQOP = "auth";
else if (serverQOPValues.contains("auth-int"))

View File

@ -633,7 +633,7 @@ public class HttpGenerator
if (values[0]==null)
{
split = field.getValue().split("\\s*,\\s*");
split = StringUtil.csvSplit(field.getValue());
if (split.length>0)
{
values=new HttpHeaderValue[split.length];

View File

@ -47,6 +47,7 @@ import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;
@ -804,7 +805,7 @@ public abstract class AbstractJettyMojo extends AbstractMojo
}
else
{
String[] files = this.jettyXml.split(",");
String[] files = StringUtil.csvSplit(this.jettyXml);
for ( String file : files )
{

View File

@ -39,6 +39,7 @@ import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.FilterMapping;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -252,7 +253,7 @@ public class JettyWebAppContext extends WebAppContext
List<String> resources = new ArrayList<String>();
for (String rl:resourceBases)
{
String[] rs = rl.split(" *, *");
String[] rs = StringUtil.csvSplit(rl);
for (String r:rs)
resources.add(r);
}

View File

@ -25,6 +25,7 @@ import java.util.Iterator;
import java.util.List;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.jetty.util.StringUtil;
/**
* OverlayConfig
@ -48,7 +49,7 @@ public class OverlayConfig
{
if (fmt == null)
return;
String[] atoms = fmt.split(",");
String[] atoms = StringUtil.csvSplit(fmt);
for (int i=0;i<atoms.length;i++)
{
String s = atoms[i].trim();

View File

@ -35,6 +35,7 @@ import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.ShutdownMonitor;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
@ -85,7 +86,7 @@ public class Starter
{
if (csv != null && !"".equals(csv))
{
String[] atoms = csv.split(",");
String[] atoms = StringUtil.csvSplit(csv);
if (atoms.length >= 3)
{
gid = atoms[0].trim();
@ -226,7 +227,7 @@ public class Starter
str = (String)props.getProperty("base.dirs");
if (str != null && !"".equals(str.trim()))
{
ResourceCollection bases = new ResourceCollection(str.split(","));
ResourceCollection bases = new ResourceCollection(StringUtil.csvSplit(str));
webApp.setWar(null);
webApp.setBaseResource(bases);
}
@ -235,7 +236,7 @@ public class Starter
str = (String)props.get("base.dirs.orig");
if (str != null && !"".equals(str.trim()))
{
ResourceCollection bases = new ResourceCollection(str.split(","));
ResourceCollection bases = new ResourceCollection(StringUtil.csvSplit(str));
webApp.setAttribute ("org.eclipse.jetty.resources.originalBases", bases);
}
@ -331,7 +332,7 @@ public class Starter
if (str != null && !"".equals(str.trim()))
{
List<File> jars = new ArrayList<File>();
String[] names = str.split(",");
String[] names = StringUtil.csvSplit(str);
for (int j=0; names != null && j < names.length; j++)
jars.add(new File(names[j].trim()));
webApp.setWebInfLib(jars);
@ -360,7 +361,7 @@ public class Starter
if ("--jetty-xml".equals(args[i]))
{
jettyXmls = new ArrayList<File>();
String[] names = args[++i].split(",");
String[] names = StringUtil.csvSplit(args[++i]);
for (int j=0; names!= null && j < names.length; j++)
{
jettyXmls.add(new File(names[j].trim()));
@ -489,7 +490,7 @@ public class Starter
{
if (csv == null || "".equals(csv.trim()))
return null;
String[] atoms = csv.split(",");
String[] atoms = StringUtil.csvSplit(csv);
List<String> list = new ArrayList<String>();
for (String a:atoms)
{

View File

@ -28,6 +28,7 @@ import java.util.List;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.jetty.util.StringUtil;
@ -105,7 +106,7 @@ public class WarPluginInfo
if (node == null)
return null;
String val = node.getValue();
_dependentMavenWarIncludes = Arrays.asList(val.split(","));
_dependentMavenWarIncludes = StringUtil.csvSplit(null,val,0,val.length());
}
return _dependentMavenWarIncludes;
}
@ -134,7 +135,7 @@ public class WarPluginInfo
if (node == null)
return null;
String val = node.getValue();
_dependentMavenWarExcludes = Arrays.asList(val.split(","));
_dependentMavenWarExcludes = StringUtil.csvSplit(null,val,0,val.length());
}
return _dependentMavenWarExcludes;
}

View File

@ -113,7 +113,7 @@ public class Main
String argValue = matcher.group(2);
if (argValue.startsWith("host,"))
{
String[] typeAndSuffix = argValue.split(",");
String[] typeAndSuffix = StringUtil.split(argValue);
if (typeAndSuffix.length != 2)
throw new IllegalArgumentException("Invalid option " + arg + ", must be of the form --" + RETRIEVER_ARG + "=host,suffix");

View File

@ -38,6 +38,7 @@ import org.eclipse.jetty.security.MappedLoginService.RolePrincipal;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.Scanner.BulkListener;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -153,7 +154,7 @@ public class PropertyUserStore extends AbstractLifeCycle
String[] roleArray = IdentityService.NO_ROLES;
if (roles != null && roles.length() > 0)
{
roleArray = roles.split(",");
roleArray = StringUtil.csvSplit(roles);
}
known.add(username);
Credential credential = Credential.getCredential(credentials);

View File

@ -1187,10 +1187,9 @@ public class Response implements HttpServletResponse
String connection = _channel.getRequest().getHttpFields().getStringField(HttpHeader.CONNECTION);
if (connection != null)
{
String[] values = connection.split(",");
for (int i = 0; values != null && i < values.length; i++)
for (String value: StringUtil.csvSplit(null,connection,0,connection.length()))
{
HttpHeaderValue cb = HttpHeaderValue.CACHE.get(values[0].trim());
HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
if (cb != null)
{

View File

@ -35,6 +35,7 @@ import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -136,7 +137,7 @@ public class CrossOriginFilter implements Filter
String allowedOriginsConfig = config.getInitParameter(ALLOWED_ORIGINS_PARAM);
if (allowedOriginsConfig == null)
allowedOriginsConfig = "*";
String[] allowedOrigins = allowedOriginsConfig.split(",");
String[] allowedOrigins = StringUtil.csvSplit(allowedOriginsConfig);
for (String allowedOrigin : allowedOrigins)
{
allowedOrigin = allowedOrigin.trim();
@ -159,7 +160,7 @@ public class CrossOriginFilter implements Filter
if (allowedMethodsConfig == null)
allowedMethods.addAll(DEFAULT_ALLOWED_METHODS);
else
allowedMethods.addAll(Arrays.asList(allowedMethodsConfig.split(",")));
allowedMethods.addAll(Arrays.asList(StringUtil.csvSplit(allowedMethodsConfig)));
String allowedHeadersConfig = config.getInitParameter(ALLOWED_HEADERS_PARAM);
if (allowedHeadersConfig == null)
@ -167,7 +168,7 @@ public class CrossOriginFilter implements Filter
else if ("*".equals(allowedHeadersConfig))
anyHeadersAllowed = true;
else
allowedHeaders.addAll(Arrays.asList(allowedHeadersConfig.split(",")));
allowedHeaders.addAll(Arrays.asList(StringUtil.csvSplit(allowedHeadersConfig)));
String preflightMaxAgeConfig = config.getInitParameter(PREFLIGHT_MAX_AGE_PARAM);
if (preflightMaxAgeConfig == null)
@ -189,7 +190,7 @@ public class CrossOriginFilter implements Filter
String exposedHeadersConfig = config.getInitParameter(EXPOSED_HEADERS_PARAM);
if (exposedHeadersConfig == null)
exposedHeadersConfig = "";
exposedHeaders.addAll(Arrays.asList(exposedHeadersConfig.split(",")));
exposedHeaders.addAll(Arrays.asList(StringUtil.csvSplit(exposedHeadersConfig)));
String chainPreflightConfig = config.getInitParameter(OLD_CHAIN_PREFLIGHT_PARAM);
if (chainPreflightConfig != null)
@ -400,7 +401,7 @@ public class CrossOriginFilter implements Filter
return Collections.emptyList();
List<String> requestedHeaders = new ArrayList<String>();
String[] headers = accessControlRequestHeaders.split(",");
String[] headers = StringUtil.csvSplit(accessControlRequestHeaders);
for (String header : headers)
{
String h = header.trim();

View File

@ -52,6 +52,7 @@ import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.ManagedOperation;
@ -1036,7 +1037,7 @@ public class DoSFilter implements Filter
public void setWhitelist(String commaSeparatedList)
{
List<String> result = new ArrayList<>();
for (String address : commaSeparatedList.split(","))
for (String address : StringUtil.csvSplit(commaSeparatedList))
addWhitelistAddress(result, address);
clearWhitelist();
_whitelist.addAll(result);

View File

@ -44,6 +44,7 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
/**
@ -331,7 +332,7 @@ public class PutFilter implements Filter
if ("Allow".equalsIgnoreCase(name))
{
Set<String> options = new HashSet<String>();
options.addAll(Arrays.asList(value.split(" *, *")));
options.addAll(Arrays.asList(StringUtil.csvSplit(value)));
options.addAll(_operations);
value=null;
for (String o : options)

View File

@ -41,6 +41,7 @@ import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletTester;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.StringUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -266,8 +267,8 @@ public class PutFilterTest
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
Set<String> options = new HashSet<String>();
options.addAll(Arrays.asList(response.get("Allow").split(" *, *")));
String allow=response.get("Allow");
options.addAll(StringUtil.csvSplit(null,allow,0,allow.length()));
assertTrue(options.contains("GET"));
assertTrue(options.contains("POST"));
assertTrue(options.contains("PUT"));

View File

@ -21,6 +21,8 @@ package org.eclipse.jetty.util;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -721,6 +723,11 @@ public class StringUtil
return str.substring(0,maxSize);
}
/**
* Parse the string representation of a list using {@link #csvSplit(List,String,int,int)}
* @param s The string to parse, expected to be enclosed as '[...]'
* @return An array of parsed values.
*/
public static String[] arrayFromString(String s)
{
if (s==null)
@ -731,7 +738,181 @@ public class StringUtil
if (s.length()==2)
return new String[]{};
return s.substring(1,s.length()-1).split(" *, *");
return csvSplit(s,1,s.length()-2);
}
/**
* Parse a CSV string using {@link #csvSplit(List,String, int, int)}
* @param s The string to parse
* @return An array of parsed values.
*/
public static String[] csvSplit(String s)
{
if (s==null)
return null;
return csvSplit(s,0,s.length());
}
/**
* Parse a CSV string using {@link #csvSplit(List,String, int, int)}
* @param s The string to parse
* @param off The offset into the string to start parsing
* @param len The len in characters to parse
* @return An array of parsed values.
*/
public static String[] csvSplit(String s, int off,int len)
{
if (s==null)
return null;
if (off<0 || len<0 || off>s.length())
throw new IllegalArgumentException();
List<String> list = new ArrayList<>();
csvSplit(list,s,off,len);
return list.toArray(new String[list.size()]);
}
enum CsvSplitState { PRE_DATA, QUOTE, SLOSH, DATA, WHITE, POST_DATA };
/** Split a quoted comma separated string to a list
* <p>Handle <a href="https://www.ietf.org/rfc/rfc4180.txt">rfc4180</a>-like
* CSV strings, with the exceptions:<ul>
* <li>quoted values may contain double quotes escaped with back-slash
* <li>Non-quoted values are trimmed of leading trailing white space
* <li>trailing commas are ignored
* <li>double commas result in a empty string value
* </ul>
* @param list The Collection to split to (or null to get a new list)
* @param s The string to parse
* @param off The offset into the string to start parsing
* @param len The len in characters to parse
* @return list containing the parsed list values
*/
public static List<String> csvSplit(List<String> list,String s, int off,int len)
{
if (list==null)
list=new ArrayList<>();
CsvSplitState state = CsvSplitState.PRE_DATA;
StringBuilder out = new StringBuilder();
int last=-1;
while (len>0)
{
char ch = s.charAt(off++);
len--;
switch(state)
{
case PRE_DATA:
if (Character.isWhitespace(ch))
continue;
if ('"'==ch)
{
state=CsvSplitState.QUOTE;
continue;
}
if (','==ch)
{
list.add("");
continue;
}
state=CsvSplitState.DATA;
out.append(ch);
continue;
case DATA:
if (Character.isWhitespace(ch))
{
last=out.length();
out.append(ch);
state=CsvSplitState.WHITE;
continue;
}
if (','==ch)
{
list.add(out.toString());
out.setLength(0);
state=CsvSplitState.PRE_DATA;
continue;
}
out.append(ch);
continue;
case WHITE:
if (Character.isWhitespace(ch))
{
out.append(ch);
continue;
}
if (','==ch)
{
out.setLength(last);
list.add(out.toString());
out.setLength(0);
state=CsvSplitState.PRE_DATA;
continue;
}
state=CsvSplitState.DATA;
out.append(ch);
last=-1;
continue;
case QUOTE:
if ('\\'==ch)
{
state=CsvSplitState.SLOSH;
continue;
}
if ('"'==ch)
{
list.add(out.toString());
out.setLength(0);
state=CsvSplitState.POST_DATA;
continue;
}
out.append(ch);
continue;
case SLOSH:
out.append(ch);
state=CsvSplitState.QUOTE;
continue;
case POST_DATA:
if (','==ch)
{
state=CsvSplitState.PRE_DATA;
continue;
}
continue;
}
}
switch(state)
{
case PRE_DATA:
case POST_DATA:
break;
case DATA:
case QUOTE:
case SLOSH:
list.add(out.toString());
break;
case WHITE:
out.setLength(last);
list.add(out.toString());
break;
}
return list;
}
public static String sanitizeXmlString(String html)

View File

@ -18,12 +18,17 @@
package org.eclipse.jetty.util;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.emptyArray;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.nio.charset.StandardCharsets;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
@ -241,4 +246,32 @@ public class StringUtilTest
assertEquals("Hello &lt;Cruel&gt; World",StringUtil.sanitizeXmlString("Hello <Cruel> World"));
assertEquals("Hello ? World",StringUtil.sanitizeXmlString("Hello \u0000 World"));
}
@Test
public void testSplit()
{
assertThat(StringUtil.csvSplit(null),nullValue());
assertThat(StringUtil.csvSplit(null),nullValue());
assertThat(StringUtil.csvSplit(""),emptyArray());
assertThat(StringUtil.csvSplit(" \t\n"),emptyArray());
assertThat(StringUtil.csvSplit("aaa"),arrayContaining("aaa"));
assertThat(StringUtil.csvSplit(" \taaa\n"),arrayContaining("aaa"));
assertThat(StringUtil.csvSplit(" \ta\n"),arrayContaining("a"));
assertThat(StringUtil.csvSplit(" \t\u1234\n"),arrayContaining("\u1234"));
assertThat(StringUtil.csvSplit("aaa,bbb,ccc"),arrayContaining("aaa","bbb","ccc"));
assertThat(StringUtil.csvSplit("aaa,,ccc"),arrayContaining("aaa","","ccc"));
assertThat(StringUtil.csvSplit(",b b,"),arrayContaining("","b b"));
assertThat(StringUtil.csvSplit(",,bbb,,"),arrayContaining("","","bbb",""));
assertThat(StringUtil.csvSplit(" aaa, bbb, ccc"),arrayContaining("aaa","bbb","ccc"));
assertThat(StringUtil.csvSplit("aaa,\t,ccc"),arrayContaining("aaa","","ccc"));
assertThat(StringUtil.csvSplit(" , b b , "),arrayContaining("","b b"));
assertThat(StringUtil.csvSplit(" ,\n,bbb, , "),arrayContaining("","","bbb",""));
assertThat(StringUtil.csvSplit("\"aaa\", \" b,\\\"\",\"\""),arrayContaining("aaa"," b,\"",""));
}
}

View File

@ -31,11 +31,14 @@ import java.security.KeyStore;
import javax.net.ssl.SSLEngine;
import org.eclipse.jetty.toolchain.test.JDK;
import org.eclipse.jetty.toolchain.test.OS;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
@ -208,6 +211,9 @@ public class SslContextFactoryTest
@Test
public void testSetIncludeCipherSuitesRegex() throws Exception
{
// Test does not work on JDK 8+ (RC4 is disabled)
Assume.assumeFalse(JDK.IS_8);
cf.setIncludeCipherSuites(".*RC4.*");
cf.start();
SSLEngine sslEngine = cf.newSSLEngine();

View File

@ -132,7 +132,7 @@ public class JsrBrowserSocket
}
case "many":
{
String parts[] = val.split(",");
String parts[] = StringUtil.csvSplit(val);
int size = Integer.parseInt(parts[0]);
int count = Integer.parseInt(parts[1]);
@ -141,7 +141,7 @@ public class JsrBrowserSocket
}
case "manythreads":
{
String parts[] = val.split(",");
String parts[] = StringUtil.csvSplit(val);
int threadCount = Integer.parseInt(parts[0]);
int size = Integer.parseInt(parts[1]);
int count = Integer.parseInt(parts[2]);