1 package dolda.jsvc.util;
8 public final static DateFormat datefmt;
9 public final static String tspecials = "()<>@,;:\\\"/[]?={} ";
11 datefmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
12 datefmt.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
15 public static class EncodingException extends ClientError {
16 public EncodingException(String msg) {
17 super("Invalid header encoding", msg);
21 public static String fmtdate(Date d) {
22 return(datefmt.format(d));
25 public static Date parsedate(String str) throws ParseException {
26 return(datefmt.parse(str));
29 public static boolean istoken(String str) {
30 for(int i = 0; i < str.length(); i++) {
31 char c = str.charAt(i);
36 if(tspecials.indexOf(c) >= 0)
42 public static String tokenquote(String str) {
45 StringBuilder buf = new StringBuilder();
47 for(int i = 0; i < str.length(); i++) {
48 char c = str.charAt(i);
49 if(((c < 32) && (c != 9)) || (c >= 127))
50 throw(new RuntimeException("Invalid character in HTTP quoted-string: `" + c + "'"));
51 if((c == '"') || (c == '\\')) {
59 return(buf.toString());
62 public static String tokenunquote(Reader in) throws IOException {
63 StringBuilder buf = new StringBuilder();
68 if(Character.isWhitespace((char)c))
72 } else if(st == "token") {
73 if((c < 0) || Character.isWhitespace((char)c) || (tspecials.indexOf((char)c) >= 0)) {
76 return(buf.toString());
77 } else if((c < 32) || (c >= 127)) {
78 throw(new EncodingException("Invalid characters in header"));
86 } else if(st == "quoted") {
88 throw(new EncodingException("Unterminated quoted-string"));
89 } else if((c < 32) && !Character.isWhitespace((char)c)) {
90 throw(new EncodingException("Invalid characters in header"));
92 return(buf.toString());
93 } else if(c == '\\') {
100 } else if(st == "q1") {
102 throw(new EncodingException("Unterminated quoted-string"));
104 throw(new EncodingException("Invalid characters in header"));