001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose.console;
021:
022: import uk.org.primrose.Logger;
023: import java.net.*;
024: import java.util.*;
025:
026: public class HttpRequest {
027:
028: protected String szMethod;
029: protected String szResource;
030: protected String szResponseMimeType;
031: protected String szVersion;
032: HashMap<String, String> headers = new HashMap<String, String>();
033: HashMap<String, String> parameters = new HashMap<String, String>();
034: Logger logger = null;
035:
036: public HttpRequest(Logger logger) {
037: this .logger = logger;
038: }
039:
040: /**
041: * Get the 'szMethod '
042: */
043: public String getMethod() {
044: return szMethod;
045: }
046:
047: /**
048: * Set the 'szMethod '
049: */
050: public void setMethod(String szMethod) {
051: this .szMethod = szMethod;
052: }
053:
054: /**
055: * Get the 'szResource '
056: */
057: public String getResource() {
058: return szResource;
059: }
060:
061: /**
062: * Set the 'szResource '
063: */
064: public void setResource(String szResource) {
065: this .szResource = szResource;
066: }
067:
068: /**
069: * Get the 'szResponseMimeType '
070: */
071: public String getResponseMimeType() {
072: return szResponseMimeType;
073: }
074:
075: /**
076: * Set the 'szResponseMimeType '
077: */
078: public void setResponseMimeType(String szResponseMimeType) {
079: this .szResponseMimeType = szResponseMimeType;
080: }
081:
082: /**
083: * Get the 'szVersion '
084: */
085: public String getVersion() {
086: return szVersion;
087: }
088:
089: /**
090: * Set the 'szVersion '
091: */
092: public void setVersion(String szVersion) {
093: this .szVersion = szVersion;
094: }
095:
096: /**
097: * Get the 'headers '
098: */
099: public HashMap<String, String> getHeaders() {
100: return headers;
101: }
102:
103: /**
104: * Get the 'parameters '
105: */
106: public HashMap<String, String> getParameters() {
107: return parameters;
108: }
109:
110: public void printDetails() {
111: logger.info("szMethod=" + szMethod);
112: logger.info("szResource=" + szResource);
113: logger.info("szResponseMimeType=" + szResponseMimeType);
114: logger.info("szVersion=" + szVersion);
115:
116: logger.info("\n\nHeaders : ");
117: Iterator<String> headersIter = headers.keySet().iterator();
118: while (headersIter.hasNext()) {
119: String key = headersIter.next();
120: logger.info(key + "=" + headers.get(key));
121: }
122:
123: logger.info("\n\nParameters : ");
124: Iterator<String> parametersIter = parameters.keySet()
125: .iterator();
126: while (parametersIter.hasNext()) {
127: String key = parametersIter.next();
128: logger.info(key + "=" + parameters.get(key));
129: }
130: }
131:
132: @SuppressWarnings("deprecation")
133: boolean parseRequest(String szRequestData) {
134: if (szRequestData != null && szRequestData.length() == 0)
135: return false;
136: // Create a tokenizer from the request
137: String[] t = szRequestData.split("\n");
138: String szTok;
139: boolean bFirstLine = true;
140:
141: // Loop the request, extacting the necessary data
142: for (int i = 0; i < t.length; i++) {
143: szTok = t[i];
144: //CReportError::Info("LINE : %s", szTok);
145: // first line :
146: // extract the method, resource and http version data
147: if (bFirstLine) {
148: bFirstLine = false;
149: int j = 0;
150: String[] t2 = szTok.split(" ");
151:
152: // populate the HTTP method
153: String szTok2 = t2[j++];
154: if (szTok2 != null) {
155: szMethod = szTok2;
156: }
157:
158: // populate the resource requested
159: szTok2 = t2[j++];
160: if (szTok2 != null) {
161: szResource = szTok2;
162:
163: // set the MIME type too
164: if (szResource.indexOf(".htm") != -1) {
165: szResponseMimeType = ("text/html");
166: } else if (szResource.indexOf(".svg") != -1) {
167: szResponseMimeType = ("image/svg+xml");
168: } else if (szResource.indexOf(".png") != -1) {
169: szResponseMimeType = ("image/png");
170: } else if (szResource.indexOf(".gif") != -1) {
171: szResponseMimeType = ("image/gif");
172: } else if ((szResource.indexOf(".jpeg") != -1)
173: || (szResource.indexOf(".jpg") != -1)) {
174: szResponseMimeType = ("image/jpeg");
175: } else if (szResource.indexOf(".txt") != -1) {
176: szResponseMimeType = ("text/plain");
177: } else if (szResource.indexOf(".log") != -1) {
178: szResponseMimeType = ("text/plain");
179: } else {
180: szResponseMimeType = ("text/html");
181: }
182:
183: // populate the request attributes for a GET
184: if (szMethod.indexOf("GET") != -1) {
185: String[] t3 = szResource.split("\\?");
186: if (t3.length > 1) {
187: String szToken = t3[1];
188: String[] t4 = szToken.split("&");
189:
190: // breaking each pair up
191: String szToken2;
192: for (int l = 0; l < t4.length; l++) {
193: szToken2 = t4[l];
194: // and finally breaking the pairs into their
195: // names and values
196: String[] t5 = szToken2.split("=");
197: if (t5.length == 2) {
198: parameters.put(URLDecoder
199: .decode(t5[0].trim()),
200: URLDecoder.decode(t5[1]
201: .trim()));
202: }
203:
204: }
205:
206: }
207: }
208: }
209:
210: // populate the HTTP version
211: szTok2 = t2[j++];
212: if (szTok2 != null) {
213: szVersion = szTok2;
214: }
215: continue;
216: }
217:
218: // non-first line :
219: // extract the header details
220: String[] t2 = szTok.split(":");
221: String szHeaderName = t2[0];
222: String szHeaderValue = t2[1];
223: if (szHeaderName == null || szHeaderValue == null) {
224: continue;
225: }
226:
227: headers.put(szHeaderName.trim(), szHeaderValue.trim());
228:
229: }
230:
231: return true;
232: }
233: }
|