You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Ilja Teterin <il...@atbusiness.com> on 2001/05/16 09:03:32 UTC

[jakarta-servletapi-3.2] javax.servlet.http.HttpUtils.parseName(...) change request - non 8859-1 troubles

Hello!

Preposition:
    I have to work with non 8859-1 locales under servlets. (Tomcat 3.2 / MS Windows 2000).

Problem:
    When I post form using GET or POST I got the ???? instead of cyrillic charasters in posted values when I'm using
request.getParameter(name).

Investigation:
    Tomcat uses servlet api implementation and javax.servlet.http.HttpUtils class for parsing posted data.
    The chars disappears in the method parseName(String, StringBuffer).

Proposal:
    Change code of parseName() method implementation in the HttpUtils class to

static private String parseName(String s, StringBuffer sb) {
    return java.net.URLDecoder().decode(s)
}
    or (if we need StringBuffer functionality further).
static private String parseName(String s, StringBuffer sb) {
  sb.setLength(0);
  sb.append(java.net.URLDecoder().decode(s));
  return sb.toString();
}
Because the java.net.URLDecoder makes the same logical thing and chars are don't dissappear ( :) ).

The code of URLDecoder very similar to current implementation of parseName method, but
have next additional part after decoding %NN's.
==================
        // Undo conversion to external encoding
        String result = sb.toString();
        try {
            byte[] inputBytes = result.getBytes("8859_1");
            result = new String(inputBytes);
        } catch (UnsupportedEncodingException e) {
            // The system should always have 8859_1
        }
==================
wbr, iit.

================================ old code state for the HttpUtils =================================
static private String parseName(String s, StringBuffer sb) {
 sb.setLength(0);
 for (int i = 0; i < s.length(); i++) {
     char c = s.charAt(i); 
     switch (c) {
     case '+':
  sb.append(' ');
  break;
     case '%':
  try {
      sb.append((char) Integer.parseInt(s.substring(i+1, i+3), 
            16));
      i += 2;
  } catch (NumberFormatException e) {
      // XXX
      // need to be more specific about illegal arg
      throw new IllegalArgumentException();
  } catch (StringIndexOutOfBoundsException e) {
      String rest  = s.substring(i);
      sb.append(rest);
      if (rest.length()==2)
   i++;
  }
  
  break;
     default:
  sb.append(c);
  break;
     }
 }
 return sb.toString();
    }
======================================the URLDecoder.decode() source ============================
    public static String decode(String s) {
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '+':
                    sb.append(' ');
                    break;
                case '%':
                    try {
                        sb.append((char)Integer.parseInt(
                                        s.substring(i+1,i+3),16));
                    } catch (NumberFormatException e) {
                        throw new IllegalArgumentException();
                    }
                    i += 2;
                    break;
                default:
                    sb.append(c);
                    break;
            }
        }
        // Undo conversion to external encoding
        String result = sb.toString();
        try {
            byte[] inputBytes = result.getBytes("8859_1");
            result = new String(inputBytes);
        } catch (UnsupportedEncodingException e) {
            // The system should always have 8859_1
        }
        return result;
    }