001: /*
002: * Copyright (c) 1998-2001 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: *
028: * $Id: HttpUtils.java,v 1.2 2004/09/29 00:12:48 cvs Exp $
029: */
030:
031: package javax.servlet.http;
032:
033: import javax.servlet.ServletInputStream;
034: import java.io.IOException;
035: import java.util.Hashtable;
036:
037: /**
038: * @deprecated
039: */
040: public class HttpUtils {
041: /**
042: * Converts a queryString to a hashtable.
043: */
044: public static Hashtable parseQueryString(String query) {
045: Hashtable table = new Hashtable();
046: int length = query.length();
047: int i = 0;
048: char ch;
049: char[] buf = new char[length];
050: int offset;
051:
052: while (i < length) {
053: for (ch = query.charAt(i); i < length
054: && (Character.isWhitespace((ch = query.charAt(i))) || ch == '&'); i++) {
055: }
056:
057: offset = 0;
058: for (; i < length && (ch = query.charAt(i)) != '='; i++) {
059: if (ch == '+')
060: buf[offset++] = ' ';
061: else if (ch == '%' && i + 2 < length) {
062: int ch1 = query.charAt(++i);
063: int ch2 = query.charAt(++i);
064:
065: buf[offset++] = (char) ((toHex(ch1) << 4) + toHex(ch2));
066: } else
067: buf[offset++] = (char) ch;
068: }
069:
070: if (offset == 0)
071: break;
072:
073: String key = new String(buf, 0, offset);
074: offset = 0;
075: for (i++; i < length && (ch = query.charAt(i)) != '&'; i++) {
076: if (ch == '+')
077: buf[offset++] = (char) ' ';
078: else if (ch == ' ') { // XXX:
079: } else if (ch == '%' && i + 2 < length) {
080: int ch1 = query.charAt(++i);
081: int ch2 = query.charAt(++i);
082:
083: buf[offset++] = (char) ((toHex(ch1) << 4) + toHex(ch2));
084: } else
085: buf[offset++] = (char) ch;
086: }
087:
088: i++;
089:
090: String value = new String(buf, 0, offset);
091: String[] oldValue = (String[]) table.get(key);
092: if (oldValue == null)
093: table.put(key, new String[] { value });
094: else {
095: String[] newValue = new String[oldValue.length + 1];
096: System.arraycopy(oldValue, 0, newValue, 0,
097: oldValue.length);
098: newValue[oldValue.length] = value;
099: table.put(key, newValue);
100: }
101: }
102:
103: return table;
104: }
105:
106: /**
107: * Parses POST data using www-form-urlencoding
108: */
109: public static Hashtable parsePostData(int length,
110: ServletInputStream is) {
111: try {
112: if (length >= 0) {
113: byte buf[] = new byte[length];
114: int offset = 0;
115:
116: while (length > 0) {
117: int sublen;
118:
119: sublen = is.read(buf, offset, length);
120:
121: if (sublen > 0) {
122: offset += sublen;
123: length -= sublen;
124: } else
125: throw new IOException("unexpected end of file");
126: }
127:
128: return parseQueryString(new String(buf, 0, buf.length));
129: } else
130: return new Hashtable();
131: } catch (IOException e) {
132: throw new IllegalArgumentException("illegal post data");
133: }
134: }
135:
136: /**
137: * Converts the request back to an original request URL.
138: */
139: public static StringBuffer getRequestURL(HttpServletRequest req) {
140: StringBuffer sb = new StringBuffer();
141:
142: sb.append(req.getScheme());
143: sb.append("://");
144: sb.append(req.getServerName());
145: if (req.getServerPort() > 0 && req.getServerPort() != 80
146: && req.getServerPort() != 443) {
147: sb.append(":");
148: sb.append(req.getServerPort());
149: }
150: sb.append(req.getRequestURI());
151:
152: return sb;
153: }
154:
155: /**
156: * Convert a single digit to a hex digit.
157: */
158: private static int toHex(int ch) {
159: if (ch >= '0' && ch <= '9')
160: return ch - '0';
161: else if (ch >= 'a' && ch <= 'f')
162: return ch - 'a' + 10;
163: else if (ch >= 'A' && ch <= 'F')
164: return ch - 'A' + 10;
165: else
166: return -1;
167: }
168: }
|