001: /*
002: * Headers.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: Headers.java,v 1.1.1.1 2002/09/24 16:09:10 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class Headers {
025: // These are indexes into the array of values. We only store values for the
026: // headers we're interested in.
027: public static final int CC = 0;
028: public static final int CONTENT_DISPOSITION = 1;
029: public static final int CONTENT_TRANSFER_ENCODING = 2;
030: public static final int CONTENT_TYPE = 3;
031: public static final int DATE = 4;
032: public static final int FROM = 5;
033: public static final int IN_REPLY_TO = 6;
034: public static final int MESSAGE_ID = 7;
035: public static final int REFERENCES = 8;
036: public static final int REPLY_TO = 9;
037: public static final int SET_COOKIE = 10;
038: public static final int SUBJECT = 11;
039: public static final int TO = 12;
040: public static final int X_J_STATUS = 13;
041: public static final int X_UIDL = 14;
042:
043: private static final int MAX_HEADERS = 15;
044:
045: private String[] values = new String[MAX_HEADERS];
046:
047: private Headers() {
048: }
049:
050: public String getValue(String name) {
051: int index = getIndex(name);
052: if (index >= 0)
053: return values[index];
054: return null;
055: }
056:
057: public String getValue(int index) {
058: if (index < MAX_HEADERS)
059: return values[index];
060: Debug.bug();
061: return null;
062: }
063:
064: private void setValue(String name, String value) {
065: int index = getIndex(name);
066: if (index >= 0)
067: values[index] = value;
068: }
069:
070: private int getIndex(String name) {
071: if (name.length() == 0)
072: return -1;
073: name = name.toLowerCase();
074: switch (name.charAt(0)) {
075: case 'c':
076: if (name.equals("cc"))
077: return CC;
078: if (name.equals("content-disposition"))
079: return CONTENT_DISPOSITION;
080: if (name.equals("content-transfer-encoding"))
081: return CONTENT_TRANSFER_ENCODING;
082: if (name.equals("content-type"))
083: return CONTENT_TYPE;
084: break;
085: case 'd':
086: if (name.equals("date"))
087: return DATE;
088: break;
089: case 'f':
090: if (name.equals("from"))
091: return FROM;
092: break;
093: case 'i':
094: if (name.equals("in-reply-to"))
095: return IN_REPLY_TO;
096: break;
097: case 'm':
098: if (name.equals("message-id"))
099: return MESSAGE_ID;
100: break;
101: case 'r':
102: if (name.equals("references"))
103: return REFERENCES;
104: if (name.equals("reply-to"))
105: return REPLY_TO;
106: break;
107: case 's':
108: if (name.equals("set-cookie"))
109: return SET_COOKIE;
110: if (name.equals("subject"))
111: return SUBJECT;
112: break;
113: case 't':
114: if (name.equals("to"))
115: return TO;
116: break;
117: case 'x':
118: if (name.equals("x-j-status"))
119: return X_J_STATUS;
120: if (name.equals("x-uidl"))
121: return X_UIDL;
122: break;
123: default:
124: break;
125: }
126: return -1;
127: }
128:
129: public static Headers parse(String s) {
130: Headers headers = new Headers();
131: FastStringBuffer sb = new FastStringBuffer();
132: String name = null;
133: int begin = 0;
134: int end;
135: while (true) {
136: end = s.indexOf('\n', begin);
137: if (end < 0) {
138: // Done.
139: if (name != null && sb.length() > 0)
140: headers.setValue(name, sb.toString());
141: break;
142: }
143: String line = s.substring(begin, end);
144: // Trim trailing '\r' if any.
145: if (line.length() > 0
146: && line.charAt(line.length() - 1) == '\r')
147: line = line.substring(0, line.length() - 1);
148: if (line.length() == 0) {
149: // Done.
150: if (name != null && sb.length() > 0)
151: headers.setValue(name, sb.toString());
152: break;
153: }
154: begin = end + 1;
155: char c = line.charAt(0);
156: if (c == ' ' || c == '\t') {
157: // Continuation.
158: sb.append(line);
159: continue;
160: }
161: if (name != null && sb.length() > 0)
162: headers.setValue(name, sb.toString());
163: // New header.
164: name = null;
165: sb.setLength(0);
166: int i = line.indexOf(':');
167: if (i < 0) {
168: Log.error("can't parse header |" + s + "|");
169: return headers;
170: }
171: // Store names in lower case.
172: name = line.substring(0, i).trim();
173: // Field value.
174: sb.append(line.substring(i + 1).trim());
175: }
176: return headers;
177: }
178: }
|