001: package com.sun.portal.netlet.client.jnlp.connect;
002:
003: import java.io.*;
004: import com.sun.portal.log.common.PortalLogger;
005: import java.util.*;
006: import java.net.InetAddress;
007: import java.net.URL;
008: import java.net.MalformedURLException;
009:
010: public class HTTPFields implements Serializable, Cloneable {
011:
012: private Vector fieldNames;
013: private Vector fieldValues;
014: private String firstLine;
015:
016: // Holds the value of fields
017: private HTTPFields requestHeader = null;
018: private byte requestBody[];
019: private HTTPFields responseHeader = null;
020: private byte responseBody[];
021: String host;
022: private int port;
023: private String path;
024: private String file;
025: private String scheme;
026: private String protocol;
027: private InetAddress clientAddress;
028: private String httpVersion;
029: private InetAddress remoteAddress;
030: private String socketInfo;
031: private String requestString;
032:
033: public HTTPFields() {
034: fieldNames = new Vector();
035: fieldValues = new Vector();
036: }
037:
038: public HTTPFields getRequestHeader() {
039: return requestHeader;
040: }
041:
042: public void setRequestHeader(HTTPFields requestHeader) {
043: this .requestHeader = requestHeader;
044: }
045:
046: public byte[] getRequestBody() {
047: return requestBody;
048: }
049:
050: public void setRequestBody(byte requestBody[]) {
051: this .requestBody = requestBody;
052: }
053:
054: public HTTPFields getResponseHeader() {
055: return responseHeader;
056: }
057:
058: public void setResponseHeader(HTTPFields responseHeader) {
059: this .responseHeader = responseHeader;
060: }
061:
062: public byte[] getResponseBody() {
063: return responseBody;
064: }
065:
066: public void setResponseBody(byte responseBody[]) {
067: this .responseBody = responseBody;
068: }
069:
070: public String getHost() {
071: return host;
072: }
073:
074: public void setHost(String host) {
075: this .host = host;
076: }
077:
078: public int getPort() {
079: return port;
080: }
081:
082: public void setPort(int port) {
083: this .port = port;
084: }
085:
086: public String getPath() {
087: return path;
088: }
089:
090: public void setPath(String path) {
091: this .path = path;
092: }
093:
094: public String getFile() {
095: return file;
096: }
097:
098: public void setFile(String file) {
099: this .file = file;
100: }
101:
102: public String getScheme() {
103: return scheme;
104: }
105:
106: public void setScheme(String scheme) {
107: this .scheme = scheme;
108: }
109:
110: public String getProtocol() {
111: return protocol;
112: }
113:
114: public void setProtocol(String protocol) {
115: this .protocol = protocol;
116: }
117:
118: public InetAddress getClientAddress() {
119: return clientAddress;
120: }
121:
122: public void setClientAddress(InetAddress clientAddress) {
123: this .clientAddress = clientAddress;
124: }
125:
126: public String getHttpVersion() {
127: return httpVersion;
128: }
129:
130: public void setHttpVersion(String httpVersion) {
131: this .httpVersion = httpVersion;
132: }
133:
134: public InetAddress getRemoteAddress() {
135: return remoteAddress;
136: }
137:
138: public void setRemoteAddress(InetAddress remoteAddress) {
139: this .remoteAddress = remoteAddress;
140: }
141:
142: public String getRequestString() {
143: return requestString;
144: }
145:
146: public void setRequestString(String requestString) {
147: this .requestString = requestString;
148: }
149:
150: public void read(HTTPRequestStream in) throws IOException {
151: firstLine = in.readLine();
152: String line = in.readLine();
153: String key = null;
154: StringBuffer value = null;
155: for (; line != null && line.length() != 0; line = in.readLine())
156: if (line.startsWith(" ")) {
157: if (value == null)
158: throw new IOException(
159: "Found header field continuation before a header field!: \""
160: + line + "\"");
161: value.append(line);
162: } else {
163: if (key != null)
164: addField(key, value.toString());
165: int colon = line.indexOf(":");
166: if (colon == -1)
167: throw new IOException(
168: "Found a line in the headers that does not define a header!: \""
169: + line + "\"");
170: key = line.substring(0, colon);
171: value = new StringBuffer();
172: value.append(line.substring(colon + 2));
173: }
174:
175: if (key != null)
176: addField(key, value.toString());
177: }
178:
179: public void write(HTTPResponseStream out) throws IOException {
180: if (firstLine != null) {
181: out.write(firstLine);
182: out.write("\r\n");
183: }
184: int n = fieldNames.size();
185: for (int i = 0; i < n; i++) {
186: String key = (String) fieldNames.get(i);
187: String value = (String) fieldValues.get(i);
188: out.write(key);
189: out.write(": ");
190: out.write(value);
191: out.write("\r\n");
192: }
193:
194: out.write("\r\n");
195: out.flush();
196: }
197:
198: public Vector getFieldNames() {
199: return fieldNames;
200: }
201:
202: public String get(String fieldName) {
203: return getField(fieldName);
204: }
205:
206: public String getField(String fieldName) {
207: int n = fieldNames.size();
208: for (int i = 0; i < n; i++) {
209: String key = (String) fieldNames.get(i);
210: if (key.equalsIgnoreCase(fieldName))
211: return (String) fieldValues.get(i);
212: }
213:
214: return null;
215: }
216:
217: public int getIntField(String fieldName)
218: throws NumberFormatException {
219: String value = getField(fieldName);
220: if (value != null)
221: return Integer.parseInt(value);
222: else
223: return 0;
224: }
225:
226: public Vector getFieldValues(String fieldName) {
227: int n = fieldNames.size();
228: Vector ray = new Vector();
229: for (int i = 0; i < n; i++) {
230: String key = (String) fieldNames.get(i);
231: if (key.equalsIgnoreCase(fieldName))
232: ray.add(fieldValues.get(i));
233: }
234:
235: if (ray.isEmpty())
236: return null;
237: else
238: return ray;
239: }
240:
241: public void addField(String key, String value) {
242: fieldNames.add(key);
243: fieldValues.add(value);
244: }
245:
246: public void appendField(String key, String value) {
247: String existingval = this .getField(key);
248: if (existingval == null)
249: existingval = value;
250: else
251: existingval = existingval + ";" + value;
252:
253: this .removeField(key);
254: this .addField(key, existingval);
255:
256: }
257:
258: public void removeField(String fieldName) {
259: int n = fieldNames.size();
260: for (int i = 0; i < n; i++) {
261: String key = (String) fieldNames.get(i);
262: if (key.equalsIgnoreCase(fieldName)) {
263: fieldNames.remove(i);
264: fieldValues.remove(i);
265: i--;
266: n--;
267: }
268: }
269:
270: }
271:
272: public void renameField(String fromFieldName, String toFieldName) {
273: String val = get(fromFieldName);
274: removeField(fromFieldName);
275: addField(toFieldName, val);
276: val = null;
277:
278: }
279:
280: public Object clone() {
281: try {
282: return super .clone();
283: } catch (CloneNotSupportedException e) {
284: throw new RuntimeException(e.toString());
285: }
286: }
287:
288: public String toString() {
289: ByteArrayOutputStream bout;
290: bout = new ByteArrayOutputStream();
291: HTTPResponseStream out = new HTTPResponseStream(bout);
292: try {
293: write(out);
294: out.flush();
295: } catch (IOException e) {
296: return new String(bout.toByteArray());
297: }
298:
299: return bout.toString();
300: }
301:
302: public String getFirstLine() {
303: return firstLine;
304: }
305:
306: public void setFirstLine(String firstLine) {
307: this .firstLine = firstLine;
308: }
309:
310: public String getRedirectURL() {
311: String value = null;
312: int index = -1;
313: System.out.println("firstLine " + firstLine);
314: if ((index = firstLine.indexOf("?")) != -1) {
315: value = firstLine.substring(firstLine.indexOf("=",
316: index + 1) + 1, firstLine.lastIndexOf(" "));
317: }
318: System.out.println("index " + index);
319: System.out.println(" index 2 " + firstLine.indexOf(" "));
320: System.out.println("value " + value);
321:
322: return value;
323: }
324:
325: /*
326: * getStatus - returns the HTTPResponse status
327: * @return int http response status, -1 otherwise
328: */
329: public int getStatus() {
330: StringTokenizer tok;
331:
332: try {
333: if (firstLine == null || !firstLine.startsWith("HTTP/")) {
334: return -1;
335: }
336:
337: tok = new StringTokenizer(firstLine, " ");
338: int status;
339:
340: String httpVersion = tok.nextToken();
341: status = Integer.parseInt(tok.nextToken());
342: return status;
343: } catch (NoSuchElementException e) {
344: return -1;
345: } catch (NumberFormatException e) {
346: return -1;
347: }
348:
349: }
350:
351: public void setSocketInfo(String info) {
352: socketInfo = info;
353: }
354:
355: public String getSocketInfo() {
356: return socketInfo;
357: }
358:
359: public static void main(String[] args) {
360: try {
361: File f = new File("c:\\temp\\http_get_response.txt");
362: InputStream in = new FileInputStream(f);
363: HTTPRequestStream httpReqStrm = new HTTPRequestStream(in);
364: HTTPFields hf = new HTTPFields();
365:
366: hf.read(httpReqStrm);
367: //Log.message("" + hf.getStatus());
368: //Log.message(hf.toString());
369: } catch (Exception e) {
370:
371: }
372:
373: }
374:
375: public void parseRequest() throws IOException {
376:
377: setRequestHeader(this );
378:
379: String firstLine = getFirstLine();
380: StringTokenizer tok = new StringTokenizer(firstLine);
381: String scheme;
382: String requestString;
383: String httpVersion;
384: try {
385:
386: scheme = tok.nextToken();
387: requestString = tok.nextToken();
388:
389: //
390: // This change is for netscape
391: //
392: if (requestString.startsWith("/"))
393: requestString = requestString.substring(1,
394: requestString.length());
395: httpVersion = tok.nextToken();
396:
397: } catch (NoSuchElementException e) {
398: //Log.error( "Invalid proxy line ");
399: throw new IOException("Invalid first proxy line \""
400: + firstLine + "\"");
401: }
402:
403: setScheme(scheme);
404: setHttpVersion(httpVersion);
405: setRequestString(requestString);
406:
407: setFirstLine(scheme + " " + requestString + " " + httpVersion);
408:
409: //if(scheme.equals("GET") || scheme.equals("POST"))
410: if (!scheme.equals("CONNECT")) {
411:
412: URL url;
413: try {
414: url = new URL(requestString);
415: } catch (MalformedURLException e) {
416: //Log.message( "MalformedURLException Proxylet" + e.getMessage());
417: throw new IOException("Malformed HTTP URL \""
418: + requestString + "\"");
419: }
420: String protocol = url.getProtocol();
421: setHost(url.getHost());
422: setPort(url.getPort());
423: setPath(extractPath(url.getFile()));
424: setFile(url.getFile());
425: setProtocol(protocol);
426: } else {
427: int i = requestString.lastIndexOf(':');
428: String host;
429: int port;
430: if (i != -1) {
431: host = requestString.substring(0, i);
432: port = Integer.parseInt(requestString.substring(i + 1));
433: } else {
434: throw new IOException(
435: "CONNECT request didn't include a port: "
436: + firstLine);
437: }
438: setHost(host);
439: setPort(port);
440: setProtocol("https");
441: }
442:
443: }
444:
445: private static String extractPath(String fileInfo) {
446: return (fileInfo.substring(0,
447: (fileInfo.indexOf('?') == -1 ? fileInfo.length()
448: : fileInfo.indexOf("?"))));
449: }
450:
451: }
|