package org.glassfish.grizzly.http.util;
import java.io.CharConversionException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.localization.LogMessages;
public final class Parameters {
private final static Logger LOGGER = Grizzly.logger(Parameters.class);
private final LinkedHashMap<String, ArrayList<String>> paramHashValues =
new LinkedHashMap<String, ArrayList<String>>();
private boolean didQueryParameters = false;
private boolean didMerge = false;
MimeHeaders ;
DataChunk queryDC;
final DataChunk decodedQuery = DataChunk.newInstance();
public static final int INITIAL_SIZE = 4;
private Parameters child = null;
private Parameters parent = null;
private Parameters currentChild = null;
Charset encoding = null;
Charset queryStringEncoding = null;
private int limit = -1;
private int parameterCount = 0;
public void setQuery(final DataChunk queryBC) {
this.queryDC = queryBC;
}
public void (final MimeHeaders headers) {
this.headers = headers;
}
public void setLimit(int limit) {
this.limit = limit;
}
public void setEncoding(final Charset encoding) {
this.encoding = encoding;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Set encoding to {0}", encoding);
}
}
public Charset getEncoding() {
return encoding;
}
public void setQueryStringEncoding(final Charset queryStringEncoding) {
this.queryStringEncoding = queryStringEncoding;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Set query string encoding to {0}",
queryStringEncoding);
}
}
public Charset getQueryStringEncoding() {
return queryStringEncoding;
}
public void recycle() {
paramHashValues.clear();
didQueryParameters = false;
currentChild = null;
didMerge = false;
encoding = null;
queryStringEncoding = null;
parameterCount = 0;
decodedQuery.recycle();
}
public Parameters getCurrentSet() {
if (currentChild == null) {
return this;
}
return currentChild;
}
public void push() {
if (currentChild == null) {
currentChild = new Parameters();
currentChild.parent = this;
return;
}
if (currentChild.child == null) {
currentChild.child = new Parameters();
currentChild.child.parent = currentChild;
}
currentChild = currentChild.child;
currentChild.setEncoding(encoding);
}
public void pop() {
if (currentChild == null) {
throw new RuntimeException("Attempt to pop without a push");
}
currentChild.recycle();
currentChild = currentChild.parent;
}
public void addParameterValues(String key, String[] newValues) {
if (key == null) {
return;
}
ArrayList<String> values;
if (paramHashValues.containsKey(key)) {
values = paramHashValues.get(key);
} else {
values = new ArrayList<String>(1);
paramHashValues.put(key, values);
}
values.ensureCapacity(values.size() + newValues.length);
Collections.addAll(values, newValues);
}
public String[] getParameterValues(String name) {
handleQueryParameters();
final ArrayList<String> values;
if (currentChild != null) {
currentChild.merge();
values = currentChild.paramHashValues.get(name);
} else {
values = paramHashValues.get(name);
}
return ((values != null) ? values.toArray(new String[values.size()]) : null);
}
public Set<String> getParameterNames() {
handleQueryParameters();
if (currentChild != null) {
currentChild.merge();
currentChild.paramHashValues.keySet();
}
return paramHashValues.keySet();
}
private void merge() {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Before merging {0} {1} {2}",
new Object[]{this, parent, didMerge});
LOGGER.log(Level.FINEST, paramsAsString());
}
handleQueryParameters();
if (didMerge) {
return;
}
if (parent == null) {
return;
}
parent.merge();
LinkedHashMap<String, ArrayList<String>> parentProps = parent.paramHashValues;
merge2(paramHashValues, parentProps);
didMerge = true;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "After {0}", paramsAsString());
}
}
public String getParameter(final String name) {
ArrayList<String> values = paramHashValues.get(name);
if (values != null) {
if (values.isEmpty()) {
return "";
}
return values.get(0);
} else {
return null;
}
}
public void handleQueryParameters() {
if (didQueryParameters) {
return;
}
didQueryParameters = true;
if (queryDC == null || queryDC.isNull()) {
return;
}
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Decoding query {0} {1}",
new Object[]{queryDC, queryStringEncoding});
}
decodedQuery.duplicate(queryDC);
processParameters(decodedQuery, queryStringEncoding);
}
private static void merge2(LinkedHashMap<String, ArrayList<String>> one,
LinkedHashMap<String, ArrayList<String>> two) {
for (String name : two.keySet()) {
ArrayList<String> oneValue = one.get(name);
ArrayList<String> twoValue = two.get(name);
ArrayList<String> combinedValue;
if (twoValue != null) {
if (oneValue == null) {
combinedValue = new ArrayList<String>(twoValue);
} else {
combinedValue = new ArrayList<String>(oneValue.size() +
twoValue.size());
combinedValue.addAll(oneValue);
combinedValue.addAll(twoValue);
}
one.put(name, combinedValue);
}
}
}
public void addParameter(String key, String value)
throws IllegalStateException {
if (key == null) {
return;
}
parameterCount++;
if (limit > -1 && parameterCount > limit) {
throw new IllegalStateException(
);
}
ArrayList<String> values = paramHashValues.get(key);
if (values == null) {
values = new ArrayList<String>(1);
paramHashValues.put(key, values);
}
values.add(value);
}
final BufferChunk tmpName = new BufferChunk();
final BufferChunk tmpValue = new BufferChunk();
private final BufferChunk origName = new BufferChunk();
private final BufferChunk origValue = new BufferChunk();
final CharChunk tmpNameC = new CharChunk(1024);
final CharChunk tmpValueC = new CharChunk(1024);
public static final String DEFAULT_ENCODING = Constants.DEFAULT_HTTP_CHARACTER_ENCODING;
public static final Charset DEFAULT_CHARSET = Constants.DEFAULT_HTTP_CHARSET;
public void processParameters(final Buffer buffer, final int start, final int len) {
processParameters(buffer, start, len, encoding);
}
public void processParameters(final Buffer buffer, final int start, final int len,
final Charset enc) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"Process parameters. Buffer: {0} start={1} len={2} content={3}",
new Object[]{
buffer,
start,
len,
buffer.toStringContent(enc, start, start + len)
});
}
int decodeFailCount = 0;
int end = start + len;
int pos = start;
while (pos < end) {
if (limit > -1 && parameterCount >= limit) {
LOGGER.warning(LogMessages.WARNING_GRIZZLY_HTTP_SEVERE_GRIZZLY_HTTP_PARAMETERS_MAX_COUNT_FAIL(limit));
break;
}
int nameStart = pos;
int nameEnd = -1;
int valueStart = -1;
int valueEnd = -1;
boolean parsingName = true;
boolean decodeName = false;
boolean decodeValue = false;
boolean parameterComplete = false;
do {
switch (buffer.get(pos)) {
case '=':
if (parsingName) {
nameEnd = pos;
parsingName = false;
valueStart = ++pos;
} else {
pos++;
}
break;
case '&':
if (parsingName) {
nameEnd = pos;
} else {
valueEnd = pos;
}
parameterComplete = true;
pos++;
break;
case '+':
case '%':
if (parsingName) {
decodeName = true;
} else {
decodeValue = true;
}
pos++;
break;
default:
pos++;
break;
}
} while (!parameterComplete && pos < end);
if (pos == end) {
if (nameEnd == -1) {
nameEnd = pos;
} else if (valueStart > -1 && valueEnd == -1) {
valueEnd = pos;
}
}
if (LOGGER.isLoggable(Level.FINEST) && valueStart == -1) {
LOGGER.log(Level.FINEST,
LogMessages.FINE_GRIZZLY_HTTP_PARAMETERS_NOEQUAL(
nameStart,
nameEnd,
buffer.toStringContent(DEFAULT_CHARSET,
nameStart, nameEnd)));
}
if (nameEnd <= nameStart) {
if (LOGGER.isLoggable(Level.INFO)) {
String extract;
if (valueEnd < nameStart) {
LOGGER.info(LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_INVALID_CHUNK(
nameStart,
nameEnd,
null));
}
}
continue;
}
tmpName.setBufferChunk(buffer, nameStart, nameEnd);
tmpValue.setBufferChunk(buffer, valueStart, valueEnd);
if (LOGGER.isLoggable(Level.FINEST)) {
origName.setBufferChunk(buffer, nameStart, nameEnd);
origValue.setBufferChunk(buffer, valueStart, valueEnd);
}
try {
String name;
String value;
if (decodeName) {
name = urlDecode(tmpName, enc);
} else {
name = tmpName.toString(enc);
}
if (valueStart != -1) {
if (decodeValue) {
value = urlDecode(tmpValue, enc);
} else {
value = tmpValue.toString(enc);
}
} else {
value = "";
}
addParameter(name, value);
} catch (Exception e) {
decodeFailCount++;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
LogMessages.FINE_GRIZZLY_HTTP_PARAMETERS_DECODE_FAIL_DEBUG(
origName.toString(), origValue.toString()));
} else if (LOGGER.isLoggable(Level.INFO) && decodeFailCount == 1) {
final String name = ((tmpName.getLength() > 0)
? tmpName.toString()
: "unavailable");
final String value = ((tmpValue.getLength() > 0)
? tmpValue.toString()
: "unavailable");
LOGGER.log(Level.INFO,
LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_DECODE_FAIL_INFO(
e.getMessage(), name, value));
LOGGER.log(Level.FINE, "Decoding stacktrace.", e);
}
} finally {
tmpName.recycle();
tmpValue.recycle();
}
}
if (!LOGGER.isLoggable(Level.FINEST) && decodeFailCount > 1) {
LOGGER.info(LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_MULTIPLE_DECODING_FAIL(decodeFailCount));
}
}
private String urlDecode(final BufferChunk bc, final Charset enc)
throws IOException {
URLDecoder.decode(bc, true);
String result;
if (enc != null) {
if (bc.getStart() == -1 && bc.getEnd() == -1) {
return "";
}
result = bc.toString(enc);
} else {
final CharChunk cc = tmpNameC;
final int length = bc.getLength();
cc.allocate(length, -1);
final Buffer bbuf = bc.getBuffer();
final char[] cbuf = cc.getBuffer();
final int start = bc.getStart();
for (int i = 0; i < length; i++) {
cbuf[i] = (char) (bbuf.get(i + start) & 0xff);
}
cc.setChars(cbuf, 0, length);
result = cc.toString();
cc.recycle();
}
return result;
}
public void processParameters(char chars[], int start, int len) {
int end = start + len;
int pos = start;
int decodeFailCount = 0;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"Process parameters. chars: {0} start={1} len={2} content={3}",
new Object[]{
chars,
start,
len,
new String(chars, start, len)
});
}
do {
if (limit > -1 && parameterCount >= limit) {
LOGGER.warning(
LogMessages.WARNING_GRIZZLY_HTTP_SEVERE_GRIZZLY_HTTP_PARAMETERS_MAX_COUNT_FAIL(
limit));
break;
}
boolean noEq = false;
int nameStart = pos;
int valStart = -1;
int valEnd = -1;
int nameEnd = CharChunk.indexOf(chars, nameStart, end, '=');
int nameEnd2 = CharChunk.indexOf(chars, nameStart, end, '&');
if ((nameEnd2 != -1)
&& (nameEnd == -1 || nameEnd > nameEnd2)) {
nameEnd = nameEnd2;
noEq = true;
valStart = nameEnd;
valEnd = nameEnd;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "no equal {0} {1} {2}",
new Object[]{
nameStart,
nameEnd,
new String(chars,
nameStart,
nameEnd - nameStart)
});
}
}
if (nameEnd == -1) {
nameEnd = end;
}
if (!noEq) {
valStart = (nameEnd < end) ? nameEnd + 1 : end;
valEnd = CharChunk.indexOf(chars, valStart, end, '&');
if (valEnd == -1) {
valEnd = (valStart < end) ? end : valStart;
}
}
pos = valEnd + 1;
if (nameEnd <= nameStart) {
continue;
}
try {
tmpNameC.append(chars, nameStart, nameEnd - nameStart);
tmpValueC.append(chars, valStart, valEnd - valStart);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "{0}= {1}",
new Object[]{tmpNameC, tmpValueC});
}
URLDecoder.decode(tmpNameC, tmpNameC, true,
queryStringEncoding.name());
URLDecoder.decode(tmpValueC, tmpValueC, true,
queryStringEncoding.name());
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "{0}= {1}",
new Object[]{tmpNameC, tmpValueC});
}
addParameter(tmpNameC.toString(), tmpValueC.toString());
} catch (Exception e) {
decodeFailCount++;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
LogMessages.FINE_GRIZZLY_HTTP_PARAMETERS_DECODE_FAIL_DEBUG(
origName.toString(),
origValue.toString()));
} else if (LOGGER.isLoggable(
Level.INFO) && decodeFailCount == 1) {
final String name = ((tmpNameC.getLength() > 0)
? tmpNameC.toString()
: "unavailable");
final String value = ((tmpValueC.getLength() > 0)
? tmpValueC.toString()
: "unavailable");
LOGGER.log(Level.INFO,
LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_DECODE_FAIL_INFO(
e.getMessage(), name, value));
LOGGER.log(Level.FINE, "Decoding stacktrace.", e);
}
} finally {
tmpNameC.recycle();
tmpValueC.recycle();
}
} while (pos < end);
if (!LOGGER.isLoggable(Level.FINEST) && decodeFailCount > 1) {
LOGGER.info(
LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_MULTIPLE_DECODING_FAIL(
decodeFailCount));
}
}
public void processParameters(final DataChunk data) {
processParameters(data, encoding);
}
public void processParameters(final DataChunk data, final Charset encoding) {
if (data == null || data.isNull() || data.getLength() <= 0) {
return;
}
try {
if (data.getType() == DataChunk.Type.Buffer) {
final BufferChunk bc = data.getBufferChunk();
processParameters(bc.getBuffer(), bc.getStart(),
bc.getLength(), encoding);
} else {
if (data.getType() != DataChunk.Type.Chars) {
data.toChars(encoding);
}
final CharChunk cc = data.getCharChunk();
processParameters(cc.getChars(), cc.getStart(),
cc.getLength());
}
} catch (CharConversionException e) {
throw new IllegalStateException(e);
}
}
public String paramsAsString() {
StringBuilder sb = new StringBuilder();
for (final String s : paramHashValues.keySet()) {
sb.append(s).append('=');
ArrayList<String> v = paramHashValues.get(s);
for (int i = 0, len = v.size(); i < len; i++) {
sb.append(v.get(i)).append(',');
}
sb.append('\n');
}
return sb.toString();
}
public void processParameters(String str) {
int end = str.length();
int pos = 0;
int decodeFailCount = 0;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
"Process parameters. String: {0}",
str);
}
do {
if (limit > -1 && parameterCount >= limit) {
LOGGER.warning(
LogMessages.WARNING_GRIZZLY_HTTP_SEVERE_GRIZZLY_HTTP_PARAMETERS_MAX_COUNT_FAIL(
limit));
break;
}
boolean noEq = false;
int valStart = -1;
int valEnd = -1;
int nameStart = pos;
int nameEnd = str.indexOf('=', nameStart);
int nameEnd2 = str.indexOf('&', nameStart);
if (nameEnd2 == -1) {
nameEnd2 = end;
}
if ((nameEnd2 != -1) &&
(nameEnd == -1 || nameEnd > nameEnd2)) {
nameEnd = nameEnd2;
noEq = true;
valStart = nameEnd;
valEnd = nameEnd;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "no equal {0} {1} {2}",
new Object[]{
nameStart,
nameEnd,
str.substring(nameStart, nameEnd)
});
}
}
if (nameEnd == -1) {
nameEnd = end;
}
if (!noEq) {
valStart = nameEnd + 1;
valEnd = str.indexOf('&', valStart);
if (valEnd == -1) {
valEnd = (valStart < end) ? end : valStart;
}
}
pos = valEnd + 1;
if (nameEnd <= nameStart) {
continue;
}
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "XXX {0} {1} {2} {3}",
new Object[]{nameStart, nameEnd, valStart, valEnd});
}
try {
tmpNameC.append(str, nameStart, nameEnd - nameStart);
tmpValueC.append(str, valStart, valEnd - valStart);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "{0}= {1}",
new Object[]{tmpNameC, tmpValueC});
}
URLDecoder.decode(tmpNameC, true);
URLDecoder.decode(tmpValueC, true);
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "{0}= {1}",
new Object[]{tmpNameC, tmpValueC});
}
addParameter(tmpNameC.toString(), tmpValueC.toString());
} catch (Exception e) {
decodeFailCount++;
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST,
LogMessages.FINE_GRIZZLY_HTTP_PARAMETERS_DECODE_FAIL_DEBUG(
origName.toString(),
origValue.toString()));
} else if (LOGGER.isLoggable(
Level.INFO) && decodeFailCount == 1) {
final String name = ((tmpNameC.getLength() > 0)
? tmpNameC.toString()
: "unavailable");
final String value = ((tmpValueC.getLength() > 0)
? tmpValueC.toString()
: "unavailable");
LOGGER.log(Level.INFO,
LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_DECODE_FAIL_INFO(
e.getMessage(), name, value));
LOGGER.log(Level.FINE, "Decoding stacktrace.", e);
}
} finally {
tmpNameC.recycle();
tmpValueC.recycle();
}
} while (pos < end);
if (!LOGGER.isLoggable(Level.FINEST) && decodeFailCount > 1) {
LOGGER.info(
LogMessages.INFO_GRIZZLY_HTTP_PARAMETERS_MULTIPLE_DECODING_FAIL(
decodeFailCount));
}
}
}