1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
6 import static org.ibex.mail.MailException.*;
7 import org.ibex.crypto.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
12 import org.ibex.io.Fountain;
17 // FIXME: this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace)
18 public class Headers extends JS.Immutable implements Fountain {
19 private final Hash head = new Hash();
20 private final Hash headModified = new Hash();
22 public final boolean mime;
25 private StringFountain fountain;
27 public String get(String s) {
28 String ret = (String)headModified.get(s.toLowerCase());
29 if (ret==null) ret = (String)head.get(s.toLowerCase());
32 public void remove(String k) { put(k, null); }
33 public void put(String k, String v) {
34 Stream stream = getStream();
35 StringBuffer all = new StringBuffer();
38 for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
39 if (!Character.isSpace(s.charAt(0))) {
40 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
41 key = s.substring(0, s.indexOf(':')).toLowerCase();
43 if (key.toLowerCase().equals(k.toLowerCase())) {
44 if (v != null) { all.append(k + ": " + v + "\r\n"); lines++; v = null; }
53 all.append(k + ": " + v + "\r\n");
56 this.raw = all.toString();
58 this.fountain = new Fountain.StringFountain(this.raw);
60 public JS get(JS s) throws JSExn { return JSU.S(get(JSU.toString(s).toLowerCase())); }
62 public Stream getStream() { return fountain.getStream(); }
63 public int getLength() { return fountain.getLength(); }
64 public int getNumLines() { return fountain.getNumLines(); }
65 public Stream getStreamWithCRLF() { return new Stream(raw+"\r\n"); }
68 public String getString() { return raw; }
70 public Headers(Stream stream) throws Malformed { this(stream, false); }
71 public Headers(Stream stream, boolean assumeMime) throws Malformed {
72 StringBuffer all = new StringBuffer();
75 for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
79 if (Character.isSpace(s.charAt(0))) {
80 if (key == null) throw new Malformed("Message began with a blank line; no headers");
81 head.put(key, head.get(key) + " " + s.trim());
84 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
85 key = s.substring(0, s.indexOf(':')).toLowerCase();
86 for(int i=0; i<key.length(); i++)
87 if (key.charAt(i) < 33 || key.charAt(i) > 126)
88 throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
89 String val = s.substring(s.indexOf(':') + 1).trim();
90 if (get(key) != null) val = get(key) + " " + val; // just append it to the previous one;
93 this.raw = all.toString();
94 this.fountain = new Fountain.StringFountain(this.raw);
96 this.mime = assumeMime | (get("mime-version") != null && get("mime-version").trim().equals("1.0"));
98 java.util.Enumeration e = head.keys();
100 String k = (String)e.next();
101 String v = (String)head.get(k);
102 if (mime) k = Encode.RFC2047.decode(k);
104 if (mime) v = Encode.RFC2047.decode(v);
110 // Helpers //////////////////////////////////////////////////////////////////////////////
112 public static Stream skip(Stream stream) {
113 for(String s = stream.readln(); s!=null && s.length() > 0;) s = stream.readln();
117 public static String uncomment(String val) {
118 boolean inquotes = false;
119 for(int i=0; i<val.length(); i++) {
120 if (val.charAt(i) == '\"') inquotes = !inquotes;
121 if (val.charAt(i) == '(' && !inquotes)
122 val = val.substring(0, i) + val.substring(val.indexOf(i--, ')') + 1);