001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx.adaptor.http;
009:
010: import java.util.Map;
011: import java.util.HashMap;
012: import java.util.StringTokenizer;
013: import java.io.UnsupportedEncodingException;
014:
015: /**
016: *
017: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
018: */
019:
020: public class HttpRequest {
021: private HttpThread thread = null;
022: private String method = "GET";
023: private String uri = "/";
024: private Map headersMap = new HashMap();
025: private Map parameterMap = new HashMap();
026:
027: private final static String[] RESERVED_HEADERS = {
028: HttpConstants.CONTENT_LENGTH_HEADER,
029: HttpConstants.CONTENT_TYPE_HEADER,
030: HttpConstants.CONNECTION, HttpConstants.DATE_HEADER,
031: HttpConstants.WWW_AUTHENTICATE_HEADER,
032: HttpConstants.AUTHORIZATION_HEADER };
033:
034: public HttpRequest(HttpThread thread) {
035: this .thread = thread;
036: }
037:
038: public void setURI(String uri) {
039: this .uri = uri;
040: parameterMap.clear();
041: parseQueryString(getQueryString());
042: }
043:
044: private void setHeader(String name, String value) {
045: headersMap.put(name.toUpperCase(), value.trim());
046: }
047:
048: public String getHeader(String name) {
049: return (String) headersMap.get(name.toUpperCase());
050: }
051:
052: void setMethod(String method) {
053: this .method = method;
054: }
055:
056: public String getMethod() {
057: return method;
058: }
059:
060: // parse a head line String
061: void parseHeadLine(String line, int line_no)
062: throws MalformedHttpHeaderException {
063: if (line == null || line.trim().length() == 0)
064: return;
065: line = line.trim();
066: if (line_no == 1) { // Method & uri
067: parseMethodAndURI(line);
068: } else {
069: parseHeader(line);
070: }
071:
072: }
073:
074: private void parseMethodAndURI(String line)
075: throws MalformedHttpHeaderException {
076: int i = line.indexOf(' ');
077: int j = line.indexOf(' ', i + 1);
078: if (i == -1)
079: throw new MalformedHttpHeaderException(
080: "Malformed request line");
081: String _method = line.substring(0, i);
082:
083: if (_method.equalsIgnoreCase("GET"))
084: method = "GET";
085: else if (_method.equalsIgnoreCase("POST"))
086: method = "POST";
087: else
088: throw new MalformedHttpHeaderException(
089: "Unsupported HTTP method");
090: if (j > 0)
091: uri = line.substring(i + 1, j);
092: else
093: uri = line.substring(i + 1);
094:
095: // System.out.println("URI: " + uri);
096: if (uri.trim().equals("/"))
097: uri = "/Index"; // the default command
098: if (uri.trim().startsWith("/?"))
099: uri = "/Index?" + uri.substring(2); // /?x=y => /Index?x=y
100:
101: uri = decodeUrl(uri);
102:
103: parseQueryString(getQueryString());
104:
105: }
106:
107: private void parseQueryString(String qstring) {
108: if (qstring == null || qstring.trim().length() == 0)
109: return;
110: StringTokenizer st = new StringTokenizer(qstring, "&");
111: while (st.hasMoreTokens()) {
112: String nvPair = st.nextToken();
113: int index = nvPair.indexOf("=");
114: if (index < 0) { // only key , not have value
115: setParameter(nvPair, "");
116: } else {
117: String name = nvPair.substring(0, index);
118: String value = nvPair.substring(index + 1);
119: setParameter(name, value);
120: }
121: }
122: }
123:
124: private void parseHeader(String line)
125: throws MalformedHttpHeaderException {
126: line = line.trim();
127: int index = line.indexOf(":");
128: if (index < 0) {
129: throw new MalformedHttpHeaderException(
130: "Malformed HTTP header");
131: } else {
132: String name = line.substring(0, index).trim();
133: String value = line.substring(index + 1).trim();
134: if (isKnownHeader(name))
135: setHeader(name, value);
136: }
137: }
138:
139: private boolean isKnownHeader(String header) {
140: boolean isKnown = false;
141: for (int i = 0; i < RESERVED_HEADERS.length; i++) {
142: if (RESERVED_HEADERS[i].equalsIgnoreCase(header.trim())) {
143: isKnown = true;
144: break;
145: }
146: }
147: return isKnown;
148: }
149:
150: public String getRequestURI() {
151: return "http://" + thread.getServer().getHost() + ":"
152: + thread.getServer().getPort() + uri;
153: }
154:
155: public String getQueryString() {
156: int index = uri.indexOf("?");
157: if (index < 0) {
158: return "";
159: } else {
160:
161: return uri.substring(index + 1);
162: }
163: }
164:
165: public String getCommand() {
166: // int index = uri.indexOf("?");
167: // if(index < 0){
168: // return uri.substring(1);
169: // }
170: // else{
171: // return uri.substring(1,index);
172: // }
173: String command = this .getParameter("action");
174: if (command == null || command.trim().length() == 0) {
175: command = "Agent";
176: }
177: return command;
178:
179: }
180:
181: public String getParameter(String name) {
182: return (String) parameterMap.get(name.toUpperCase());
183: }
184:
185: public HttpThread getThread() {
186: return thread;
187: }
188:
189: private void setParameter(String name, String value) {
190: parameterMap.put(name.toUpperCase(), value.trim());
191: }
192:
193: // decode url
194: private String decodeUrl(String encodeURL) {
195: int length = encodeURL.length();
196: char[] charArray = encodeURL.toCharArray();
197: byte[] byteArray = new byte[length];
198: int j = 0;
199: boolean flag = true;
200: for (int k = 0; k < length; k++) {
201: char c = charArray[k];
202: if (c < 0 || c > '\177')
203: throw new IllegalArgumentException("Not encoded");
204: byte byte0 = (byte) (0x7f & c);
205: if (c == '+') {
206: flag = false;
207: byte0 = 32;
208: } else if (c == '%' && k + 2 < length) {
209: flag = false;
210: byte0 = (byte) (0xff & Integer.parseInt(encodeURL
211: .substring(k + 1, k + 3), 16));
212: k += 2;
213: }
214: byteArray[j++] = byte0;
215: }
216:
217: if (flag)
218: return encodeURL;
219: try {
220: return new String(byteArray, 0, j, "ISO-8859-1");
221: } catch (UnsupportedEncodingException unsupportedencodingexception) {
222: return new String(byteArray, 0, j);
223: }
224: }
225:
226: }
227:
228: /*
229: GET / HTTP/1.1
230: Accept: * /*
231: Accept-Language: zh-cn
232: Accept-Encoding: gzip, deflate
233: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
234: Host: localhost:8082
235: Connection: Keep-Alive
236: */
|