X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FHeaders.java;h=6094c795bc5a46cded38109c144ae3bc71c267bc;hb=7272d8be4b638874b40c152a575b6c76292f53c8;hp=26c12adb752bdd55508d59de2ebd64eb6d994378;hpb=7bc79e9bf3e8dfc7ebc1e9eab162da4768a9cb7d;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/Headers.java b/src/org/ibex/mail/Headers.java index 26c12ad..6094c79 100644 --- a/src/org/ibex/mail/Headers.java +++ b/src/org/ibex/mail/Headers.java @@ -15,22 +15,9 @@ import java.net.*; import java.io.*; // FIXME: this is important: folded headers: can insert CRLF anywhere that whitespace appears (before the whitespace) -public class Headers extends JS.Immutable implements Fountain { - private final Hash head = new Hash(); - private final Hash headModified = new Hash(); - public int lines; - public final boolean mime; +public abstract class Headers extends JS.Immutable implements Fountain { - private String raw; - private StringFountain fountain; - - public String get(String s) { - String ret = (String)headModified.get(s.toLowerCase()); - if (ret==null) ret = (String)head.get(s.toLowerCase()); - return ret; - } - public void remove(String k) { put(k, null); } - public void put(String k, String v) { + public Headers set(String k, String v) { Stream stream = getStream(); StringBuffer all = new StringBuffer(); int lines = 0; @@ -53,59 +40,85 @@ public class Headers extends JS.Immutable implements Fountain { all.append(k + ": " + v + "\r\n"); } all.append("\r\n"); - this.raw = all.toString(); - this.lines = lines; - this.fountain = new Fountain.StringFountain(this.raw); + return new Original(new Stream(all.toString())); } - public JS get(JS s) throws JSExn { return JSU.S(get(JSU.toString(s).toLowerCase())); } - - public Stream getStream() { return fountain.getStream(); } - public int getLength() { return fountain.getLength(); } - public int getNumLines() { return fountain.getNumLines(); } - public Stream getStreamWithCRLF() { return new Stream(raw+"\r\n"); } // FIXME - public String getString() { return raw; } + public abstract String getString(); - public Headers(Stream stream) throws Malformed { this(stream, false); } - public Headers(Stream stream, boolean assumeMime) throws Malformed { - StringBuffer all = new StringBuffer(); - String key = null; - int lines = 0; - for(String s = stream.readln(); s != null && !s.equals(""); s = stream.readln()) { - all.append(s); - all.append("\r\n"); - lines++; - if (Character.isSpace(s.charAt(0))) { - if (key == null) throw new Malformed("Message began with a blank line; no headers"); - head.put(key, head.get(key) + " " + s.trim()); - continue; + public abstract String get(String s); + public abstract java.util.Enumeration names(); + + public Headers set(String[] keyval) { + Headers ret = this; + for(int i=0; i 126) + throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\""); + String val = s.substring(s.indexOf(':') + 1).trim(); + if (get(key) != null) val = get(key) + " " + val; // just append it to the previous one; + head.put(key, val); } - if (s.indexOf(':') == -1) throw new Malformed("Header line does not contain colon: " + s); - key = s.substring(0, s.indexOf(':')).toLowerCase(); - for(int i=0; i 126) - throw new Malformed("Header key \""+key+"\" contains invalid character \"" + key.charAt(i) + "\""); - String val = s.substring(s.indexOf(':') + 1).trim(); - if (get(key) != null) val = get(key) + " " + val; // just append it to the previous one; - head.put(key, val); + this.raw = all.toString(); + this.fountain = new Fountain.StringFountain(this.raw); + this.lines = lines; + this.mime = assumeMime | (get("mime-version") != null && get("mime-version").trim().equals("1.0")); + /* + java.util.Enumeration e = head.keys(); + while(e.hasNext()) { + String k = (String)e.next(); + String v = (String)head.get(k); + if (mime) k = Encode.RFC2047.decode(k); + v = uncomment(v); + if (mime) v = Encode.RFC2047.decode(v); + head.put(k, v); + } + */ } - this.raw = all.toString(); - this.fountain = new Fountain.StringFountain(this.raw); - this.lines = lines; - this.mime = assumeMime | (get("mime-version") != null && get("mime-version").trim().equals("1.0")); - /* - java.util.Enumeration e = head.keys(); - while(e.hasNext()) { - String k = (String)e.next(); - String v = (String)head.get(k); - if (mime) k = Encode.RFC2047.decode(k); - v = uncomment(v); - if (mime) v = Encode.RFC2047.decode(v); - head.put(k, v); - } - */ - } + // Helpers ////////////////////////////////////////////////////////////////////////////// @@ -123,4 +136,5 @@ public class Headers extends JS.Immutable implements Fountain { } return val; } + } }