1

I have an http get request coming into my servlet:

/servlet?param1=val1&param2=val2&...&param5=val5

It's hitting the servlet as ApplicationHttpRequest (from Catalina library) object, and it has both queryParamString and queryString set to the query string above.

Then when I'm calling request.getParameter("param1") inside of it, it's calling parseParameters() which populates parameters object on the request.

Weird thing is that it populates it like so:

[
 0 => ["val1","val1"] , 
 1 => ["val2","val2"] , 
 2 => ["val3","val3"] , 
 3 => ["val4","val4"] , 
 4 => ["val5","val5"] 
]

This is happening within the classes provided by the tomcat 8 as I've stepped through them on my debugger (I've tried on version 8.0.33 and 8.0.41).

Anybody has any idea as to why this is happening? This doesn't seem like expected behavior at all.

Thank you!

PS. Select involved methods from Tomcat Application:

public String getParameter(String name) {

    parseParameters();

    String[] value = parameters.get(name);
    if (value == null) {
        return null;
    }
    return value[0];

}

void parseParameters() {

    if (parsedParams) {
        return;
    }

    parameters = new ParameterMap<>();
    parameters.putAll(getRequest().getParameterMap());
    mergeParameters();
    ((ParameterMap<String,String[]>) parameters).setLocked(true);
    parsedParams = true;
}

private void mergeParameters() {

    if ((queryParamString == null) || (queryParamString.length() < 1))
        return;

    // Parse the query string from the dispatch target
    Parameters paramParser = new Parameters();
    MessageBytes queryMB = MessageBytes.newInstance();
    queryMB.setString(queryParamString);

    String encoding = getCharacterEncoding();
    // No need to process null value, as ISO-8859-1 is the default encoding
    // in MessageBytes.toBytes().
    if (encoding != null) {
        try {
            queryMB.setCharset(B2CConverter.getCharset(encoding));
        } catch (UnsupportedEncodingException ignored) {
            // Fall-back to ISO-8859-1
        }
    }

    paramParser.setQuery(queryMB);
    paramParser.setQueryStringEncoding(encoding);
    paramParser.handleQueryParameters();

    // Insert the additional parameters from the dispatch target
    Enumeration<String> dispParamNames = paramParser.getParameterNames();
    while (dispParamNames.hasMoreElements()) {
        String dispParamName = dispParamNames.nextElement();
        String[] dispParamValues = paramParser.getParameterValues(dispParamName);
        String[] originalValues = parameters.get(dispParamName);
        if (originalValues == null) {
            parameters.put(dispParamName, dispParamValues);
            continue;
        }
        parameters.put(dispParamName, mergeValues(dispParamValues, originalValues));
    }
}

protected String[] mergeValues(Object values1, Object values2) {

    ArrayList<Object> results = new ArrayList<>();

    if (values1 == null) {
        // Skip - nothing to merge
    } else if (values1 instanceof String[]) {
        for (String value : (String[]) values1) {
            results.add(value);
        }
    } else { // String
        results.add(values1.toString());
    }

    if (values2 == null) {
        // Skip - nothing to merge
    } else if (values2 instanceof String[]) {
        for (String value : (String[]) values2) {
            results.add(value);
        }
    } else { // String
        results.add(values2.toString());
    }

    String values[] = new String[results.size()];
    return results.toArray(values);

}
  • Show your proof that there is doubling parameters please! – LHA Mar 07 '17 at 19:39
  • @Loc so on parseParameters() I am hitting mergeParameters(), which is using mergeValues(). In mergeValues() it has value1 and value2, which for param1 will be both value1. The result of mergeValues() is an array with 2 element - ["value1","value1"] for param1. Likewise for all other elements it's doing the same. This is bubbling up to the ApplicationHttpRequest object. I'm looking for a more detailed explanation for this myself at the moment, any advice is appreciated. – Tim Zhukov-Khovanskiy Mar 07 '17 at 19:52
  • What I mean is please show some codes that show the issue. – LHA Mar 07 '17 at 19:55
  • @Loc here are the code from the ApplicationHttpRequest that are involved in this – Tim Zhukov-Khovanskiy Mar 07 '17 at 21:06

0 Answers0