001: /*
002: * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: */
007: package javax.servlet.http;
008:
009: import java.util.Hashtable;
010: import javax.servlet.ServletInputStream;
011: import java.util.StringTokenizer;
012:
013: /**
014: * Generic utility functions for use in the servlet container.
015: *
016: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
017: */
018: public class HttpUtils {
019: /**
020: * @deprecated Reconstructs the URL the client used to make the request,
021: * using information in the HttpServletRequest object.
022: */
023: public static StringBuffer getRequestURL(HttpServletRequest req) {
024: return req.getRequestURL();
025: }
026:
027: /**
028: * @deprecated Parses data from an HTML form that the client sends to the
029: * server using the HTTP POST method and the
030: * application/x-www-form-urlencoded MIME type.
031: */
032: public static Hashtable parsePostData(int len, ServletInputStream in) {
033: try {
034: byte body[] = new byte[len];
035: int startPos = 0;
036: int readChars = in.read(body, startPos, len - startPos);
037: while ((readChars != -1) && (len != startPos)) {
038: startPos += readChars;
039: readChars = in.read(body, startPos, len - startPos);
040: }
041: if (len != startPos)
042: throw new java.io.IOException("Stream ended early");
043: else {
044: String post = new String(body, 0, len, "8859_1");
045: return parseQueryString(post);
046: }
047: } catch (java.io.IOException err) {
048: throw new IllegalArgumentException(
049: "Error parsing request body - " + err.getMessage());
050: }
051: }
052:
053: /**
054: * @deprecated Parses a query string passed from the client to the server
055: * and builds a HashTable object with key-value pairs.
056: */
057: public static Hashtable parseQueryString(String urlEncodedParams) {
058: Hashtable params = new Hashtable();
059: StringTokenizer st = new StringTokenizer(urlEncodedParams, "&",
060: false);
061: while (st.hasMoreTokens()) {
062: String token = st.nextToken();
063: int equalPos = token.indexOf('=');
064: if (equalPos == -1)
065: continue;
066: String name = token.substring(0, equalPos);
067: String value = token.substring(equalPos + 1);
068: String decodedName = decodeURLToken(name);
069: String decodedValue = decodeURLToken(value);
070:
071: Object already = params.get(decodedName);
072: if (already == null)
073: params.put(decodedName, new String[] { decodedValue });
074: else if (already instanceof String[]) {
075: String alreadyArray[] = (String[]) already;
076: String oneMore[] = new String[alreadyArray.length + 1];
077: System.arraycopy(alreadyArray, 0, oneMore, 0,
078: alreadyArray.length);
079: oneMore[oneMore.length - 1] = decodedValue;
080: params.put(decodedName, oneMore);
081: }
082: }
083: return params;
084: }
085:
086: private static String decodeURLToken(String in) {
087: StringBuffer workspace = new StringBuffer();
088: for (int n = 0; n < in.length(); n++) {
089: char this Char = in.charAt(n);
090: if (this Char == '+')
091: workspace.append(' ');
092: else if (this Char == '%') {
093: int decoded = Integer.parseInt(in.substring(n + 1,
094: n + 3), 16);
095: workspace.append((char) decoded);
096: n += 2;
097: } else
098: workspace.append(thisChar);
099: }
100: return workspace.toString();
101: }
102: }
|