001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Enumeration;
042: import java.util.Map;
043: import java.util.NoSuchElementException;
044:
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.PushbackInputStream;
048:
049: import com.quadcap.util.Debug;
050:
051: /**
052: * Return an enumeration of the headers of an RFC-822 style message.
053: * This implementation assumes that the headers have already been separated
054: * from the body, so that the input stream passed to the HeaderEnumeration
055: * constructor returns only the header portion of the message.
056: *
057: * <p><i>XXX It would be pretty easy to have this class detect the
058: * CRLF, CRLF sequence that ends the headers, as well....</i>
059: *
060: * @author Stan Bailes
061: */
062: public class HeaderEnumeration implements Enumeration {
063: String next;
064: PushbackInputStream is;
065: StringBuffer sb = new StringBuffer();
066:
067: static final byte CR = 0x0d;
068: static final byte LF = 0x0a;
069:
070: public HeaderEnumeration(InputStream is) {
071: this .is = new PushbackInputStream(is);
072: }
073:
074: /**
075: * Tests if this enumeration contains more elements.
076: *
077: * @return <code>true</code> if this enumeration contains more elements;
078: * <code>false</code> otherwise.
079: */
080: public boolean hasMoreElements() {
081: if (next == null)
082: getNext();
083: return (next != null);
084: }
085:
086: /**
087: * Returns the next element of this enumeration.
088: *
089: * @return the next element of this enumeration.
090: * @exception NoSuchElementException if no more elements exist.
091: */
092: public Object nextElement() {
093: if (next == null)
094: getNext();
095: if (next == null)
096: throw new NoSuchElementException("no more elements");
097: Object ret = next;
098: next = null;
099: return ret;
100: }
101:
102: /**
103: * Run the state machine to produce the next header.
104: */
105: void getNext() {
106: sb.setLength(0);
107:
108: int c;
109: int state = 0;
110:
111: try {
112: while ((c = is.read()) >= 0) {
113: switch (state) {
114: case 0:
115: if (c == CR) {
116: state = 1;
117: } else {
118: sb.append((char) c);
119: }
120: break;
121: case 1:
122: if (c == LF) {
123: state = 2;
124: } else if (c != CR) {
125: sb.append((char) c);
126: state = 0;
127: }
128: break;
129: case 2:
130: state = 0;
131: if (c == ' ' || c == '\t') {
132: sb.append(' ');
133: } else {
134: is.unread(c);
135: next = sb.toString();
136: sb.setLength(0);
137: return;
138: }
139: break;
140: }
141: }
142: if (state == 2 && sb.toString().trim().length() > 0) {
143: next = sb.toString().trim();
144: sb.setLength(0);
145: }
146: } catch (IOException e) {
147: Debug.print(e);
148: }
149: }
150:
151: /**
152: * Helper function to build map for header (name -> value).
153: *
154: * @exception IOException may be thrown.
155: */
156: public void getHeaderMap(Map headerMap) throws IOException {
157: while (hasMoreElements()) {
158: String header = (String) nextElement();
159: int idx = header.indexOf(':');
160: if (idx >= 0) {
161: String headerName = header.substring(0, idx).trim();
162: String headerVal = header.substring(idx + 1).trim();
163: headerMap.put(headerName.toLowerCase(), headerVal);
164: } else {
165: throw new IOException("Bad header: '" + header + "'");
166: }
167: }
168: }
169:
170: }
|