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 public class Headers extends JS.Immutable implements Fountain {
18 private final Hash head = new Hash();
19 private final Hash headModified = new Hash();
21 public final boolean mime;
24 private StringFountain fountain;
26 public String get(String s) {
27 String ret = (String)headModified.get(s.toLowerCase());
28 if (ret==null) ret = (String)head.get(s.toLowerCase());
31 public void put(String k, String v) {
32 Stream stream = getStream();
33 StringBuffer all = new StringBuffer();
37 for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
38 if (Character.isSpace(s.charAt(0))) { all.append(s); all.append("\r\n"); lines++; continue; }
39 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
40 key = s.substring(0, s.indexOf(':')).toLowerCase();
42 if (key.toLowerCase().equals(k.toLowerCase())) {
44 all.append(k + ": " + v + "\r\n");
52 all.append(k + ": " + v + "\r\n");
55 this.raw = all.toString();
57 this.fountain = new Fountain.StringFountain(this.raw);
59 public JS get(JS s) throws JSExn { return JSU.S(get(JSU.toString(s).toLowerCase())); }
61 public Stream getStream() { return fountain.getStream(); }
62 public int getLength() { return fountain.getLength(); }
63 public int getNumLines() { return fountain.getNumLines(); }
64 public Stream getStreamWithCRLF() { return new Stream(raw+"\r\n"); }
67 public String getString() { return raw; }
69 public Headers(Stream stream) throws Malformed { this(stream, false); }
70 public Headers(Stream stream, boolean assumeMime) throws Malformed {
71 StringBuffer all = new StringBuffer();
74 for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) {
78 if (Character.isSpace(s.charAt(0))) {
79 if (key == null) throw new Malformed("Message began with a blank line; no headers");
80 head.put(key, head.get(key) + " " + s.trim());
83 if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s);
84 key = s.substring(0, s.indexOf(':')).toLowerCase();
85 for(int i=0; i<key.length(); i++)
86 if (key.charAt(i) < 33 || key.charAt(i) > 126)
87 throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\"");
88 String val = s.substring(s.indexOf(':') + 1).trim();
89 if (get(key) != null) val = get(key) + " " + val; // just append it to the previous one;
92 this.raw = all.toString();
93 this.fountain = new Fountain.StringFountain(this.raw);
95 this.mime = assumeMime | (get("mime-version") != null && get("mime-version").trim().equals("1.0"));
97 java.util.Enumeration e = head.keys();
99 String k = (String)e.next();
100 String v = (String)head.get(k);
101 if (mime) k = Encode.RFC2047.decode(k);
103 if (mime) v = Encode.RFC2047.decode(v);
109 // Helpers //////////////////////////////////////////////////////////////////////////////
111 public static Stream skip(Stream stream) {
112 for(String s = stream.readln(); s!=null && s.length() > 0;) s = stream.readln();
116 public static String uncomment(String val) {
117 boolean inquotes = false;
118 for(int i=0; i<val.length(); i++) {
119 if (val.charAt(i) == '\"') inquotes = !inquotes;
120 if (val.charAt(i) == '(' && !inquotes)
121 val = val.substring(0, i) + val.substring(val.indexOf(i--, ')') + 1);